]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scenery/tilemgr.cxx
Avoid SGLocation usage.
[flightgear.git] / src / Scenery / tilemgr.cxx
index b3c1e3ec43ec722708a4028b50e7c0f0624b8df4..6066ebd0a8afa1a50b9ed628d86c5f8f7c42d8f5 100644 (file)
 #include <algorithm>
 #include <functional>
 
+#include <osgViewer/Viewer>
+
 #include <simgear/constants.h>
 #include <simgear/debug/logstream.hxx>
-#include <simgear/math/point3d.hxx>
 #include <simgear/math/polar3d.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/math/vector.hxx>
 #include <simgear/structure/exception.hxx>
 #include <simgear/scene/model/modellib.hxx>
+#include <simgear/scene/tgdb/SGReaderWriterBTGOptions.hxx>
 
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
 #include <Main/viewer.hxx>
 #include <Scripting/NasalSys.hxx>
 
-#include "newcache.hxx"
 #include "scenery.hxx"
 #include "SceneryPager.hxx"
 #include "tilemgr.hxx"
 
 using std::for_each;
 using flightgear::SceneryPager;
+using simgear::SGModelLib;
+using simgear::TileEntry;
+using simgear::TileCache;
 
-#define TEST_LAST_HIT_CACHE
-
-// Constructor
 FGTileMgr::FGTileMgr():
     state( Start ),
-    current_tile( NULL ),
     vis( 16000 )
 {
 }
 
 
-// Destructor
 FGTileMgr::~FGTileMgr() {
+    // remove all nodes we might have left behind
+    osg::Group* group = globals->get_scenery()->get_terrain_branch();
+    group->removeChildren(0, group->getNumChildren());
 }
 
 
