| 1 | #include <math.h> |
|---|
| 2 | |
|---|
| 3 | #include <osg/GL> |
|---|
| 4 | #include <osg/Group> |
|---|
| 5 | #include <osg/Geode> |
|---|
| 6 | #include <osg/Geometry> |
|---|
| 7 | #include <osg/Texture2D> |
|---|
| 8 | #include <osg/TexEnv> |
|---|
| 9 | #include <osg/StateSet> |
|---|
| 10 | #include <osg/Matrix> |
|---|
| 11 | |
|---|
| 12 | #include <osgDB/ReadFile> |
|---|
| 13 | |
|---|
| 14 | #ifdef _MSC_VER |
|---|
| 15 | #pragma warning( disable : 4244 ) |
|---|
| 16 | #pragma warning( disable : 4305 ) |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | using namespace osg; |
|---|
| 20 | |
|---|
| 21 | extern void getDatabaseCenterRadius( float dbcenter[3], float *dbradius ); |
|---|
| 22 | |
|---|
| 23 | static float radius = 2.0; |
|---|
| 24 | static float dbcenter[3], dbradius; |
|---|
| 25 | |
|---|
| 26 | static void conv( const Vec3& a, const Matrix& mat, Vec3& b ) |
|---|
| 27 | { |
|---|
| 28 | int i; |
|---|
| 29 | Vec3 t; |
|---|
| 30 | |
|---|
| 31 | for( i = 0; i < 3; i++ ) |
|---|
| 32 | { |
|---|
| 33 | t[i] = (a[0] * mat(0,i)) + |
|---|
| 34 | (a[1] * mat(1,i)) + |
|---|
| 35 | (a[2] * mat(2,i)) + |
|---|
| 36 | mat(3,i); |
|---|
| 37 | } |
|---|
| 38 | b[0] = t[0]; |
|---|
| 39 | b[1] = t[1]; |
|---|
| 40 | b[2] = t[2]; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | Node *makeTank( void ) |
|---|
| 45 | { |
|---|
| 46 | |
|---|
| 47 | Geode *geode = new Geode; |
|---|
| 48 | |
|---|
| 49 | getDatabaseCenterRadius( dbcenter, &dbradius ); |
|---|
| 50 | |
|---|
| 51 | Matrix mat( |
|---|
| 52 | 0.05, 0, 0, 0, |
|---|
| 53 | 0, 0.05, 0, 0, |
|---|
| 54 | 0, 0, 0.05, 0, |
|---|
| 55 | 1.5999 - 0.3, |
|---|
| 56 | 3.1474, |
|---|
| 57 | dbcenter[2] + 0.6542 - 0.09, |
|---|
| 58 | 1 |
|---|
| 59 | ); |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | Vec3Array& vc = *(new Vec3Array(42+22)); |
|---|
| 63 | Vec2Array& tc = *(new Vec2Array(42+22)); |
|---|
| 64 | |
|---|
| 65 | Geometry *gset = new Geometry; |
|---|
| 66 | gset->setVertexArray( &vc ); |
|---|
| 67 | gset->setTexCoordArray( 0, &tc ); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | unsigned int i, c = 0; |
|---|
| 71 | for( i = 0; i <= 360; i += 18 ) |
|---|
| 72 | { |
|---|
| 73 | float x, y, z; |
|---|
| 74 | float s, t; |
|---|
| 75 | float theta = osg::DegreesToRadians((float)i); |
|---|
| 76 | |
|---|
| 77 | s = (float)i/90.0; |
|---|
| 78 | t = 1.0; |
|---|
| 79 | |
|---|
| 80 | x = radius * cosf( theta ); |
|---|
| 81 | y = radius * sinf( theta ); |
|---|
| 82 | z = 1.0; |
|---|
| 83 | |
|---|
| 84 | vc[c][0] = x; |
|---|
| 85 | vc[c][1] = y; |
|---|
| 86 | vc[c][2] = z; |
|---|
| 87 | |
|---|
| 88 | tc[c][0] = s; |
|---|
| 89 | tc[c][1] = t; |
|---|
| 90 | |
|---|
| 91 | c++; |
|---|
| 92 | |
|---|
| 93 | t = 0.0; |
|---|
| 94 | z = 0.0; |
|---|
| 95 | |
|---|
| 96 | vc[c][0] = x; |
|---|
| 97 | vc[c][1] = y; |
|---|
| 98 | vc[c][2] = z; |
|---|
| 99 | |
|---|
| 100 | tc[c][0] = s; |
|---|
| 101 | tc[c][1] = t; |
|---|
| 102 | c++; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | gset->addPrimitiveSet( new DrawArrays(PrimitiveSet::TRIANGLE_STRIP,0,c) ); |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | int prev_c = c; |
|---|
| 110 | |
|---|
| 111 | vc[c][0] = 0.0f; |
|---|
| 112 | vc[c][1] = 0.0f; |
|---|
| 113 | vc[c][2] = 1.0f; |
|---|
| 114 | |
|---|
| 115 | tc[c][0] = 0.0f; |
|---|
| 116 | tc[c][1] = 0.0f; |
|---|
| 117 | c++; |
|---|
| 118 | |
|---|
| 119 | for( i = 0; i <= 360; i += 18 ) |
|---|
| 120 | { |
|---|
| 121 | float x, y, z; |
|---|
| 122 | float s, t; |
|---|
| 123 | float theta = osg::DegreesToRadians((float)i); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | s = cosf( theta ); |
|---|
| 128 | t = sinf( theta ); |
|---|
| 129 | |
|---|
| 130 | x = radius * cosf( theta ); |
|---|
| 131 | y = radius * sinf( theta ); |
|---|
| 132 | z = 1.0; |
|---|
| 133 | |
|---|
| 134 | vc[c][0] = x; |
|---|
| 135 | vc[c][1] = y; |
|---|
| 136 | vc[c][2] = z; |
|---|
| 137 | |
|---|
| 138 | tc[c][0] = s; |
|---|
| 139 | tc[c][1] = t; |
|---|
| 140 | |
|---|
| 141 | c++; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | for( i = 0; i < c; i++ ) |
|---|
| 145 | conv( vc[i], mat, vc[i] ); |
|---|
| 146 | |
|---|
| 147 | gset->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLE_FAN,prev_c,c-prev_c)); |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | Texture2D *tex = new Texture2D; |
|---|
| 153 | |
|---|
| 154 | tex->setWrap( Texture2D::WRAP_S, Texture2D::REPEAT ); |
|---|
| 155 | tex->setWrap( Texture2D::WRAP_T, Texture2D::REPEAT ); |
|---|
| 156 | tex->setImage(osgDB::readImageFile("Images/tank.rgb")); |
|---|
| 157 | |
|---|
| 158 | StateSet *dstate = new StateSet; |
|---|
| 159 | dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON ); |
|---|
| 160 | dstate->setTextureAttribute(0, new TexEnv ); |
|---|
| 161 | |
|---|
| 162 | gset->setStateSet( dstate ); |
|---|
| 163 | geode->addDrawable( gset ); |
|---|
| 164 | |
|---|
| 165 | return geode; |
|---|
| 166 | } |
|---|