1 // tileentry.cxx -- routines to handle a scenery tile
3 // Written by Curtis Olson, started May 1998.
5 // Copyright (C) 1998 - 2001 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
28 #include <simgear/compiler.h>
30 #include <Main/main.hxx>
35 #include <simgear/bucket/newbucket.hxx>
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/math/polar3d.hxx>
38 #include <simgear/math/sg_geodesy.hxx>
39 #include <simgear/math/sg_random.h>
40 #include <simgear/misc/sgstream.hxx>
41 #include <simgear/scene/material/mat.hxx>
42 #include <simgear/scene/material/matlib.hxx>
43 #include <simgear/scene/tgdb/apt_signs.hxx>
44 #include <simgear/scene/tgdb/obj.hxx>
45 #include <simgear/scene/tgdb/vasi.hxx>
46 #include <simgear/scene/model/placementtrans.hxx>
48 #include <Aircraft/aircraft.hxx>
49 #include <Include/general.hxx>
50 #include <Main/fg_props.hxx>
51 #include <Main/globals.hxx>
52 #include <Main/viewer.hxx>
53 #include <Scenery/scenery.hxx>
54 #include <Time/light.hxx>
56 #include "tileentry.hxx"
57 #include "tilemgr.hxx"
63 FGTileEntry::FGTileEntry ( const SGBucket& b )
64 : center( Point3D( 0.0 ) ),
66 terra_transform( new ssgPlacementTransform ),
67 vasi_lights_transform( new ssgPlacementTransform ),
68 rwy_lights_transform( new ssgPlacementTransform ),
69 taxi_lights_transform( new ssgPlacementTransform ),
70 terra_range( new ssgRangeSelector ),
71 vasi_lights_selector( new ssgSelector ),
72 rwy_lights_selector( new ssgSelector ),
73 taxi_lights_selector( new ssgSelector ),
79 // update the contents
80 // if ( vec3_ptrs.size() || vec2_ptrs.size() || index_ptrs.size() ) {
81 // SG_LOG( SG_TERRAIN, SG_ALERT,
82 // "Attempting to overwrite existing or"
83 // << " not properly freed leaf data." );
90 FGTileEntry::~FGTileEntry () {
91 // cout << "nodes = " << nodes.size() << endl;;
97 // Please keep this for reference. We use Norman's optimized routine,
98 // but here is what the routine really is doing.
100 FGTileEntry::WorldCoordinate( sgCoord *obj_pos, Point3D center,
101 double lat, double lon, double elev, double hdg)
104 Point3D geod( lon * SGD_DEGREES_TO_RADIANS,
105 lat * SGD_DEGREES_TO_RADIANS,
108 Point3D world_pos = sgGeodToCart( geod );
109 Point3D offset = world_pos - center;
112 sgMakeTransMat4( POS, offset.x(), offset.y(), offset.z() );
114 sgVec3 obj_rt, obj_up;
115 sgSetVec3( obj_rt, 0.0, 1.0, 0.0); // Y axis
116 sgSetVec3( obj_up, 0.0, 0.0, 1.0); // Z axis
118 sgMat4 ROT_lon, ROT_lat, ROT_hdg;
119 sgMakeRotMat4( ROT_lon, lon, obj_up );
120 sgMakeRotMat4( ROT_lat, 90 - lat, obj_rt );
121 sgMakeRotMat4( ROT_hdg, hdg, obj_up );
124 sgCopyMat4( TUX, ROT_hdg );
125 sgPostMultMat4( TUX, ROT_lat );
126 sgPostMultMat4( TUX, ROT_lon );
127 sgPostMultMat4( TUX, POS );
129 sgSetCoord( obj_pos, TUX );
134 // Norman's 'fast hack' for above
135 static void WorldCoordinate( sgCoord *obj_pos, Point3D center, double lat,
136 double lon, double elev, double hdg )
138 double lon_rad = lon * SGD_DEGREES_TO_RADIANS;
139 double lat_rad = lat * SGD_DEGREES_TO_RADIANS;
140 double hdg_rad = hdg * SGD_DEGREES_TO_RADIANS;
143 Point3D geod( lon_rad, lat_rad, elev );
145 Point3D world_pos = sgGeodToCart( geod );
146 Point3D offset = world_pos - center;
150 SGfloat sin_lat = (SGfloat)sin( lat_rad );
151 SGfloat cos_lat = (SGfloat)cos( lat_rad );
152 SGfloat cos_lon = (SGfloat)cos( lon_rad );
153 SGfloat sin_lon = (SGfloat)sin( lon_rad );
154 SGfloat sin_hdg = (SGfloat)sin( hdg_rad ) ;
155 SGfloat cos_hdg = (SGfloat)cos( hdg_rad ) ;
157 mat[0][0] = cos_hdg * (SGfloat)sin_lat * (SGfloat)cos_lon - sin_hdg * (SGfloat)sin_lon;
158 mat[0][1] = cos_hdg * (SGfloat)sin_lat * (SGfloat)sin_lon + sin_hdg * (SGfloat)cos_lon;
159 mat[0][2] = -cos_hdg * (SGfloat)cos_lat;
162 mat[1][0] = -sin_hdg * (SGfloat)sin_lat * (SGfloat)cos_lon - cos_hdg * (SGfloat)sin_lon;
163 mat[1][1] = -sin_hdg * (SGfloat)sin_lat * (SGfloat)sin_lon + cos_hdg * (SGfloat)cos_lon;
164 mat[1][2] = sin_hdg * (SGfloat)cos_lat;
167 mat[2][0] = (SGfloat)cos_lat * (SGfloat)cos_lon;
168 mat[2][1] = (SGfloat)cos_lat * (SGfloat)sin_lon;
169 mat[2][2] = (SGfloat)sin_lat;
172 mat[3][0] = offset.x();
173 mat[3][1] = offset.y();
174 mat[3][2] = offset.z();
177 sgSetCoord( obj_pos, mat );
181 // recurse an ssg tree and call removeKid() on every node from the
182 // bottom up. Leaves the original branch in existance, but empty so
183 // it can be removed by the calling routine.
184 static void my_remove_branch( ssgBranch * branch ) {
185 for ( ssgEntity *k = branch->getKid( 0 );
187 k = branch->getNextKid() )
189 if ( k -> isAKindOf ( ssgTypeBranch() ) ) {
190 my_remove_branch( (ssgBranch *)k );
191 branch -> removeKid ( k );
192 } else if ( k -> isAKindOf ( ssgTypeLeaf() ) ) {
193 branch -> removeKid ( k ) ;
199 // Free "n" leaf elements of an ssg tree. returns the number of
200 // elements freed. An empty branch node is considered a leaf. This
201 // is intended to spread the load of freeing a complex tile out over
203 static int fgPartialFreeSSGtree( ssgBranch *b, int n ) {
206 // for testing: we could call the following two lines and replace
207 // the functionality of this entire function and everything will
208 // get properly freed, but it will happen all at once and could
209 // cause a huge frame rate hit.
217 // we still have some delete budget left
218 // if ( b->getNumKids() > 100 ) {
219 // cout << "large family = " << b->getNumKids() << endl;
221 // deleting in reverse would help if my plib patch get's
222 // applied, but for now it will make things slower.
223 // for ( int i = b->getNumKids() - 1; i >= 0 ; --i ) {
224 for ( int i = 0; i < b->getNumKids(); ++i ) {
225 ssgEntity *kid = b->getKid(i);
226 if ( kid->isAKindOf( ssgTypeBranch() ) && kid->getRef() <= 1 ) {
227 int result = fgPartialFreeSSGtree( (ssgBranch *)kid, n );
228 num_deletes += result;
234 // remove the kid if (a) it is now empty -or- (b) it's ref
235 // count is > zero at which point we don't care if it's
236 // empty, we don't want to touch it's contents.
237 if ( kid->getNumKids() == 0 || kid->getRef() > 1 ) {
249 // Clean up the memory used by this tile and delete the arrays used by
250 // ssg as well as the whole ssg branch
251 bool FGTileEntry::free_tile() {
252 int delete_size = 100;
253 SG_LOG( SG_TERRAIN, SG_DEBUG,
254 "FREEING TILE = (" << tile_bucket << ")" );
256 SG_LOG( SG_TERRAIN, SG_DEBUG, "(start) free_tracker = " << free_tracker );
258 if ( !(free_tracker & NODES) ) {
259 free_tracker |= NODES;
260 } else if ( !(free_tracker & VEC_PTRS) ) {
261 free_tracker |= VEC_PTRS;
262 } else if ( !(free_tracker & TERRA_NODE) ) {
263 // delete the terrain branch (this should already have been
264 // disconnected from the scene graph)
265 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING terra_transform" );
266 if ( fgPartialFreeSSGtree( terra_transform, delete_size ) == 0 ) {
267 ssgDeRefDelete( terra_transform );
268 free_tracker |= TERRA_NODE;
270 } else if ( !(free_tracker & GROUND_LIGHTS) && gnd_lights_transform ) {
271 // delete the terrain lighting branch (this should already have been
272 // disconnected from the scene graph)
273 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING gnd_lights_transform" );
274 if ( fgPartialFreeSSGtree( gnd_lights_transform, delete_size ) == 0 ) {
275 ssgDeRefDelete( gnd_lights_transform );
276 free_tracker |= GROUND_LIGHTS;
278 } else if ( !(free_tracker & VASI_LIGHTS) && vasi_lights_selector ) {
279 // delete the runway lighting branch (this should already have
280 // been disconnected from the scene graph)
281 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING vasi_lights_selector" );
282 if ( fgPartialFreeSSGtree( vasi_lights_selector, delete_size ) == 0 ) {
283 ssgDeRefDelete( vasi_lights_selector );
284 free_tracker |= VASI_LIGHTS;
286 } else if ( !(free_tracker & RWY_LIGHTS) && rwy_lights_selector ) {
287 // delete the runway lighting branch (this should already have
288 // been disconnected from the scene graph)
289 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING rwy_lights_selector" );
290 if ( fgPartialFreeSSGtree( rwy_lights_selector, delete_size ) == 0 ) {
291 ssgDeRefDelete( rwy_lights_selector );
292 free_tracker |= RWY_LIGHTS;
294 } else if ( !(free_tracker & TAXI_LIGHTS) && taxi_lights_selector ) {
295 // delete the taxi lighting branch (this should already have been
296 // disconnected from the scene graph)
297 SG_LOG( SG_TERRAIN, SG_DEBUG, "FREEING taxi_lights_selector" );
298 if ( fgPartialFreeSSGtree( taxi_lights_selector, delete_size ) == 0 ) {
299 ssgDeRefDelete( taxi_lights_selector );
300 free_tracker |= TAXI_LIGHTS;
302 } else if ( !(free_tracker & LIGHTMAPS) ) {
303 free_tracker |= LIGHTMAPS;
308 SG_LOG( SG_TERRAIN, SG_DEBUG, "(end) free_tracker = " << free_tracker );
310 // if we fall down to here, we still have work todo, return false
315 // Update the ssg transform node for this tile so it can be
316 // properly drawn relative to our (0,0,0) point
317 void FGTileEntry::prep_ssg_node( const Point3D& p, sgVec3 up, float vis) {
318 if ( !loaded ) return;
320 // visibility can change from frame to frame so we update the
321 // range selector cutoff's each time.
322 terra_range->setRange( 0, SG_ZERO );
323 terra_range->setRange( 1, vis + bounding_radius );
325 if ( gnd_lights_range ) {
326 gnd_lights_range->setRange( 0, SG_ZERO );
327 gnd_lights_range->setRange( 1, vis * 1.5 + bounding_radius );
331 sgdSetVec3( sgdTrans, center.x(), center.y(), center.z() );
333 FGLight *l = (FGLight *)(globals->get_subsystem("lighting"));
334 if ( gnd_lights_transform ) {
335 // we need to lift the lights above the terrain to avoid
336 // z-buffer fighting. We do this based on our altitude and
337 // the distance this tile is away from scenery center.
339 // we expect 'up' to be a unit vector coming in, but since we
340 // modify the value of lift_vec, we need to create a local
343 sgCopyVec3( lift_vec, up );
346 agl = globals->get_current_view()->getAltitudeASL_ft()*SG_FEET_TO_METER
347 - globals->get_current_view()->getSGLocation()->get_cur_elev_m();
349 // Compute the distance of the scenery center from the view position.
350 double dist = center.distance3D(p);
352 if ( general.get_glDepthBits() > 16 ) {
353 sgScaleVec3( lift_vec, 10.0 + agl / 100.0 + dist / 10000 );
355 sgScaleVec3( lift_vec, 10.0 + agl / 20.0 + dist / 5000 );
359 sgdCopyVec3( dlt_trans, sgdTrans );
361 sgdSetVec3( dlift_vec, lift_vec );
362 sgdAddVec3( dlt_trans, dlift_vec );
363 gnd_lights_transform->setTransform( dlt_trans );
365 // select which set of lights based on sun angle
366 float sun_angle = l->get_sun_angle() * SGD_RADIANS_TO_DEGREES;
367 if ( sun_angle > 95 ) {
368 gnd_lights_brightness->select(0x04);
369 } else if ( sun_angle > 92 ) {
370 gnd_lights_brightness->select(0x02);
371 } else if ( sun_angle > 89 ) {
372 gnd_lights_brightness->select(0x01);
374 gnd_lights_brightness->select(0x00);
378 if ( vasi_lights_transform ) {
379 // we need to lift the lights above the terrain to avoid
380 // z-buffer fighting. We do this based on our altitude and
381 // the distance this tile is away from scenery center.
384 sgCopyVec3( lift_vec, up );
386 // we fudge agl by 30 meters so that the lifting function
387 // doesn't phase in until we are > 30m agl.
389 agl = globals->get_current_view()->getAltitudeASL_ft()*SG_FEET_TO_METER
390 - globals->get_current_view()->getSGLocation()->get_cur_elev_m()
396 if ( general.get_glDepthBits() > 16 ) {
397 sgScaleVec3( lift_vec, 0.25 + agl / 400.0 + agl*agl / 5000000.0 );
399 sgScaleVec3( lift_vec, 0.25 + agl / 150.0 );
403 sgdCopyVec3( dlt_trans, sgdTrans );
405 sgdSetVec3( dlift_vec, lift_vec );
406 sgdAddVec3( dlt_trans, dlift_vec );
407 vasi_lights_transform->setTransform( dlt_trans );
409 // generally, vasi lights are always on
410 vasi_lights_selector->select(0x01);
413 if ( rwy_lights_transform ) {
414 // we need to lift the lights above the terrain to avoid
415 // z-buffer fighting. We do this based on our altitude and
416 // the distance this tile is away from scenery center.
419 sgCopyVec3( lift_vec, up );
421 // we fudge agl by 30 meters so that the lifting function
422 // doesn't phase in until we are > 30m agl.
424 agl = globals->get_current_view()->getAltitudeASL_ft()*SG_FEET_TO_METER
425 - globals->get_current_view()->getSGLocation()->get_cur_elev_m()
431 if ( general.get_glDepthBits() > 16 ) {
432 sgScaleVec3( lift_vec, 0.01 + agl / 400.0 + agl*agl / 5000000.0 );
434 sgScaleVec3( lift_vec, 0.25 + agl / 150.0 );
438 sgdCopyVec3( dlt_trans, sgdTrans );
440 sgdSetVec3( dlift_vec, lift_vec );
441 sgdAddVec3( dlt_trans, dlift_vec );
442 rwy_lights_transform->setTransform( dlt_trans );
444 // turn runway lights on/off based on sun angle and visibility
445 float sun_angle = l->get_sun_angle() * SGD_RADIANS_TO_DEGREES;
446 if ( sun_angle > 85 ||
447 (fgGetDouble("/environment/visibility-m") < 5000.0) ) {
448 rwy_lights_selector->select(0x01);
450 rwy_lights_selector->select(0x00);
454 if ( taxi_lights_transform ) {
455 // we need to lift the lights above the terrain to avoid
456 // z-buffer fighting. We do this based on our altitude and
457 // the distance this tile is away from scenery center.
460 sgCopyVec3( lift_vec, up );
462 // we fudge agl by 30 meters so that the lifting function
463 // doesn't phase in until we are > 30m agl.
465 agl = globals->get_current_view()->getAltitudeASL_ft()*SG_FEET_TO_METER
466 - globals->get_current_view()->getSGLocation()->get_cur_elev_m()
472 if ( general.get_glDepthBits() > 16 ) {
473 sgScaleVec3( lift_vec, 0.01 + agl / 400.0 + agl*agl / 5000000.0 );
475 sgScaleVec3( lift_vec, 0.25 + agl / 150.0 );
479 sgdCopyVec3( dlt_trans, sgdTrans );
481 sgdSetVec3( dlift_vec, lift_vec );
482 sgdAddVec3( dlt_trans, dlift_vec );
483 taxi_lights_transform->setTransform( dlt_trans );
485 // turn taxi lights on/off based on sun angle and visibility
486 float sun_angle = l->get_sun_angle() * SGD_RADIANS_TO_DEGREES;
487 if ( sun_angle > 85 ||
488 (fgGetDouble("/environment/visibility-m") < 5000.0) ) {
489 taxi_lights_selector->select(0x01);
491 taxi_lights_selector->select(0x00);
495 if ( vasi_lights_transform && is_inner_ring ) {
496 // now we need to traverse the list of vasi lights and update
497 // their coloring (but only for the inner ring, no point in
498 // doing this extra work for tiles that are relatively far
500 for ( int i = 0; i < vasi_lights_transform->getNumKids(); ++i ) {
501 // cout << "vasi root = " << i << endl;
502 ssgBranch *v = (ssgBranch *)vasi_lights_transform->getKid(i);
503 for ( int j = 0; j < v->getNumKids(); ++j ) {
504 // cout << " vasi branch = " << j << endl;
505 ssgTransform *kid = (ssgTransform *)v->getKid(j);
506 SGVASIUserData *vasi = (SGVASIUserData *)kid->getUserData();
508 if ( vasi != NULL ) {
510 sgdCopyVec3( s, vasi->get_abs_pos() );
511 Point3D start(s[0], s[1], s[2]);
514 sgdCopyVec3( d, globals->get_current_view()->get_absolute_view_pos() );
516 double dist = sgdDistanceVec3( s, d );
518 if ( dist < 10000 ) {
520 = globals->get_current_view()->getAltitudeASL_ft()
523 double y = cur_alt - vasi->get_alt_m();
527 angle = asin( y / dist );
532 vasi->set_color(angle * SG_RADIANS_TO_DEGREES);
534 // cout << " dist = " << dist
535 // << " angle = " << angle * SG_RADIANS_TO_DEGREES
538 SG_LOG( SG_TERRAIN, SG_ALERT, "no vasi data!" );
546 // Set up lights rendering call backs
547 static int fgLightsPredraw( ssgEntity *e ) {
549 if (glPointParameterIsSupported) {
550 static float quadratic[3] = {1.0, 0.01, 0.0001};
551 glPointParameterfvProc(GL_DISTANCE_ATTENUATION_EXT, quadratic);
552 glPointParameterfProc(GL_POINT_SIZE_MIN_EXT, 1.0);
559 static int fgLightsPostdraw( ssgEntity *e ) {
561 if (glPointParameterIsSupported) {
562 static float default_attenuation[3] = {1.0, 0.0, 0.0};
563 glPointParameterfvProc(GL_DISTANCE_ATTENUATION_EXT,
564 default_attenuation);
572 ssgLeaf* FGTileEntry::gen_lights( SGMaterialLib *matlib, ssgVertexArray *lights,
573 int inc, float bright )
575 // generate a repeatable random seed
576 float *p1 = lights->get( 0 );
577 unsigned int *seed = (unsigned int *)p1;
580 int size = lights->getNum() / inc;
582 // Allocate ssg structure
583 ssgVertexArray *vl = new ssgVertexArray( size + 1 );
584 ssgNormalArray *nl = NULL;
585 ssgTexCoordArray *tl = NULL;
586 ssgColourArray *cl = new ssgColourArray( size + 1 );
589 for ( int i = 0; i < lights->getNum(); ++i ) {
590 // this loop is slightly less efficient than it otherwise
591 // could be, but we want a red light to always be red, and a
592 // yellow light to always be yellow, etc. so we are trying to
593 // preserve the random sequence.
594 float zombie = sg_random();
595 if ( i % inc == 0 ) {
596 vl->add( lights->get(i) );
598 // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
599 float factor = sg_random();
602 if ( zombie > 0.5 ) {
603 // 50% chance of yellowish
604 sgSetVec4( color, 0.9, 0.9, 0.3, bright - factor * 0.2 );
605 } else if ( zombie > 0.15 ) {
606 // 35% chance of whitish
607 sgSetVec4( color, 0.9, 0.9, 0.8, bright - factor * 0.2 );
608 } else if ( zombie > 0.05 ) {
609 // 10% chance of orangish
610 sgSetVec4( color, 0.9, 0.6, 0.2, bright - factor * 0.2 );
612 // 5% chance of redish
613 sgSetVec4( color, 0.9, 0.2, 0.2, bright - factor * 0.2 );
621 new ssgVtxTable ( GL_POINTS, vl, nl, tl, cl );
624 SGMaterial *mat = matlib->find( "GROUND_LIGHTS" );
625 leaf->setState( mat->get_state() );
626 leaf->setCallback( SSG_CALLBACK_PREDRAW, fgLightsPredraw );
627 leaf->setCallback( SSG_CALLBACK_POSTDRAW, fgLightsPostdraw );
633 bool FGTileEntry::obj_load( const string& path,
635 ssgBranch *vasi_lights,
636 ssgBranch *rwy_lights,
637 ssgBranch *taxi_lights,
638 ssgVertexArray *ground_lights, bool is_base )
640 Point3D c; // returned center point
641 double br; // returned bounding radius
643 bool use_random_objects =
644 fgGetBool("/sim/rendering/random-objects", true);
646 // try loading binary format
647 if ( sgBinObjLoad( path, is_base,
648 &c, &br, globals->get_matlib(), use_random_objects,
649 geometry, vasi_lights, rwy_lights, taxi_lights,
654 bounding_radius = br;
658 return (geometry != NULL);
671 // storage class for deferred object processing in FGTileEntry::load()
673 Object(object_type t, string& token, const SGPath& p, istream& in)
678 in >> lon >> lat >> elev >> hdg;
682 SG_LOG(SG_TERRAIN, SG_INFO, token << " " << name);
684 SG_LOG(SG_TERRAIN, SG_INFO, token << " " << name << " lon=" << lon
685 << " lat=" << lat << " elev=" << elev << " hdg=" << hdg);
690 double lon, lat, elev, hdg;
695 FGTileEntry::load( const string_list &path_list, bool is_base )
697 bool found_tile_base = false;
700 vector<const Object*> objects;
702 // scan and parse all files and store information
703 for (int i = 0; i < path_list.size(); i++) {
705 bool has_base = false;
707 // Generate names for later use
708 string index_str = tile_bucket.gen_index_str();
710 SGPath tile_path = path_list[i];
711 tile_path.append( tile_bucket.gen_base_path() );
713 SGPath basename = tile_path;
714 basename.append( index_str );
716 SG_LOG( SG_TERRAIN, SG_INFO, "Loading tile " << basename.str() );
719 // Check for master .stg (scene terra gear) file
720 SGPath stg_name = basename;
721 stg_name.concat( ".stg" );
723 sg_gzifstream in( stg_name.str() );
727 while ( ! in.eof() ) {
731 if ( token[0] == '#' ) {
735 // Load only once (first found)
736 if ( token == "OBJECT_BASE" ) {
738 in >> name >> ::skipws;
739 SG_LOG( SG_TERRAIN, SG_INFO, token << " " << name );
741 if (!found_tile_base) {
742 found_tile_base = true;
745 object_base = tile_path;
746 object_base.append(name);
749 SG_LOG(SG_TERRAIN, SG_INFO, " (skipped)");
751 // Load only if base is not in another file
752 } else if ( token == "OBJECT" ) {
753 if (!found_tile_base || has_base)
754 objects.push_back(new Object(OBJECT, token, tile_path, in));
757 in >> name >> ::skipeol;
758 SG_LOG(SG_TERRAIN, SG_INFO, token << " " << name << " (skipped)");
762 } else if ( token == "OBJECT_STATIC" ) {
763 objects.push_back(new Object(OBJECT_STATIC, token, tile_path, in));
765 } else if ( token == "OBJECT_SHARED" ) {
766 objects.push_back(new Object(OBJECT_SHARED, token, tile_path, in));
768 } else if ( token == "OBJECT_TAXI_SIGN" ) {
769 objects.push_back(new Object(OBJECT_TAXI_SIGN, token, tile_path, in));
771 } else if ( token == "OBJECT_RUNWAY_SIGN" ) {
772 objects.push_back(new Object(OBJECT_RUNWAY_SIGN, token, tile_path, in));
775 SG_LOG( SG_TERRAIN, SG_DEBUG,
776 "Unknown token '" << token << "' in " << stg_name.str() );
783 // obj_load() will generate ground lighting for us ...
784 ssgVertexArray *light_pts = new ssgVertexArray( 100 );
785 ssgBranch* new_tile = new ssgBranch;
788 if (found_tile_base) {
789 // load tile if found ...
790 ssgBranch *geometry = new ssgBranch;
791 if ( obj_load( object_base.str(), geometry,
792 NULL, NULL, NULL, light_pts, true ) ) {
793 geometry->getKid( 0 )->setTravCallback(SSG_CALLBACK_PRETRAV,
794 &FGTileMgr::tile_filter_cb);
796 new_tile -> addKid( geometry );
802 // ... or generate an ocean tile on the fly
803 ssgBranch *geometry = new ssgBranch;
806 if ( sgGenTile( path_list[0], tile_bucket, &c, &br,
807 globals->get_matlib(), geometry ) ) {
809 bounding_radius = br;
810 new_tile -> addKid( geometry );
813 SG_LOG( SG_TERRAIN, SG_ALERT,
814 "Warning: failed to generate ocean tile!" );
819 // now that we have a valid center, process all the objects
820 for (int j = 0; j < objects.size(); j++) {
821 const Object *obj = objects[j];
823 if (obj->type == OBJECT) {
824 SGPath custom_path = obj->path;
825 custom_path.append( obj->name );
827 ssgBranch *geometry = new ssgBranch;
828 ssgBranch *vasi_lights = new ssgBranch;
829 ssgBranch *rwy_lights = new ssgBranch;
830 ssgBranch *taxi_lights = new ssgBranch;
832 if ( obj_load( custom_path.str(),
833 geometry, vasi_lights, rwy_lights,
834 taxi_lights, NULL, false ) ) {
836 if ( geometry -> getNumKids() > 0 ) {
837 geometry->getKid( 0 )->setTravCallback(
838 SSG_CALLBACK_PRETRAV,
839 &FGTileMgr::tile_filter_cb );
840 new_tile -> addKid( geometry );
845 if ( vasi_lights -> getNumKids() > 0 )
846 vasi_lights_transform -> addKid( vasi_lights );
850 if ( rwy_lights -> getNumKids() > 0 )
851 rwy_lights_transform -> addKid( rwy_lights );
855 if ( taxi_lights -> getNumKids() > 0 )
856 taxi_lights_transform -> addKid( taxi_lights );
868 } else if (obj->type == OBJECT_SHARED || obj->type == OBJECT_STATIC) {
869 // object loading is deferred to main render thread,
870 // but lets figure out the paths right now.
872 if ( obj->type == OBJECT_STATIC ) {
873 custom_path = obj->path;
875 custom_path = globals->get_fg_root();
877 custom_path.append( obj->name );
880 WorldCoordinate( &obj_pos, center, obj->lat, obj->lon, obj->elev, obj->hdg );
882 ssgTransform *obj_trans = new ssgTransform;
883 obj_trans->setTransform( &obj_pos );
885 // wire as much of the scene graph together as we can
886 new_tile->addKid( obj_trans );
889 // push an entry onto the model load queue
891 = new FGDeferredModel( custom_path.str(),
895 FGTileMgr::model_ready( dm );
898 } else if (obj->type == OBJECT_TAXI_SIGN || obj->type == OBJECT_RUNWAY_SIGN) {
899 ssgBranch *(*make_sign)(SGMaterialLib *, const string, const string);
900 make_sign = obj->type == OBJECT_TAXI_SIGN ? sgMakeTaxiSign : sgMakeRunwaySign;
902 // load the object itself
903 SGPath custom_path = obj->path;
904 custom_path.append( obj->name );
907 WorldCoordinate( &obj_pos, center, obj->lat, obj->lon, obj->elev, obj->hdg );
909 ssgTransform *obj_trans = new ssgTransform;
910 obj_trans->setTransform( &obj_pos );
912 ssgBranch *custom_obj
913 = (*make_sign)(globals->get_matlib(), custom_path.str(), obj->name);
915 // wire the pieces together
916 if ( custom_obj != NULL ) {
917 obj_trans -> addKid( custom_obj );
919 new_tile->addKid( obj_trans );
926 if ( new_tile != NULL ) {
927 terra_range->addKid( new_tile );
930 terra_transform->addKid( terra_range );
932 // calculate initial tile offset
934 sgdSetVec3( sgdTrans, center.x(), center.y(), center.z() );
935 terra_transform->setTransform( sgdTrans );
937 // Add ground lights to scene graph if any exist
938 gnd_lights_transform = NULL;
939 gnd_lights_range = NULL;
940 if ( light_pts->getNum() ) {
941 SG_LOG( SG_TERRAIN, SG_DEBUG, "generating lights" );
942 gnd_lights_transform = new ssgPlacementTransform;
943 gnd_lights_range = new ssgRangeSelector;
944 gnd_lights_brightness = new ssgSelector;
947 lights = gen_lights( globals->get_matlib(), light_pts, 4, 0.7 );
948 gnd_lights_brightness->addKid( lights );
950 lights = gen_lights( globals->get_matlib(), light_pts, 2, 0.85 );
951 gnd_lights_brightness->addKid( lights );
953 lights = gen_lights( globals->get_matlib(), light_pts, 1, 1.0 );
954 gnd_lights_brightness->addKid( lights );
956 gnd_lights_range->addKid( gnd_lights_brightness );
957 gnd_lights_transform->addKid( gnd_lights_range );
958 gnd_lights_transform->setTransform( sgdTrans );
961 // Update vasi lights transform
962 if ( vasi_lights_transform->getNumKids() > 0 ) {
963 vasi_lights_transform->setTransform( sgdTrans );
966 // Update runway lights transform
967 if ( rwy_lights_transform->getNumKids() > 0 ) {
968 rwy_lights_transform->setTransform( sgdTrans );
971 // Update taxi lights transform
972 if ( taxi_lights_transform->getNumKids() > 0 ) {
973 taxi_lights_transform->setTransform( sgdTrans );
980 FGTileEntry::makeDList( ssgBranch *b )
982 int nb = b->getNumKids();
983 for (int i = 0; i<nb; i++) {
984 ssgEntity *e = b->getKid(i);
985 if (e->isAKindOf(ssgTypeLeaf())) {
986 ((ssgLeaf*)e)->makeDList();
987 } else if (e->isAKindOf(ssgTypeBranch())) {
988 makeDList( (ssgBranch*)e );
994 FGTileEntry::add_ssg_nodes( ssgBranch *terrain_branch,
995 ssgBranch *gnd_lights_branch,
996 ssgBranch *vasi_lights_branch,
997 ssgBranch *rwy_lights_branch,
998 ssgBranch *taxi_lights_branch )
1000 // bump up the ref count so we can remove this later without
1001 // having ssg try to free the memory.
1002 #if PLIB_VERSION > 183
1003 if ( fgGetBool( "/sim/rendering/use-display-list", true ) ) {
1004 makeDList( terra_transform );
1008 terra_transform->ref();
1009 terrain_branch->addKid( terra_transform );
1010 globals->get_scenery()->register_placement_transform(terra_transform);
1012 SG_LOG( SG_TERRAIN, SG_DEBUG,
1013 "connected a tile into scene graph. terra_transform = "
1014 << terra_transform );
1015 SG_LOG( SG_TERRAIN, SG_DEBUG, "num parents now = "
1016 << terra_transform->getNumParents() );
1018 if ( gnd_lights_transform != NULL ) {
1019 // bump up the ref count so we can remove this later without
1020 // having ssg try to free the memory.
1021 gnd_lights_transform->ref();
1022 gnd_lights_branch->addKid( gnd_lights_transform );
1023 globals->get_scenery()->register_placement_transform(gnd_lights_transform);
1026 if ( vasi_lights_transform != NULL ) {
1027 // bump up the ref count so we can remove this later without
1028 // having ssg try to free the memory.
1029 vasi_lights_selector->ref();
1030 vasi_lights_selector->addKid( vasi_lights_transform );
1031 globals->get_scenery()->register_placement_transform(vasi_lights_transform);
1032 vasi_lights_branch->addKid( vasi_lights_selector );
1035 if ( rwy_lights_transform != NULL ) {
1036 // bump up the ref count so we can remove this later without
1037 // having ssg try to free the memory.
1038 rwy_lights_selector->ref();
1039 rwy_lights_selector->addKid( rwy_lights_transform );
1040 globals->get_scenery()->register_placement_transform(rwy_lights_transform);
1041 rwy_lights_branch->addKid( rwy_lights_selector );
1044 if ( taxi_lights_transform != NULL ) {
1045 // bump up the ref count so we can remove this later without
1046 // having ssg try to free the memory.
1047 taxi_lights_selector->ref();
1048 taxi_lights_selector->addKid( taxi_lights_transform );
1049 globals->get_scenery()->register_placement_transform(taxi_lights_transform);
1050 taxi_lights_branch->addKid( taxi_lights_selector );
1058 FGTileEntry::disconnect_ssg_nodes()
1060 SG_LOG( SG_TERRAIN, SG_DEBUG, "disconnecting ssg nodes" );
1063 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a not-fully loaded tile!" );
1065 SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a fully loaded tile! terra_transform = " << terra_transform );
1068 // Unregister that one at the scenery manager
1069 globals->get_scenery()->unregister_placement_transform(terra_transform);
1071 // find the terrain branch parent
1072 int pcount = terra_transform->getNumParents();
1074 // find the first parent (should only be one)
1075 ssgBranch *parent = terra_transform->getParent( 0 ) ;
1077 // disconnect the tile (we previously ref()'d it so it
1078 // won't get freed now)
1079 parent->removeKid( terra_transform );
1081 SG_LOG( SG_TERRAIN, SG_ALERT,
1082 "parent pointer is NULL! Dying" );
1086 SG_LOG( SG_TERRAIN, SG_ALERT,
1087 "Parent count is zero for an ssg tile! Dying" );
1091 // find the ground lighting branch
1092 if ( gnd_lights_transform ) {
1093 // Unregister that one at the scenery manager
1094 globals->get_scenery()->unregister_placement_transform(gnd_lights_transform);
1095 pcount = gnd_lights_transform->getNumParents();
1097 // find the first parent (should only be one)
1098 ssgBranch *parent = gnd_lights_transform->getParent( 0 ) ;
1100 // disconnect the light branch (we previously ref()'d
1101 // it so it won't get freed now)
1102 parent->removeKid( gnd_lights_transform );
1104 SG_LOG( SG_TERRAIN, SG_ALERT,
1105 "parent pointer is NULL! Dying" );
1109 SG_LOG( SG_TERRAIN, SG_ALERT,
1110 "Parent count is zero for an ssg light tile! Dying" );
1115 // find the vasi lighting branch
1116 if ( vasi_lights_transform ) {
1117 // Unregister that one at the scenery manager
1118 globals->get_scenery()->unregister_placement_transform(vasi_lights_transform);
1119 pcount = vasi_lights_transform->getNumParents();
1121 // find the first parent (should only be one)
1122 ssgBranch *parent = vasi_lights_transform->getParent( 0 ) ;
1124 // disconnect the light branch (we previously ref()'d
1125 // it so it won't get freed now)
1126 parent->removeKid( vasi_lights_transform );
1128 SG_LOG( SG_TERRAIN, SG_ALERT,
1129 "parent pointer is NULL! Dying" );
1133 SG_LOG( SG_TERRAIN, SG_ALERT,
1134 "Parent count is zero for an ssg light tile! Dying" );
1139 // find the runway lighting branch
1140 if ( rwy_lights_transform ) {
1141 // Unregister that one at the scenery manager
1142 globals->get_scenery()->unregister_placement_transform(rwy_lights_transform);
1143 pcount = rwy_lights_transform->getNumParents();
1145 // find the first parent (should only be one)
1146 ssgBranch *parent = rwy_lights_transform->getParent( 0 ) ;
1148 // disconnect the light branch (we previously ref()'d
1149 // it so it won't get freed now)
1150 parent->removeKid( rwy_lights_transform );
1152 SG_LOG( SG_TERRAIN, SG_ALERT,
1153 "parent pointer is NULL! Dying" );
1157 SG_LOG( SG_TERRAIN, SG_ALERT,
1158 "Parent count is zero for an ssg light tile! Dying" );
1163 // find the taxi lighting branch
1164 if ( taxi_lights_transform ) {
1165 // Unregister that one at the scenery manager
1166 globals->get_scenery()->unregister_placement_transform(taxi_lights_transform);
1167 pcount = taxi_lights_transform->getNumParents();
1169 // find the first parent (should only be one)
1170 ssgBranch *parent = taxi_lights_transform->getParent( 0 ) ;
1172 // disconnect the light branch (we previously ref()'d
1173 // it so it won't get freed now)
1174 parent->removeKid( taxi_lights_transform );
1176 SG_LOG( SG_TERRAIN, SG_ALERT,
1177 "parent pointer is NULL! Dying" );
1181 SG_LOG( SG_TERRAIN, SG_ALERT,
1182 "Parent count is zero for an ssg light tile! Dying" );