]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/modellib.cxx
Improved tile cache priority scheme.
[simgear.git] / simgear / scene / model / modellib.cxx
index e5b5a744e680cd63e1a70c2de1771f78a6580a41..ae90f1a46b6d6d4033c636fcacbbdec04c45adf7 100644 (file)
@@ -19,6 +19,8 @@
 #  include <simgear_config.h>
 #endif
 
+#include <boost/algorithm/string.hpp>
+
 #include <osgDB/ReadFile>
 #include <osgDB/WriteFile>
 #include <osgDB/Registry>
@@ -26,7 +28,9 @@
 #include <simgear/constants.h>
 #include <simgear/props/props.hxx>
 #include <simgear/props/props_io.hxx>
+#include <simgear/scene/model/model.hxx>
 #include <simgear/scene/model/ModelRegistry.hxx>
+#include <simgear/misc/ResourceManager.hxx>
 
 #include "SGPagedLOD.hxx"
 #include "SGReaderWriterXML.hxx"
 
 #include "modellib.hxx"
 
-#include <simgear/Math/SGMath.hxx>
-
+#include <simgear/math/SGMath.hxx>
 
+using std::string;
 using namespace simgear;
 
 osgDB::RegisterReaderWriterProxy<SGReaderWriterXML> g_readerWriter_XML_Proxy;
 ModelRegistryCallbackProxy<LoadOnlyCallback> g_xmlCallbackProxy("xml");
 
-\f
+\fSGPropertyNode_ptr SGModelLib::static_propRoot;
+SGModelLib::panel_func SGModelLib::static_panelFunc = NULL;
+
 ////////////////////////////////////////////////////////////////////////
 // Implementation of SGModelLib.
 ////////////////////////////////////////////////////////////////////////
@@ -51,6 +57,29 @@ void SGModelLib::init(const string &root_dir)
     osgDB::Registry::instance()->getDataFilePathList().push_front(root_dir);
 }
 
+void SGModelLib::setPropRoot(SGPropertyNode* root)
+{
+  static_propRoot = root;
+}
+    
+void SGModelLib::setPanelFunc(panel_func pf)
+{
+  static_panelFunc = pf;
+}
+
+std::string SGModelLib::findDataFile(const std::string& file, 
+  const osgDB::ReaderWriter::Options* opts,
+  SGPath currentPath)
+{
+  SGPath p = ResourceManager::instance()->findPath(file, currentPath);
+  if (p.exists()) {
+    return p.str();
+  }
+      
+  // finally hand on to standard OSG behaviour
+  return osgDB::findDataFile(file, opts);
+}
+
 SGModelLib::SGModelLib()
 {
 }
@@ -59,15 +88,37 @@ SGModelLib::~SGModelLib()
 {
 }
 
+namespace
+{
+osg::Node* loadFile(const string& path, SGReaderWriterXMLOptions* options)
+{
+    using namespace osg;
+    using namespace osgDB;
+    if (boost::iends_with(path, ".ac"))
+        options->setInstantiateEffects(true);
+    ref_ptr<Node> model = readRefNodeFile(path, options);
+    if (!model)
+        return 0;
+    else
+     return model.release();
+}
+}
+
 osg::Node*
 SGModelLib::loadModel(const string &path,
                        SGPropertyNode *prop_root,
-                       SGModelData *data)
+                       SGModelData *data,
+                       bool load2DPanels)
 {
     osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
-    opt->setPropRoot(prop_root);
+    opt->setPropRoot(prop_root ? prop_root: static_propRoot.get());
     opt->setModelData(data);
-    osg::Node *n = readNodeFile(path, opt.get());
+    
+    if (load2DPanels) {
+       opt->setLoadPanel(static_panelFunc);
+    }
+    
+    osg::Node *n = loadFile(path, opt.get());
     if (n && n->getName().empty())
         n->setName("Direct loaded model \"" + path + "\"");
     return n;
@@ -75,19 +126,7 @@ SGModelLib::loadModel(const string &path,
 }
 
 osg::Node*
-SGModelLib::loadModel(const string &path,
-                                SGPropertyNode *prop_root,
-                                panel_func pf)
-{
-    osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
-    opt->setPropRoot(prop_root);
-    opt->setLoadPanel(pf);
-    return readNodeFile(path, opt.get());
-}
-
-osg::Node*
-SGModelLib::loadPagedModel(const string &path,
-                           SGPropertyNode *prop_root,
+SGModelLib::loadPagedModel(const string &path, SGPropertyNode *prop_root,
                            SGModelData *data)
 {
     SGPagedLOD *plod = new SGPagedLOD;
@@ -95,9 +134,14 @@ SGModelLib::loadPagedModel(const string &path,
     plod->setFileName(0, path);
     plod->setRange(0, 0.0, 50.0*SG_NM_TO_METER);
 
-    osg::ref_ptr<SGReaderWriterXMLOptions> opt = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()->getOptions()));
-    opt->setPropRoot(prop_root);
+    osg::ref_ptr<SGReaderWriterXMLOptions> opt
+        = new SGReaderWriterXMLOptions(*(osgDB::Registry::instance()
+                                         ->getOptions()));
+    opt->setPropRoot(prop_root ? prop_root: static_propRoot.get());
     opt->setModelData(data);
+    opt->setLoadPanel(static_panelFunc);
+    if (boost::iends_with(path, ".ac"))
+        opt->setInstantiateEffects(true);
     plod->setReaderWriterOptions(opt.get());
     return plod;
 }