]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scenery/tilemgr.cxx
Clean fg_init.hxx
[flightgear.git] / src / Scenery / tilemgr.cxx
index 033bf2d5b2d54f2a0f975be04563ff11060427fe..a7bbb388bf7bf32802caec5364b072d409b56bd3 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>
@@ -237,13 +236,27 @@ void FGTileMgr::initialize_queue()
 osg::Node*
 FGTileMgr::loadTileModel(const string& modelPath, bool cacheModel)
 {
+    SGPath fullPath;
+    if (fgGetBool("/sim/paths/use-custom-scenery-data") == true) {
+        string_list sc = globals->get_fg_scenery();
+
+        for (string_list_iterator it = sc.begin(); it != sc.end(); ++it) {
+            SGPath tmpPath(*it);
+            tmpPath.append(modelPath);
+            if (tmpPath.exists()) {
+                fullPath = tmpPath;
+                break;
+            } 
+        }
+    } else {
+         fullPath.append(modelPath);
+    }
     osg::Node* result = 0;
     try {
         if(cacheModel)
             result =
-                SGModelLib::loadModel(modelPath, globals->get_props(),
+                SGModelLib::loadModel(fullPath.str(), globals->get_props(),
                                       new FGNasalModelData);
-
         else
             result=
                 SGModelLib::loadPagedModel(modelPath, globals->get_props(),
@@ -279,7 +292,9 @@ public:
             _pager->queueRequest(entry->tileFileName,
                                  entry->getNode(),
                                  entry->get_inner_ring() ? 10.0f : 1.0f,
-                                 _framestamp, _options);
+                                 _framestamp,
+                                 entry->getDatabaseRequest(),
+                                 _options);
         }
     }
 private:
@@ -295,9 +310,12 @@ 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(),
+                      framestamp,
                       globals->get_scenery()->get_terrain_branch(), _options.get()));
 }
 
@@ -307,30 +325,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() );
@@ -383,21 +392,27 @@ void FGTileMgr::prep_ssg_nodes(float vis) {
     }
 }
 
-bool FGTileMgr::scenery_available(double lat, double lon, double range_m)
+bool FGTileMgr::scenery_available(const SGGeod& position, double range_m)
 {
   // sanity check (unfortunately needed!)
-  if ( lon <= -180.0 || lon >= 180.0 || lat <= -90.0 || lat >= 90.0 )
+  if (position.getLongitudeDeg() < -180 || position.getLongitudeDeg() > 180 ||
+      position.getLatitudeDeg() < -90 || position.getLatitudeDeg() > 90)
     return false;
   
-  SGBucket bucket(lon, lat);
+  SGBucket bucket(position);
   TileEntry *te = tile_cache.get_tile(bucket);
   if (!te || !te->is_loaded())
     return false;
 
+  SGVec3d cartPos = SGVec3d::fromGeod(position);
+
   // 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();
+  double tile_r = 0.5*sqrt(tile_width*tile_width + tile_height*tile_height);
+  double max_dist = tile_r + range_m;
+  double max_dist2 = max_dist*max_dist;
   
   int xrange = (int)fabs(range_m / tile_width) + 1;
   int yrange = (int)fabs(range_m / tile_height) + 1;
@@ -406,7 +421,11 @@ bool FGTileMgr::scenery_available(double lat, double lon, double range_m)
     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 );
+        SGBucket b = sgBucketOffset( position.getLongitudeDeg(),
+                                     position.getLatitudeDeg(), x, y );
+        // Do not ask if it is just the next tile but way out of range.
+        if (max_dist2 < distSqr(cartPos, SGVec3d::fromGeod(b.get_center())))
+          continue;
         TileEntry *te = tile_cache.get_tile(b);
         if (!te || !te->is_loaded())
           return false;