From c2dda7e8437ce6d8210115e3f90fd2cdf87cad8c Mon Sep 17 00:00:00 2001
From: till busch <buti@bux.at>
Date: Tue, 11 Mar 2008 18:07:09 +0100
Subject: [PATCH] model-paging v0.4.0 (beta) flightgear

---
 src/AIModel/AIBase.cxx       |   66 ++++++++++++++++++++---------------------
 src/AIModel/AIBase.hxx       |    9 ++---
 src/AIModel/AICarrier.cxx    |   36 ++++++++++++++---------
 src/AIModel/AICarrier.hxx    |    1 +
 src/AIModel/AIModelData.cxx  |   11 +++++++
 src/AIModel/AIModelData.hxx  |   17 +++++++++++
 src/AIModel/Makefile.am      |    1 +
 src/AIModel/submodel.cxx     |    6 ++--
 src/ATC/AIEntity.hxx         |    1 -
 src/ATC/AIMgr.cxx            |   16 ++-------
 src/ATC/AIPlane.hxx          |    1 -
 src/Main/globals.cxx         |    3 --
 src/Main/globals.hxx         |    9 ------
 src/Main/main.cxx            |   14 +++++++-
 src/Main/renderer.cxx        |    2 -
 src/Model/acmodel.cxx        |   25 ++--------------
 src/Model/model_panel.cxx    |   19 +++++-------
 src/Model/model_panel.hxx    |    4 +--
 src/Model/modelmgr.cxx       |    7 +----
 src/Network/multiplay.hxx    |    1 -
 src/Scenery/SceneryPager.cxx |   11 +++++++
 src/Scenery/SceneryPager.hxx |    5 +++
 src/Scenery/scenery.cxx      |    3 +-
 src/Scenery/tileentry.cxx    |    2 +-
 src/Scenery/tilemgr.cxx      |   19 +++++++----
 src/Scripting/NasalSys.cxx   |   10 ++++--
 26 files changed, 157 insertions(+), 142 deletions(-)
 create mode 100644 src/AIModel/AIModelData.cxx
 create mode 100644 src/AIModel/AIModelData.hxx

diff --git a/src/AIModel/AIBase.cxx b/src/AIModel/AIBase.cxx
index 35be09b..63ba97d 100644
--- a/src/AIModel/AIBase.cxx
+++ b/src/AIModel/AIBase.cxx
@@ -24,20 +24,20 @@
 #  include <config.h>
 #endif
 
-
 #include <simgear/compiler.h>
 
 #include STL_STRING
 
 #include <osg/ref_ptr>
 #include <osg/Node>
+#include <osgDB/FileUtils>
 
 #include <simgear/math/point3d.hxx>
 #include <simgear/math/polar3d.hxx>
 #include <simgear/math/sg_geodesy.hxx>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/scene/model/location.hxx>
-#include <simgear/scene/model/model.hxx>
+#include <simgear/scene/model/modellib.hxx>
 #include <simgear/scene/util/SGNodeMasks.hxx>
 #include <simgear/debug/logstream.hxx>
 #include <simgear/props/props.hxx>
@@ -47,6 +47,7 @@
 #include <Scripting/NasalSys.hxx>
 
 #include "AIBase.hxx"
+#include "AIModelData.hxx"
 #include "AIManager.hxx"
 
 const double FGAIBase::e = 2.71828183;
