| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <stdlib.h> |
|---|
| 20 | |
|---|
| 21 | #include <osg/Billboard> |
|---|
| 22 | #include <osg/Group> |
|---|
| 23 | #include <osg/Geode> |
|---|
| 24 | #include <osg/Geometry> |
|---|
| 25 | #include <osg/Texture2D> |
|---|
| 26 | #include <osg/TexEnv> |
|---|
| 27 | #include <osg/BlendFunc> |
|---|
| 28 | #include <osg/AlphaFunc> |
|---|
| 29 | |
|---|
| 30 | #include <osgDB/ReadFile> |
|---|
| 31 | |
|---|
| 32 | #include "hat.h" |
|---|
| 33 | |
|---|
| 34 | #ifdef _MSC_VER |
|---|
| 35 | #pragma warning( disable : 4244 ) |
|---|
| 36 | #pragma warning( disable : 4305 ) |
|---|
| 37 | #endif |
|---|
| 38 | |
|---|
| 39 | using namespace osg; |
|---|
| 40 | |
|---|
| 41 | #define sqr(x) ((x)*(x)) |
|---|
| 42 | |
|---|
| 43 | extern void getDatabaseCenterRadius( float dbcenter[3], float *dbradius ); |
|---|
| 44 | |
|---|
| 45 | static float dbcenter[3], dbradius; |
|---|
| 46 | |
|---|
| 47 | static struct _tree |
|---|
| 48 | { |
|---|
| 49 | int n; |
|---|
| 50 | float x, y, z; |
|---|
| 51 | float w, h; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | trees[] = |
|---|
| 56 | { |
|---|
| 57 | { 0, -0.4769, -0.8972, -0.4011, 0.2000, 0.1200 }, |
|---|
| 58 | { 1, -0.2543, -0.9117, -0.3873, 0.2000, 0.1200 }, |
|---|
| 59 | { 2, -0.0424, -0.8538, -0.3728, 0.2000, 0.1200 }, |
|---|
| 60 | { 3, 0.1590, -0.8827, -0.3594, 0.2000, 0.1200 }, |
|---|
| 61 | { 4, -0.4981, -1.0853, -0.4016, 0.3500, 0.1200 }, |
|---|
| 62 | { 5, -0.5405, -1.2590, -0.4050, 0.2000, 0.1200 }, |
|---|
| 63 | { 6, -0.5723, -1.5339, -0.4152, 0.2000, 0.1200 }, |
|---|
| 64 | { 7, -0.6252, -1.8667, -0.4280, 0.2000, 0.1200 }, |
|---|
| 65 | { 8, -0.5617, -2.1851, -0.4309, 0.2000, 0.1200 }, |
|---|
| 66 | { 9, -0.5087, -2.4166, -0.4215, 0.2000, 0.1200 }, |
|---|
| 67 | { 10, -0.4345, -2.3443, -0.4214, 0.2000, 0.1200 }, |
|---|
| 68 | { 11, -3.0308, -1.5484, -0.4876, 0.2000, 0.1200 }, |
|---|
| 69 | { 12, -3.0202, -1.6497, -0.4963, 0.2000, 0.1200 }, |
|---|
| 70 | { 13, -2.9355, -1.8378, -0.4969, 0.2000, 0.1200 }, |
|---|
| 71 | { 14, -0.6040, -2.0259, -0.4300, 0.2000, 0.1200 }, |
|---|
| 72 | { 15, -0.5442, -1.3442, -0.4080, 0.1000, 0.1200 }, |
|---|
| 73 | { 16, -0.5639, -1.6885, -0.4201, 0.1000, 0.1200 }, |
|---|
| 74 | { 17, 0.9246, 3.4835, 0.5898, 0.2500, 0.1000 }, |
|---|
| 75 | { 18, 0.0787, 3.8687, 0.3329, 0.2500, 0.1200 }, |
|---|
| 76 | { 19, 0.2885, 3.7130, 0.4047, 0.2500, 0.1200 }, |
|---|
| 77 | { 20, 0.2033, 3.6228, 0.3704, 0.2500, 0.1200 }, |
|---|
| 78 | { 21, -0.2098, 3.9015, 0.2327, 0.2500, 0.1200 }, |
|---|
| 79 | { 22, -0.3738, 3.7376, 0.1722, 0.2500, 0.1200 }, |
|---|
| 80 | { 23, -0.2557, 3.6064, 0.1989, 0.2500, 0.1200 }, |
|---|
| 81 | { 24, 0.0590, 3.7294, 0.3210, 0.2500, 0.1200 }, |
|---|
| 82 | { 25, -0.4721, 3.8851, 0.1525, 0.2500, 0.1200 }, |
|---|
| 83 | { 26, 0.9639, 3.2048, 0.5868, 0.1200, 0.0800 }, |
|---|
| 84 | { 27, 0.7082, -1.0409, -0.3221, 0.1000, 0.1000 }, |
|---|
| 85 | { 28, -0.2426, -2.3442, -0.4150, 0.1000, 0.1380 }, |
|---|
| 86 | { 29, -0.1770, -2.4179, -0.4095, 0.1000, 0.1580 }, |
|---|
| 87 | { 30, -0.0852, -2.5327, -0.4056, 0.1000, 0.1130 }, |
|---|
| 88 | { 31, -0.0131, -2.6065, -0.4031, 0.1000, 0.1150 }, |
|---|
| 89 | { 32, 0.0787, -2.6638, -0.4012, 0.1000, 0.1510 }, |
|---|
| 90 | { 33, 0.1049, -2.7622, -0.3964, 0.1000, 0.1270 }, |
|---|
| 91 | { 34, 0.1770, -2.8687, -0.3953, 0.1000, 0.1100 }, |
|---|
| 92 | { 35, 0.3213, -2.9507, -0.3974, 0.1000, 0.1190 }, |
|---|
| 93 | { 36, 0.4065, -3.0163, -0.4014, 0.1000, 0.1120 }, |
|---|
| 94 | { 37, 0.3738, -3.1802, -0.4025, 0.1000, 0.1860 }, |
|---|
| 95 | { 38, 0.5508, -3.2048, -0.3966, 0.1000, 0.1490 }, |
|---|
| 96 | { 39, 0.5836, -3.3031, -0.3900, 0.1000, 0.1670 }, |
|---|
| 97 | { 40, -0.3082, -2.7212, -0.3933, 0.1000, 0.1840 }, |
|---|
| 98 | { 41, -0.1967, -2.6474, -0.4017, 0.1000, 0.1600 }, |
|---|
| 99 | { 42, -0.1180, -2.7458, -0.3980, 0.1000, 0.1250 }, |
|---|
| 100 | { 43, -0.3344, -2.8359, -0.3964, 0.1000, 0.1430 }, |
|---|
| 101 | { 44, -0.2492, -2.8933, -0.3838, 0.1000, 0.1890 }, |
|---|
| 102 | { 45, -0.1246, -3.0491, -0.3768, 0.1000, 0.1830 }, |
|---|
| 103 | { 46, 0.0000, -3.0818, -0.3696, 0.1000, 0.1370 }, |
|---|
| 104 | { 47, -0.2295, -3.0409, -0.3706, 0.1000, 0.1660 }, |
|---|
| 105 | { 48, -1.3245, 2.6638, 0.0733, 0.0500, 0.0500 }, |
|---|
| 106 | { 49, 2.2425, -1.5491, -0.2821, 0.2300, 0.1200 }, |
|---|
| 107 | { 50, 0.2164, -2.1311, -0.4000, 0.1000, 0.0690 }, |
|---|
| 108 | { 51, 0.2885, -2.2130, -0.4000, 0.1000, 0.0790 }, |
|---|
| 109 | { 52, 0.3606, -2.2786, -0.4000, 0.1000, 0.0565 }, |
|---|
| 110 | { 53, 0.4328, -2.3442, -0.4000, 0.1000, 0.0575 }, |
|---|
| 111 | { 54, 0.5246, -2.4343, -0.4086, 0.1000, 0.0755 }, |
|---|
| 112 | { 55, 0.6360, -2.5245, -0.4079, 0.1000, 0.0635 }, |
|---|
| 113 | { 56, 0.7541, -2.4261, -0.4007, 0.1000, 0.0550 }, |
|---|
| 114 | { 57, 0.7934, -2.2786, -0.3944, 0.1000, 0.0595 }, |
|---|
| 115 | { 58, 1.0295, -2.2868, -0.3837, 0.1000, 0.0560 }, |
|---|
| 116 | { 59, 0.8459, -2.6474, -0.4051, 0.1000, 0.0930 }, |
|---|
| 117 | { 60, 1.0426, -2.6884, -0.4001, 0.1000, 0.0745 }, |
|---|
| 118 | { 61, 1.1475, -2.7458, -0.3883, 0.1000, 0.0835 }, |
|---|
| 119 | { 62, -0.1967, -1.4180, -0.3988, 0.1000, 0.0920 }, |
|---|
| 120 | { 63, -0.0131, -1.2704, -0.3856, 0.1000, 0.0690 }, |
|---|
| 121 | { 64, 0.2098, -1.2049, -0.3664, 0.1000, 0.0790 }, |
|---|
| 122 | { 65, 0.3410, -1.3196, -0.3652, 0.1000, 0.0565 }, |
|---|
| 123 | { 66, 0.5705, -1.2704, -0.3467, 0.1000, 0.0575 }, |
|---|
| 124 | { 67, 0.6360, -1.4344, -0.3532, 0.1000, 0.0755 }, |
|---|
| 125 | { 68, 0.9246, -1.4180, -0.3329, 0.1000, 0.0635 }, |
|---|
| 126 | { 69, 1.0623, -1.3360, -0.3183, 0.1000, 0.0550 }, |
|---|
| 127 | { 70, 1.2393, -1.3934, -0.3103, 0.1000, 0.0595 }, |
|---|
| 128 | { 71, 1.3639, -1.4753, -0.3079, 0.1000, 0.0560 }, |
|---|
| 129 | { 72, 1.4819, -1.5983, -0.3210, 0.1000, 0.0930 }, |
|---|
| 130 | { 73, 1.7835, -1.5819, -0.3065, 0.1000, 0.0745 }, |
|---|
| 131 | { 74, 1.9343, -2.1065, -0.3307, 0.1000, 0.0835 }, |
|---|
| 132 | { 75, 2.1245, -2.3196, -0.3314, 0.1000, 0.0920 }, |
|---|
| 133 | { 76, 2.2556, -2.3032, -0.3230, 0.1000, 0.0800 }, |
|---|
| 134 | { 77, 2.4196, -2.3688, -0.3165, 0.1000, 0.0625 }, |
|---|
| 135 | { 78, 1.7835, -2.5327, -0.3543, 0.1000, 0.0715 }, |
|---|
| 136 | { 79, 1.7180, -2.8933, -0.3742, 0.1000, 0.0945 }, |
|---|
| 137 | { 80, 1.9343, -3.0409, -0.3727, 0.1000, 0.0915 }, |
|---|
| 138 | { 81, 2.4524, -3.4671, -0.3900, 0.1000, 0.0685 }, |
|---|
| 139 | { 82, 2.4786, -2.8851, -0.3538, 0.1000, 0.0830 }, |
|---|
| 140 | { 83, 2.3343, -2.6228, -0.3420, 0.1000, 0.0830 }, |
|---|
| 141 | { 84, 2.8130, -2.0737, -0.2706, 0.1000, 0.0890 }, |
|---|
| 142 | { 85, 2.6360, -1.8278, -0.2661, 0.1000, 0.0975 }, |
|---|
| 143 | { 86, 2.3958, -1.7130, -0.2774, 0.2000, 0.1555 }, |
|---|
| 144 | { 87, 2.2688, -1.2868, -0.2646, 0.1000, 0.0835 }, |
|---|
| 145 | { 88, 2.4196, -1.1147, -0.2486, 0.1000, 0.0770 }, |
|---|
| 146 | { 89, 2.7802, -2.3933, -0.3017, 0.1000, 0.0655 }, |
|---|
| 147 | { 90, 3.0163, -2.4179, -0.2905, 0.1000, 0.0725 }, |
|---|
| 148 | { 91, 2.9310, -2.2540, -0.2798, 0.1000, 0.0910 }, |
|---|
| 149 | { 92, 2.6622, -2.0983, -0.2823, 0.1000, 0.0680 }, |
|---|
| 150 | { 93, 2.3147, -1.9753, -0.2973, 0.1000, 0.0620 }, |
|---|
| 151 | { 94, 2.1573, -1.8770, -0.3013, 0.1000, 0.0525 }, |
|---|
| 152 | { 95, 2.0196, -1.7868, -0.3044, 0.1000, 0.0970 }, |
|---|
| 153 | { 96, 2.7802, -3.3031, -0.3900, 0.1000, 0.0510 }, |
|---|
| 154 | { 97, 2.8589, -3.1720, -0.3900, 0.1000, 0.0755 }, |
|---|
| 155 | { 98, 3.0163, -2.8114, -0.3383, 0.1000, 0.0835 }, |
|---|
| 156 | { 99, 3.5081, -2.4179, -0.2558, 0.1000, 0.0770 }, |
|---|
| 157 | { 100, 3.5277, -2.3196, -0.2366, 0.1000, 0.0765 }, |
|---|
| 158 | { 101, 3.6654, -2.5819, -0.2566, 0.1000, 0.0805 }, |
|---|
| 159 | { 102, 3.7179, -2.7622, -0.2706, 0.1000, 0.0980 }, |
|---|
| 160 | { 103, 3.7769, -2.4671, -0.2339, 0.1000, 0.0640 }, |
|---|
| 161 | { 104, 3.3441, -2.4671, -0.2693, 0.1000, 0.0940 }, |
|---|
| 162 | { -1, 0, 0, 0, 0, 0 }, |
|---|
| 163 | }; |
|---|
| 164 | |
|---|
| 165 | static Geometry *makeTree( _tree *tree, StateSet *dstate ) |
|---|
| 166 | { |
|---|
| 167 | float vv[][3] = |
|---|
| 168 | { |
|---|
| 169 | { -tree->w/2.0, 0.0, 0.0 }, |
|---|
| 170 | { tree->w/2.0, 0.0, 0.0 }, |
|---|
| 171 | { tree->w/2.0, 0.0, 2 * tree->h }, |
|---|
| 172 | { -tree->w/2.0, 0.0, 2 * tree->h }, |
|---|
| 173 | }; |
|---|
| 174 | |
|---|
| 175 | Vec3Array& v = *(new Vec3Array(4)); |
|---|
| 176 | Vec2Array& t = *(new Vec2Array(4)); |
|---|
| 177 | Vec4Array& l = *(new Vec4Array(1)); |
|---|
| 178 | |
|---|
| 179 | int i; |
|---|
| 180 | |
|---|
| 181 | l[0][0] = l[0][1] = l[0][2] = l[0][3] = 1; |
|---|
| 182 | |
|---|
| 183 | for( i = 0; i < 4; i++ ) |
|---|
| 184 | { |
|---|
| 185 | v[i][0] = vv[i][0]; |
|---|
| 186 | v[i][1] = vv[i][1]; |
|---|
| 187 | v[i][2] = vv[i][2]; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | t[0][0] = 0.0; t[0][1] = 0.0; |
|---|
| 191 | t[1][0] = 1.0; t[1][1] = 0.0; |
|---|
| 192 | t[2][0] = 1.0; t[2][1] = 1.0; |
|---|
| 193 | t[3][0] = 0.0; t[3][1] = 1.0; |
|---|
| 194 | |
|---|
| 195 | Geometry *geom = new Geometry; |
|---|
| 196 | |
|---|
| 197 | geom->setVertexArray( &v ); |
|---|
| 198 | |
|---|
| 199 | geom->setTexCoordArray( 0, &t ); |
|---|
| 200 | |
|---|
| 201 | geom->setColorArray( &l ); |
|---|
| 202 | geom->setColorBinding( Geometry::BIND_OVERALL ); |
|---|
| 203 | |
|---|
| 204 | geom->addPrimitiveSet( new DrawArrays(PrimitiveSet::QUADS,0,4) ); |
|---|
| 205 | |
|---|
| 206 | geom->setStateSet( dstate ); |
|---|
| 207 | |
|---|
| 208 | return geom; |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | static float ttx, tty; |
|---|
| 213 | |
|---|
| 214 | static int ct( const void *a, const void *b ) |
|---|
| 215 | { |
|---|
| 216 | _tree *ta = (_tree *)a; |
|---|
| 217 | _tree *tb = (_tree *)b; |
|---|
| 218 | |
|---|
| 219 | float da = sqrtf( sqr(ta->x - ttx) + sqr(ta->y - tty) ); |
|---|
| 220 | float db = sqrtf( sqr(tb->x - ttx) + sqr(tb->y - tty) ); |
|---|
| 221 | |
|---|
| 222 | if( da < db ) |
|---|
| 223 | return -1; |
|---|
| 224 | else |
|---|
| 225 | return 1; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | Node *makeTrees( void ) |
|---|
| 230 | { |
|---|
| 231 | Group *group = new Group; |
|---|
| 232 | int i; |
|---|
| 233 | |
|---|
| 234 | getDatabaseCenterRadius( dbcenter, &dbradius ); |
|---|
| 235 | struct _tree *t; |
|---|
| 236 | |
|---|
| 237 | Texture2D *tex = new Texture2D; |
|---|
| 238 | tex->setImage(osgDB::readImageFile("Images/tree0.rgba")); |
|---|
| 239 | |
|---|
| 240 | StateSet *dstate = new StateSet; |
|---|
| 241 | |
|---|
| 242 | dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON ); |
|---|
| 243 | dstate->setTextureAttribute(0, new TexEnv ); |
|---|
| 244 | |
|---|
| 245 | dstate->setAttributeAndModes( new BlendFunc, StateAttribute::ON ); |
|---|
| 246 | |
|---|
| 247 | AlphaFunc* alphaFunc = new AlphaFunc; |
|---|
| 248 | alphaFunc->setFunction(AlphaFunc::GEQUAL,0.05f); |
|---|
| 249 | dstate->setAttributeAndModes( alphaFunc, StateAttribute::ON ); |
|---|
| 250 | |
|---|
| 251 | dstate->setMode( GL_LIGHTING, StateAttribute::OFF ); |
|---|
| 252 | |
|---|
| 253 | dstate->setRenderingHint( StateSet::TRANSPARENT_BIN ); |
|---|
| 254 | |
|---|
| 255 | int tt[] = { 15, 30, 45, 58, 72, 75, 93, 96, 105, -1 }; |
|---|
| 256 | int *ttp = tt; |
|---|
| 257 | |
|---|
| 258 | i = 0; |
|---|
| 259 | while( i < 105 ) |
|---|
| 260 | { |
|---|
| 261 | ttx = trees[i].x; |
|---|
| 262 | tty = trees[i].y; |
|---|
| 263 | qsort( &trees[i], 105 - i, sizeof( _tree ), ct ); |
|---|
| 264 | |
|---|
| 265 | i += *ttp; |
|---|
| 266 | ttp++; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | t = trees; |
|---|
| 270 | i = 0; |
|---|
| 271 | ttp = tt; |
|---|
| 272 | while( *ttp != -1 ) |
|---|
| 273 | { |
|---|
| 274 | Billboard *bb = new Billboard; |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | for( ; i < (*ttp); i++ ) |
|---|
| 278 | { |
|---|
| 279 | t->x -= 0.3f; |
|---|
| 280 | float h = Hat(t->x, t->y, t->z ); |
|---|
| 281 | Vec3 pos( t->x, t->y, t->z-h ); |
|---|
| 282 | Geometry *geom = makeTree( t, dstate ); |
|---|
| 283 | bb->addDrawable( geom, pos ); |
|---|
| 284 | t++; |
|---|
| 285 | } |
|---|
| 286 | group->addChild( bb ); |
|---|
| 287 | ttp++; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | return group; |
|---|
| 291 | } |
|---|