| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 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 | |
|---|
| 37 | using namespace osg; |
|---|
| 38 | |
|---|
| 39 | extern void getDatabaseCenterRadius( float dbcenter[3], float *dbradius ); |
|---|
| 40 | |
|---|
| 41 | static float radius = 2.0; |
|---|
| 42 | static float dbcenter[3], dbradius; |
|---|
| 43 | |
|---|
| 44 | static 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 | |
|---|
| 62 | Node *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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 144 | |
|---|
| 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 | } |
|---|