@@ -155,38 +156,41 @@ void FGAIBase::Transform() {
 }
 
 bool FGAIBase::init(bool search_in_AI_path) {
+    osg::ref_ptr<osgDB::ReaderWriter::Options> opt=
+        new osgDB::ReaderWriter::Options(*osgDB::Registry::instance()->getOptions());
+
+    if(search_in_AI_path)
+    {
+        SGPath ai_path(globals->get_fg_root());
+        ai_path.append("AI");
+        opt->getDatabasePathList().push_front(ai_path.str());
+    }
 
-    if (!model_path.empty()) {
-
-        if ( search_in_AI_path
-                && (model_path.substr(model_path.size() - 4, 4) == ".xml")) {
-            SGPath ai_path("AI");
-            ai_path.append(model_path);
-            try {
-                model = load3DModel( globals->get_fg_root(), ai_path.str(), props,
-                        globals->get_sim_time_sec() );
-            } catch (const sg_exception &e) {
-                model = NULL;
-            }
-        } else
-            model = NULL;
-
-        if (!model.get()) {
-            try {
-                model = load3DModel( globals->get_fg_root(), model_path, props,
-                        globals->get_sim_time_sec() );
-            } catch (const sg_exception &e) {
-                model = NULL;
-            }
-        }
+    string f = osgDB::findDataFile(model_path, opt.get());
 
-    }
+    if(f.empty())
+        f="Models/Geometry/glider.ac";
+
+    model = load3DModel(f, props);
 
     if (model.get()) {
+        model->setNodeMask(model->getNodeMask() & ~SG_NODEMASK_TERRAIN_BIT);
         aip.init( model.get() );
         aip.setVisible(true);
         invisible = false;
         globals->get_scenery()->get_scene_graph()->addChild(aip.getSceneGraph());
+
+    } else if (!model_path.empty()) {
+        SG_LOG(SG_INPUT, SG_WARN, "AIBase: Could not load model " << model_path);
+    }
+
+    setDie(false);
+    return true;
+}
+
+void FGAIBase::initModel(osg::Node *node)
+{
+    if (model.get()) {
         fgSetString("/ai/models/model-added", props->getPath());
 
     } else if (!model_path.empty()) {
@@ -195,18 +199,12 @@ bool FGAIBase::init(bool search_in_AI_path) {
 
     props->setStringValue("submodels/path", _path.c_str());
     setDie(false);
-    return true;
 }
 
 
-osg::Node* FGAIBase::load3DModel(const string& fg_root,
-                      const string &path,
-                      SGPropertyNode *prop_root,
-                      double sim_time_sec)
+osg::Node* FGAIBase::load3DModel(const string &path, SGPropertyNode *prop_root)
 {
-  model = sgLoad3DModel(fg_root, path, prop_root, sim_time_sec, 0,
-                        new FGNasalModelData(prop_root));
-  model->setNodeMask(model->getNodeMask() & ~SG_NODEMASK_TERRAIN_BIT);
+  model = SGModelLib::loadPagedModel(path, prop_root, new FGAIModelData(this, prop_root));
   return model.get();
 }
 
diff --git a/src/AIModel/AIBase.hxx b/src/AIModel/AIBase.hxx
index 0bdd6f8..86a4b13 100644
--- a/src/AIModel/AIBase.hxx
+++ b/src/AIModel/AIBase.hxx
@@ -54,6 +54,7 @@ public:
     virtual void readFromScenario(SGPropertyNode* scFileNode);
 
     virtual bool init(bool search_in_AI_path=false);
+    virtual void initModel(osg::Node *node);
     virtual void update(double dt);
     virtual void bind();
     virtual void unbind();
@@ -266,11 +267,9 @@ public:
     inline double _getRange() { return range; };
     inline double _getBearing() { return bearing; };
 
-    osg::Node* load3DModel(const string& fg_root,
-                            const string &path,
-                            SGPropertyNode *prop_root,
-                            double sim_time_sec);
-
+    osg::Node* load3DModel(const string &path,
+                           SGPropertyNode *prop_root);
+ 
     static bool _isNight();
 };
 
diff --git a/src/AIModel/AICarrier.cxx b/src/AIModel/AICarrier.cxx
index d3fa369..ef84520 100644
--- a/src/AIModel/AICarrier.cxx
+++ b/src/AIModel/AICarrier.cxx
@@ -71,6 +71,7 @@ public:
         != mSolidObjects.end()) {
       mFoundHot = true;
       mUserData = FGAICarrierHardware::newSolid(mCarrier);
+      SG_LOG(SG_GENERAL, SG_ALERT, "AICarrierVisitor::apply() solidObject" );
     }
     node.setUserData(mUserData.get());
 
