]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.cxx
0e63d7e555ada0be121f20df09920294e7d57280
[flightgear.git] / src / Scenery / tileentry.cxx
1 // tileentry.cxx -- routines to handle a scenery tile
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998 - 2001  Curtis L. Olson  - curt@flightgear.org
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <simgear/bucket/newbucket.hxx>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/math/sg_geodesy.hxx>
33 #include <simgear/math/sg_random.h>
34 #include <simgear/misc/sgstream.hxx>
35
36 #include <Aircraft/aircraft.hxx>
37 #include <Include/general.hxx>
38 #include <Main/globals.hxx>
39 #include <Main/viewer.hxx>
40 #include <Scenery/scenery.hxx>
41 #include <Time/light.hxx>
42 #include <Objects/apt_signs.hxx>
43 #include <Objects/matlib.hxx>
44 #include <Objects/newmat.hxx>
45 #include <Objects/obj.hxx>
46
47 #include "tileentry.hxx"
48 #include "tilemgr.hxx"
49
50
51 // Constructor
52 FGTileEntry::FGTileEntry ( const SGBucket& b )
53     : ncount( 0 ),
54       center( Point3D( 0.0 ) ),
55       tile_bucket( b ),
56       terra_transform( new ssgTransform ),
57       terra_range( new ssgRangeSelector ),
58       loaded(false),
59       pending_models(0)
60 {
61     nodes.clear();
62
63     // update the contents
64     // if ( vec3_ptrs.size() || vec2_ptrs.size() || index_ptrs.size() ) {
65     //     SG_LOG( SG_TERRAIN, SG_ALERT, 
66     //             "Attempting to overwrite existing or"
67     //             << " not properly freed leaf data." );
68     //     exit(-1);
69     // }
70 }
71
72
73 // Destructor
74 FGTileEntry::~FGTileEntry () {
75     // cout << "nodes = " << nodes.size() << endl;;
76     // delete[] nodes;
77 }
78
79
80 #if 0
81 // This is the current method cut and pasted from 
82 //  FGTileEntry::load( const SGPath& base, bool is_base )
83 void
84 FGTileEntry::WorldCoordinate( sgCoord *obj_pos, Point3D center,
85                               double lat, double lon, double elev, double hdg)
86 {
87     // setup transforms
88     Point3D geod( lon * SGD_DEGREES_TO_RADIANS,
89                   lat * SGD_DEGREES_TO_RADIANS,
90                   elev );
91         
92     Point3D world_pos = sgGeodToCart( geod );
93     Point3D offset = world_pos - center;
94         
95     sgMat4 POS;
96     sgMakeTransMat4( POS, offset.x(), offset.y(), offset.z() );
97
98     sgVec3 obj_rt, obj_up;
99     sgSetVec3( obj_rt, 0.0, 1.0, 0.0); // Y axis
100     sgSetVec3( obj_up, 0.0, 0.0, 1.0); // Z axis
101
102     sgMat4 ROT_lon, ROT_lat, ROT_hdg;
103     sgMakeRotMat4( ROT_lon, lon, obj_up );
104     sgMakeRotMat4( ROT_lat, 90 - lat, obj_rt );
105     sgMakeRotMat4( ROT_hdg, hdg, obj_up );
106
107     sgMat4 TUX;
108     sgCopyMat4( TUX, ROT_hdg );
109     sgPostMultMat4( TUX, ROT_lat );
110     sgPostMultMat4( TUX, ROT_lon );
111     sgPostMultMat4( TUX, POS );
112
113     sgSetCoord( obj_pos, TUX );
114 }
115 #endif
116
117
118 // Norman's 'fast hack' for above
119 static void WorldCoordinate( sgCoord *obj_pos, Point3D center, double lat,
120                              double lon, double elev, double hdg )
121 {
122     double lon_rad = lon * SGD_DEGREES_TO_RADIANS;
123     double lat_rad = lat * SGD_DEGREES_TO_RADIANS;
124     double hdg_rad = hdg * SGD_DEGREES_TO_RADIANS;
125
126     // setup transforms
127     Point3D geod( lon_rad, lat_rad, elev );
128         
129     Point3D world_pos = sgGeodToCart( geod );
130     Point3D offset = world_pos - center;
131
132     sgMat4 mat;
133
134     SGfloat sin_lat = (SGfloat)sin( lat_rad );
135     SGfloat cos_lat = (SGfloat)cos( lat_rad );
136     SGfloat cos_lon = (SGfloat)cos( lon_rad );
137     SGfloat sin_lon = (SGfloat)sin( lon_rad );
138     SGfloat sin_hdg = (SGfloat)sin( hdg_rad ) ;
139     SGfloat cos_hdg = (SGfloat)cos( hdg_rad ) ;
140
141     mat[0][0] =  cos_hdg * (SGfloat)sin_lat * (SGfloat)cos_lon - sin_hdg * (SGfloat)sin_lon;
142     mat[0][1] =  cos_hdg * (SGfloat)sin_lat * (SGfloat)sin_lon + sin_hdg * (SGfloat)cos_lon;
143     mat[0][2] = -cos_hdg * (SGfloat)cos_lat;
144     mat[0][3] =  SG_ZERO;
145
146     mat[1][0] = -sin_hdg * (SGfloat)sin_lat * (SGfloat)cos_lon - cos_hdg * (SGfloat)sin_lon;
147     mat[1][1] = -sin_hdg * (SGfloat)sin_lat * (SGfloat)sin_lon + cos_hdg * (SGfloat)cos_lon;
148     mat[1][2] =  sin_hdg * (SGfloat)cos_lat;
149     mat[1][3] =  SG_ZERO;
150
151     mat[2][0] = (SGfloat)cos_lat * (SGfloat)cos_lon;
152     mat[2][1] = (SGfloat)cos_lat * (SGfloat)sin_lon;
153     mat[2][2] = (SGfloat)sin_lat;
154     mat[2][3] =  SG_ZERO;
155
156     mat[3][0] = offset.x();
157     mat[3][1] = offset.y();
158     mat[3][2] = offset.z();
159     mat[3][3] = SG_ONE ;
160
161     sgSetCoord( obj_pos, mat );
162 }
163
164
165 // recurse an ssg tree and call removeKid() on every node from the
166 // bottom up.  Leaves the original branch in existance, but empty so
167 // it can be removed by the calling routine.
168 static void my_remove_branch( ssgBranch * branch ) {
169     for ( ssgEntity *k = branch->getKid( 0 );
170           k != NULL; 
171           k = branch->getNextKid() )
172     {
173         if ( k -> isAKindOf ( ssgTypeBranch() ) ) {
174             my_remove_branch( (ssgBranch *)k );
175             branch -> removeKid ( k );
176         } else if ( k -> isAKindOf ( ssgTypeLeaf() ) ) {
177             branch -> removeKid ( k ) ;
178         }
179     }
180 }
181
182 // ADA
183 #define TEXRES_X 256
184 #define TEXRES_Y 256
185 unsigned char env_map[TEXRES_X][TEXRES_Y][4];
186 // SetColor & SetColor2 functions were provided by Christian Mayer (26 April 2001) - used without change
187 void setColor(float x, float y, float z, float angular_size, float r,
188 float g, float b, float a)
189 {
190     //normalize
191     float inv_length = 1.0 / sqrt(x*x + y*y + z*z);
192     x *= inv_length; y *= inv_length; z *= inv_length;
193   
194     float cos_angular_size = cos(angular_size*(22.0/7.0)/180.0);
195
196     for( int s = 0; s < TEXRES_X; s++) {
197         for( int t = 0; t < TEXRES_Y; t++) {
198
199             float s_2 = (float)s/TEXRES_X - 0.5; // centre of texture 0,0
200             float t_2 = (float)t/TEXRES_Y - 0.5; // elev
201
202             float rx, ry, rz;
203
204             if ((1.0 - 4.0*s_2*s_2 - 4.0*t_2*t_2) >= 0.0) {
205                 // sphere
206                 float m = 4.0 * sqrt(1.0 - 4.0*s_2*s_2 - 4.0*t_2*t_2);
207                 rx = m * s_2;
208                 ry = m * t_2;
209                 rz = m*m / 8.0 - 1.0;
210             } else {
211                 // singularity
212                 rx = 0.0;
213                 ry = 0.0;
214                 rz = -1.0;
215             }
216
217             float tx = rx;  //mirroring on the z=0 plane
218             float ty = ry;  //assumes that the normal is allways
219             float tz = -rz; //n(0.0, 0.0, 1.0)
220          
221             if ( cos_angular_size < (x*tx + y*ty + z*tz) ) {
222                 env_map[s][t][0] = (unsigned char) r * 255;  
223                 env_map[s][t][1] = (unsigned char) g * 255;
224                 env_map[s][t][2] = (unsigned char) b * 255;
225                 env_map[s][t][3] = (unsigned char) a * 255;
226             }
227         }
228     }
229 }
230
231 // elevation_size, float azimuth_size are the *total* angular size of the light
232 void setColor2(float elevation_size,float azimuth_size, float r, float g, float b, float a)
233 {
234     for( int s = 0; s < TEXRES_X; s++) {
235         for( int t = 0; t < TEXRES_Y; t++) {
236             float s_2 = (float)s/TEXRES_X - 0.5;
237             float t_2 = (float)t/TEXRES_Y - 0.5;
238
239             float rx, ry, rz;
240
241             if ((1.0 - 4.0*s_2*s_2 - 4.0*t_2*t_2) >= 0.0) {
242
243                 float m = 4.0 * sqrt(1.0 - 4.0*s_2*s_2 - 4.0*t_2*t_2);
244                 rx = m * s_2;
245                 ry = m * t_2;
246                 rz = m*m / 8.0 - 1.0;
247             } else {
248                 rx = 0.0;
249                 ry = 0.0;
250                 rz = -1.0;
251             }
252
253             float tx = rx;  //mirroring on the z=0 plane to reverse
254             float ty = ry;  //OpenGLs automatic mirroring
255             float tz = -rz; 
256
257             //get elevation => project t onto the x-z-plane
258             float tz_proj1 = tz / sqrt(tx*tx + tz*tz);
259             float televation = acos( -tz_proj1 ) * 180.0 / 3.1415;
260
261             //get azi => project t onto the y-z-plane
262             float tz_proj2 = tz / sqrt(ty*ty + tz*tz);
263             float tazimuth = acos( -tz_proj2 ) * 180.0 / 3.1415;
264
265             //note televation and tazimuth are the angles *between* the
266             //temporary vector and the normal (0,0,-1). They are *NOT*
267             //the elevation and azimuth angles
268
269             //square:
270             //if (((elevation_size > televation) || (elevation_size < -televation)) &&
271             //    ((azimuth_size   > tazimuth  ) || (azimuth_size   < -tazimuth  ))) 
272             //elliptical
273             if (((televation*televation) / (elevation_size*elevation_size / 4.0) +
274                  (tazimuth  *tazimuth  ) / (azimuth_size  *azimuth_size   / 4.0)) <= 1.0)
275             {
276                 env_map[s][t][0] = (unsigned char) r * 255;  
277                 env_map[s][t][1] = (unsigned char) g * 255;
278                 env_map[s][t][2] = (unsigned char) b * 255;
279                 env_map[s][t][3] = (unsigned char) a * 255;
280             }
281         }
282     }
283 }
284
285 // 23 March 2001
286 // This function performs billboarding of polygons drawn using the UP and RIGHT vectors obtained
287 // from the transpose of the MODEL_VIEW_MATRIX of the ssg_current_context. Each polygon is drawn
288 // at the coordinate array and material state as passed thro arguments.
289 void *fgBillboard( ssgBranch *lightmaps, ssgVertexArray *light_maps, ssgSimpleState *lightmap_state, float size) {
290     sgMat4 tmat;
291     sgVec3 rt, up, nrt, nup, pt, quads[4], lmaps[4];
292
293     ssgGetModelviewMatrix ( tmat );
294     sgSetVec3 (rt, tmat[0][0], tmat[1][0], tmat[2][0]);
295     sgSetVec3 (up, tmat[0][1], tmat[1][1], tmat[2][1]);
296     sgSetVec3 (nrt, tmat[0][0], tmat[1][0], tmat[2][0]);
297     sgSetVec3 (nup, tmat[0][1], tmat[1][1], tmat[2][1]);
298     sgNegateVec3 (nrt);
299     sgNegateVec3 (nup);
300
301     sgAddVec3 (quads[0], nrt, nup);
302     sgAddVec3 (quads[1],  rt, nup);
303     sgAddVec3 (quads[2],  rt,  up);
304     sgAddVec3 (quads[3], nrt,  up);
305
306     sgScaleVec3 (quads[0], size);
307     sgScaleVec3 (quads[1], size);
308     sgScaleVec3 (quads[2], size);
309     sgScaleVec3 (quads[3], size);
310
311     sgVec4 color;
312     sgSetVec4( color, 1.0, 1.0, 0.0, 1.0 );
313
314     sgVec2 texcoords[4];
315     sgSetVec2( texcoords[0], 1.0, 1.0 );
316     sgSetVec2( texcoords[1], 0.0, 1.0 );
317     sgSetVec2( texcoords[2], 0.0, 0.0 );
318     sgSetVec2( texcoords[3], 1.0, 0.0 );
319     
320     for (int j = 0; j < 4; j++ ) {
321         sgCopyVec3(lmaps[j]     ,quads[j]);
322     }
323
324     for ( int i = 0; i < light_maps->getNum(); ++i ) {
325         // Allocate ssg structure
326         ssgVertexArray   *vl = new ssgVertexArray( 1 );
327         ssgTexCoordArray *tl = new ssgTexCoordArray( 1 );
328         ssgColourArray   *cl = new ssgColourArray( 1 );
329
330         float *temp = light_maps->get(i);
331         sgSetVec3(pt,temp[0],temp[1],temp[2]);
332
333         for (int k=0; k<4; k++) {
334             sgAddVec3( quads[k],lmaps[k], pt );                 
335             vl->add(quads[k]);
336             tl->add(texcoords[k]);
337             cl->add(color);
338         }
339
340         ssgLeaf *leaf = NULL;
341         leaf = new ssgVtxTable ( GL_TRIANGLE_FAN, vl, NULL, tl, cl );
342         leaf->setState( lightmap_state );
343         lightmaps->addKid( leaf );
344     }
345
346     return NULL;
347 }
348
349 ssgBranch* FGTileEntry::gen_runway_lights( ssgVertexArray *points,ssgVertexArray *normal,
350                                            ssgVertexArray *dir, int type[]) 
351 {
352
353     //************** HARD CODED RUNWAY LIGHT TEXTURES BEGIN ************************    
354     GLuint texEdge, texTaxi, texCenter, texTouchdown;
355     GLuint texThreshold, texCrossbar, texUndershoot, texApproach;
356     GLuint texRabbit, texVasi, texWhite, texRed, texGreen, texYellow;
357
358     //VASI lights
359     setColor(0.0,0.0,1.0,360.0, 0, 0, 0, 0);
360     setColor2(10.0, 40.0, 1, 1, 1, 1);
361     setColor2(6.0, 40.0, 1, 0.5, 0.5, 1);
362     setColor2(5.0, 40.0, 1, 0, 0, 1);
363     glGenTextures(1, &texVasi);
364     glBindTexture(GL_TEXTURE_2D, texVasi);
365     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
366     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
367     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
368     ssgSimpleState *vasi_state;
369     vasi_state = new ssgSimpleState();
370     vasi_state->ref();
371     vasi_state->setTexture( texVasi );
372     vasi_state->disable( GL_LIGHTING );
373     vasi_state->enable( GL_TEXTURE_2D );
374     vasi_state->setShadeModel( GL_SMOOTH );
375
376     //EDGE
377     setColor(0.0,0.0,-1.0,180.0, 1, 1, 0.5, 1);
378     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
379     glGenTextures(1, &texEdge);
380     glBindTexture(GL_TEXTURE_2D, texEdge);
381     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
382     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
383     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
384     ssgSimpleState *edge_state;
385     edge_state = new ssgSimpleState();
386     edge_state->ref();
387     edge_state->setTexture( texEdge );
388     edge_state->disable( GL_LIGHTING );
389     edge_state->enable( GL_TEXTURE_2D );
390     edge_state->setShadeModel( GL_SMOOTH );
391
392     //TOUCHDOWN
393     setColor(0.0,0.0,-1.0,180.0, 0, 1, 0, 1);
394     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
395     glGenTextures(1, &texTouchdown);
396     glBindTexture(GL_TEXTURE_2D, texTouchdown);
397     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
398     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
399     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
400     ssgSimpleState *touchdown_state;
401     touchdown_state = new ssgSimpleState();
402     touchdown_state->ref();
403     touchdown_state->setTexture( texTouchdown );
404     touchdown_state->disable( GL_LIGHTING );
405     touchdown_state->enable( GL_TEXTURE_2D );
406     touchdown_state->setShadeModel( GL_SMOOTH );
407         
408     //THRESHOLD
409     setColor(0.0,0.0,-1.0,180.0, 1, 0, 0, 1);
410     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
411     glGenTextures(1, &texThreshold);
412     glBindTexture(GL_TEXTURE_2D, texThreshold);
413     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
414     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
415     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
416     ssgSimpleState *threshold_state;
417     threshold_state = new ssgSimpleState();
418     threshold_state->ref();
419     threshold_state->setTexture( texThreshold );
420     threshold_state->disable( GL_LIGHTING );
421     threshold_state->enable( GL_TEXTURE_2D );
422     threshold_state->setShadeModel( GL_SMOOTH );
423
424     //TAXI
425     setColor(0.0,0.0,-1.0,180.0, 0, 0, 1, 1);
426     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
427     glGenTextures(1, &texTaxi);
428     glBindTexture(GL_TEXTURE_2D, texTaxi);
429     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
430     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
431     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
432     ssgSimpleState *taxi_state;
433     taxi_state = new ssgSimpleState();
434     taxi_state->ref();
435     taxi_state->setTexture( texTaxi );
436     taxi_state->disable( GL_LIGHTING );
437     taxi_state->enable( GL_TEXTURE_2D );
438     taxi_state->setShadeModel( GL_SMOOTH );
439
440     //WHITE
441     setColor(0.0,0.0,-1.0,180.0, 1, 1, 1, 1);
442     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
443     glGenTextures(1, &texWhite);
444     glBindTexture(GL_TEXTURE_2D, texWhite);
445     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
446     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
447     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
448     ssgSimpleState *white_state;
449     white_state = new ssgSimpleState();
450     white_state->ref();
451     white_state->setTexture( texWhite );
452     white_state->disable( GL_LIGHTING );
453     white_state->enable( GL_TEXTURE_2D );
454     white_state->setShadeModel( GL_SMOOTH );
455
456     //RED
457     setColor(0.0,0.0,-1.0,180.0, 1, 0, 0, 1);
458     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
459     glGenTextures(1, &texRed);
460     glBindTexture(GL_TEXTURE_2D, texRed);
461     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
462     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
463     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
464     ssgSimpleState *red_state;
465     red_state = new ssgSimpleState();
466     red_state->ref();
467     red_state->setTexture( texRed );
468     red_state->disable( GL_LIGHTING );
469     red_state->enable( GL_TEXTURE_2D );
470     red_state->setShadeModel( GL_SMOOTH );
471
472     //GREEN
473     setColor(0.0,0.0,-1.0,180.0, 0, 1, 0, 1);
474     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
475     glGenTextures(1, &texGreen);
476     glBindTexture(GL_TEXTURE_2D, texGreen);
477     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
478     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
479     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
480     ssgSimpleState *green_state;
481     green_state = new ssgSimpleState();
482     green_state->ref();
483     green_state->setTexture( texGreen );
484     green_state->disable( GL_LIGHTING );
485     green_state->enable( GL_TEXTURE_2D );
486     green_state->setShadeModel( GL_SMOOTH );
487
488     //YELLOW
489     setColor(0.0,0.0,-1.0,180.0, 1, 1, 0, 1);
490     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
491     glGenTextures(1, &texYellow);
492     glBindTexture(GL_TEXTURE_2D, texYellow);
493     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
494     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
495     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEXRES_X, TEXRES_Y, 0, GL_RGBA, GL_UNSIGNED_BYTE, env_map);
496     ssgSimpleState *yellow_state;
497     yellow_state = new ssgSimpleState();
498     yellow_state->ref();
499     yellow_state->setTexture( texYellow );
500     yellow_state->disable( GL_LIGHTING );
501     yellow_state->enable( GL_TEXTURE_2D );
502     yellow_state->setShadeModel( GL_SMOOTH );
503 //************** HARD CODED RUNWAY LIGHT TEXTURES END ************************    
504
505     ssgBranch *runway_lights = new ssgBranch;
506     sgVec3 v2,v3,inf,side;
507
508     ssgLeaf *leaf1 = NULL;
509     ssgLeaf *leaf2 = NULL;
510     ssgLeaf *leaf7 = NULL;
511     ssgLeaf *leaf8 = NULL;
512     ssgLeaf *leaf9 = NULL;
513
514     ssgVertexArray   *vlw = new ssgVertexArray( 1 );
515     ssgNormalArray   *nlw = new ssgNormalArray( 1 );
516     ssgVertexArray   *vlt = new ssgVertexArray( 1 );
517     ssgNormalArray   *nlt = new ssgNormalArray( 1 );
518     ssgVertexArray   *vlr = new ssgVertexArray( 1 );
519     ssgNormalArray   *nlr = new ssgNormalArray( 1 );
520     ssgVertexArray   *vlg = new ssgVertexArray( 1 );
521     ssgNormalArray   *nlg = new ssgNormalArray( 1 );
522     ssgVertexArray   *vly = new ssgVertexArray( 1 );
523     ssgNormalArray   *nly = new ssgNormalArray( 1 );
524
525     for ( int i = 0; i < points->getNum()-1; i=i++ ) {
526
527         // Allocate ssg structure
528         ssgVertexArray   *vl = new ssgVertexArray( 1 );
529         ssgNormalArray   *nl = new ssgNormalArray( 1 );
530         ssgVertexArray   *vl1 = new ssgVertexArray( 1 );
531         ssgNormalArray   *nl1 = new ssgNormalArray( 1 );
532
533         float *n1 = normal->get(i);
534         float *d1 = dir->get(i);
535
536         /* TEMPORARY CODE BEGIN 
537            // calculate normal using 1st, 2nd & last vertices of the group
538            sgVec3 n1;
539            sgMakeNormal (n1, points->get(0), points->get(1), points->get(points->getNum()-1) );
540            sgVec3 d1;
541            sgSubVec3(d1,points->get(1),points->get(0));
542            printf("%f %f %f\n",n1[0],n1[1],n1[2]);
543            printf("%f %f %f\n",d1[0],d1[1],d1[2]);
544            type[i] = 2;
545            ----TEMPORARY CODE END */
546
547
548         sgNormaliseVec3 ( n1 );
549         sgNormaliseVec3 ( d1 );
550         sgVec3 d2;
551         d2[0] = -d1[0];
552         d2[1] = -d1[1];
553         d2[2] = -d1[2];
554
555         sgVectorProductVec3(side,n1,d1);
556         sgScaleVec3 (inf,n1,-50);
557         sgScaleVec3 (side,5);
558
559         float *v1 = points->get(i);
560         sgAddVec3(v2,v1,inf);
561         sgAddVec3(v3,v2,side);
562
563         if ( type[i] == 1) {        //POINT,WHITE
564
565             vlw->add(v1);
566             nlw->add(d1);
567
568         } else if (type[i] == 2) {  //POINT,TAXI
569
570             vlt->add(v1);
571             nlt->add(d1);
572
573         } else if (type[i] == 3) {  //SINGLE POLYGON,VASI
574
575             vl->add(v1);
576             nl->add(d1);
577             vl->add(v3);
578             nl->add(d1);
579             vl->add(v2);
580             nl->add(d1);
581
582             ssgLeaf *leaf3 = NULL;
583             leaf3 = new ssgVtxTable ( GL_POLYGON, vl, nl, NULL, NULL );
584             leaf3->setState( vasi_state );
585             runway_lights->addKid( leaf3 );
586
587         } else if (type[i] == 4) {  //BACK-TO-BACK POLYGONS,TOUCHDOWN/THRESHOLD
588                 
589             vl->add(v1);
590             nl->add(d1);
591             vl->add(v3);
592             nl->add(d1);
593             vl->add(v2);
594             nl->add(d1);
595
596             vl1->add(v3);
597             nl1->add(d2);
598             vl1->add(v1);
599             nl1->add(d2);
600             vl1->add(v2);
601             nl1->add(d2);
602
603             ssgLeaf *leaf41 = NULL;
604             leaf41 = new ssgVtxTable ( GL_POLYGON, vl, nl, NULL, NULL );
605             leaf41->setState( touchdown_state );
606             runway_lights->addKid( leaf41 );
607
608             ssgLeaf *leaf42 = NULL;
609             leaf42 = new ssgVtxTable ( GL_POLYGON, vl1, nl1, NULL, NULL );
610             leaf42->setState( threshold_state );
611             runway_lights->addKid( leaf42 );
612
613         } else if ( type[i] == 5) {        //POINT,WHITE, SEQUENCE LIGHTS (RABBIT)
614
615             vl->add(v1);
616             nl->add(d1);
617             ssgLeaf *leaf5 = NULL;
618             leaf5 = new ssgVtxTable ( GL_POINTS, vl, nl, NULL, NULL );
619             leaf5->setState( white_state );
620             lightmaps_sequence->addKid (leaf5);
621
622         } else if ( type[i] == 6) {        //POINT,WHITE, SEQUENCE LIGHTS (RABBIT)
623
624             vl->add(v1);
625             nl->add(d1);
626             ssgLeaf *leaf6 = NULL;
627             leaf6 = new ssgVtxTable ( GL_POINTS, vl, nl, NULL, NULL );
628             leaf6->setState( yellow_state );
629             ols_transform->addKid (leaf6);
630             // DO NOT DELETE THIS CODE - This is to compare a discrete FLOLS (without LOD) with analog FLOLS
631             //                  lightmaps_sequence->addKid (leaf6);
632             // DO NOT DELETE THIS CODE - This is to compare a discrete FLOLS (without LOD) with analog FLOLS
633
634         } else if (type[i] == 7) {  //POINT,RED
635
636             vlr->add(v1);
637             nlr->add(d1);
638
639         } else if (type[i] == 8) {  //POINT,GREEN
640
641             vlg->add(v1);
642             nlg->add(d1);
643
644         } else if (type[i] == 9) {  //POINT,YELLOW
645
646             vly->add(v1);
647             nly->add(d1);
648
649         }
650
651     }
652
653     leaf1 = new ssgVtxTable ( GL_POINTS, vlw, nlw, NULL, NULL );
654     leaf1->setState( white_state );
655     runway_lights->addKid( leaf1 );
656
657     leaf2 = new ssgVtxTable ( GL_POINTS, vlt, nlt, NULL, NULL );
658     leaf2->setState( taxi_state );
659     runway_lights->addKid( leaf2 );
660         
661     leaf7 = new ssgVtxTable ( GL_POINTS, vlr, nlr, NULL, NULL );
662     leaf7->setState( red_state );
663     runway_lights->addKid( leaf7 );
664
665     leaf8 = new ssgVtxTable ( GL_POINTS, vlg, nlg, NULL, NULL );
666     leaf8->setState( green_state );
667     // DO NOT DELETE THIS CODE - This is to compare a discrete FLOLS (without LOD) with analog FLOLS
668     ols_transform->ref();
669     lightmaps_sequence->addKid (ols_transform);
670     // DO NOT DELETE THIS CODE - This is to compare a discrete FLOLS (without LOD) with analog FLOLS
671     lightmaps_sequence->addKid (leaf8);
672
673     leaf9 = new ssgVtxTable ( GL_POINTS, vly, nly, NULL, NULL );
674     leaf9->setState( yellow_state );
675     runway_lights->addKid( leaf9 );
676
677     lightmaps_sequence->select(0xFFFFFF);       
678     return runway_lights;
679 }
680 // ADA
681         
682 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
683
684 // Schedule tile to be freed/removed
685 void FGTileEntry::sched_removal() {
686     global_tile_mgr.ready_to_delete( this );
687 }
688
689 #endif
690
691
692 // Clean up the memory used by this tile and delete the arrays used by
693 // ssg as well as the whole ssg branch
694 void FGTileEntry::free_tile() {
695     int i;
696     SG_LOG( SG_TERRAIN, SG_INFO,
697             "FREEING TILE = (" << tile_bucket << ")" );
698
699     SG_LOG( SG_TERRAIN, SG_DEBUG,
700             "  deleting " << nodes.size() << " nodes" );
701     nodes.clear();
702
703     // delete the ssg structures
704     SG_LOG( SG_TERRAIN, SG_DEBUG,
705             "  deleting (leaf data) vertex, normal, and "
706             << " texture coordinate arrays" );
707
708     for ( i = 0; i < (int)vec3_ptrs.size(); ++i ) {
709         delete [] vec3_ptrs[i];
710     }
711     vec3_ptrs.clear();
712
713     for ( i = 0; i < (int)vec2_ptrs.size(); ++i ) {
714         delete [] vec2_ptrs[i];
715     }
716     vec2_ptrs.clear();
717
718     for ( i = 0; i < (int)index_ptrs.size(); ++i ) {
719         delete index_ptrs[i];
720     }
721     index_ptrs.clear();
722
723     // delete the terrain branch (this should already have been
724     // disconnected from the scene graph)
725     ssgDeRefDelete( terra_transform );
726
727     if ( lights_transform ) {
728         // delete the terrain lighting branch (this should already have been
729     // disconnected from the scene graph)
730         ssgDeRefDelete( lights_transform );
731     }
732
733     // ADA
734     if ( lightmaps_transform ) {
735         // delete the terrain lighting branch (this should already have been
736         // disconnected from the scene graph)
737         ssgDeRefDelete( lightmaps_transform );
738     }
739     // ADA
740 }
741
742
743 // Update the ssg transform node for this tile so it can be
744 // properly drawn relative to our (0,0,0) point
745 void FGTileEntry::prep_ssg_node( const Point3D& p, float vis) {
746     if ( !loaded ) return;
747
748     SetOffset( p );
749
750 // #define USE_UP_AND_COMING_PLIB_FEATURE
751 #ifdef USE_UP_AND_COMING_PLIB_FEATURE
752     terra_range->setRange( 0, SG_ZERO );
753     terra_range->setRange( 1, vis + bounding_radius );
754     lights_range->setRange( 0, SG_ZERO );
755     lights_range->setRange( 1, vis * 1.5 + bounding_radius );
756 #else
757     float ranges[2];
758     ranges[0] = SG_ZERO;
759     ranges[1] = vis + bounding_radius;
760     terra_range->setRanges( ranges, 2 );
761     if ( lights_range ) {
762         ranges[1] = vis * 1.5 + bounding_radius;
763         lights_range->setRanges( ranges, 2 );
764     }
765 #endif
766     sgVec3 sgTrans;
767     sgSetVec3( sgTrans, offset.x(), offset.y(), offset.z() );
768     terra_transform->setTransform( sgTrans );
769
770     if ( lights_transform ) {
771         // we need to lift the lights above the terrain to avoid
772         // z-buffer fighting.  We do this based on our altitude and
773         // the distance this tile is away from scenery center.
774
775         sgVec3 up;
776         sgCopyVec3( up, globals->get_current_view()->get_world_up() );
777
778         double agl;
779         if ( current_aircraft.fdm_state ) {
780             agl = current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
781                 - scenery.get_cur_elev();
782         } else {
783             agl = 0.0;
784         }
785
786         // sgTrans just happens to be the
787         // vector from scenery center to the center of this tile which
788         // is what we want to calculate the distance of
789         sgVec3 to;
790         sgCopyVec3( to, sgTrans );
791         double dist = sgLengthVec3( to );
792
793         if ( general.get_glDepthBits() > 16 ) {
794             sgScaleVec3( up, 10.0 + agl / 100.0 + dist / 10000 );
795         } else {
796             sgScaleVec3( up, 10.0 + agl / 20.0 + dist / 5000 );
797         }
798         sgAddVec3( sgTrans, up );
799         lights_transform->setTransform( sgTrans );
800
801         // select which set of lights based on sun angle
802         float sun_angle = cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES;
803         if ( sun_angle > 95 ) {
804             lights_brightness->select(0x04);
805         } else if ( sun_angle > 92 ) {
806             lights_brightness->select(0x02);
807         } else if ( sun_angle > 89 ) {
808             lights_brightness->select(0x01);
809         } else {
810             lights_brightness->select(0x00);
811         }
812     }
813
814     // ADA
815     // Transform & Render runway lights - 23 Mar 2001
816     sgSetVec3( sgTrans, offset.x(), offset.y(), offset.z() );
817     if ( lightmaps_transform ) {
818         static unsigned int selectnode = 0;
819         // Run-time extension check.
820         if (!glutExtensionSupported("GL_EXT_point_parameters")) {
821             //use lightmaps on billboarded polygons
822         } else {
823             // using GL_EXT_point_parameters
824                 
825             // This part is same as ground-lights code above by Curt
826             sgVec3 up1;
827             sgCopyVec3( up1, globals->get_current_view()->get_world_up() );
828             
829             double agl1;
830             if ( current_aircraft.fdm_state ) {
831                 agl1 = current_aircraft.fdm_state->get_Altitude() * SG_FEET_TO_METER
832                     - scenery.get_cur_elev();
833             } else {
834                 agl1 = 0.0;
835             }
836
837             // sgTrans just happens to be the
838             // vector from scenery center to the center of this tile which
839             // is what we want to calculate the distance of
840             sgVec3 to1;
841             sgCopyVec3( to1, sgTrans );
842             double dist1 = sgLengthVec3( to1 );
843
844             if ( general.get_glDepthBits() > 16 ) {
845                 sgScaleVec3( up1, 0.0 + agl1 / 2000.0 + dist1 / 10000 );
846             } else {
847                 sgScaleVec3( up1, 0.0 + agl1 / 20.0 + dist1 / 5000 );
848             }
849             sgAddVec3( sgTrans, up1 );
850             lightmaps_transform->setTransform( sgTrans );
851
852             float sun_angle1 = cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES;
853             if ( (sun_angle1 > 89) ) {
854                 lightmaps_brightness->select(0x01);
855                 selectnode *=2;
856                 selectnode = selectnode | 0x000001;
857                 if (selectnode > 0xFFFFFF) selectnode = 1;
858                 lightmaps_sequence->select(selectnode);
859             } else {
860                 lightmaps_brightness->select(0x00);
861                 lightmaps_sequence->select(0x000000);
862             } 
863         } // end of GL_EXT_point_parameters section
864
865     } // end of runway lights section
866     // ADA
867
868 }
869
870
871 // Set up lights rendering call backs
872 static int fgLightsPredraw( ssgEntity *e ) {
873 #if 0
874 #ifdef GL_EXT_point_parameters
875     if (glutExtensionSupported("GL_EXT_point_parameters")) {
876         static float quadratic[3] = {1.0, 0.01, 0.0001};
877         glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, quadratic);
878         glPointParameterfEXT(GL_POINT_SIZE_MIN_EXT, 1.0); 
879         glPointSize(4.0);
880     }
881 #endif
882 #endif
883     return true;
884 }
885
886 static int fgLightsPostdraw( ssgEntity *e ) {
887 #if 0
888 #ifdef GL_EXT_point_parameters
889     if (glutExtensionSupported("GL_EXT_point_parameters")) {
890         static float default_attenuation[3] = {1.0, 0.0, 0.0};
891         glPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT,
892                               default_attenuation);
893         glPointSize(1.0);
894     }
895 #endif
896 #endif
897     return true;
898 }
899
900
901 ssgLeaf* FGTileEntry::gen_lights( ssgVertexArray *lights, int inc, float bright ) {
902     // generate a repeatable random seed
903     float *p1 = lights->get( 0 );
904     unsigned int *seed = (unsigned int *)p1;
905     sg_srandom( *seed );
906
907     int size = lights->getNum() / inc;
908
909     // Allocate ssg structure
910     ssgVertexArray   *vl = new ssgVertexArray( size + 1 );
911     ssgNormalArray   *nl = NULL;
912     ssgTexCoordArray *tl = NULL;
913     ssgColourArray   *cl = new ssgColourArray( size + 1 );
914
915     sgVec4 color;
916     for ( int i = 0; i < lights->getNum(); ++i ) {
917         // this loop is slightly less efficient than it otherwise
918         // could be, but we want a red light to always be red, and a
919         // yellow light to always be yellow, etc. so we are trying to
920         // preserve the random sequence.
921         float zombie = sg_random();
922         if ( i % inc == 0 ) {
923             vl->add( lights->get(i) );
924
925             // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
926             float factor = sg_random();
927             factor *= factor;
928
929             if ( zombie > 0.5 ) {
930                 // 50% chance of yellowish
931                 sgSetVec4( color, 0.9, 0.9, 0.3, bright - factor * 0.2 );
932             } else if ( zombie > 0.15 ) {
933                 // 35% chance of whitish
934                 sgSetVec4( color, 0.9, 0.9, 0.8, bright - factor * 0.2 );
935             } else if ( zombie > 0.05 ) {
936                 // 10% chance of orangish
937                 sgSetVec4( color, 0.9, 0.6, 0.2, bright - factor * 0.2 );
938             } else {
939                 // 5% chance of redish
940                 sgSetVec4( color, 0.9, 0.2, 0.2, bright - factor * 0.2 );
941             }
942             cl->add( color );
943         }
944     }
945
946     // create ssg leaf
947     ssgLeaf *leaf = 
948         new ssgVtxTable ( GL_POINTS, vl, nl, tl, cl );
949
950     // assign state
951     FGNewMat *newmat = material_lib.find( "LIGHTS" );
952     leaf->setState( newmat->get_state() );
953     leaf->setCallback( SSG_CALLBACK_PREDRAW, fgLightsPredraw );
954     leaf->setCallback( SSG_CALLBACK_POSTDRAW, fgLightsPostdraw );
955
956     return leaf;
957 }
958
959
960 ssgBranch*
961 FGTileEntry::obj_load( const std::string& path,
962                        ssgVertexArray* lights, bool is_base )
963 {
964     ssgBranch* result = 0;
965
966     // try loading binary format
967     result = fgBinObjLoad( path, this, lights, is_base );
968     if ( result == NULL ) {
969         // next try the older ascii format
970         result = fgAsciiObjLoad( path, this, lights, is_base );
971         if ( result == NULL ) {
972             // default to an ocean tile
973             result = fgGenTile( path, this );
974         }
975     }
976
977     return result;
978 }
979
980
981 void
982 FGTileEntry::load( const SGPath& base, bool is_base )
983 {
984     cout << "load() base = " << base.str() << endl;
985
986     // Generate names for later use
987     string index_str = tile_bucket.gen_index_str();
988
989     SGPath tile_path = base;
990     tile_path.append( tile_bucket.gen_base_path() );
991
992     SGPath basename = tile_path;
993     basename.append( index_str );
994     // string path = basename.str();
995
996     SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << basename.str() );
997
998 #define FG_MAX_LIGHTS 1000
999
1000     // obj_load() will generate ground lighting for us ...
1001     ssgVertexArray *light_pts = new ssgVertexArray( 100 );
1002
1003     // ADA
1004     ssgVertexArray *lights_rway = new ssgVertexArray( 100 );
1005     ssgVertexArray *lights_dir = new ssgVertexArray( 100 );
1006     ssgVertexArray *lights_normal = new ssgVertexArray( 100 );
1007     int lights_type[FG_MAX_LIGHTS];
1008     // ADA
1009
1010     ssgBranch* new_tile = new ssgBranch;
1011
1012     // Check for master .stg (scene terra gear) file
1013     SGPath stg_name = basename;
1014     stg_name.concat( ".stg" );
1015
1016     sg_gzifstream in( stg_name.str() );
1017
1018     if ( in.is_open() ) {
1019         string token, name;
1020
1021         while ( ! in.eof() ) {
1022             in >> token;
1023
1024             if ( token == "OBJECT_BASE" ) {
1025                 in >> name >> ::skipws;
1026                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1027                         << " name = " << name );
1028
1029                 SGPath custom_path = tile_path;
1030                 custom_path.append( name );
1031
1032                 ssgBranch *custom_obj
1033                     = obj_load( custom_path.str(), light_pts, true );
1034
1035                 if ( custom_obj != NULL ) {
1036                     new_tile -> addKid( custom_obj );
1037                 }
1038             } else if ( token == "OBJECT" ) {
1039                 in >> name >> ::skipws;
1040                 SG_LOG( SG_TERRAIN, SG_DEBUG, "token = " << token
1041                         << " name = " << name );
1042
1043                 SGPath custom_path = tile_path;
1044                 custom_path.append( name );
1045
1046                 ssgBranch *custom_obj
1047                     = obj_load( custom_path.str(), NULL, false );
1048                 if ( custom_obj != NULL ) {
1049                     new_tile -> addKid( custom_obj );
1050                 }
1051             } else if ( token == "OBJECT_STATIC" ) {
1052                 // load object info
1053                 double lon, lat, elev, hdg;
1054                 in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1055                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1056                         << " name = " << name 
1057                         << " pos = " << lon << ", " << lat
1058                         << " elevation = " << elev
1059                         << " heading = " << hdg );
1060
1061                 // object loading is deferred to main render thread,
1062                 // but lets figure out the paths right now.
1063                 SGPath custom_path = tile_path;
1064                 custom_path.append( name );
1065
1066                 sgCoord obj_pos;
1067                 WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1068                 
1069                 ssgTransform *obj_trans = new ssgTransform;
1070                 obj_trans->setTransform( &obj_pos );
1071
1072                 // wire as much of the scene graph together as we can
1073                 new_tile->addKid( obj_trans );
1074
1075                 // bump up the pending models count
1076                 pending_models++;
1077
1078                 // push an entry onto the model load queue
1079                 FGDeferredModel *dm
1080                     = new FGDeferredModel( custom_path.str(), tile_path.str(),
1081                                            this, obj_trans );
1082                 FGTileMgr::model_ready( dm );
1083             } else if ( token == "OBJECT_TAXI_SIGN" ) {
1084                 // load object info
1085                 double lon, lat, elev, hdg;
1086                 in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1087                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1088                         << " name = " << name 
1089                         << " pos = " << lon << ", " << lat
1090                         << " elevation = " << elev
1091                         << " heading = " << hdg );
1092
1093                 // load the object itself
1094                 SGPath custom_path = tile_path;
1095                 custom_path.append( name );
1096
1097                 sgCoord obj_pos;
1098                 WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1099
1100                 ssgTransform *obj_trans = new ssgTransform;
1101                 obj_trans->setTransform( &obj_pos );
1102
1103                 ssgBranch *custom_obj
1104                     = gen_taxi_sign( custom_path.str(), name );
1105
1106                 // wire the pieces together
1107                 if ( custom_obj != NULL ) {
1108                     obj_trans -> addKid( custom_obj );
1109                 }
1110                 new_tile->addKid( obj_trans );
1111             } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
1112                 // load object info
1113                 double lon, lat, elev, hdg;
1114                 in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1115                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1116                         << " name = " << name 
1117                         << " pos = " << lon << ", " << lat
1118                         << " elevation = " << elev
1119                         << " heading = " << hdg );
1120
1121                 // load the object itself
1122                 SGPath custom_path = tile_path;
1123                 custom_path.append( name );
1124
1125                 sgCoord obj_pos;
1126                 WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1127
1128                 ssgTransform *obj_trans = new ssgTransform;
1129                 obj_trans->setTransform( &obj_pos );
1130
1131                 ssgBranch *custom_obj
1132                     = gen_runway_sign( custom_path.str(), name );
1133
1134                 // wire the pieces together
1135                 if ( custom_obj != NULL ) {
1136                     obj_trans -> addKid( custom_obj );
1137                 }
1138                 new_tile->addKid( obj_trans );
1139             } else if ( token == "RWY_LIGHTS" ) {
1140                 double lon, lat, hdg, len, width;
1141                 string common, end1, end2;
1142                 in >> lon >> lat >> hdg >> len >> width
1143                    >> common >> end1 >> end2;
1144                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1145                         << " pos = " << lon << ", " << lat
1146                         << " hdg = " << hdg
1147                         << " size = " << len << ", " << width
1148                         << " codes = " << common << " "
1149                         << end1 << " " << end2 );
1150             } else {
1151                 SG_LOG( SG_TERRAIN, SG_ALERT,
1152                         "Unknown token " << token << " in "
1153                         << stg_name.str() );
1154                 in >> ::skipws;
1155             }
1156         }
1157     } else {
1158         // no .stg file so this must be old scenery
1159
1160         new_tile = obj_load( basename.str(), light_pts, true );
1161
1162         // load custom objects
1163         SG_LOG( SG_TERRAIN, SG_DEBUG, "Checking for custom objects ..." );
1164
1165         SGPath index_path = tile_path;
1166         index_path.append( index_str );
1167         index_path.concat( ".ind" );
1168
1169         SG_LOG( SG_TERRAIN, SG_DEBUG, "Looking in " << index_path.str() );
1170
1171         sg_gzifstream in( index_path.str() );
1172
1173         if ( in.is_open() ) {
1174             string token, name;
1175
1176             while ( ! in.eof() ) {
1177                 in >> token;
1178
1179                 if ( token == "OBJECT" ) {
1180                     in >> name >> ::skipws;
1181                     SG_LOG( SG_TERRAIN, SG_DEBUG, "token = " << token
1182                             << " name = " << name );
1183
1184                     SGPath custom_path = tile_path;
1185                     custom_path.append( name );
1186                     ssgBranch *custom_obj
1187                         = obj_load( custom_path.str(), NULL, false );
1188                     if ( (new_tile != NULL) && (custom_obj != NULL) ) {
1189                         new_tile -> addKid( custom_obj );
1190                     }
1191                 } else if ( token == "OBJECT_STATIC" ) {
1192                     // load object info
1193                     double lon, lat, elev, hdg;
1194                     in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1195                     SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1196                             << " name = " << name 
1197                             << " pos = " << lon << ", " << lat
1198                             << " elevation = " << elev
1199                             << " heading = " << hdg );
1200
1201                     // object loading is deferred to main render thread,
1202                     // but lets figure out the paths right now.
1203                     SGPath custom_path = tile_path;
1204                     custom_path.append( name );
1205
1206                     sgCoord obj_pos;
1207                     WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1208                 
1209                     ssgTransform *obj_trans = new ssgTransform;
1210                     obj_trans->setTransform( &obj_pos );
1211
1212                     // wire as much of the scene graph together as we can
1213                     new_tile->addKid( obj_trans );
1214
1215                     // bump up the pending models count
1216                     pending_models++;
1217
1218                     // push an entry onto the model load queue
1219                     FGDeferredModel *dm
1220                         = new FGDeferredModel( custom_path.str(),
1221                                                tile_path.str(),
1222                                                this, obj_trans );
1223                     FGTileMgr::model_ready( dm );
1224                 } else if ( token == "OBJECT_TAXI_SIGN" ) {
1225                     // load object info
1226                     double lon, lat, elev, hdg;
1227                     in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1228                     SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1229                             << " name = " << name 
1230                             << " pos = " << lon << ", " << lat
1231                             << " elevation = " << elev
1232                             << " heading = " << hdg );
1233
1234                     // load the object itself
1235                     SGPath custom_path = tile_path;
1236                     custom_path.append( name );
1237
1238                     sgCoord obj_pos;
1239                     WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1240
1241                     ssgTransform *obj_trans = new ssgTransform;
1242                     obj_trans->setTransform( &obj_pos );
1243
1244                     ssgBranch *custom_obj
1245                         = gen_taxi_sign( custom_path.str(), name );
1246
1247                     // wire the pieces together
1248                     if ( (new_tile != NULL) && (custom_obj != NULL) ) {
1249                         obj_trans -> addKid( custom_obj );
1250                     }
1251                     new_tile->addKid( obj_trans );
1252                 } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
1253                     // load object info
1254                     double lon, lat, elev, hdg;
1255                     in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1256                     SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1257                             << " name = " << name 
1258                             << " pos = " << lon << ", " << lat
1259                             << " elevation = " << elev
1260                             << " heading = " << hdg );
1261
1262                     // load the object itself
1263                     SGPath custom_path = tile_path;
1264                     custom_path.append( name );
1265
1266                     sgCoord obj_pos;
1267                     WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1268
1269                     ssgTransform *obj_trans = new ssgTransform;
1270                     obj_trans->setTransform( &obj_pos );
1271
1272                     ssgBranch *custom_obj
1273                         = gen_runway_sign( custom_path.str(), name );
1274
1275                     // wire the pieces together
1276                     if ( (new_tile != NULL) && (custom_obj != NULL) ) {
1277                         obj_trans -> addKid( custom_obj );
1278                     }
1279                     new_tile->addKid( obj_trans );
1280                 } else {
1281                     SG_LOG( SG_TERRAIN, SG_ALERT,
1282                             "Unknown token " << token << " in "
1283                             << index_path.str() );
1284                     in >> ::skipws;
1285                 }
1286             }
1287         }
1288     }
1289
1290     if ( new_tile != NULL ) {
1291         terra_range->addKid( new_tile );
1292     }
1293
1294     terra_transform->addKid( terra_range );
1295
1296     // calculate initial tile offset
1297     SetOffset( scenery.get_center() );
1298     sgCoord sgcoord;
1299     sgSetCoord( &sgcoord,
1300                 offset.x(), offset.y(), offset.z(),
1301                 0.0, 0.0, 0.0 );
1302     terra_transform->setTransform( &sgcoord );
1303     // terrain->addKid( terra_transform );
1304
1305     lights_transform = NULL;
1306     lights_range = NULL;
1307     /* uncomment this section for testing ground lights */
1308     if ( light_pts->getNum() ) {
1309         SG_LOG( SG_TERRAIN, SG_DEBUG, "generating lights" );
1310         lights_transform = new ssgTransform;
1311         lights_range = new ssgRangeSelector;
1312         lights_brightness = new ssgSelector;
1313         ssgLeaf *lights;
1314
1315         lights = gen_lights( light_pts, 4, 0.7 );
1316         lights_brightness->addKid( lights );
1317
1318         lights = gen_lights( light_pts, 2, 0.85 );
1319         lights_brightness->addKid( lights );
1320
1321         lights = gen_lights( light_pts, 1, 1.0 );
1322         lights_brightness->addKid( lights );
1323
1324         lights_range->addKid( lights_brightness );
1325         lights_transform->addKid( lights_range );
1326         lights_transform->setTransform( &sgcoord );
1327         // ground->addKid( lights_transform );
1328     }
1329     /* end of ground light section */
1330
1331     // ADA
1332     // Create runway lights - 23 Mar 2001
1333     lightmaps_transform = NULL;
1334     lightmaps_sequence = NULL;
1335     ols_transform = NULL;
1336     //    lightmaps_range = NULL;
1337
1338     if ( lights_rway->getNum() ) {
1339         SG_LOG( SG_TERRAIN, SG_DEBUG, "generating airport lights" );
1340         lightmaps_transform = new ssgTransform;
1341         //      lightmaps_range = new ssgRangeSelector;
1342         lightmaps_brightness = new ssgSelector;
1343         lightmaps_sequence = new ssgSelector;
1344         ols_transform = new ssgTransform;
1345         ssgBranch *lightmaps_branch;
1346
1347         // call function to generate the runway lights
1348         lightmaps_branch = gen_runway_lights( lights_rway, 
1349                                               lights_normal, lights_dir, lights_type);
1350         lightmaps_brightness->addKid( lightmaps_branch );
1351         
1352         // build the runway lights' scene
1353         //    lightmaps_range->addKid( lightmaps_brightness ); //dont know why this doesnt work !!
1354         //      lightmaps_transform->addKid( lightmaps_range );    //dont know why this doesnt work !!
1355         lightmaps_transform->addKid( lightmaps_brightness );
1356         lightmaps_sequence->setTraversalMaskBits( SSGTRAV_HOT );
1357         lightmaps_transform->addKid( lightmaps_sequence );
1358         lightmaps_transform->setTransform( &sgcoord );
1359     }
1360     // ADA
1361 }
1362
1363
1364 void
1365 FGTileEntry::add_ssg_nodes( ssgBranch* terrain, ssgBranch* ground )
1366 {
1367     // bump up the ref count so we can remove this later without
1368     // having ssg try to free the memory.
1369     terra_transform->ref();
1370     terrain->addKid( terra_transform );
1371
1372     SG_LOG( SG_TERRAIN, SG_DEBUG,
1373             "connected a tile into scene graph.  terra_transform = "
1374             << terra_transform );
1375     SG_LOG( SG_TERRAIN, SG_DEBUG, "num parents now = "
1376             << terra_transform->getNumParents() );
1377
1378     if ( lights_transform != 0 ) {
1379         // bump up the ref count so we can remove this later without
1380         // having ssg try to free the memory.
1381         lights_transform->ref();
1382         ground->addKid( lights_transform );
1383     }
1384
1385     // ADA
1386     if ( lightmaps_transform != 0 ) {
1387         // bump up the ref count so we can remove this later without
1388         // having ssg try to free the memory.
1389         lightmaps_transform->ref();
1390         ground->addKid( lightmaps_transform );
1391     }
1392     // ADA
1393
1394     loaded = true;
1395 }
1396
1397
1398 void
1399 FGTileEntry::disconnect_ssg_nodes()
1400 {
1401     SG_LOG( SG_TERRAIN, SG_INFO, "disconnecting ssg nodes" );
1402
1403     if ( ! loaded ) {
1404         SG_LOG( SG_TERRAIN, SG_INFO, "removing a not-fully loaded tile!" );
1405     } else {
1406         SG_LOG( SG_TERRAIN, SG_INFO, "removing a fully loaded tile!  terra_transform = " << terra_transform );
1407     }
1408         
1409     // find the terrain branch parent
1410     int pcount = terra_transform->getNumParents();
1411     if ( pcount > 0 ) {
1412         // find the first parent (should only be one)
1413         ssgBranch *parent = terra_transform->getParent( 0 ) ;
1414         if( parent ) {
1415             // disconnect the tile (we previously ref()'d it so it
1416             // won't get freed now)
1417             parent->removeKid( terra_transform );
1418         } else {
1419             SG_LOG( SG_TERRAIN, SG_ALERT,
1420                     "parent pointer is NULL!  Dying" );
1421             exit(-1);
1422         }
1423     } else {
1424         SG_LOG( SG_TERRAIN, SG_ALERT,
1425                 "Parent count is zero for an ssg tile!  Dying" );
1426         exit(-1);
1427     }
1428
1429     // find the terrain lighting branch
1430     if ( lights_transform ) {
1431         pcount = lights_transform->getNumParents();
1432         if ( pcount > 0 ) {
1433             // find the first parent (should only be one)
1434             ssgBranch *parent = lights_transform->getParent( 0 ) ;
1435             if( parent ) {
1436                 // disconnect the light branch (we previously ref()'d
1437                 // it so it won't get freed now)
1438                 parent->removeKid( lights_transform );
1439             } else {
1440                 SG_LOG( SG_TERRAIN, SG_ALERT,
1441                         "parent pointer is NULL!  Dying" );
1442                 exit(-1);
1443             }
1444         } else {
1445             SG_LOG( SG_TERRAIN, SG_ALERT,
1446                     "Parent count is zero for an ssg light tile!  Dying" );
1447             exit(-1);
1448         }
1449     }
1450
1451     // ADA
1452     //runway lights - 23 Mar 2001
1453     // Delete runway lights and free memory
1454     if ( lightmaps_transform ) {
1455         // delete the runway lighting branch
1456         pcount = lightmaps_transform->getNumParents();
1457         if ( pcount > 0 ) {
1458             // find the first parent (should only be one)
1459             ssgBranch *parent = lightmaps_transform->getParent( 0 ) ;
1460             if( parent ) {
1461                 parent->removeKid( lightmaps_transform );
1462                 lightmaps_transform = NULL;
1463             } else {
1464                 SG_LOG( SG_TERRAIN, SG_ALERT,
1465                         "lightmaps parent pointer is NULL!  Dying" );
1466                 exit(-1);
1467             }
1468         } else {
1469             SG_LOG( SG_TERRAIN, SG_ALERT,
1470                     "Parent count is zero for an ssg lightmap tile!  Dying" );
1471             exit(-1);
1472         }
1473     }
1474     // ADA
1475 }