]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scenery/tilemgr.cxx
Further restructuring of the scenery loading code.
[flightgear.git] / src / Scenery / tilemgr.cxx
index fec39fa20e666996e8492fe9972b5844df557c6e..4b6cffc7400c8ab19d27c29974c58ac529193f68 100644 (file)
 #include <simgear/math/vector.hxx>
 
 #include <Main/globals.hxx>
+#include <Main/fg_props.hxx>
 #include <Main/viewer.hxx>
 #include <Objects/obj.hxx>
 
-#ifndef FG_OLD_WEATHER
-#  include <WeatherCM/FGLocalWeatherDatabase.h>
-#else
-#  include <Weather/weather.hxx>
-#endif
-
 #include "newcache.hxx"
 #include "scenery.hxx"
 #include "tilemgr.hxx"
@@ -57,8 +52,9 @@
 #define TEST_LAST_HIT_CACHE
 
 extern ssgRoot *scene;
-extern ssgBranch *terrain;
-extern ssgBranch *ground;
+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;
@@ -76,6 +72,7 @@ queue<FGDeferredModel *> FGTileMgr::model_queue;
 // Constructor
 FGTileMgr::FGTileMgr():
     state( Start ),
+    current_tile( NULL ),
     vis( 16000 ),
     counter_hack(0)
 {
@@ -154,17 +151,21 @@ void FGTileMgr::sched_tile( const SGBucket& b ) {
 
 // schedule a needed buckets for loading
 void FGTileMgr::schedule_needed() {
-    cout << "scheduling needed tiles for " << longitude << " " << latitude << endl;
-#ifndef FG_OLD_WEATHER
-    if ( WeatherDatabase != NULL ) {
-       vis = WeatherDatabase->getWeatherVisibility();
-    } else {
-       vis = 16000;
+    // sanity check (unfortunately needed!)
+    if ( longitude < -180.0 || longitude > 180.0 
+         || latitude < -90.0 || latitude > 90.0 )
+    {
+        SG_LOG( SG_TERRAIN, SG_ALERT,
+                "Attempting to schedule tiles for bogus latitude and" );
+        SG_LOG( SG_TERRAIN, SG_ALERT,
+                "longitude.  This is a FATAL error.  Exiting!" );
+        exit(-1);        
     }
-#else
-    vis = current_weather.get_visibility();
-#endif
-    // cout << "visibility = " << vis << endl;
+
+   SG_LOG( SG_TERRAIN, SG_INFO,
+           "scheduling needed tiles for " << longitude << " " << latitude );
+
+   vis = fgGetDouble("/environment/visibility-m");
 
     double tile_width = current_bucket.get_width_m();
     double tile_height = current_bucket.get_height_m();
@@ -251,7 +252,8 @@ void FGTileMgr::initialize_queue()
 // chunks.  If the chunk isn't already in the cache, then read it from
 // disk.
 int FGTileMgr::update( double lon, double lat ) {
-    SG_LOG( SG_TERRAIN, SG_INFO, "FGTileMgr::update() for " << lon << " " << lat );
+    // SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update() for "
+    //         << lon << " " << lat );
 
     longitude = lon;
     latitude = lat;
@@ -266,6 +268,7 @@ int FGTileMgr::update( double lon, double lat ) {
         scenery.set_next_center( current_tile->center );
     } else {
         SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
+        scenery.set_next_center( Point3D(0.0) );
     }
 
     if ( state == Running ) {
@@ -318,6 +321,7 @@ int FGTileMgr::update( double lon, double lat ) {
     // activate loader thread one out of every 5 frames
     if ( counter_hack == 0 ) {
         // Notify the tile loader that it can load another tile
+
         loader.update();
 
     }
@@ -330,15 +334,17 @@ int FGTileMgr::update( double lon, double lat ) {
        FGTileEntry* e = attach_queue.front();
        attach_queue.pop();
 #endif
-       e->add_ssg_nodes( terrain, ground );
+       e->add_ssg_nodes( terrain_branch,
+                         gnd_lights_branch,
+                         rwy_lights_branch );
        // cout << "Adding ssg nodes for "
     }
 
     sgdVec3 sc;
     sgdSetVec3( sc,
-               scenery.get_center()[0],
-               scenery.get_center()[1],
-               scenery.get_center()[2] );
+                scenery.get_center()[0],
+                scenery.get_center()[1],
+                scenery.get_center()[2] );
 
 #if 0
     if ( scenery.center == Point3D(0.0) ) {
@@ -367,21 +373,50 @@ int FGTileMgr::update( double lon, double lat ) {
        cout << "result = " << scenery.get_cur_elev() << endl;
     } else {
 #endif
-       // cout << "abs view pos = " << current_view.abs_view_pos
-       //      << " view pos = " << current_view.view_pos << endl;
-       double tmp_elev;
-       double tmp_radius;
-       sgdVec3 tmp_normal;
-       if ( fgCurrentElev(globals->get_current_view()->get_abs_view_pos(),
-                          sc, &hit_list,
-                          &tmp_elev, &tmp_radius, tmp_normal) )
-       {
-           scenery.set_cur_elev( tmp_elev );
-           scenery.set_cur_radius( tmp_radius );
-           scenery.set_cur_normal( tmp_normal );
-       } else {
-           scenery.set_cur_elev( -9999.0 );
-       }  
+        /*
+       cout << "abs view pos = "
+             << globals->get_current_view()->get_abs_view_pos()[0] << ","
+             << globals->get_current_view()->get_abs_view_pos()[1] << ","
+             << globals->get_current_view()->get_abs_view_pos()[2]
+            << " view pos = " 
+             << globals->get_current_view()->get_view_pos()[0] << ","
+             << globals->get_current_view()->get_view_pos()[1] << ","
+             << globals->get_current_view()->get_view_pos()[2]
+             << endl;
+        cout << "current_tile = " << current_tile << endl;
+        cout << "Scenery center = " << sc[0] << "," << sc[1] << "," << sc[2]
+             << endl;
+        */
+
+        // overridden with actual values if a terrain intersection is
+        // found
+       double hit_elev = -9999.0;
+       double hit_radius = 0.0;
+       sgdVec3 hit_normal = { 0.0, 0.0, 0.0 };
+
+        bool hit = false;
+        if ( fabs(sc[0]) > 1.0 || fabs(sc[1]) > 1.0 || fabs(sc[2]) > 1.0 ) {
+            // scenery center has been properly defined so any hit
+            // should be valid (and not just luck)
+            hit = fgCurrentElev(globals->get_current_view()->get_abs_view_pos(),
+                                sc,
+                                &hit_list,
+                                &hit_elev,
+                                &hit_radius,
+                                hit_normal);
+        }
+
+        if ( hit ) {
+            scenery.set_cur_elev( hit_elev );
+            scenery.set_cur_radius( hit_radius );
+            scenery.set_cur_normal( hit_normal );
+        } else {
+            scenery.set_cur_elev( -9999.0 );
+            scenery.set_cur_radius( 0.0 );
+            scenery.set_cur_normal( hit_normal );
+        }
+
+       // cout << "Current elevation = " << scenery.get_cur_elev() << endl;
 #if 0
     }
 #endif
@@ -393,16 +428,7 @@ int FGTileMgr::update( double lon, double lat ) {
 void FGTileMgr::prep_ssg_nodes() {
     float vis = 0.0;
 
-#ifndef FG_OLD_WEATHER
-    if ( WeatherDatabase ) {
-       vis = WeatherDatabase->getWeatherVisibility();
-    } else {
-       vis = 16000;
-    }
-#else
-    vis = current_weather.get_visibility();
-#endif
-    // cout << "visibility = " << vis << endl;
+    vis = fgGetDouble("/environment/visibility-m");
 
     // traverse the potentially viewable tile list and update range
     // selector and transform