@@ -313,20 +314,6 @@ bool FGAICarrier::init(bool search_in_AI_path) {
     if (!FGAIShip::init(search_in_AI_path))
         return false;
 
-    // process the 3d model here
-    // mark some objects solid, mark the wires ...
-
-    // The model should be used for altitude computations.
-    // To avoid that every detail in a carrier 3D model will end into
-    // the aircraft local cache, only set the HOT traversal bit on
-    // selected objects.
-    osg::Node* sel = aip.getSceneGraph();
-    // Clear the HOT traversal flag
-    // Selectively set that flag again for wires/cats/solid objects.
-    // Attach a pointer to this carrier class to those objects.
-    FGCarrierVisitor carrierVisitor(this, wire_objects, catapult_objects, solid_objects);
-    sel->accept(carrierVisitor);
-
     _longitude_node = fgGetNode("/position/longitude-deg", true);
     _latitude_node = fgGetNode("/position/latitude-deg", true);
     _altitude_node = fgGetNode("/position/altitude-ft", true);
@@ -357,6 +344,27 @@ bool FGAICarrier::init(bool search_in_AI_path) {
     return true;
 }
 
+void FGAICarrier::initModel(osg::Node *node)
+{
+    SG_LOG(SG_GENERAL, SG_ALERT, "AICarrier::initModel()" );
+    FGAIShip::initModel(node);
+    // process the 3d model here
+    // mark some objects solid, mark the wires ...
+
+    // The model should be used for altitude computations.
+    // To avoid that every detail in a carrier 3D model will end into
+    // the aircraft local cache, only set the HOT traversal bit on
+    // selected objects.
+
+    // Clear the HOT traversal flag
+    // Selectively set that flag again for wires/cats/solid objects.
+    // Attach a pointer to this carrier class to those objects.
+    SG_LOG(SG_GENERAL, SG_ALERT, "AICarrier::initModel() visit" );
+    FGCarrierVisitor carrierVisitor(this, wire_objects, catapult_objects, solid_objects);
+    model->accept(carrierVisitor);
+//    model->setNodeMask(node->getNodeMask() & SG_NODEMASK_TERRAIN_BIT | model->getNodeMask());
+}
+
 void FGAICarrier::bind() {
     FGAIShip::bind();
 
diff --git a/src/AIModel/AICarrier.hxx b/src/AIModel/AICarrier.hxx
index 33c6740..0e88ba4 100644
--- a/src/AIModel/AICarrier.hxx
+++ b/src/AIModel/AICarrier.hxx
@@ -103,6 +103,7 @@ public:
     bool OutsideBox();
 
     bool init(bool search_in_AI_path=false);
+    void initModel(osg::Node *node);
 
     virtual const char* getTypeString(void) const { return "carrier"; }
 
diff --git a/src/AIModel/AIModelData.cxx b/src/AIModel/AIModelData.cxx
new file mode 100644
index 0000000..2b291b0
--- /dev/null
+++ b/src/AIModel/AIModelData.cxx
@@ -0,0 +1,11 @@
+#include "AIBase.hxx"
+#include "AIModelData.hxx"
+
+void FGAIModelData::modelLoaded(const string& path, SGPropertyNode *prop,
+                                   osg::Node *n)
+{
+    FGNasalModelData::modelLoaded(path, prop, n);
+    // SG_LOG(SG_NASAL, SG_ALERT, "FGAIModelData::modelLoaded(" << path << ")");
+
+    _base->initModel(n);
+}
diff --git a/src/AIModel/AIModelData.hxx b/src/AIModel/AIModelData.hxx
new file mode 100644
index 0000000..cc53547
--- /dev/null
+++ b/src/AIModel/AIModelData.hxx
@@ -0,0 +1,17 @@
+#ifndef __FGAIMODELDATA_HXX
+#define __FGAIMODELDATA_HXX
+
+#include <Scripting/NasalSys.hxx>
+
+class FGAIBase;
+
+class FGAIModelData : public FGNasalModelData {
+public:
+    FGAIModelData(FGAIBase *b, SGPropertyNode *props = 0) : FGNasalModelData(props), _base(b) {}
+    virtual void modelLoaded(const string& path, SGPropertyNode *prop, osg::Node *);
+
+private:
+    FGAIBase *_base;
+};
+
+#endif
diff --git a/src/AIModel/Makefile.am b/src/AIModel/Makefile.am
index 6dbfd7b..350d69e 100644
--- a/src/AIModel/Makefile.am
+++ b/src/AIModel/Makefile.am
@@ -3,6 +3,7 @@ noinst_LIBRARIES = libAIModel.a
 libAIModel_a_SOURCES = submodel.cxx submodel.hxx 	\
 			AIManager.hxx AIManager.cxx \
 			AIBase.hxx AIBase.cxx 	\
+			AIModelData.cxx \
 			AIAircraft.hxx AIAircraft.cxx \
 			AIMultiplayer.hxx AIMultiplayer.cxx \
 			AIShip.hxx AIShip.cxx \
diff --git a/src/AIModel/submodel.cxx b/src/AIModel/submodel.cxx
index e246523..774820d 100644
--- a/src/AIModel/submodel.cxx
+++ b/src/AIModel/submodel.cxx
@@ -514,7 +514,7 @@ void FGSubmodelMgr::loadAI()
     sm_list = ai->get_ai_list();
 
     if (sm_list.empty()) {
-        SG_LOG(SG_GENERAL, SG_ALERT, "Submodels: Unable to read AI submodel list");
+        SG_LOG(SG_GENERAL, SG_DEBUG, "Submodels: Unable to read AI submodel list");
         return;
     }
 
@@ -559,7 +559,7 @@ void FGSubmodelMgr::setData(int id, string& path, bool serviceable)
                 "Submodels: Trying to read AI submodels file: " << config.str());
         readProperties(config.str(), &root);
     } catch (const sg_exception &e) {
-        SG_LOG(SG_GENERAL, SG_ALERT,
+        SG_LOG(SG_GENERAL, SG_DEBUG,
                 "Submodels: Unable to read AI submodels file: " << config.str());
         return;
     }
@@ -664,7 +664,7 @@ void FGSubmodelMgr::setSubData(int id, string& path, bool serviceable)
         readProperties(config.str(), &root);
 
     } catch (const sg_exception &e) {
-        SG_LOG(SG_GENERAL, SG_ALERT,
+        SG_LOG(SG_GENERAL, SG_DEBUG,
                 "Submodels: Unable to read AI submodels file: " << config.str());
         return;
     }
diff --git a/src/ATC/AIEntity.hxx b/src/ATC/AIEntity.hxx
index dc9070b..1b184a8 100644
--- a/src/ATC/AIEntity.hxx
+++ b/src/ATC/AIEntity.hxx
@@ -22,7 +22,6 @@
 #define _FG_AIEntity_HXX
 
 #include <simgear/math/point3d.hxx>
-#include <simgear/scene/model/model.hxx>
 #include <simgear/scene/model/placement.hxx>
 
 
diff --git a/src/ATC/AIMgr.cxx b/src/ATC/AIMgr.cxx
index 8b38de6..fb5ce5a 100644
--- a/src/ATC/AIMgr.cxx
+++ b/src/ATC/AIMgr.cxx
@@ -27,6 +27,7 @@
 #include <Main/globals.hxx>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/math/sg_random.h>
+#include <simgear/scene/model/modellib.hxx>
 #include <list>
 
 #ifdef _MSC_VER
@@ -81,10 +82,7 @@ void FGAIMgr::init() {
 	string planepath = "Aircraft/c172p/Models/c172p.xml";
 	bool _loadedDefaultOK = true;
 	try {
-		_defaultModel = sgLoad3DModel( globals->get_fg_root(),
-	    	                           planepath.c_str(),
-									   globals->get_props(),
-									   globals->get_sim_time_sec() );
+		_defaultModel = SGModelLib::loadPagedModel(planepath.c_str(), globals->get_props());
 	} catch(sg_exception&) {
 		_loadedDefaultOK = false;
 	}
@@ -93,18 +91,12 @@ void FGAIMgr::init() {
 		// Just load the same 3D model as the default user plane - that's *bound* to exist!
 		// TODO - implement robust determination of availability of GA AI aircraft models
 		planepath = "Aircraft/c172p/Models/c172p.ac";
-		_defaultModel = sgLoad3DModel( globals->get_fg_root(),
-	    	                           planepath.c_str(),
-									   globals->get_props(),
-									   globals->get_sim_time_sec() );
+		_defaultModel = SGModelLib::loadPagedModel(planepath.c_str(), globals->get_props());
 	}
 
 	planepath = "Aircraft/pa28-161/Models/pa28-161.ac";
 	try {
-		_piperModel = sgLoad3DModel( globals->get_fg_root(),
-	    	                         planepath.c_str(),
-									 globals->get_props(),
-									 globals->get_sim_time_sec() );
+		_piperModel = SGModelLib::loadPagedModel(planepath.c_str(), globals->get_props());
 	} catch(sg_exception&) {
 		_havePiperModel = false;
 	}
diff --git a/src/ATC/AIPlane.hxx b/src/ATC/AIPlane.hxx
index 3cf218d..b29726c 100644
--- a/src/ATC/AIPlane.hxx
+++ b/src/ATC/AIPlane.hxx
@@ -22,7 +22,6 @@
 #define _FG_AI_PLANE_HXX
 
 #include <simgear/math/point3d.hxx>
-#include <simgear/scene/model/model.hxx>
 
 #include "AIEntity.hxx"
 #include "ATC.hxx"
diff --git a/src/Main/globals.cxx b/src/Main/globals.cxx
index 52fbfff..ada7bcb 100644
--- a/src/Main/globals.cxx
+++ b/src/Main/globals.cxx
@@ -24,7 +24,6 @@
 #  include <config.h>
 #endif
 
-#include <simgear/scene/model/modellib.hxx>
 #include <simgear/sound/soundmgr_openal.hxx>
 #include <simgear/structure/commands.hxx>
 #include <simgear/misc/sg_path.hxx>
@@ -92,7 +91,6 @@ FGGlobals::FGGlobals() :
     initial_state( NULL ),
     locale( NULL ),
     commands( SGCommandMgr::instance() ),
-    model_lib( NULL ),
     acmodel( NULL ),
     model_mgr( NULL ),
     channel_options_list( NULL ),
@@ -147,7 +145,6 @@ FGGlobals::~FGGlobals()
      delete initial_state;
 //     //delete locale; Don't delete locale
 //     delete commands;
-     delete model_lib;
      delete acmodel;
      delete model_mgr;
      delete channel_options_list;
diff --git a/src/Main/globals.hxx b/src/Main/globals.hxx
index 2e3e524..19f3a52 100644
--- a/src/Main/globals.hxx
+++ b/src/Main/globals.hxx
@@ -57,7 +57,6 @@ class SGEphemeris;
 class SGCommandMgr;
 class SGMagVar;
 class SGMaterialLib;
-class SGModelLib;
 class SGPropertyNode;
 class SGTime;
 class SGSoundMgr;
@@ -171,8 +170,6 @@ private:
 
     SGCommandMgr *commands;
 
-    SGModelLib *model_lib;
-
   //FGFlightPlanDispatcher *fpDispatcher;
 
     FGAircraftModel *acmodel;
@@ -299,12 +296,6 @@ public:
 
     inline SGCommandMgr *get_commands () { return commands; }
 
-    inline SGModelLib * get_model_lib () { return model_lib; }
-
-    inline void set_model_lib (SGModelLib *m) {
-        model_lib = m;
-    }
-
     inline FGAircraftModel *get_aircraft_model () { return acmodel; }
 
     inline void set_aircraft_model (FGAircraftModel * model)
diff --git a/src/Main/main.cxx b/src/Main/main.cxx
index d90002a..ad3ad39 100644
--- a/src/Main/main.cxx
+++ b/src/Main/main.cxx
@@ -47,12 +47,15 @@
 #include <simgear/timing/sg_time.hxx>
 #include <simgear/math/sg_random.h>
 
+#include <osgDB/Registry>
+
 // Class references
 #include <simgear/ephemeris/ephemeris.hxx>
 #include <simgear/scene/model/modellib.hxx>
 #include <simgear/scene/material/matlib.hxx>
 #include <simgear/scene/model/animation.hxx>
 #include <simgear/scene/sky/sky.hxx>
+#include <simgear/scene/model/CheckSceneryVisitor.hxx>
 #include <Time/light.hxx>
 #include <Include/general.hxx>
 #include <Aircraft/replay.hxx>
@@ -137,12 +140,19 @@ void fgUpdateTimeDepCalcs() {
         // the aircraft.
         double range = 50.0;
         if (globals->get_tile_mgr()->scenery_available(lat, lon, range)) {
-            SG_LOG(SG_FLIGHT,SG_INFO, "Finally initializing fdm");
+            double elev;
+            globals->get_scenery()->get_elevation_m(lat, lon, 9000, elev, 0);
+            SGVec3f p = SGVec3f::fromGeod(SGGeod::fromDegM(lon,lat,elev));
+            simgear::CheckSceneryVisitor csnv(p.osg());
+            globals->get_scenery()->get_scene_graph()->accept(csnv);
+            if(csnv.isLoaded()) {
+            SG_LOG(SG_FLIGHT,SG_ALERT, "Finally initializing fdm");
             cur_fdm_state->init();
             if ( cur_fdm_state->get_bound() ) {
                 cur_fdm_state->unbind();
             }
             cur_fdm_state->bind();
+            }
         }
     }
 
@@ -766,7 +776,7 @@ static void fgIdleFunction ( void ) {
         // Initialize the material manager
         ////////////////////////////////////////////////////////////////////
         globals->set_matlib( new SGMaterialLib );
-        globals->set_model_lib(new SGModelLib);
+        SGModelLib::init(globals->get_fg_root());
 
 
         ////////////////////////////////////////////////////////////////////
diff --git a/src/Main/renderer.cxx b/src/Main/renderer.cxx
index 0cbe274..53ca426 100644
--- a/src/Main/renderer.cxx
+++ b/src/Main/renderer.cxx
@@ -66,8 +66,6 @@
 #include <simgear/screen/extensions.hxx>
 #include <simgear/scene/material/matlib.hxx>
 #include <simgear/scene/model/animation.hxx>
-#include <simgear/scene/model/model.hxx>
-#include <simgear/scene/model/modellib.hxx>
 #include <simgear/scene/model/placement.hxx>
 #include <simgear/scene/util/SGUpdateVisitor.hxx>
 #include <simgear/scene/util/RenderConstants.hxx>
diff --git a/src/Model/acmodel.cxx b/src/Model/acmodel.cxx
index d3af3b4..1f9b15a 100644
--- a/src/Model/acmodel.cxx
+++ b/src/Model/acmodel.cxx
@@ -50,36 +50,17 @@ FGAircraftModel::~FGAircraftModel ()
 void 
 FGAircraftModel::init ()
 {
-  SGPath liveryPath;
   _aircraft = new SGModelPlacement;
   string path = fgGetString("/sim/model/path", "Models/Geometry/glider.ac");
-  string texture_path = fgGetString("/sim/model/texture-path");
-  if( texture_path.size() ) {
-      SGPath temp_path;
-      if ( !ulIsAbsolutePathName( texture_path.c_str() ) ) {
-          temp_path = globals->get_fg_root();
-          temp_path.append( SGPath( path ).dir() );
-          temp_path.append( texture_path );
-          liveryPath = temp_path;
-      } else
-          liveryPath = texture_path;
-  }
   try {
-    osg::Node *model = fgLoad3DModelPanel( globals->get_fg_root(),
-                                           path,
-                                           globals->get_props(),
-                                           globals->get_sim_time_sec(),
-                                           liveryPath);
+    osg::Node *model = fgLoad3DModelPanel( path, globals->get_props());
     _aircraft->init( model );
   } catch (const sg_exception &ex) {
     SG_LOG(SG_GENERAL, SG_ALERT, "Failed to load aircraft from " << path << ':');
     SG_LOG(SG_GENERAL, SG_ALERT, "  " << ex.getFormattedMessage());
     SG_LOG(SG_GENERAL, SG_ALERT, "(Falling back to glider.ac.)");
-    osg::Node *model = fgLoad3DModelPanel( globals->get_fg_root(),
-                                           "Models/Geometry/glider.ac",
-                                           globals->get_props(),
-                                           globals->get_sim_time_sec(),
-                                           liveryPath);
+    osg::Node *model = fgLoad3DModelPanel( "Models/Geometry/glider.ac",
+                                           globals->get_props());
     _aircraft->init( model );
   }
   _selector->addChild(_aircraft->getSceneGraph(), true);
diff --git a/src/Model/model_panel.cxx b/src/Model/model_panel.cxx
index ef98544..8ccca9e 100644
--- a/src/Model/model_panel.cxx
+++ b/src/Model/model_panel.cxx
@@ -12,7 +12,7 @@
 #include <osg/Geode>
 
 #include <simgear/props/props.hxx>
-#include <simgear/scene/model/model.hxx>
+#include <simgear/scene/model/modellib.hxx>
 #include <simgear/scene/util/SGNodeMasks.hxx>
 
 #include "panelnode.hxx"
@@ -24,9 +24,9 @@ SG_USING_STD(vector);
 static
 osg::Node* load_panel(SGPropertyNode *n)
 {
-  osg::Geode* geode = new osg::Geode;
-  geode->addDrawable(new FGPanelNode(n));
-  return geode;
+    osg::Geode* geode = new osg::Geode;
+    geode->addDrawable(new FGPanelNode(n));
+    return geode;
 }
 
 
@@ -35,14 +35,11 @@ osg::Node* load_panel(SGPropertyNode *n)
 ////////////////////////////////////////////////////////////////////////
 
 osg::Node *
-fgLoad3DModelPanel( const string &fg_root, const string &path,
-                    SGPropertyNode *prop_root,
-                    double sim_time_sec, const SGPath& livery )
+fgLoad3DModelPanel(const string &path, SGPropertyNode *prop_root)
 {
-  osg::Node* node = sgLoad3DModel( fg_root, path, prop_root, sim_time_sec,
-                                   load_panel, 0, livery );
-  node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
-  return node;
+    osg::Node* node = SGModelLib::loadModel(path, prop_root, load_panel);
+    node->setNodeMask(~SG_NODEMASK_TERRAIN_BIT);
+    return node;
 }
 
 
diff --git a/src/Model/model_panel.hxx b/src/Model/model_panel.hxx
index 0c1c34a..0b82d33 100644
--- a/src/Model/model_panel.hxx
+++ b/src/Model/model_panel.hxx
@@ -46,9 +46,7 @@ class FGLocation;
  * Subsystems should not normally invoke this function directly;
  * instead, they should use the SGModelLoader declared in loader.hxx.
  */
-osg::Node *fgLoad3DModelPanel( const string& fg_root, const string &path,
-                               SGPropertyNode *prop_root,
-                               double sim_time_sec, const SGPath& livery );
+osg::Node *fgLoad3DModelPanel( const string &path, SGPropertyNode *prop_root);
 
 
 #endif // __MODEL_HXX
diff --git a/src/Model/modelmgr.cxx b/src/Model/modelmgr.cxx
index 4d19b7b..e8fcd88 100644
--- a/src/Model/modelmgr.cxx
+++ b/src/Model/modelmgr.cxx
@@ -68,17 +68,12 @@ FGModelMgr::add_model (SGPropertyNode * node)
   SGModelPlacement *model = new SGModelPlacement;
   instance->model = model;
   instance->node = node;
-  SGModelLib *model_lib = globals->get_model_lib();
 
   const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
   osg::Node *object;
 
   try {
-    object = model_lib->load_model(
-        globals->get_fg_root(),
-        path,
-        globals->get_props(),
-        globals->get_sim_time_sec(), /*cache_object=*/false);
+    object = SGModelLib::loadPagedModel(path, globals->get_props());
   } catch (const sg_throwable& t) {
     SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n  "
         << t.getFormattedMessage() << t.getOrigin());
diff --git a/src/Network/multiplay.hxx b/src/Network/multiplay.hxx
index f810591..ce3cc9b 100644
--- a/src/Network/multiplay.hxx
+++ b/src/Network/multiplay.hxx
@@ -32,7 +32,6 @@
 #include STL_STRING
 
 #include <simgear/props/props.hxx>
-#include <simgear/scene/model/model.hxx>
 
 #include <Main/globals.hxx>
 #include <Main/fg_props.hxx>
diff --git a/src/Scenery/SceneryPager.cxx b/src/Scenery/SceneryPager.cxx
index 5615436..6da6d4b 100644
--- a/src/Scenery/SceneryPager.cxx
+++ b/src/Scenery/SceneryPager.cxx
@@ -16,6 +16,7 @@
 // along with this program; if not, write to the Free Software
 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
+#include <simgear/scene/model/SGPagedLOD.hxx>
 #include "SceneryPager.hxx"
 #include <algorithm>
 #include <functional>
@@ -39,6 +40,16 @@ SceneryPager::~SceneryPager()
 {
 }
 
+void SceneryPager::requestNodeFile(const std::string& fileName,osg::Group* group,
+                                    float priority, const osg::FrameStamp* framestamp)
+{
+    simgear::SGPagedLOD *sgplod = dynamic_cast<simgear::SGPagedLOD*>(group);
+    if(sgplod)
+        DatabasePager::requestNodeFile(fileName,group,priority,framestamp,sgplod->getReaderWriterOptions());
+    else
+        DatabasePager::requestNodeFile(fileName,group,priority,framestamp);
+}
+
 void SceneryPager::queueRequest(const std::string& fileName, osg::Group* group,
                                 float priority, osg::FrameStamp* frameStamp)
 {
diff --git a/src/Scenery/SceneryPager.hxx b/src/Scenery/SceneryPager.hxx
index df9ebe3..35f0eff 100644
--- a/src/Scenery/SceneryPager.hxx
+++ b/src/Scenery/SceneryPager.hxx
@@ -33,6 +33,11 @@ class SceneryPager : public osgDB::DatabasePager
 public:
     SceneryPager();
     SceneryPager(const SceneryPager& rhs);
+
+    // reimplement to add readerWriterOptions from SGPagedLOD
+    virtual void requestNodeFile(const std::string& fileName,osg::Group* group,
+                                 float priority, const osg::FrameStamp* framestamp);
+
     void queueRequest(const std::string& fileName, osg::Group* node,
                       float priority, osg::FrameStamp* frameStamp);
     // This is passed a ref_ptr so that it can "take ownership" of the
diff --git a/src/Scenery/scenery.cxx b/src/Scenery/scenery.cxx
index 9b183cd..7a85d50 100644
--- a/src/Scenery/scenery.cxx
+++ b/src/Scenery/scenery.cxx
@@ -100,8 +100,7 @@ void FGScenery::init() {
     scene_graph->addChild( aircraft_branch.get() );
 
     // Initials values needed by the draw-time object loader
-    sgUserDataInit( globals->get_model_lib(), globals->get_fg_root(),
-                    globals->get_props(), globals->get_sim_time_sec() );
+    sgUserDataInit( globals->get_props() );
 }
 
 
diff --git a/src/Scenery/tileentry.cxx b/src/Scenery/tileentry.cxx
index efb07c9..2ffb8d8 100644
--- a/src/Scenery/tileentry.cxx
+++ b/src/Scenery/tileentry.cxx
@@ -436,7 +436,7 @@ FGTileEntry::loadTileByName(const string& index_str,
             if ( obj->type == OBJECT_STATIC ) {
                 custom_path = obj->path;
             } else {
-                custom_path = globals->get_fg_root();
+                // custom_path = globals->get_fg_root();
             }
             custom_path.append( obj->name );
 
diff --git a/src/Scenery/tilemgr.cxx b/src/Scenery/tilemgr.cxx
index d356aba..83bd9b6 100644
--- a/src/Scenery/tilemgr.cxx
+++ b/src/Scenery/tilemgr.cxx
@@ -244,13 +244,18 @@ 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 += " ";
diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx
index 8f4da31..f5e39fa 100644
--- a/src/Scripting/NasalSys.cxx
+++ b/src/Scripting/NasalSys.cxx
@@ -1082,12 +1082,16 @@ bool FGNasalListener::changed(SGPropertyNode* node)
 void FGNasalModelData::modelLoaded(const string& path, SGPropertyNode *prop,
                                    osg::Node *)
 {
+    if(!prop)
+        return;
+
+/*
     SGPropertyNode *n = prop->getNode("nasal"), *load;
     if(!n)
         return;
-
-    load = n->getNode("load");
-    _unload = n->getNode("unload");
+*/
+    SGPropertyNode *load = load = prop->getNode("load");
+    _unload = prop->getNode("unload");
     if(!load && !_unload)
         return;
 
-- 
1.5.2.5


