]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/SGReaderWriterXML.cxx
Merge branch 'jmt/waypt' into next
[simgear.git] / simgear / scene / model / SGReaderWriterXML.cxx
index 75026bcfebad5a807b3720bff60b783e39bc88cf..3efc0ded6f1609d287fcd26dbb6580bd79c9e63c 100644 (file)
 #  include <simgear_config.h>
 #endif
 
+#include <algorithm>
+//yuck
+#include <cstring>
+
+#include <boost/bind.hpp>
+
+#include <osg/Geode>
 #include <osg/MatrixTransform>
 #include <osgDB/WriteFile>
 #include <osgDB/Registry>
 #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,
@@ -129,6 +139,56 @@ public:
 private:
     osg::ref_ptr<osg::Referenced> mReferenced;
 };
+
+void makeEffectAnimations(PropertyList& animation_nodes,
+                          PropertyList& effect_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)
+            continue;
+        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(),
+                                    !boost::bind(&SGPropertyNode_ptr::valid,
+                                                 _1)),
+                          animation_nodes.end());
+}
 }
 
 static osg::Node *
@@ -193,6 +253,9 @@ sgLoad3DModel_internal(const string &path,
         SGPropertyNode *mp = props->getNode("multiplay");
         if (mp && prop_root && prop_root->getParent())
             copyProperties(mp, prop_root);
+    } else {
+        SG_LOG(SG_INPUT, SG_DEBUG, "model without wrapper: "
+               << modelpath.str());
     }
 
     osg::ref_ptr<SGReaderWriterXMLOptions> options
@@ -207,13 +270,13 @@ sgLoad3DModel_internal(const string &path,
             texturepath = texturepath.dir();
 
         options->setDatabasePath(texturepath.str());
-        osg::Node* origModel
-            = osgDB::readNodeFile(modelpath.str(), options.get());
-
-        if (!origModel)
+        osgDB::ReaderWriter::ReadResult modelResult
+            = osgDB::Registry::instance()->readNode(modelpath.str(),
+                                                    options.get());
+        if (!modelResult.validNode())
             throw sg_io_exception("Failed to load 3D model",
                                   sg_location(modelpath.str()));
-        model = copyModel(origModel);
+        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
         // it is still in use. Note that the object cache will think
@@ -221,7 +284,7 @@ sgLoad3DModel_internal(const string &path,
         // clone all structural nodes here we need that extra
         // reference to the original object
         SGDatabaseReference* databaseReference;
-        databaseReference = new SGDatabaseReference(origModel);
+        databaseReference = new SGDatabaseReference(modelResult.getNode());
         model->addObserver(databaseReference);
 
         // Update liveries
@@ -271,7 +334,8 @@ sgLoad3DModel_internal(const string &path,
         SGPath submodelpath;
         osg::ref_ptr<osg::Node> submodel;
         string submodelFileName = sub_props->getStringValue("path");
-        if ( submodelFileName.size() > 2 && submodelFileName.substr( 0, 2 ) == "./" ) {
+        if (submodelFileName.size() > 2
+            && !submodelFileName.compare(0, 2, "./" )) {
             submodelpath = modelpath.dir();
             submodelpath.append( submodelFileName.substr( 2 ) );
         } else {
@@ -289,7 +353,7 @@ sgLoad3DModel_internal(const string &path,
             throw;
         }
 
-        osg::ref_ptr<osg::Node> submodel_final=submodel.get();
+        osg::ref_ptr<osg::Node> submodel_final = submodel;
         SGPropertyNode *offs = sub_props->getNode("offsets", false);
         if (offs) {
             osg::Matrix res_matrix;
@@ -311,7 +375,7 @@ sgLoad3DModel_internal(const string &path,
                                offs->getDoubleValue("z-m", 0));
             align->setMatrix(res_matrix*tmat);
             align->addChild(submodel.get());
-            submodel_final=align.get();
+            submodel_final = align;
         }
         submodel_final->setName(sub_props->getStringValue("name", ""));
 
@@ -360,9 +424,15 @@ sgLoad3DModel_internal(const string &path,
                         prop_root,
                         options.get()));
     }
-
-    std::vector<SGPropertyNode_ptr> animation_nodes;
-    animation_nodes = props->getChildren("animation");
+    PropertyList effect_nodes = props->getChildren("effect");
+    PropertyList animation_nodes = props->getChildren("animation");
+    // Some material animations (eventually all) are actually effects.
+    makeEffectAnimations(animation_nodes, effect_nodes);
+    {
+        ref_ptr<Node> modelWithEffects
+            = instantiateEffects(group.get(), effect_nodes, options.get());
+        group = static_cast<Group*>(modelWithEffects.get());
+    }
     for (unsigned i = 0; i < animation_nodes.size(); ++i)
         /// OSGFIXME: duh, why not only model?????
         SGAnimation::animate(group.get(), animation_nodes[i], prop_root,