From 78f2320f50bf86eff2a0c437982536132e83ab90 Mon Sep 17 00:00:00 2001
From: till busch <buti@bux.at>
Date: Fri, 21 Mar 2008 22:41:21 +0100
Subject: [PATCH] modelpaging v0.4.4.1 (incremetal) - simgear
cleanups, docs
---
 simgear/constants.h                              |    2 +
 simgear/scene/material/matmodel.cxx              |    2 +
 simgear/scene/material/matmodel.hxx              |    1 -
 simgear/scene/model/CheckSceneryVisitor.cxx      |   28 ++++++---------
 simgear/scene/model/CheckSceneryVisitor.hxx      |   18 +++++++--
 simgear/scene/model/Makefile.am                  |   14 ++++----
 simgear/scene/model/SGPagedLOD.cxx               |   10 +++---
 simgear/scene/model/SGPagedLOD.hxx               |    9 +++--
 simgear/scene/model/SGReaderWriterXML.cxx        |    1 +
 simgear/scene/model/SGReaderWriterXMLOptions.hxx |    4 +-
 simgear/scene/model/model.cxx                    |   14 +-------
 simgear/scene/model/model.hxx                    |   35 ------------------
 simgear/scene/model/modellib.cxx                 |   13 ++-----
 simgear/scene/model/modellib.hxx                 |   43 ++++++++++++++++++++--
 14 files changed, 93 insertions(+), 101 deletions(-)

diff --git a/simgear/constants.h b/simgear/constants.h
index 0cc6ae6..410c834 100644
--- a/simgear/constants.h
+++ b/simgear/constants.h
@@ -75,6 +75,8 @@
  *  6378.165 but this is probably close enough */
 #define SG_EARTH_RAD 6378.155
 
+// Maximum terrain elevation from sea level
+#define SG_MAX_ELEVATION_M 9000.0
 
 // Earth parameters for WGS 84, taken from LaRCsim/ls_constants.h
 
diff --git a/simgear/scene/material/matmodel.cxx b/simgear/scene/material/matmodel.cxx
index fdf3996..49c67ee 100644
--- a/simgear/scene/material/matmodel.cxx
+++ b/simgear/scene/material/matmodel.cxx
@@ -48,6 +48,8 @@ SG_USING_STD(map);
 
 #include "matmodel.hxx"
 
+using namespace simgear;
+
 
 ////////////////////////////////////////////////////////////////////////
 // Local static functions.
diff --git a/simgear/scene/material/matmodel.hxx b/simgear/scene/material/matmodel.hxx
index b814007..703b52c 100644
--- a/simgear/scene/material/matmodel.hxx
+++ b/simgear/scene/material/matmodel.hxx
@@ -46,7 +46,6 @@ SG_USING_STD(string);
 
 
 class SGMatModelGroup;
