]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.cxx
Return a positive shortage when there is still fuel in the tank;
[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 ssgLeaf* FGTileEntry::gen_lights( ssgVertexArray *lights, int inc, float bright ) {
872     // generate a repeatable random seed
873     float *p1 = lights->get( 0 );
874     unsigned int *seed = (unsigned int *)p1;
875     sg_srandom( *seed );
876
877     int size = lights->getNum() / inc;
878
879     // Allocate ssg structure
880     ssgVertexArray   *vl = new ssgVertexArray( size + 1 );
881     ssgNormalArray   *nl = NULL;
882     ssgTexCoordArray *tl = NULL;
883     ssgColourArray   *cl = new ssgColourArray( size + 1 );
884
885     sgVec4 color;
886     for ( int i = 0; i < lights->getNum(); ++i ) {
887         // this loop is slightly less efficient than it otherwise
888         // could be, but we want a red light to always be red, and a
889         // yellow light to always be yellow, etc. so we are trying to
890         // preserve the random sequence.
891         float zombie = sg_random();
892         if ( i % inc == 0 ) {
893             vl->add( lights->get(i) );
894
895             // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
896             float factor = sg_random();
897             factor *= factor;
898
899             if ( zombie > 0.5 ) {
900                 // 50% chance of yellowish
901                 sgSetVec4( color, 0.9, 0.9, 0.3, bright - factor * 0.2 );
902             } else if ( zombie > 0.15 ) {
903                 // 35% chance of whitish
904                 sgSetVec4( color, 0.9, 0.9, 0.8, bright - factor * 0.2 );
905             } else if ( zombie > 0.05 ) {
906                 // 10% chance of orangish
907                 sgSetVec4( color, 0.9, 0.6, 0.2, bright - factor * 0.2 );
908             } else {
909                 // 5% chance of redish
910                 sgSetVec4( color, 0.9, 0.2, 0.2, bright - factor * 0.2 );
911             }
912             cl->add( color );
913         }
914     }
915
916     // create ssg leaf
917     ssgLeaf *leaf = 
918         new ssgVtxTable ( GL_POINTS, vl, nl, tl, cl );
919
920     // assign state
921     FGNewMat *newmat = material_lib.find( "LIGHTS" );
922     leaf->setState( newmat->get_state() );
923
924     return leaf;
925 }
926
927
928 ssgBranch*
929 FGTileEntry::obj_load( const std::string& path,
930                        ssgVertexArray* lights, bool is_base )
931 {
932     ssgBranch* result = 0;
933
934     // try loading binary format
935     result = fgBinObjLoad( path, this, lights, is_base );
936     if ( result == NULL ) {
937         // next try the older ascii format
938         result = fgAsciiObjLoad( path, this, lights, is_base );
939         if ( result == NULL ) {
940             // default to an ocean tile
941             result = fgGenTile( path, this );
942         }
943     }
944
945     return result;
946 }
947
948
949 void
950 FGTileEntry::load( const SGPath& base, bool is_base )
951 {
952     cout << "load() base = " << base.str() << endl;
953
954     // Generate names for later use
955     string index_str = tile_bucket.gen_index_str();
956
957     SGPath tile_path = base;
958     tile_path.append( tile_bucket.gen_base_path() );
959
960     SGPath basename = tile_path;
961     basename.append( index_str );
962     // string path = basename.str();
963
964     SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << basename.str() );
965
966 #define FG_MAX_LIGHTS 1000
967
968     // obj_load() will generate ground lighting for us ...
969     ssgVertexArray *light_pts = new ssgVertexArray( 100 );
970
971     // ADA
972     ssgVertexArray *lights_rway = new ssgVertexArray( 100 );
973     ssgVertexArray *lights_dir = new ssgVertexArray( 100 );
974     ssgVertexArray *lights_normal = new ssgVertexArray( 100 );
975     int lights_type[FG_MAX_LIGHTS];
976     // ADA
977
978     ssgBranch* new_tile = new ssgBranch;
979
980     // Check for master .stg (scene terra gear) file
981     SGPath stg_name = basename;
982     stg_name.concat( ".stg" );
983
984     sg_gzifstream in( stg_name.str() );
985
986     if ( in.is_open() ) {
987         string token, name;
988
989         while ( ! in.eof() ) {
990             in >> token;
991
992             if ( token == "OBJECT_BASE" ) {
993                 in >> name >> ::skipws;
994                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
995                         << " name = " << name );
996
997                 SGPath custom_path = tile_path;
998                 custom_path.append( name );
999
1000                 ssgBranch *custom_obj
1001                     = obj_load( custom_path.str(), light_pts, true );
1002
1003                 if ( custom_obj != NULL ) {
1004                     new_tile -> addKid( custom_obj );
1005                 }
1006             } else if ( token == "OBJECT" ) {
1007                 in >> name >> ::skipws;
1008                 SG_LOG( SG_TERRAIN, SG_DEBUG, "token = " << token
1009                         << " name = " << name );
1010
1011                 SGPath custom_path = tile_path;
1012                 custom_path.append( name );
1013                 ssgBranch *custom_obj
1014                     = obj_load( custom_path.str(), NULL, false );
1015                 if ( custom_obj != NULL ) {
1016                     new_tile -> addKid( custom_obj );
1017                 }
1018             } else if ( token == "OBJECT_STATIC" ) {
1019                 // load object info
1020                 double lon, lat, elev, hdg;
1021                 in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1022                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1023                         << " name = " << name 
1024                         << " pos = " << lon << ", " << lat
1025                         << " elevation = " << elev
1026                         << " heading = " << hdg );
1027
1028                 // object loading is deferred to main render thread,
1029                 // but lets figure out the paths right now.
1030                 SGPath custom_path = tile_path;
1031                 custom_path.append( name );
1032
1033                 sgCoord obj_pos;
1034                 WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1035                 
1036                 ssgTransform *obj_trans = new ssgTransform;
1037                 obj_trans->setTransform( &obj_pos );
1038
1039                 // wire as much of the scene graph together as we can
1040                 new_tile->addKid( obj_trans );
1041
1042                 // bump up the pending models count
1043                 pending_models++;
1044
1045                 // push an entry onto the model load queue
1046                 FGDeferredModel *dm
1047                     = new FGDeferredModel( custom_path.str(), tile_path.str(),
1048                                            this, obj_trans );
1049                 FGTileMgr::model_ready( dm );
1050             } else if ( token == "OBJECT_TAXI_SIGN" ) {
1051                 // load object info
1052                 double lon, lat, elev, hdg;
1053                 in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1054                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1055                         << " name = " << name 
1056                         << " pos = " << lon << ", " << lat
1057                         << " elevation = " << elev
1058                         << " heading = " << hdg );
1059
1060                 // load the object itself
1061                 SGPath custom_path = tile_path;
1062                 custom_path.append( name );
1063
1064                 sgCoord obj_pos;
1065                 WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1066
1067                 ssgTransform *obj_trans = new ssgTransform;
1068                 obj_trans->setTransform( &obj_pos );
1069
1070                 ssgBranch *custom_obj
1071                     = gen_taxi_sign( custom_path.str(), name );
1072
1073                 // wire the pieces together
1074                 if ( custom_obj != NULL ) {
1075                     obj_trans -> addKid( custom_obj );
1076                 }
1077                 new_tile->addKid( obj_trans );
1078             } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
1079                 // load object info
1080                 double lon, lat, elev, hdg;
1081                 in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1082                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1083                         << " name = " << name 
1084                         << " pos = " << lon << ", " << lat
1085                         << " elevation = " << elev
1086                         << " heading = " << hdg );
1087
1088                 // load the object itself
1089                 SGPath custom_path = tile_path;
1090                 custom_path.append( name );
1091
1092                 sgCoord obj_pos;
1093                 WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1094
1095                 ssgTransform *obj_trans = new ssgTransform;
1096                 obj_trans->setTransform( &obj_pos );
1097
1098                 ssgBranch *custom_obj
1099                     = gen_runway_sign( custom_path.str(), name );
1100
1101                 // wire the pieces together
1102                 if ( custom_obj != NULL ) {
1103                     obj_trans -> addKid( custom_obj );
1104                 }
1105                 new_tile->addKid( obj_trans );
1106             } else if ( token == "RWY_LIGHTS" ) {
1107                 double lon, lat, hdg, len, width;
1108                 string common, end1, end2;
1109                 in >> lon >> lat >> hdg >> len >> width
1110                    >> common >> end1 >> end2;
1111                 SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1112                         << " pos = " << lon << ", " << lat
1113                         << " hdg = " << hdg
1114                         << " size = " << len << ", " << width
1115                         << " codes = " << common << " "
1116                         << end1 << " " << end2 );
1117             } else {
1118                 SG_LOG( SG_TERRAIN, SG_ALERT,
1119                         "Unknown token " << token << " in "
1120                         << stg_name.str() );
1121                 in >> ::skipws;
1122             }
1123         }
1124     } else {
1125         // no .stg file so this must be old scenery
1126
1127         new_tile = obj_load( basename.str(), light_pts, true );
1128
1129         // load custom objects
1130         SG_LOG( SG_TERRAIN, SG_DEBUG, "Checking for custom objects ..." );
1131
1132         SGPath index_path = tile_path;
1133         index_path.append( index_str );
1134         index_path.concat( ".ind" );
1135
1136         SG_LOG( SG_TERRAIN, SG_DEBUG, "Looking in " << index_path.str() );
1137
1138         sg_gzifstream in( index_path.str() );
1139
1140         if ( in.is_open() ) {
1141             string token, name;
1142
1143             while ( ! in.eof() ) {
1144                 in >> token;
1145
1146                 if ( token == "OBJECT" ) {
1147                     in >> name >> ::skipws;
1148                     SG_LOG( SG_TERRAIN, SG_DEBUG, "token = " << token
1149                             << " name = " << name );
1150
1151                     SGPath custom_path = tile_path;
1152                     custom_path.append( name );
1153                     ssgBranch *custom_obj
1154                         = obj_load( custom_path.str(), NULL, false );
1155                     if ( (new_tile != NULL) && (custom_obj != NULL) ) {
1156                         new_tile -> addKid( custom_obj );
1157                     }
1158                 } else if ( token == "OBJECT_STATIC" ) {
1159                     // load object info
1160                     double lon, lat, elev, hdg;
1161                     in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1162                     SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1163                             << " name = " << name 
1164                             << " pos = " << lon << ", " << lat
1165                             << " elevation = " << elev
1166                             << " heading = " << hdg );
1167
1168                     // object loading is deferred to main render thread,
1169                     // but lets figure out the paths right now.
1170                     SGPath custom_path = tile_path;
1171                     custom_path.append( name );
1172
1173                     sgCoord obj_pos;
1174                     WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1175                 
1176                     ssgTransform *obj_trans = new ssgTransform;
1177                     obj_trans->setTransform( &obj_pos );
1178
1179                     // wire as much of the scene graph together as we can
1180                     new_tile->addKid( obj_trans );
1181
1182                     // bump up the pending models count
1183                     pending_models++;
1184
1185                     // push an entry onto the model load queue
1186                     FGDeferredModel *dm
1187                         = new FGDeferredModel( custom_path.str(),
1188                                                tile_path.str(),
1189                                                this, obj_trans );
1190                     FGTileMgr::model_ready( dm );
1191                 } else if ( token == "OBJECT_TAXI_SIGN" ) {
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                     // load the object itself
1202                     SGPath custom_path = tile_path;
1203                     custom_path.append( name );
1204
1205                     sgCoord obj_pos;
1206                     WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1207
1208                     ssgTransform *obj_trans = new ssgTransform;
1209                     obj_trans->setTransform( &obj_pos );
1210
1211                     ssgBranch *custom_obj
1212                         = gen_taxi_sign( custom_path.str(), name );
1213
1214                     // wire the pieces together
1215                     if ( (new_tile != NULL) && (custom_obj != NULL) ) {
1216                         obj_trans -> addKid( custom_obj );
1217                     }
1218                     new_tile->addKid( obj_trans );
1219                 } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
1220                     // load object info
1221                     double lon, lat, elev, hdg;
1222                     in >> name >> lon >> lat >> elev >> hdg >> ::skipws;
1223                     SG_LOG( SG_TERRAIN, SG_INFO, "token = " << token
1224                             << " name = " << name 
1225                             << " pos = " << lon << ", " << lat
1226                             << " elevation = " << elev
1227                             << " heading = " << hdg );
1228
1229                     // load the object itself
1230                     SGPath custom_path = tile_path;
1231                     custom_path.append( name );
1232
1233                     sgCoord obj_pos;
1234                     WorldCoordinate( &obj_pos, center, lat, lon, elev, hdg );
1235
1236                     ssgTransform *obj_trans = new ssgTransform;
1237                     obj_trans->setTransform( &obj_pos );
1238
1239                     ssgBranch *custom_obj
1240                         = gen_runway_sign( custom_path.str(), name );
1241
1242                     // wire the pieces together
1243                     if ( (new_tile != NULL) && (custom_obj != NULL) ) {
1244                         obj_trans -> addKid( custom_obj );
1245                     }
1246                     new_tile->addKid( obj_trans );
1247                 } else {
1248                     SG_LOG( SG_TERRAIN, SG_ALERT,
1249                             "Unknown token " << token << " in "
1250                             << index_path.str() );
1251                     in >> ::skipws;
1252                 }
1253             }
1254         }
1255     }
1256
1257     if ( new_tile != NULL ) {
1258         terra_range->addKid( new_tile );
1259     }
1260
1261     terra_transform->addKid( terra_range );
1262
1263     // calculate initial tile offset
1264     SetOffset( scenery.get_center() );
1265     sgCoord sgcoord;
1266     sgSetCoord( &sgcoord,
1267                 offset.x(), offset.y(), offset.z(),
1268                 0.0, 0.0, 0.0 );
1269     terra_transform->setTransform( &sgcoord );
1270     // terrain->addKid( terra_transform );
1271
1272     lights_transform = NULL;
1273     lights_range = NULL;
1274     /* uncomment this section for testing ground lights */
1275     if ( light_pts->getNum() ) {
1276         SG_LOG( SG_TERRAIN, SG_DEBUG, "generating lights" );
1277         lights_transform = new ssgTransform;
1278         lights_range = new ssgRangeSelector;
1279         lights_brightness = new ssgSelector;
1280         ssgLeaf *lights;
1281
1282         lights = gen_lights( light_pts, 4, 0.7 );
1283         lights_brightness->addKid( lights );
1284
1285         lights = gen_lights( light_pts, 2, 0.85 );
1286         lights_brightness->addKid( lights );
1287
1288         lights = gen_lights( light_pts, 1, 1.0 );
1289         lights_brightness->addKid( lights );
1290
1291         lights_range->addKid( lights_brightness );
1292         lights_transform->addKid( lights_range );
1293         lights_transform->setTransform( &sgcoord );
1294         // ground->addKid( lights_transform );
1295     }
1296     /* end of ground light section */
1297
1298     // ADA
1299     // Create runway lights - 23 Mar 2001
1300     lightmaps_transform = NULL;
1301     lightmaps_sequence = NULL;
1302     ols_transform = NULL;
1303     //    lightmaps_range = NULL;
1304
1305     if ( lights_rway->getNum() ) {
1306         SG_LOG( SG_TERRAIN, SG_DEBUG, "generating airport lights" );
1307         lightmaps_transform = new ssgTransform;
1308         //      lightmaps_range = new ssgRangeSelector;
1309         lightmaps_brightness = new ssgSelector;
1310         lightmaps_sequence = new ssgSelector;
1311         ols_transform = new ssgTransform;
1312         ssgBranch *lightmaps_branch;
1313
1314         // call function to generate the runway lights
1315         lightmaps_branch = gen_runway_lights( lights_rway, 
1316                                               lights_normal, lights_dir, lights_type);
1317         lightmaps_brightness->addKid( lightmaps_branch );
1318         
1319         // build the runway lights' scene
1320         //    lightmaps_range->addKid( lightmaps_brightness ); //dont know why this doesnt work !!
1321         //      lightmaps_transform->addKid( lightmaps_range );    //dont know why this doesnt work !!
1322         lightmaps_transform->addKid( lightmaps_brightness );
1323         lightmaps_sequence->setTraversalMaskBits( SSGTRAV_HOT );
1324         lightmaps_transform->addKid( lightmaps_sequence );
1325         lightmaps_transform->setTransform( &sgcoord );
1326     }
1327     // ADA
1328 }
1329
1330
1331 void
1332 FGTileEntry::add_ssg_nodes( ssgBranch* terrain, ssgBranch* ground )
1333 {
1334     // bump up the ref count so we can remove this later without
1335     // having ssg try to free the memory.
1336     terra_transform->ref();
1337     terrain->addKid( terra_transform );
1338
1339     SG_LOG( SG_TERRAIN, SG_DEBUG,
1340             "connected a tile into scene graph.  terra_transform = "
1341             << terra_transform );
1342     SG_LOG( SG_TERRAIN, SG_DEBUG, "num parents now = "
1343             << terra_transform->getNumParents() );
1344
1345     if ( lights_transform != 0 ) {
1346         // bump up the ref count so we can remove this later without
1347         // having ssg try to free the memory.
1348         lights_transform->ref();
1349         ground->addKid( lights_transform );
1350     }
1351
1352     // ADA
1353     if ( lightmaps_transform != 0 ) {
1354         // bump up the ref count so we can remove this later without
1355         // having ssg try to free the memory.
1356         lightmaps_transform->ref();
1357         ground->addKid( lightmaps_transform );
1358     }
1359     // ADA
1360
1361     loaded = true;
1362 }
1363
1364
1365 void
1366 FGTileEntry::disconnect_ssg_nodes()
1367 {
1368     SG_LOG( SG_TERRAIN, SG_INFO, "disconnecting ssg nodes" );
1369
1370     if ( ! loaded ) {
1371         SG_LOG( SG_TERRAIN, SG_INFO, "removing a not-fully loaded tile!" );
1372     } else {
1373         SG_LOG( SG_TERRAIN, SG_INFO, "removing a fully loaded tile!  terra_transform = " << terra_transform );
1374     }
1375         
1376     // find the terrain branch parent
1377     int pcount = terra_transform->getNumParents();
1378     if ( pcount > 0 ) {
1379         // find the first parent (should only be one)
1380         ssgBranch *parent = terra_transform->getParent( 0 ) ;
1381         if( parent ) {
1382             // disconnect the tile (we previously ref()'d it so it
1383             // won't get freed now)
1384             parent->removeKid( terra_transform );
1385         } else {
1386             SG_LOG( SG_TERRAIN, SG_ALERT,
1387                     "parent pointer is NULL!  Dying" );
1388             exit(-1);
1389         }
1390     } else {
1391         SG_LOG( SG_TERRAIN, SG_ALERT,
1392                 "Parent count is zero for an ssg tile!  Dying" );
1393         exit(-1);
1394     }
1395
1396     // find the terrain lighting branch
1397     if ( lights_transform ) {
1398         pcount = lights_transform->getNumParents();
1399         if ( pcount > 0 ) {
1400             // find the first parent (should only be one)
1401             ssgBranch *parent = lights_transform->getParent( 0 ) ;
1402             if( parent ) {
1403                 // disconnect the light branch (we previously ref()'d
1404                 // it so it won't get freed now)
1405                 parent->removeKid( lights_transform );
1406             } else {
1407                 SG_LOG( SG_TERRAIN, SG_ALERT,
1408                         "parent pointer is NULL!  Dying" );
1409                 exit(-1);
1410             }
1411         } else {
1412             SG_LOG( SG_TERRAIN, SG_ALERT,
1413                     "Parent count is zero for an ssg light tile!  Dying" );
1414             exit(-1);
1415         }
1416     }
1417
1418     // ADA
1419     //runway lights - 23 Mar 2001
1420     // Delete runway lights and free memory
1421     if ( lightmaps_transform ) {
1422         // delete the runway lighting branch
1423         pcount = lightmaps_transform->getNumParents();
1424         if ( pcount > 0 ) {
1425             // find the first parent (should only be one)
1426             ssgBranch *parent = lightmaps_transform->getParent( 0 ) ;
1427             if( parent ) {
1428                 parent->removeKid( lightmaps_transform );
1429                 lightmaps_transform = NULL;
1430             } else {
1431                 SG_LOG( SG_TERRAIN, SG_ALERT,
1432                         "lightmaps parent pointer is NULL!  Dying" );
1433                 exit(-1);
1434             }
1435         } else {
1436             SG_LOG( SG_TERRAIN, SG_ALERT,
1437                     "Parent count is zero for an ssg lightmap tile!  Dying" );
1438             exit(-1);
1439         }
1440     }
1441     // ADA
1442 }