root/OpenSceneGraph/trunk/examples/osghangglide/tank.cpp @ 10070

Revision 10070, 4.2 kB (checked in by robert, 5 years ago)

From Chris Hanson, spelling and grammer fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osghangglide.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <math.h>
20
21#include <osg/GL>
22#include <osg/Group>
23#include <osg/Geode>
24#include <osg/Geometry>
25#include <osg/Texture2D>
26#include <osg/TexEnv>
27#include <osg/StateSet>
28#include <osg/Matrix>
29
30#include <osgDB/ReadFile>
31
32#ifdef _MSC_VER
33#pragma warning( disable : 4244 )
34#pragma warning( disable : 4305 )
35#endif
36
37using namespace osg;
38
39extern void getDatabaseCenterRadius( float dbcenter[3], float *dbradius );
40
41static float radius = 2.0;
42static float dbcenter[3], dbradius;
43
44static void conv( const Vec3& a, const Matrix& mat, Vec3& b )
45{
46    int i;
47    Vec3 t;
48
49    for( i = 0; i < 3; i++ )
50    {
51        t[i] =  (a[0] * mat(0,i)) +
52            (a[1] * mat(1,i)) +
53            (a[2] * mat(2,i)) +
54            mat(3,i);
55    }
56    b[0] = t[0];
57    b[1] = t[1];
58    b[2] = t[2];
59}
60
61
62Node *makeTank( void )
63{
64
65    Geode *geode = new Geode;
66
67    getDatabaseCenterRadius( dbcenter, &dbradius );
68
69    Matrix mat(
70        0.05, 0, 0, 0,
71        0, 0.05, 0, 0,
72        0, 0, 0.05, 0,
73        1.5999 - 0.3,
74        3.1474,
75        dbcenter[2] + 0.6542 - 0.09,
76        1
77        );
78
79    // 42 required for sides, 22 for the top.
80    Vec3Array& vc = *(new Vec3Array(42+22));
81    Vec2Array& tc = *(new Vec2Array(42+22));
82
83    Geometry *gset = new Geometry;
84    gset->setVertexArray( &vc );
85    gset->setTexCoordArray( 0, &tc );
86
87    // create the sides of the tank.
88    unsigned int i, c = 0;
89    for( i = 0; i <= 360; i += 18 )
90    {
91        float x, y, z;
92        float s, t;
93        float theta = osg::DegreesToRadians((float)i);
94
95        s = (float)i/90.0;
96        t = 1.0;
97
98        x = radius * cosf( theta );
99        y = radius * sinf( theta );
100        z = 1.0;
101
102        vc[c][0] = x;
103        vc[c][1] = y;
104        vc[c][2] = z;
105
106        tc[c][0] = s;
107        tc[c][1] = t;
108
109        c++;
110
111        t = 0.0;
112        z = 0.0;
113
114        vc[c][0] = x;
115        vc[c][1] = y;
116        vc[c][2] = z;
117
118        tc[c][0] = s;
119        tc[c][1] = t;
120        c++;
121    }
122
123    gset->addPrimitiveSet( new DrawArrays(PrimitiveSet::TRIANGLE_STRIP,0,c) );
124
125    // create the top of the tank.
126
127    int prev_c = c;
128
129    vc[c][0] = 0.0f;
130    vc[c][1] = 0.0f;
131    vc[c][2] = 1.0f;
132
133    tc[c][0] = 0.0f;
134    tc[c][1] = 0.0f;
135    c++;
136
137    for( i = 0; i <= 360; i += 18 )
138    {
139        float x, y, z;
140        float s, t;
141        float theta = osg::DegreesToRadians((float)i);
142
143        //    s = (float)i/360.0;
144        //   t = 1.0;
145        s = cosf( theta );
146        t = sinf( theta );
147
148        x = radius * cosf( theta );
149        y = radius * sinf( theta );
150        z = 1.0;
151
152        vc[c][0] = x;
153        vc[c][1] = y;
154        vc[c][2] = z;
155
156        tc[c][0] = s;
157        tc[c][1] = t;
158
159        c++;
160    }
161
162    for( i = 0; i < c; i++ )
163        conv( vc[i], mat, vc[i] );
164     
165     gset->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLE_FAN,prev_c,c-prev_c));
166
167
168
169
170    Texture2D *tex = new Texture2D;
171
172    tex->setWrap( Texture2D::WRAP_S, Texture2D::REPEAT );
173    tex->setWrap( Texture2D::WRAP_T, Texture2D::REPEAT );
174    tex->setImage(osgDB::readImageFile("Images/tank.rgb"));
175
176    StateSet *dstate = new StateSet;
177    dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON );
178    dstate->setTextureAttribute(0, new TexEnv );
179
180    gset->setStateSet( dstate );
181    geode->addDrawable( gset );
182
183    return geode;
184}
Note: See TracBrowser for help on using the browser.