]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scenery/tilemgr.cxx
Modified Files:
[flightgear.git] / src / Scenery / tilemgr.cxx
index ea10fd64699726e4065a926cf0d862211d3c6dce..16104e612f2051abb6ccc91e4ee2e250d5628b1e 100644 (file)
@@ -16,7 +16,7 @@
 //
 // You should have received a copy of the GNU General Public License
 // along with this program; if not, write to the Free Software
-// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 //
 // $Id$
 
@@ -25,8 +25,6 @@
 #  include <config.h>
 #endif
 
-#include <plib/ssg.h>
-
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/math/point3d.hxx>
@@ -39,6 +37,7 @@
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
 #include <Main/viewer.hxx>
+#include <Scripting/NasalSys.hxx>
 
 #include "newcache.hxx"
 #include "scenery.hxx"
@@ -46,7 +45,7 @@
 
 #define TEST_LAST_HIT_CACHE
 
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
 SGLockedQueue<FGTileEntry *> FGTileMgr::attach_queue;
 SGLockedQueue<FGDeferredModel *> FGTileMgr::model_queue;
 #else
@@ -55,8 +54,6 @@ queue<FGDeferredModel *> FGTileMgr::model_queue;
 #endif // ENABLE_THREADS
 queue<FGTileEntry *> FGTileMgr::delete_queue;
 
-bool FGTileMgr::tile_filter = true;
-
 // Constructor
 FGTileMgr::FGTileMgr():
     state( Start ),