-class SGModelLib;
 
 
 /**
diff --git a/simgear/scene/model/CheckSceneryVisitor.cxx b/simgear/scene/model/CheckSceneryVisitor.cxx
index 08091ff..d6fdd12 100644
--- a/simgear/scene/model/CheckSceneryVisitor.cxx
+++ b/simgear/scene/model/CheckSceneryVisitor.cxx
@@ -23,13 +23,11 @@
 
 using namespace simgear;
 
-CheckSceneryVisitor::CheckSceneryVisitor(osg::Vec3 &position)
-        :osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
-                          osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
-        _loaded(true), _position(position)
+CheckSceneryVisitor::CheckSceneryVisitor(osgDB::DatabasePager* dbp, osg::Vec3 &position, double range)
+:osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
+                  osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
+_loaded(true), _position(position), _range(range), _dbp(dbp)
 {
-    //SG_LOG(SG_GENERAL, SG_ALERT, "CheckSceneryVisitor:: pos=" << position.x() << ", " << position.y() << ", " << position.z());
-    _currentDepth=0;
     _viewMatrices.push_back(osg::Matrix::identity());
 }
 
@@ -43,12 +41,14 @@ void CheckSceneryVisitor::apply(osg::PagedLOD& node)
     SGPagedLOD *sgplod = dynamic_cast<SGPagedLOD*>(&node);
     if (sgplod) {
         osg::Vec3 pos = sgplod->getCenter() * _viewMatrices.back();
-        float dist = (pos-_position).length();
-        if (dist < 1000.0) {
-            //SG_LOG(SG_GENERAL, SG_ALERT, "CheckSceneryVisitor::[" << sgplod->getFileName(0) << "](" << sgplod->getNumChildren() <<  ") d=" << dist);
+        double dist = (pos-_position).length();
+        if (dist < _range) {
             if (sgplod->getNumChildren() < 1) {
-                sgplod->forceLoad();
-                // setLoaded(false);
+                // if the DatabasePager would load LODs while the splashscreen
+                // is there, we could just wait for the models to be loaded
+                // by only setting setLoaded(false) here
+                sgplod->forceLoad(_dbp);
+                setLoaded(false);
             }
         }
     }
@@ -57,20 +57,14 @@ void CheckSceneryVisitor::apply(osg::PagedLOD& node)
 
 void CheckSceneryVisitor::apply(osg::Transform &node)
 {
-    // Compute transform for current node
     osg::Matrix currMatrix = _viewMatrices.back();
     bool pushMatrix = node.computeLocalToWorldMatrix(currMatrix, this);
-    //bool pushMatrix = node.computeWorldToLocalMatrix(currMatrix, this);
 
     if (pushMatrix) {
-        // Store the new modelview matrix and view frustum
         _viewMatrices.push_back(currMatrix);
     }
-    _currentDepth++;
     traverse(node);
-    _currentDepth--;
     if (pushMatrix) {
-        // Restore the old modelview matrix and view frustum
         _viewMatrices.pop_back();
     }
 }
diff --git a/simgear/scene/model/CheckSceneryVisitor.hxx b/simgear/scene/model/CheckSceneryVisitor.hxx
index 5b4c8ff..c89fc18 100644
--- a/simgear/scene/model/CheckSceneryVisitor.hxx
+++ b/simgear/scene/model/CheckSceneryVisitor.hxx
@@ -20,17 +20,25 @@
 #include <osg/NodeVisitor>
 #include <osg/fast_back_stack>
 
+namespace osgDB {
+class DatabasePager;
+}
+
+
 namespace simgear
 {
 
 class SGPagedLOD;
 
-// check the scene graph for nearby SGPagedLODs and load them
+// Checks the scene graph for SGPagedLODs that are within range
+// (compared to postion) and injects them into the DatabasePager.
+// After visiting, isLoaded() returns true if all models in range
+// are available.
 
 class CheckSceneryVisitor : public osg::NodeVisitor
 {
 public:
-    CheckSceneryVisitor(osg::Vec3 &position);
+    CheckSceneryVisitor(osgDB::DatabasePager* dbp, osg::Vec3 &position, double range);
 
     virtual void apply(osg::Node& node);
     virtual void apply(osg::PagedLOD& node);
@@ -47,10 +55,12 @@ public:
     }
 
 private:
-    bool _loaded;
     osg::Vec3 _position;
+    double _range;
+    bool _loaded;
+    osgDB::DatabasePager* _dbp;
+
     osg::fast_back_stack<osg::Matrix> _viewMatrices;
-    int _currentDepth;
 };
 
 }
diff --git a/simgear/scene/model/Makefile.am b/simgear/scene/model/Makefile.am
index 9c36c26..51ff392 100644
--- a/simgear/scene/model/Makefile.am
+++ b/simgear/scene/model/Makefile.am
@@ -14,13 +14,13 @@ include_HEADERS = \
 	persparam.hxx \
 	placement.hxx \
 	placementtrans.hxx \
-        CheckSceneryVisitor.hxx \
+	CheckSceneryVisitor.hxx \
 	SGClipGroup.hxx \
 	SGMaterialAnimation.hxx \
 	SGOffsetTransform.hxx \
-        SGPagedLOD.hxx \
-        SGReaderWriterXML.hxx \
-        SGReaderWriterXMLOptions.hxx \
+	SGPagedLOD.hxx \
+	SGReaderWriterXML.hxx \
+	SGReaderWriterXMLOptions.hxx \
 	SGRotateTransform.hxx \
 	SGScaleTransform.hxx \
 	SGTranslateTransform.hxx
@@ -36,12 +36,12 @@ libsgmodel_a_SOURCES = \
 	placement.cxx \
 	placementtrans.cxx \
 	shadanim.cxx \
-        CheckSceneryVisitor.cxx \
+	CheckSceneryVisitor.cxx \
 	SGClipGroup.cxx \
 	SGMaterialAnimation.cxx \
 	SGOffsetTransform.cxx \
-        SGPagedLOD.cxx \
-        SGReaderWriterXML.cxx \
+	SGPagedLOD.cxx \
+	SGReaderWriterXML.cxx \
 	SGRotateTransform.cxx \
 	SGScaleTransform.cxx \
 	SGTranslateTransform.cxx
diff --git a/simgear/scene/model/SGPagedLOD.cxx b/simgear/scene/model/SGPagedLOD.cxx
index a2a4530..5158cfc 100644
--- a/simgear/scene/model/SGPagedLOD.cxx
+++ b/simgear/scene/model/SGPagedLOD.cxx
@@ -17,8 +17,8 @@
 #include <osgDB/ReadFile>
 
 #include <simgear/debug/logstream.hxx>
-#include "model.hxx"
 
+#include "modellib.hxx"
 #include "SGReaderWriterXMLOptions.hxx"
 #include "SGPagedLOD.hxx"
 
@@ -64,11 +64,11 @@ bool SGPagedLOD::addChild(osg::Node *child)
     return true;
 }
 
-void SGPagedLOD::forceLoad()
+void SGPagedLOD::forceLoad(osgDB::DatabasePager *dbp)
 {
+    //SG_LOG(SG_GENERAL, SG_ALERT, "SGPagedLOD::forceLoad(" << getFileName(getNumChildren()) << ")");
     setTimeStamp(getNumChildren(),0);
-    addChild(osgDB::readNodeFile(getFileName(getNumChildren()),
-                                 _readerWriterOptions.get()));
-    //dbp->requestNodeFile(_perRangeDataList[numChildren]._filename,this,priority,nv.getFrameStamp(), _readerWriterOptions.get());
+    double priority=1.0;
+    dbp->requestNodeFile(getFileName(getNumChildren()),this,priority,0, _readerWriterOptions.get());
 }
 
diff --git a/simgear/scene/model/SGPagedLOD.hxx b/simgear/scene/model/SGPagedLOD.hxx
index 98e8ce2..d45c1bb 100644
--- a/simgear/scene/model/SGPagedLOD.hxx
+++ b/simgear/scene/model/SGPagedLOD.hxx
@@ -19,13 +19,16 @@
 
 #include <osg/PagedLOD>
 #include <osgDB/ReaderWriter>
-#include <simgear/scene/model/CheckSceneryVisitor.hxx>
 #include <simgear/props/props.hxx>
 
+namespace osgDB {
+class DatabasePager;
+}
+
+
 namespace simgear
 {
 
-
 class SGPagedLOD : public osg::PagedLOD
 {
 public:
@@ -36,7 +39,7 @@ public:
     META_Node(osg, PagedLOD);
 
     // virtual void traverse(osg::NodeVisitor& nv);
-    virtual void forceLoad();
+    virtual void forceLoad(osgDB::DatabasePager* dbp);
 
     // reimplemented to notify the loading through ModelData
     bool addChild(osg::Node *child);
diff --git a/simgear/scene/model/SGReaderWriterXML.cxx b/simgear/scene/model/SGReaderWriterXML.cxx
index 4dbffed..656818a 100644
--- a/simgear/scene/model/SGReaderWriterXML.cxx
+++ b/simgear/scene/model/SGReaderWriterXML.cxx
@@ -28,6 +28,7 @@
 #include <simgear/props/condition.hxx>
 #include <simgear/scene/util/SGNodeMasks.hxx>
 
+#include "modellib.hxx"
 #include "SGPagedLOD.hxx"
 #include "SGReaderWriterXML.hxx"
 #include "SGReaderWriterXMLOptions.hxx"
diff --git a/simgear/scene/model/SGReaderWriterXMLOptions.hxx b/simgear/scene/model/SGReaderWriterXMLOptions.hxx
index 482f6f4..b9f4654 100644
--- a/simgear/scene/model/SGReaderWriterXMLOptions.hxx
+++ b/simgear/scene/model/SGReaderWriterXMLOptions.hxx
@@ -19,13 +19,13 @@
 #define SGREADERWRITERXMLOPTIONS_HXX 1
 
 #include <osgDB/ReaderWriter>
-#include <simgear/misc/sg_path.hxx>
+#include <simgear/props/props.hxx>
 
 class SGPropertyNode;
-class SGModelData;
 
 namespace simgear
 {
+class SGModelData;
 
 class SGReaderWriterXMLOptions : public osgDB::ReaderWriter::Options
 {
diff --git a/simgear/scene/model/model.cxx b/simgear/scene/model/model.cxx
index b671fbb..cdd5086 100644
--- a/simgear/scene/model/model.cxx
+++ b/simgear/scene/model/model.cxx
@@ -8,32 +8,20 @@
 #endif
 
 #include <osg/ref_ptr>
-#include <osg/NodeCallback>
-#include <osgDB/FileNameUtils>
-#include <osgDB/FileUtils>
+#include <osgDB/ReadFile>
 #include <osgDB/SharedStateManager>
-#include <osgUtil/Optimizer>
 
 #include <simgear/scene/util/SGSceneFeatures.hxx>
-#include <simgear/scene/util/SGStateAttributeVisitor.hxx>
-#include <simgear/scene/util/SGTextureStateAttributeVisitor.hxx>
 
 #include <simgear/structure/exception.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/props/props_io.hxx>
 #include <simgear/props/condition.hxx>
 
-#include "SGPagedLOD.hxx"
-#include "SGReaderWriterXML.hxx"
-#include "SGReaderWriterXMLOptions.hxx"
-#include "animation.hxx"
-#include "particles.hxx"
 #include "model.hxx"
 
 SG_USING_STD(vector);
 
-using namespace simgear;
-
 osg::Texture2D*
 SGLoadTexture2D(bool staticTexture, const std::string& path,
                 const osgDB::ReaderWriter::Options* options,
diff --git a/simgear/scene/model/model.hxx b/simgear/scene/model/model.hxx
index dbab05f..1de3bf4 100644
--- a/simgear/scene/model/model.hxx
+++ b/simgear/scene/model/model.hxx
@@ -23,41 +23,6 @@ SG_USING_STD(set);
 #include <osgDB/ReaderWriter>
 
 #include <simgear/misc/sg_path.hxx>
-#include <simgear/props/props.hxx>
-
-/**
- * Abstract class for adding data to the scene graph.  modelLoaded() is
- * called by sgLoad3DModel() after the model was loaded, and the destructor
- * when the branch is removed from the graph.
- */
-class SGModelData : public osg::Referenced {
-public:
-    virtual ~SGModelData() {}
-    virtual void modelLoaded( const string& path, SGPropertyNode *prop,
-                              osg::Node*branch) = 0;
-
-    virtual void setProps(SGPropertyNode *p)
-        { _props = p; }
-
-    SGPropertyNode *getProperties()
-        { return _props; }
-protected:
-    SGPropertyNode_ptr _props;
-};
-
-
-/**
- * Make the animation
- */
-void
-sgMakeAnimation( osg::Node* model,
-                 const char * name,
-                 vector<SGPropertyNode_ptr> &name_nodes,
-                 SGPropertyNode *prop_root,
-                 SGPropertyNode_ptr node,
-                 double sim_time_sec,
-                 SGPath &texture_path,
-                 set<osg::Node*> &ignore_branches );
 
 osg::Texture2D*
 SGLoadTexture2D(bool staticTexture, const std::string& path,
diff --git a/simgear/scene/model/modellib.cxx b/simgear/scene/model/modellib.cxx
index b1b8a70..5cb6965 100644
--- a/simgear/scene/model/modellib.cxx
+++ b/simgear/scene/model/modellib.cxx
@@ -19,27 +19,20 @@
 #  include <simgear_config.h>
 #endif
 
+#include <osgDB/ReadFile>
 #include <osgDB/WriteFile>
 #include <osgDB/Registry>
-#include <osg/Switch>
 
-#include <simgear/compiler.h>
-#include <simgear/structure/exception.hxx>
+#include <simgear/constants.h>
 #include <simgear/props/props.hxx>
 #include <simgear/props/props_io.hxx>
-#include <simgear/props/condition.hxx>
-#include <simgear/scene/util/SGNodeMasks.hxx>
 #include <simgear/scene/model/ModelRegistry.hxx>
 
 #include "SGPagedLOD.hxx"
 #include "SGReaderWriterXML.hxx"
 #include "SGReaderWriterXMLOptions.hxx"
-#include "animation.hxx"
-#include "particles.hxx"
-
-#include "model.hxx"
-#include "animation.hxx"
 
+//#include "model.hxx"
 #include "modellib.hxx"
 
 
diff --git a/simgear/scene/model/modellib.hxx b/simgear/scene/model/modellib.hxx
index 2c7780c..b2d038e 100644
--- a/simgear/scene/model/modellib.hxx
+++ b/simgear/scene/model/modellib.hxx
@@ -28,30 +28,43 @@
 
 #include <osg/Node>
 
+#include <simgear/props/props.hxx>
+
 SG_USING_STD(map);
 SG_USING_STD(string);
 
-class SGPropertyNode;
-class SGModelData;
+namespace simgear {
+
+class SGModelData; // defined below
 
 /**
  * Class for loading and managing models with XML wrappers.
  */
-typedef osg::Node *(*panel_func)(SGPropertyNode *);
-
 class SGModelLib
 {
 public:
+    typedef osg::Node *(*panel_func)(SGPropertyNode *);
+
     static void init(const string &root_dir);
 
+    // Load a 3D model (any format)
+    // data->modelLoaded() will be called after the model is loaded
     static osg::Node* loadModel(const string &path,
                                 SGPropertyNode *prop_root,
                                 SGModelData *data=0);
 
+    // Load a 3D model (any format)
+    // with a panel_func to load a panel
     static osg::Node* loadModel(const string &path,
                                 SGPropertyNode *prop_root,
                                 panel_func pf);
 
+    // Load a 3D model (any format) through the DatabasePager.
+    // Most models should be loaded using this function!
+    // This function will initially return an SGPagedLOD node.
+    // data->modelLoaded() will be called after the model is loaded and
+    // connected to the scene graph. See AIModelData on how to use this.
+    // NOTE: AIModelData uses observer_ptr to avoid circular references.
     static osg::Node* loadPagedModel(const string &path,
                                      SGPropertyNode *prop_root,
                                      SGModelData *data=0);
@@ -62,4 +75,26 @@ protected:
 };
 
 
+/**
+ * Abstract class for adding data to the scene graph.  modelLoaded() is
+ * called after the model was loaded, and the destructor when the branch
+ * is removed from the scene graph.
+ */
+class SGModelData : public osg::Referenced {
+public:
+    virtual ~SGModelData() {}
+    virtual void modelLoaded( const string& path, SGPropertyNode *prop,
+                              osg::Node*branch) = 0;
+
+    virtual void setProps(SGPropertyNode *p)
+        { _props = p; }
+
+    SGPropertyNode *getProperties()
+        { return _props; }
+protected:
+    SGPropertyNode_ptr _props;
+};
+
+}
+
 #endif // _SG_MODEL_LIB_HXX
-- 
1.5.2.5


