]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/SGReaderWriterXML.cxx
Improved tile cache priority scheme.
[simgear.git] / simgear / scene / model / SGReaderWriterXML.cxx
index a097b809741160c81183044c7ef9151405a90639..0b5def18b10c8cfb6f9a5fb2d902b1d8a8b0d107 100644 (file)
@@ -23,6 +23,7 @@
 #include <algorithm>
 //yuck
 #include <cstring>
+#include <cassert>
 
 #include <boost/bind.hpp>
 
 #include "particles.hxx"
 #include "model.hxx"
 #include "SGText.hxx"
+#include "SGMaterialAnimation.hxx"
 
 using namespace std;
 using namespace simgear;
 using namespace osg;
 
 static osg::Node *
-sgLoad3DModel_internal(const std::string& path,
+sgLoad3DModel_internal(const SGPath& path,
                        const osgDB::ReaderWriter::Options* options,
                        SGPropertyNode *overlay = 0);
 
@@ -80,9 +82,15 @@ SGReaderWriterXML::readNode(const std::string& fileName,
 {
     osg::Node *result=0;
     try {
-        result=sgLoad3DModel_internal(fileName, options);
-    } catch (const sg_throwable &t) {
-        SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage());
+        SGPath p = SGModelLib::findDataFile(fileName);
+        if (!p.exists()) {
+          return ReadResult::FILE_NOT_FOUND;
+        }
+        
+        result=sgLoad3DModel_internal(p, options);
+    } catch (const sg_exception &t) {
+        SG_LOG(SG_INPUT, SG_ALERT, "Failed to load model: " << t.getFormattedMessage()
+          << "\n\tfrom:" << fileName);
         result=new osg::Node;
     }
     if (result)
@@ -145,31 +153,42 @@ void makeEffectAnimations(PropertyList& animation_nodes,
     for (PropertyList::iterator itr = animation_nodes.begin();
          itr != animation_nodes.end();
          ++itr) {
+        SGPropertyNode_ptr effectProp;
         SGPropertyNode* animProp = itr->ptr();
         SGPropertyNode* typeProp = animProp->getChild("type");
-        if (!typeProp || strcmp(typeProp->getStringValue(), "shader"))
-            continue;
-        SGPropertyNode* shaderProp = animProp->getChild("shader");
-        if (!shaderProp || strcmp(shaderProp->getStringValue(), "chrome"))
-            continue;
-        *itr = 0;
-        SGPropertyNode* textureProp = animProp->getChild("texture");
-        if (!textureProp)
+        if (!typeProp)
             continue;
-        SGPropertyNode_ptr effectProp = new SGPropertyNode();
-        makeChild(effectProp.ptr(), "inherits-from")
-            ->setValue("Effects/chrome");
-        SGPropertyNode* paramsProp = makeChild(effectProp.get(), "parameters");
-        makeChild(paramsProp, "chrome-texture")
-            ->setValue(textureProp->getStringValue());
-        PropertyList objectNameNodes = animProp->getChildren("object-name");
-        for (PropertyList::iterator objItr = objectNameNodes.begin(),
-                 end = objectNameNodes.end();
-             objItr != end;
-            ++objItr)
-            effectProp->addChild("object-name")
-                ->setStringValue((*objItr)->getStringValue());
-        effect_nodes.push_back(effectProp);
+        const char* typeString = typeProp->getStringValue();
+        if (!strcmp(typeString, "material")) {
+            effectProp
+                = SGMaterialAnimation::makeEffectProperties(animProp);
+        } else if (!strcmp(typeString, "shader")) {
+            
+            SGPropertyNode* shaderProp = animProp->getChild("shader");
+            if (!shaderProp || strcmp(shaderProp->getStringValue(), "chrome"))
+                continue;
+            *itr = 0;           // effect replaces animation
+            SGPropertyNode* textureProp = animProp->getChild("texture");
+            if (!textureProp)
+                continue;
+            effectProp = new SGPropertyNode();
+            makeChild(effectProp.ptr(), "inherits-from")
+                ->setValue("Effects/chrome");
+            SGPropertyNode* paramsProp = makeChild(effectProp.get(), "parameters");
+            makeChild(paramsProp, "chrome-texture")
+                ->setValue(textureProp->getStringValue());
+        }
+        if (effectProp.valid()) {
+            PropertyList objectNameNodes = animProp->getChildren("object-name");
+            for (PropertyList::iterator objItr = objectNameNodes.begin(),
+                     end = objectNameNodes.end();
+                 objItr != end;
+                 ++objItr)
+                effectProp->addChild("object-name")
+                    ->setStringValue((*objItr)->getStringValue());
+            effect_nodes.push_back(effectProp);
+
+        }
     }
     animation_nodes.erase(remove_if(animation_nodes.begin(),
                                     animation_nodes.end(),
@@ -180,37 +199,35 @@ void makeEffectAnimations(PropertyList& animation_nodes,
 }
 
 static osg::Node *
-sgLoad3DModel_internal(const string &path,
+sgLoad3DModel_internal(const SGPath& path,
                        const osgDB::ReaderWriter::Options* options_,
                        SGPropertyNode *overlay)
 {
+    if (!path.exists()) {
+      SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path.str() << "\"");
+      return NULL;
+    }
+    
     const SGReaderWriterXMLOptions* xmlOptions;
     xmlOptions = dynamic_cast<const SGReaderWriterXMLOptions*>(options_);
 
     SGSharedPtr<SGPropertyNode> prop_root;
     osg::Node *(*load_panel)(SGPropertyNode *)=0;
     osg::ref_ptr<SGModelData> data;
-
+    SGPath modelpath(path);
+    SGPath texturepath(path);
+    SGPath modelDir(modelpath.dir());
+    
     if (xmlOptions) {
         prop_root = xmlOptions->getPropRoot();
         load_panel = xmlOptions->getLoadPanel();
         data = xmlOptions->getModelData();
     }
+    
     if (!prop_root) {
         prop_root = new SGPropertyNode;
     }
 
-    osgDB::FilePathList filePathList;
-    filePathList = osgDB::Registry::instance()->getDataFilePathList();
-    filePathList.push_front(std::string());
-
-    SGPath modelpath = osgDB::findFileInPath(path, filePathList);
-    if (modelpath.str().empty()) {
-        SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path << "\"");
-        return 0;
-    }
-    SGPath texturepath = modelpath;
-
     osg::ref_ptr<osg::Node> model;
     osg::ref_ptr<osg::Group> group;
     SGPropertyNode_ptr props = new SGPropertyNode;
@@ -219,7 +236,7 @@ sgLoad3DModel_internal(const string &path,
     if (modelpath.extension() == "xml") {
        try {
             readProperties(modelpath.str(), props);
-        } catch (const sg_throwable &t) {
+        } catch (const sg_exception &t) {
             SG_LOG(SG_INPUT, SG_ALERT, "Failed to load xml: "
                    << t.getFormattedMessage());
             throw;
@@ -228,11 +245,9 @@ sgLoad3DModel_internal(const string &path,
             copyProperties(overlay, props);
 
         if (props->hasValue("/path")) {
-            modelpath = modelpath.dir();
-            modelpath.append(props->getStringValue("/path"));
+            modelpath = SGModelLib::findDataFile(props->getStringValue("/path"), NULL, modelDir);
             if (props->hasValue("/texture-path")) {
-                texturepath = texturepath.dir();
-                texturepath.append(props->getStringValue("/texture-path"));
+                texturepath = SGModelLib::findDataFile(props->getStringValue("/texture-path"), NULL, modelDir);
             }
         } else {
             model = new osg::Node;
@@ -242,15 +257,14 @@ sgLoad3DModel_internal(const string &path,
         if (mp && prop_root && prop_root->getParent())
             copyProperties(mp, prop_root);
     } else {
-        SG_LOG(SG_INPUT, SG_DEBUG, "model without wrapper: "
-               << modelpath.str());
+        // model without wrapper
     }
 
     osg::ref_ptr<SGReaderWriterXMLOptions> options
     = new SGReaderWriterXMLOptions(*options_);
     options->setPropRoot(prop_root);
     options->setLoadPanel(load_panel);
-
+    
     // Assume that textures are in
     // the same location as the XML file.
     if (!model) {
@@ -262,8 +276,8 @@ sgLoad3DModel_internal(const string &path,
             = osgDB::Registry::instance()->readNode(modelpath.str(),
                                                     options.get());
         if (!modelResult.validNode())
-            throw sg_io_exception("Failed to load 3D model",
-                                  sg_location(modelpath.str()));
+            throw sg_io_exception("Failed to load 3D model:" + modelResult.message(),
+                                  modelpath.str());
         model = copyModel(modelResult.getNode());
         // Add an extra reference to the model stored in the database.
         // That is to avoid expiring the object from the cache even if
@@ -321,24 +335,22 @@ sgLoad3DModel_internal(const string &path,
 
         SGPath submodelpath;
         osg::ref_ptr<osg::Node> submodel;
-        string submodelFileName = sub_props->getStringValue("path");
-        if (submodelFileName.size() > 2
-            && !submodelFileName.compare(0, 2, "./" )) {
-            submodelpath = modelpath.dir();
-            submodelpath.append( submodelFileName.substr( 2 ) );
-        } else {
-            submodelpath = submodelFileName;
-        }
+        
+        SGPath submodelPath = SGModelLib::findDataFile(sub_props->getStringValue("path"), 
+          NULL, modelDir);
+
         osg::ref_ptr<SGReaderWriterXMLOptions> options;
         options = new SGReaderWriterXMLOptions(*options_);
         options->setPropRoot(prop_root);
         options->setLoadPanel(load_panel);
+        
         try {
-            submodel = sgLoad3DModel_internal(submodelpath.str(), options.get(),
+            submodel = sgLoad3DModel_internal(submodelPath, options.get(),
                                               sub_props->getNode("overlay"));
-        } catch (const sg_throwable &t) {
-            SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
-            throw;
+        } catch (const sg_exception &t) {
+            SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage()
+              << "\n\tfrom:" << t.getOrigin());
+            continue;
         }
 
         osg::ref_ptr<osg::Node> submodel_final = submodel;