diff --git a/src/ATC/AIEntity.cxx b/src/ATC/AIEntity.cxx index 67ba889eb..2eb8ba1d6 100644 --- a/src/ATC/AIEntity.cxx +++ b/src/ATC/AIEntity.cxx @@ -36,8 +36,6 @@ #include "AIEntity.hxx" -extern ssgRoot* scene; // The global Flightgear scene graph - FGAIEntity::~FGAIEntity() { } @@ -55,7 +53,7 @@ void FGAIEntity::Transform() { sgCoord shippos; FastWorldCoordinate(&shippos, sc); position->setTransform( &shippos ); - scene->addKid(position); + globals->get_scene_graph()->addKid(position); //cout << "Transform called\n"; } diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx index 9d5b0518b..67bea6200 100644 --- a/src/Main/globals.hxx +++ b/src/Main/globals.hxx @@ -67,6 +67,9 @@ class FGAIMgr; class FGAircraftModel; class FGModelMgr; +class ssgRoot; +class ssgBranch; + /** * Bucket for subsystem pointers representing the sim's state. @@ -158,6 +161,14 @@ private: // list of serial port-like configurations string_list *channel_options_list; + // SSG scene graph + ssgRoot * scene_graph; + ssgBranch * terrain_branch; + ssgBranch * gnd_lights_branch; + ssgBranch * rwy_lights_branch; + ssgBranch * models_branch; + ssgBranch * aircraft_branch; + public: FGGlobals(); @@ -268,6 +279,40 @@ public: channel_options_list = l; } + inline ssgRoot * get_scene_graph () const { return scene_graph; } + inline void set_scene_graph (ssgRoot * s) { scene_graph = s; } + + inline ssgBranch * get_terrain_branch () const { return terrain_branch; } + inline void set_terrain_branch (ssgBranch * t) { terrain_branch = t; } + + inline ssgBranch * get_gnd_lights_branch () const { + return gnd_lights_branch; + } + inline void set_gnd_lights_branch (ssgBranch * t) { + gnd_lights_branch = t; + } + + inline ssgBranch * get_rwy_lights_branch () const { + return rwy_lights_branch; + } + inline void set_rwy_lights_branch (ssgBranch * t) { + rwy_lights_branch = t; + } + + inline ssgBranch * get_models_branch () const { + return models_branch; + } + inline void set_models_branch (ssgBranch * t) { + models_branch = t; + } + + inline ssgBranch * get_aircraft_branch () const { + return aircraft_branch; + } + inline void set_aircraft_branch (ssgBranch * t) { + aircraft_branch = t; + } + /** * Save the current state as the initial state. diff --git a/src/Main/main.cxx b/src/Main/main.cxx index 9ce9970a5..c2b46a6bf 100644 --- a/src/Main/main.cxx +++ b/src/Main/main.cxx @@ -183,12 +183,6 @@ static long global_multi_loop; // forward declaration void fgReshape( int width, int height ); -// ssg variables -ssgRoot *scene = NULL; -ssgBranch *terrain_branch = NULL; -ssgBranch *gnd_lights_branch = NULL; -ssgBranch *rwy_lights_branch = NULL; - // fog constants. I'm a little nervous about putting actual code out // here but it seems to work (?) static const double m_log01 = -log( 0.01 ); @@ -375,7 +369,7 @@ void trRenderFrame( void ) { ssgGetLight( 0 ) -> setColour( GL_DIFFUSE, l->scene_diffuse ); glEnable( GL_DEPTH_TEST ); ssgSetNearFar( scene_nearplane, scene_farplane ); - ssgCullAndDraw( scene ); + ssgCullAndDraw( globals->get_scene_graph() ); // draw the lights glFogf (GL_FOG_DENSITY, fog_exp2_punch_through); @@ -655,7 +649,7 @@ void fgRenderFrame( void ) { glEnable( GL_DEPTH_TEST ); ssgSetNearFar( scene_nearplane, scene_farplane ); - ssgCullAndDraw( scene ); + ssgCullAndDraw( globals->get_scene_graph() ); // change state for lighting here @@ -1483,6 +1477,35 @@ int mainLoop( int argc, char **argv ) { SGPath modelpath( globals->get_fg_root() ); ssgModelPath( (char *)modelpath.c_str() ); + // Scene graph root + globals->set_scene_graph(new ssgRoot); + globals->get_scene_graph()->setName( "Scene" ); + + lighting = new ssgRoot; + lighting->setName( "Lighting" ); + + // Terrain branch + globals->set_terrain_branch(new ssgBranch); + globals->get_terrain_branch()->setName( "Terrain" ); + globals->get_scene_graph()->addKid( globals->get_terrain_branch() ); + + globals->set_models_branch(new ssgBranch); + globals->get_models_branch()->setName( "Models" ); + globals->get_scene_graph()->addKid( globals->get_models_branch() ); + + globals->set_aircraft_branch(new ssgBranch); + globals->get_aircraft_branch()->setName( "Aircraft" ); + globals->get_scene_graph()->addKid( globals->get_aircraft_branch() ); + + // Lighting + globals->set_gnd_lights_branch(new ssgBranch); + globals->get_gnd_lights_branch()->setName( "Ground Lighting" ); + lighting->addKid( globals->get_gnd_lights_branch() ); + + globals->set_rwy_lights_branch(new ssgBranch); + globals->get_rwy_lights_branch()->setName( "Runway Lighting" ); + lighting->addKid( globals->get_rwy_lights_branch() ); + //////////////////////////////////////////////////////////////////// // Initialize the general model subsystem. //////////////////////////////////////////////////////////////////// @@ -1509,13 +1532,6 @@ int mainLoop( int argc, char **argv ) { viewmgr->bind(); - // Scene graph root - scene = new ssgRoot; - scene->setName( "Scene" ); - - lighting = new ssgRoot; - lighting->setName( "Lighting" ); - // Initialize the sky SGPath ephem_data_path( globals->get_fg_root() ); ephem_data_path.append( "Astro" ); @@ -1555,20 +1571,6 @@ int mainLoop( int argc, char **argv ) { SGMagVar *magvar = new SGMagVar(); globals->set_mag( magvar ); - // Terrain branch - terrain_branch = new ssgBranch; - terrain_branch->setName( "Terrain" ); - scene->addKid( terrain_branch ); - - // Lighting - gnd_lights_branch = new ssgBranch; - gnd_lights_branch->setName( "Ground Lighting" ); - lighting->addKid( gnd_lights_branch ); - - rwy_lights_branch = new ssgBranch; - rwy_lights_branch->setName( "Runway Lighting" ); - lighting->addKid( rwy_lights_branch ); - // airport = new ssgBranch; // airport->setName( "Airport Lighting" ); // lighting->addKid( airport ); @@ -1580,7 +1582,8 @@ int mainLoop( int argc, char **argv ) { #ifdef FG_NETWORK_OLK // Do the network intialization if ( fgGetBool("/sim/networking/network-olk") ) { - printf("Multipilot mode %s\n", fg_net_init( scene ) ); + printf("Multipilot mode %s\n", + fg_net_init( globals->get_scene_graph() ) ); } #endif @@ -1819,7 +1822,7 @@ void fgLoadDCS(void) { //dummy_tile->lightmaps_sequence->setTraversalMaskBits( SSGTRAV_HOT ); lightpoints_transform->addKid( dummy_tile->lightmaps_sequence ); lightpoints_transform->ref(); - gnd_lights_branch->addKid( lightpoints_transform ); + globals->get_gnd_lights_branch()->addKid( lightpoints_transform ); } } //if in1 } //if objc @@ -1833,7 +1836,7 @@ void fgLoadDCS(void) { SG_LOG ( SG_TERRAIN, SG_ALERT, "Finished object processing." ); - terrain_branch->addKid( ship_sel ); //add selector node to root node + globals->get_terrain_branch()->addKid( ship_sel ); //add selector node to root node } return; diff --git a/src/Model/acmodel.cxx b/src/Model/acmodel.cxx index 16c1f1c56..fd6bda35e 100644 --- a/src/Model/acmodel.cxx +++ b/src/Model/acmodel.cxx @@ -30,6 +30,7 @@ FGAircraftModel::FGAircraftModel () : _aircraft(0), + _selector(new ssgSelector), _scene(new ssgRoot), _nearplane(0.01f), _farplane(100.0f) @@ -40,6 +41,8 @@ FGAircraftModel::~FGAircraftModel () { delete _aircraft; delete _scene; + // SSG will delete it + globals->get_aircraft_branch()->removeKid(_selector); } void @@ -48,6 +51,8 @@ FGAircraftModel::init () _aircraft = new FG3DModel; _aircraft->init(fgGetString("/sim/model/path", "Models/Geometry/glider.ac")); _scene->addKid(_aircraft->getSceneGraph()); + _selector->addKid(_aircraft->getSceneGraph()); + globals->get_aircraft_branch()->addKid(_selector); } void @@ -90,13 +95,14 @@ FGAircraftModel::draw () // FIXME: view number shouldn't be // hard-coded. int view_number = globals->get_viewmgr()->get_current(); - if (_aircraft->getVisible()) { - if (view_number == 0) { - glClearDepth(1); - glClear(GL_DEPTH_BUFFER_BIT); - ssgSetNearFar(_nearplane, _farplane); - } + if (_aircraft->getVisible() && view_number == 0) { + glClearDepth(1); + glClear(GL_DEPTH_BUFFER_BIT); + ssgSetNearFar(_nearplane, _farplane); ssgCullAndDraw(_scene); + _selector->select(0); + } else { + _selector->select(1); } } diff --git a/src/Model/acmodel.hxx b/src/Model/acmodel.hxx index 743f162ea..08ffd139f 100644 --- a/src/Model/acmodel.hxx +++ b/src/Model/acmodel.hxx @@ -39,6 +39,7 @@ public: private: FG3DModel * _aircraft; + ssgSelector * _selector; ssgRoot * _scene; float _nearplane; float _farplane; diff --git a/src/Model/modelmgr.cxx b/src/Model/modelmgr.cxx index 90fd78cc2..4ed1c6bbb 100644 --- a/src/Model/modelmgr.cxx +++ b/src/Model/modelmgr.cxx @@ -9,18 +9,17 @@ FGModelMgr::FGModelMgr () - : _scene(new ssgRoot), - _nearplane(0.5f), - _farplane(120000.0f) + : _selector(new ssgSelector) { } FGModelMgr::~FGModelMgr () { for (int i = 0; i < _instances.size(); i++) { + globals->get_models_branch() + ->removeKid(_instances[i]->model->getSceneGraph()); delete _instances[i]; } - delete _scene; } void @@ -76,8 +75,8 @@ FGModelMgr::init () else model->setHeadingDeg(node->getDoubleValue("heading-deg")); - // Add this model to the scene graph - _scene->addKid(model->getSceneGraph()); + // Add this model to the global scene graph + globals->get_scene_graph()->addKid(model->getSceneGraph()); // Save this instance for updating _instances.push_back(instance); @@ -124,8 +123,8 @@ FGModelMgr::update (int dt) void FGModelMgr::draw () { - ssgSetNearFar(_nearplane, _farplane); - ssgCullAndDraw(_scene); +// ssgSetNearFar(_nearplane, _farplane); +// ssgCullAndDraw(_scene); } diff --git a/src/Model/modelmgr.hxx b/src/Model/modelmgr.hxx index 1ea8aef6d..15933d63c 100644 --- a/src/Model/modelmgr.hxx +++ b/src/Model/modelmgr.hxx @@ -57,9 +57,7 @@ private: vector _instances; - ssgRoot * _scene; - float _nearplane; - float _farplane; + ssgSelector * _selector; }; diff --git a/src/Scenery/hitlist.cxx b/src/Scenery/hitlist.cxx index 21ef767fd..9e2d6eb19 100644 --- a/src/Scenery/hitlist.cxx +++ b/src/Scenery/hitlist.cxx @@ -20,8 +20,6 @@ #include "hitlist.hxx" -extern ssgBranch *terrain_branch; - // forward declaration of our helper/convenience functions static void sgMultMat4(sgdMat4 dst, sgdMat4 m1, sgMat4 m2); static void ssgGetEntityTransform(ssgEntity *entity, sgMat4 m ); @@ -509,8 +507,7 @@ bool fgCurrentElev( sgdVec3 abs_view_pos, sgdVec3 scenery_center, sgdCopyVec3(orig, view_pos ); sgdCopyVec3(dir, abs_view_pos ); - // !! why is terrain not globals->get_terrain() - hit_list->Intersect( terrain_branch, orig, dir ); + hit_list->Intersect( globals->get_terrain_branch(), orig, dir ); int this_hit=0; Point3D geoc; diff --git a/src/Scenery/newcache.cxx b/src/Scenery/newcache.cxx index 4968b08e0..f7f599fb9 100644 --- a/src/Scenery/newcache.cxx +++ b/src/Scenery/newcache.cxx @@ -182,8 +182,6 @@ bool FGNewCache::make_space() { // Clear all completely loaded tiles (ignores partially loaded tiles) void FGNewCache::clear_cache() { - // This is a hack that should really get cleaned up at some point - extern ssgBranch *terrain_branch; tile_map_iterator current = tile_cache.begin(); tile_map_iterator end = tile_cache.end(); @@ -199,7 +197,7 @@ void FGNewCache::clear_cache() { } // and ... just in case we missed something ... - terrain_branch->removeAllKids(); + globals->get_terrain_branch()->removeAllKids(); } diff --git a/src/Scenery/tilemgr.cxx b/src/Scenery/tilemgr.cxx index 0847248d4..19a46febf 100644 --- a/src/Scenery/tilemgr.cxx +++ b/src/Scenery/tilemgr.cxx @@ -51,11 +51,6 @@ #define TEST_LAST_HIT_CACHE -extern ssgRoot *scene; -extern ssgBranch *terrain_branch; // branch that holds world geometry -extern ssgBranch *gnd_lights_branch; // branch that holds ground lighting -extern ssgBranch *rwy_lights_branch; // branch that holds runway lighting - // the tile manager FGTileMgr global_tile_mgr; @@ -335,9 +330,9 @@ int FGTileMgr::update( double lon, double lat, double visibility_meters ) { FGTileEntry* e = attach_queue.front(); attach_queue.pop(); #endif - e->add_ssg_nodes( terrain_branch, - gnd_lights_branch, - rwy_lights_branch ); + e->add_ssg_nodes( globals->get_terrain_branch(), + globals->get_gnd_lights_branch(), + globals->get_rwy_lights_branch() ); // cout << "Adding ssg nodes for " }