@@ -71,6 +73,17 @@ FGTileMgr::~FGTileMgr() {
 int FGTileMgr::init() {
     SG_LOG( SG_TERRAIN, SG_INFO, "Initializing Tile Manager subsystem." );
 
+    _options = new SGReaderWriterBTGOptions;
+    _options->setMatlib(globals->get_matlib());
+    _options->setUseRandomObjects(fgGetBool("/sim/rendering/random-objects", true));
+    _options->setUseRandomVegetation(fgGetBool("/sim/rendering/random-vegetation", true));
+    osgDB::FilePathList &fp = _options->getDatabasePathList();
+    const string_list &sc = globals->get_fg_scenery();
+    fp.clear();
+    std::copy(sc.begin(), sc.end(), back_inserter(fp));
+
+    TileEntry::setModelLoadHelper(this);
+
     tile_cache.init();
 
     state = Inited;
@@ -83,22 +96,21 @@ int FGTileMgr::init() {
     return 1;
 }
 
-
 // schedule a tile for loading
 void FGTileMgr::sched_tile( const SGBucket& b, const bool is_inner_ring ) {
     // see if tile already exists in the cache
-    FGTileEntry *t = tile_cache.get_tile( b );
+    TileEntry *t = tile_cache.get_tile( b );
 
-    if ( t == NULL ) {
+    if ( !t ) {
         // make space in the cache
         SceneryPager* pager = FGScenery::getPagerSingleton();
         while ( (int)tile_cache.get_size() > tile_cache.get_max_cache_size() ) {
             long index = tile_cache.get_oldest_tile();
             if ( index >= 0 ) {
-                FGTileEntry *old = tile_cache.get_tile( index );
+                TileEntry *old = tile_cache.get_tile( index );
                 tile_cache.clear_entry( index );
                 osg::ref_ptr<osg::Object> subgraph = old->getNode();
-                old->disconnect_ssg_nodes();
+                old->removeFromSceneGraph();
                 delete old;
                 // zeros out subgraph ref_ptr, so subgraph is owned by
                 // the pager and will be deleted in the pager thread.
@@ -110,7 +122,7 @@ void FGTileMgr::sched_tile( const SGBucket& b, const bool is_inner_ring ) {
         }
 
         // create a new entry
-        FGTileEntry *e = new FGTileEntry( b );
+        TileEntry *e = new TileEntry( b );
 
         // insert the tile into the cache
         if ( tile_cache.insert_tile( e ) ) {
@@ -121,7 +133,7 @@ void FGTileMgr::sched_tile( const SGBucket& b, const bool is_inner_ring ) {
             delete e;
         }
         // Attach to scene graph
-        e->add_ssg_nodes(globals->get_scenery()->get_terrain_branch());
+        e->addToSceneGraph(globals->get_scenery()->get_terrain_branch());
     } else {
         t->set_inner_ring( is_inner_ring );
     }
@@ -158,7 +170,8 @@ void FGTileMgr::schedule_needed( double vis, const SGBucket& curr_bucket) {
     if ( xrange < 1 ) { xrange = 1; }
     if ( yrange < 1 ) { yrange = 1; }
 
-    // note * 2 at end doubles cache size (for fdm and viewer)
+    // make the cache twice as large to avoid losing terrain when switching
+    // between aircraft and tower views
     tile_cache.set_max_cache_size( (2*xrange + 2) * (2*yrange + 2) * 2 );
     // cout << "xrange = " << xrange << "  yrange = " << yrange << endl;
     // cout << "max cache size = " << tile_cache.get_max_cache_size()
@@ -220,23 +233,6 @@ void FGTileMgr::initialize_queue()
 
     double visibility_meters = fgGetDouble("/environment/visibility-m");
     schedule_needed(visibility_meters, current_bucket);
-
-    // do we really want to lose this? CLO
-#if 0
-    // Now force a load of the center tile and inner ring so we
-    // have something to see in our first frame.
-    int i;
-    for ( i = 0; i < 9; ++i ) {
-        if ( load_queue.size() ) {
-            SG_LOG( SG_TERRAIN, SG_DEBUG, 
-                    "Load queue not empty, loading a tile" );
-
-            SGBucket pending = load_queue.front();
-            load_queue.pop_front();
-            load_tile( pending );
-        }
-    }
-#endif
 }
 
 osg::Node*
@@ -244,13 +240,15 @@ FGTileMgr::loadTileModel(const string& modelPath, bool cacheModel)
 {
     osg::Node* result = 0;
     try {
-        result =
-            globals->get_model_lib()->load_model(".",
-                                                 modelPath,
-                                                 globals->get_props(),
-                                                 globals->get_sim_time_sec(),
-                                                 cacheModel,
-                                                 new FGNasalModelData );
+        if(cacheModel)
+            result =
+                SGModelLib::loadModel(modelPath, globals->get_props(),
+                                      new FGNasalModelData);
+
+        else
+            result=
+                SGModelLib::loadPagedModel(modelPath, globals->get_props(),
+                                           new FGNasalModelData);
     } catch (const sg_io_exception& exc) {
         string m(exc.getMessage());
         m += " ";
@@ -263,28 +261,36 @@ FGTileMgr::loadTileModel(const string& modelPath, bool cacheModel)
 }
 
 // Helper class for STL fun
-class TileLoad : public std::unary_function<FGNewCache::tile_map::value_type,
+class TileLoad : public std::unary_function<TileCache::tile_map::value_type,
                                             void>
 {
 public:
     TileLoad(SceneryPager *pager, osg::FrameStamp* framestamp,
-             osg::Group* terrainBranch) :
-        _pager(pager), _framestamp(framestamp) {}
+             osg::Group* terrainBranch, osgDB::ReaderWriter::Options* options) :
+        _pager(pager), _framestamp(framestamp), _options(options) {}
+
     TileLoad(const TileLoad& rhs) :
-        _pager(rhs._pager), _framestamp(rhs._framestamp) {}
-    void operator()(FGNewCache::tile_map::value_type& tilePair)
+        _pager(rhs._pager), _framestamp(rhs._framestamp),
+        _options(rhs._options) {}
+
+    void operator()(TileCache::tile_map::value_type& tilePair)
     {
-        FGTileEntry* entry = tilePair.second;
+        TileEntry* entry = tilePair.second;
         if (entry->getNode()->getNumChildren() == 0) {
             _pager->queueRequest(entry->tileFileName,
                                  entry->getNode(),
                                  entry->get_inner_ring() ? 10.0f : 1.0f,
-                                 _framestamp);
+                                 _framestamp,
+#ifdef FGOSGPAGER25
+                                 entry->getDatabaseRequest(),
+#endif
+                                 _options);
         }
     }
 private:
     SceneryPager* _pager;
     osg::FrameStamp* _framestamp;
+    osgDB::ReaderWriter::Options* _options;
 };
 
 /**
@@ -294,10 +300,13 @@ private:
 void FGTileMgr::update_queues()
 {
     SceneryPager* pager = FGScenery::getPagerSingleton();
+    osg::FrameStamp* framestamp
+        = globals->get_renderer()->getViewer()->getFrameStamp();
+    tile_cache.set_current_time(framestamp->getReferenceTime());
     for_each(tile_cache.begin(), tile_cache.end(),
              TileLoad(pager,
-                      globals->get_renderer()->getViewer()->getFrameStamp(),
-                      globals->get_scenery()->get_terrain_branch()));
+                      framestamp,
+                      globals->get_scenery()->get_terrain_branch(), _options.get()));
 }
 
 
@@ -306,30 +315,21 @@ void FGTileMgr::update_queues()
 // disk.
 int FGTileMgr::update( double visibility_meters )
 {
-    SGLocation *location = globals->get_current_view()->getSGLocation();
-    return update( location, visibility_meters );
+    SGVec3d viewPos = globals->get_current_view()->get_view_pos();
+    return update(SGGeod::fromCart(viewPos), visibility_meters);
 }
 
-
-int FGTileMgr::update( SGLocation *location, double visibility_meters )
+int FGTileMgr::update( const SGGeod& location, double visibility_meters)
 {
     SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update()" );
 
-    longitude = location->getLongitude_deg();
-    latitude = location->getLatitude_deg();
-    // add 1.0m to the max altitude to give a little leeway to the
-    // ground reaction code.
-    altitude_m = location->getAltitudeASL_ft() * SG_FEET_TO_METER + 1.0;
+    longitude = location.getLongitudeDeg();
+    latitude = location.getLatitudeDeg();
 
-    // if current altitude is apparently not initialized, set max
-    // altitude to something big.
-    if ( altitude_m < -1000 ) {
-        altitude_m = 10000;
-    }
     // SG_LOG( SG_TERRAIN, SG_DEBUG, "FGTileMgr::update() for "
     //         << longitude << " " << latatitude );
 
-    current_bucket.set_bucket( longitude, latitude );
+    current_bucket.set_bucket( location );
     // SG_LOG( SG_TERRAIN, SG_DEBUG, "Updating tile list for "
     //         << current_bucket );
     fgSetInt( "/environment/current-tile-id", current_bucket.gen_index() );
@@ -368,7 +368,7 @@ void FGTileMgr::prep_ssg_nodes(float vis) {
     // traverse the potentially viewable tile list and update range
     // selector and transform
 
-    FGTileEntry *e;
+    TileEntry *e;
     tile_cache.reset_traversal();
 
     while ( ! tile_cache.at_end() ) {
@@ -382,6 +382,12 @@ void FGTileMgr::prep_ssg_nodes(float vis) {
     }
 }
 
+bool FGTileMgr::scenery_available(const SGGeod& position, double range_m)
+{
+    return scenery_available(position.getLatitudeDeg(),
+                             position.getLongitudeDeg(), range_m);
+}
+
 bool FGTileMgr::scenery_available(double lat, double lon, double range_m)
 {
   // sanity check (unfortunately needed!)
@@ -389,7 +395,7 @@ bool FGTileMgr::scenery_available(double lat, double lon, double range_m)
     return false;
   
   SGBucket bucket(lon, lat);
-  FGTileEntry *te = tile_cache.get_tile(bucket);
+  TileEntry *te = tile_cache.get_tile(bucket);
   if (!te || !te->is_loaded())
     return false;
 
@@ -406,7 +412,7 @@ bool FGTileMgr::scenery_available(double lat, double lon, double range_m)
       // 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);
+        TileEntry *te = tile_cache.get_tile(b);
         if (!te || !te->is_loaded())
           return false;
       }
@@ -420,9 +426,9 @@ bool FGTileMgr::scenery_available(double lat, double lon, double range_m)
 namespace
 {
 struct IsTileLoaded :
-        public std::unary_function<FGNewCache::tile_map::value_type, bool>
+        public std::unary_function<TileCache::tile_map::value_type, bool>
 {
-    bool operator()(const FGNewCache::tile_map::value_type& tilePair) const
+    bool operator()(const TileCache::tile_map::value_type& tilePair) const
     {
         return tilePair.second->is_loaded();
     }