@@ -89,7 +86,7 @@ int FGTileMgr::init() {
     }
 
     while ( ! model_queue.empty() ) {
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
         FGDeferredModel* dm = model_queue.pop();
 #else
         FGDeferredModel* dm = model_queue.front();
@@ -100,8 +97,6 @@ int FGTileMgr::init() {
     loader.reinit();
 #endif
 
-    hit_list.clear();
-
     state = Inited;
 
     previous_bucket.make_bad();
@@ -124,6 +119,8 @@ void FGTileMgr::sched_tile( const SGBucket& b, const bool is_inner_ring ) {
             long index = tile_cache.get_oldest_tile();
             if ( index >= 0 ) {
                 FGTileEntry *old = tile_cache.get_tile( index );
+                // OSGFIXME
+//                 shadows->deleteOccluderFromTile( (ssgBranch *) old->get_terra_transform() );
                 old->disconnect_ssg_nodes();
                 delete_queue.push( old );
                 tile_cache.clear_entry( index );
@@ -152,7 +149,7 @@ void FGTileMgr::sched_tile( const SGBucket& b, const bool is_inner_ring ) {
 
 
 // schedule a needed buckets for loading
-void FGTileMgr::schedule_needed( double vis, SGBucket curr_bucket) {
+void FGTileMgr::schedule_needed( double vis, const SGBucket& curr_bucket) {
     // sanity check (unfortunately needed!)
     if ( longitude < -180.0 || longitude > 180.0 
          || latitude < -90.0 || latitude > 90.0 )
@@ -160,9 +157,10 @@ void FGTileMgr::schedule_needed( double vis, SGBucket curr_bucket) {
         SG_LOG( SG_TERRAIN, SG_ALERT,
                 "Attempting to schedule tiles for bogus lon and lat  = ("
                 << longitude << "," << latitude << ")" );
+        return;                // FIXME
         SG_LOG( SG_TERRAIN, SG_ALERT,
                 "This is a FATAL error.  Exiting!" );
-        exit(-1);        
+        exit(-1);
     }
 
     SG_LOG( SG_TERRAIN, SG_INFO,
@@ -295,7 +293,7 @@ void FGTileMgr::update_queues()
 
             // cout << "loading next model ..." << endl;
             // load the next tile in the queue
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
             FGDeferredModel* dm = model_queue.pop();
 #else
             FGDeferredModel* dm = model_queue.front();
@@ -306,18 +304,30 @@ void FGTileMgr::update_queues()
             // tile cache
             FGTileEntry *t = tile_cache.get_tile( dm->get_bucket() );
             if ( t != NULL ) {
-                ssgTexturePath( (char *)(dm->get_texture_path().c_str()) );
+              //OSGFIXME
+//                 ssgTexturePath( (char *)(dm->get_texture_path().c_str()) );
                 try {
-                    ssgEntity *obj_model =
+                    osg::Node *obj_model =
                         globals->get_model_lib()->load_model( ".",
                                                   dm->get_model_path(),
                                                   globals->get_props(),
-                                                  globals->get_sim_time_sec() );
+                                                  globals->get_sim_time_sec(),
+                                                  dm->get_cache_state(),
+                                                  new FGNasalModelData );
                     if ( obj_model != NULL ) {
-                        dm->get_obj_trans()->addKid( obj_model );
+                        dm->get_obj_trans()->addChild( obj_model );
+              //OSGFIXME
+//                         shadows->addOccluder( (ssgBranch *) obj_model->getParent(0),
+//                             SGShadowVolume::occluderTypeTileObject,
+//                             (ssgBranch *) dm->get_tile()->get_terra_transform());
                     }
-                } catch (const sg_exception& exc) {
-                    SG_LOG( SG_ALL, SG_ALERT, exc.getMessage() );
+                } catch (const sg_io_exception& exc) {
+                                       string m(exc.getMessage());
+                                       m += " ";
+                                       m += exc.getLocation().asString();
+                    SG_LOG( SG_ALL, SG_ALERT, m );
+                } catch (const sg_exception& exc) { // XXX may be redundant
+                    SG_LOG( SG_ALL, SG_ALERT, exc.getMessage());
                 }
                 
                 dm->get_tile()->dec_pending_models();
@@ -326,23 +336,17 @@ void FGTileMgr::update_queues()
         }
     }
     
-    // cout << "current elevation (ssg) == " << scenery.get_cur_elev() << endl;
-
     // Notify the tile loader that it can load another tile
     loader.update();
 
     if ( !attach_queue.empty() ) {
-#if defined(ENABLE_THREADS) && ENABLE_THREADS
+#if defined(ENABLE_THREADS)
         FGTileEntry* e = attach_queue.pop();
 #else
         FGTileEntry* e = attach_queue.front();
         attach_queue.pop();
 #endif
-        e->add_ssg_nodes( globals->get_scenery()->get_terrain_branch(),
-                          globals->get_scenery()->get_gnd_lights_root(),
-                          globals->get_scenery()->get_vasi_lights_root(),
-                          globals->get_scenery()->get_rwy_lights_root(),
-                          globals->get_scenery()->get_taxi_lights_root() );
+        e->add_ssg_nodes( globals->get_scenery()->get_terrain_branch() );
         // cout << "Adding ssg nodes for "
     }
 
@@ -359,8 +363,8 @@ void FGTileMgr::update_queues()
                 // get real serious and agressively free up some tiles so
                 // we don't explode our memory usage.
 
-                SG_LOG( SG_TERRAIN, SG_ALERT,
-                        "Alert: catching up on tile delete queue" );
+                SG_LOG( SG_TERRAIN, SG_WARN,
+                        "Warning: catching up on tile delete queue" );
             }
 
             FGTileEntry* e = delete_queue.front();
@@ -379,15 +383,11 @@ void FGTileMgr::update_queues()
 int FGTileMgr::update( double visibility_meters )
 {
     SGLocation *location = globals->get_current_view()->getSGLocation();
-    sgdVec3 abs_pos_vector;
-    sgdCopyVec3( abs_pos_vector,
-                 globals->get_current_view()->get_absolute_view_pos() );
-    return update( location, visibility_meters, abs_pos_vector );
+    return update( location, visibility_meters );
 }
 
 
-int FGTileMgr::update( SGLocation *location, double visibility_meters,
-                       sgdVec3 abs_pos_vector )
+int FGTileMgr::update( SGLocation *location, double visibility_meters )
 {
     SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
 
@@ -410,15 +410,6 @@ int FGTileMgr::update( SGLocation *location, double visibility_meters,
     //         << current_bucket );
     fgSetInt( "/environment/current-tile-id", current_bucket.gen_index() );
 
-    // set global scenery center from current tile center
-    current_tile = tile_cache.get_tile( current_bucket );
-    if ( current_tile != NULL ) {
-        globals->get_scenery()->set_next_center( current_tile->center );
-    } else {
-        SG_LOG( SG_TERRAIN, SG_WARN, "Tile not found (Ok if initializing)" );
-        globals->get_scenery()->set_next_center( Point3D(0.0) );
-    }
-
     // do tile load scheduling. 
     // Note that we need keep track of both viewer buckets and fdm buckets.
     if ( state == Running ) {
@@ -444,9 +435,6 @@ int FGTileMgr::update( SGLocation *location, double visibility_meters,
     // save bucket...
     previous_bucket = current_bucket;
 
-    updateCurrentElevAtPos( abs_pos_vector, altitude_m,
-                            location->get_tile_center() );
-
     return 1;
 }
 
@@ -463,67 +451,18 @@ void FGTileMgr::refresh_view_timestamps() {
     }
 }
 
-
-int FGTileMgr::updateCurrentElevAtPos( sgdVec3 abs_pos_vector,
-                                       double max_alt_m,
-                                       Point3D center)
-{
-    sgdVec3 sc;
-
-    sgdSetVec3( sc, center[0], center[1], center[2]);
-
-    // 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(abs_pos_vector,
-                            max_alt_m,
-                            sc,
-               // uncomment next paramater to fly under
-               // bridges and a slightly faster algorithm
-               // but you won't be able to land on aircraft carriers
-                            // current_tile->get_terra_transform(),
-                            &hit_list,
-                            &hit_elev,
-                            &hit_radius,
-                            hit_normal);
-    }
-
-    if ( hit ) {
-        // cout << "elev = " << hit_elev << " " << hit_radius << endl;
-        globals->get_scenery()->set_cur_elev( hit_elev );
-        globals->get_scenery()->set_cur_radius( hit_radius );
-        globals->get_scenery()->set_cur_normal( hit_normal );
-    } else {
-        globals->get_scenery()->set_cur_elev( -9999.0 );
-        globals->get_scenery()->set_cur_radius( 0.0 );
-        globals->get_scenery()->set_cur_normal( hit_normal );
-    }
-    return hit;
-}
-
-
-void FGTileMgr::prep_ssg_nodes( SGLocation *location, float vis ) {
+void FGTileMgr::prep_ssg_nodes(float vis) {
 
     // traverse the potentially viewable tile list and update range
     // selector and transform
 
-    Point3D center = location->get_tile_center();
-    float *up = location->get_world_up();
-
     FGTileEntry *e;
     tile_cache.reset_traversal();
 
     while ( ! tile_cache.at_end() ) {
         // cout << "processing a tile" << endl;
         if ( (e = tile_cache.get_current()) ) {
-            e->prep_ssg_node( center, up, vis);
+            e->prep_ssg_node(vis);
         } else {
             SG_LOG(SG_INPUT, SG_ALERT, "warning ... empty tile in cache");
         }
@@ -531,13 +470,37 @@ void FGTileMgr::prep_ssg_nodes( SGLocation *location, float vis ) {
     }
 }
 
-bool FGTileMgr::set_tile_filter( bool f ) {
-    bool old = tile_filter;
-    tile_filter = f;
-    return old;
-}
-
-int FGTileMgr::tile_filter_cb( ssgEntity *, int )
+bool FGTileMgr::scenery_available(double lat, double lon, double range_m)
 {
-  return tile_filter ? 1 : 0;
+  // sanity check (unfortunately needed!)
+  if ( lon <= -180.0 || lon >= 180.0 || lat <= -90.0 || lat >= 90.0 )
+    return false;
+  
+  SGBucket bucket(lon, lat);
+  FGTileEntry *te = tile_cache.get_tile(bucket);
+  if (!te || !te->is_loaded())
+    return false;
+
+  // Traverse all tiles required to be there for the given visibility.
+  // This uses exactly the same algorithm like the tile scheduler.
+  double tile_width = bucket.get_width_m();
+  double tile_height = bucket.get_height_m();
+  
+  int xrange = (int)fabs(range_m / tile_width) + 1;
+  int yrange = (int)fabs(range_m / tile_height) + 1;
+  
+  for ( int x = -xrange; x <= xrange; ++x ) {
+    for ( int y = -yrange; y <= yrange; ++y ) {
+      // We have already checked for the center tile.
+      if ( x != 0 || y != 0 ) {
+        SGBucket b = sgBucketOffset( lon, lat, x, y );
+        FGTileEntry *te = tile_cache.get_tile(b);
+        if (!te || !te->is_loaded())
+          return false;
+      }
+    }
+  }
+
+  // Survived all tests.
+  return true;
 }