]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/SGReaderWriterXML.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / model / SGReaderWriterXML.cxx
index 81ccd85a52b49d95d09e128938deeeca2e8ef2d4..83d6d36dadd4731e8769645c4088248912fa3783 100644 (file)
@@ -29,6 +29,7 @@
 
 #include <osg/Geode>
 #include <osg/MatrixTransform>
+#include <osgDB/ReadFile>
 #include <osgDB/WriteFile>
 #include <osgDB/Registry>
 #include <osg/Switch>
@@ -57,7 +58,7 @@ using namespace osg;
 
 static osg::Node *
 sgLoad3DModel_internal(const SGPath& path,
-                       const osgDB::ReaderWriter::Options* options,
+                       const osgDB::Options* options,
                        SGPropertyNode *overlay = 0);
 
 
@@ -76,9 +77,11 @@ const char* SGReaderWriterXML::className() const
 }
 
 osgDB::ReaderWriter::ReadResult
-SGReaderWriterXML::readNode(const std::string& fileName,
-                            const osgDB::ReaderWriter::Options* options) const
+SGReaderWriterXML::readNode(const std::string& name,
+                            const osgDB::Options* options) const
 {
+    std::string fileName = osgDB::findDataFile(name, options);
+
     osg::Node *result=0;
     try {
         SGPath p = SGModelLib::findDataFile(fileName);
@@ -157,6 +160,7 @@ void makeEffectAnimations(PropertyList& animation_nodes,
         SGPropertyNode* typeProp = animProp->getChild("type");
         if (!typeProp)
             continue;
+        
         const char* typeString = typeProp->getStringValue();
         if (!strcmp(typeString, "material")) {
             effectProp
@@ -197,40 +201,88 @@ void makeEffectAnimations(PropertyList& animation_nodes,
 }
 }
 
+namespace simgear {
+class SetNodeMaskVisitor : public osg::NodeVisitor {
+public:
+    SetNodeMaskVisitor(osg::Node::NodeMask nms, osg::Node::NodeMask nmc) :
+        osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), nodeMaskSet(nms), nodeMaskClear(nmc)
+    {}
+    virtual void apply(osg::Geode& node) {
+        node.setNodeMask((node.getNodeMask() | nodeMaskSet) & ~nodeMaskClear);
+        traverse(node);
+    }
+private:
+    osg::Node::NodeMask nodeMaskSet;
+    osg::Node::NodeMask nodeMaskClear;
+};
+    
+}
+
+namespace {
+    class ExcludeInPreview
+    {
+    public:
+        bool operator()(SGPropertyNode* aNode) const
+        {
+            string typeString(aNode->getStringValue("type"));
+            // exclude these so we don't show yellow outlines in preview mode
+            return (typeString == "pick") || (typeString == "knob") || (typeString == "slider");
+        }
+    };
+    
+    bool removeNamedNode(osg::Group* aGroup, const std::string& aName)
+    {
+        int nKids = aGroup->getNumChildren();
+        for (int i = 0; i < nKids; i++) {
+            osg::Node* child = aGroup->getChild(i);
+            if (child->getName() == aName) {
+                aGroup->removeChild(child);
+                return true;
+            }
+        }
+        
+        for (int i = 0; i < nKids; i++) {
+            osg::Group* childGroup = aGroup->getChild(i)->asGroup();
+            if (!childGroup)
+                continue;
+            
+            if (removeNamedNode(childGroup, aName))
+                return true;
+        } // of child groups traversal
+        
+        return false;
+    }
+}
+
 static osg::Node *
 sgLoad3DModel_internal(const SGPath& path,
-                       const osgDB::ReaderWriter::Options* options_,
+                       const osgDB::Options* dbOptions,
                        SGPropertyNode *overlay)
 {
     if (!path.exists()) {
       SG_LOG(SG_INPUT, SG_ALERT, "Failed to load file: \"" << path.str() << "\"");
       return NULL;
     }
-    
-    const SGReaderWriterOptions* xmlOptions;
-    xmlOptions = dynamic_cast<const SGReaderWriterOptions*>(options_);
 
-    SGSharedPtr<SGPropertyNode> prop_root;
-    osg::Node *(*load_panel)(SGPropertyNode *)=0;
-    osg::ref_ptr<SGModelData> data;
+    osg::ref_ptr<SGReaderWriterOptions> options;
+    options = SGReaderWriterOptions::copyOrCreate(dbOptions);
+    
     SGPath modelpath(path);
     SGPath texturepath(path);
     SGPath modelDir(modelpath.dir());
     
-    if (xmlOptions) {
-        prop_root = xmlOptions->getPropertyNode();
-        load_panel = xmlOptions->getLoadPanel();
-        data = xmlOptions->getModelData();
-    }
-    
-    if (!prop_root) {
+    SGSharedPtr<SGPropertyNode> prop_root = options->getPropertyNode();
+    if (!prop_root.valid())
         prop_root = new SGPropertyNode;
-    }
-
+    // The model data appear to be only used in the topmost model
+    osg::ref_ptr<SGModelData> data = options->getModelData();
+    options->setModelData(0);
+    
     osg::ref_ptr<osg::Node> model;
     osg::ref_ptr<osg::Group> group;
     SGPropertyNode_ptr props = new SGPropertyNode;
-
+    bool previewMode = (dbOptions->getPluginStringData("SimGear::PREVIEW") == "ON");
+    
     // Check for an XML wrapper
     if (modelpath.extension() == "xml") {
        try {
@@ -243,6 +295,10 @@ sgLoad3DModel_internal(const SGPath& path,
         if (overlay)
             copyProperties(overlay, props);
 
+        if (previewMode && props->hasChild("nopreview")) {
+            return NULL;
+        }
+        
         if (props->hasValue("/path")) {
             string modelPathStr = props->getStringValue("/path");
             modelpath = SGModelLib::findDataFile(modelPathStr, NULL, modelDir);
@@ -252,10 +308,13 @@ sgLoad3DModel_internal(const SGPath& path,
 
             if (props->hasValue("/texture-path")) {
                 string texturePathStr = props->getStringValue("/texture-path");
-                texturepath = SGModelLib::findDataFile(texturePathStr, NULL, modelDir);
-                if (texturepath.isNull())
-                    throw sg_io_exception("Texture file not found: '" + texturePathStr + "'",
-                            path.str());
+                if (!texturePathStr.empty())
+                {
+                    texturepath = SGModelLib::findDataFile(texturePathStr, NULL, modelDir);
+                    if (texturepath.isNull())
+                        throw sg_io_exception("Texture file not found: '" + texturePathStr + "'",
+                                path.str());
+                }
             }
         } else {
             model = new osg::Node;
@@ -268,11 +327,6 @@ sgLoad3DModel_internal(const SGPath& path,
         // model without wrapper
     }
 
-    osg::ref_ptr<SGReaderWriterOptions> options
-    = new SGReaderWriterOptions(*options_);
-    options->setPropertyNode(prop_root);
-    options->setLoadPanel(load_panel);
-    
     // Assume that textures are in
     // the same location as the XML file.
     if (!model) {
@@ -280,9 +334,8 @@ sgLoad3DModel_internal(const SGPath& path,
             texturepath = texturepath.dir();
 
         options->setDatabasePath(texturepath.str());
-        osgDB::ReaderWriter::ReadResult modelResult
-            = osgDB::Registry::instance()->readNode(modelpath.str(),
-                                                    options.get());
+        osgDB::ReaderWriter::ReadResult modelResult;
+        modelResult = osgDB::readNodeFile(modelpath.str(), options.get());
         if (!modelResult.validNode())
             throw sg_io_exception("Failed to load 3D model:" + modelResult.message(),
                                   modelpath.str());
@@ -305,6 +358,9 @@ sgLoad3DModel_internal(const SGPath& path,
         // but introducing new data for velocities.
         UserDataCopyVisitor userDataCopyVisitor;
         model->accept(userDataCopyVisitor);
+
+        SetNodeMaskVisitor setNodeMaskVisitor(0, simgear::MODELLIGHT_BIT);
+        model->accept(setNodeMaskVisitor);
     }
     model->setName(modelpath.str());
 
@@ -353,11 +409,6 @@ sgLoad3DModel_internal(const SGPath& path,
           continue;
         }
 
-        osg::ref_ptr<SGReaderWriterOptions> options;
-        options = new SGReaderWriterOptions(*options_);
-        options->setPropertyNode(prop_root);
-        options->setLoadPanel(load_panel);
-        
         try {
             submodel = sgLoad3DModel_internal(submodelPath, options.get(),
                                               sub_props->getNode("overlay"));
@@ -366,6 +417,9 @@ sgLoad3DModel_internal(const SGPath& path,
               << "\n\tfrom:" << t.getOrigin());
             continue;
         }
+        
+        if (!submodel)
+            continue;
 
         osg::ref_ptr<osg::Node> submodel_final = submodel;
         SGPropertyNode *offs = sub_props->getNode("offsets", false);
@@ -405,6 +459,7 @@ sgLoad3DModel_internal(const SGPath& path,
         }
     } // end of submodel loading
 
+    osg::Node *(*load_panel)(SGPropertyNode *) = options->getLoadPanel();
     if ( load_panel ) {
         // Load panels
         vector<SGPropertyNode_ptr> panel_nodes = props->getChildren("panel");
@@ -417,18 +472,22 @@ sgLoad3DModel_internal(const SGPath& path,
         }
     }
 
-    std::vector<SGPropertyNode_ptr> particle_nodes;
-    particle_nodes = props->getChildren("particlesystem");
-    for (unsigned i = 0; i < particle_nodes.size(); ++i) {
-        if (i==0) {
-            if (!texturepath.extension().empty())
-                texturepath = texturepath.dir();
-
-            options->setDatabasePath(texturepath.str());
+    if (dbOptions->getPluginStringData("SimGear::PARTICLESYSTEM") != "OFF") {
+        std::vector<SGPropertyNode_ptr> particle_nodes;
+        particle_nodes = props->getChildren("particlesystem");
+        for (unsigned i = 0; i < particle_nodes.size(); ++i) {
+            osg::ref_ptr<SGReaderWriterOptions> options2;
+            options2 = new SGReaderWriterOptions(*options);
+            if (i==0) {
+                if (!texturepath.extension().empty())
+                    texturepath = texturepath.dir();
+                
+                options2->setDatabasePath(texturepath.str());
+            }
+            group->addChild(Particles::appendParticles(particle_nodes[i],
+                                                       prop_root,
+                                                       options2.get()));
         }
-        group->addChild(Particles::appendParticles(particle_nodes[i],
-                        prop_root,
-                        options.get()));
     }
 
     std::vector<SGPropertyNode_ptr> text_nodes;
@@ -438,8 +497,16 @@ sgLoad3DModel_internal(const SGPath& path,
                         prop_root,
                         options.get()));
     }
+
     PropertyList effect_nodes = props->getChildren("effect");
     PropertyList animation_nodes = props->getChildren("animation");
+    
+    if (previewMode) {
+        PropertyList::iterator it;
+        it = std::remove_if(animation_nodes.begin(), animation_nodes.end(), ExcludeInPreview());
+        animation_nodes.erase(it, animation_nodes.end());
+    }
+    
     // Some material animations (eventually all) are actually effects.
     makeEffectAnimations(animation_nodes, effect_nodes);
     {
@@ -447,11 +514,21 @@ sgLoad3DModel_internal(const SGPath& path,
             = instantiateEffects(group.get(), effect_nodes, options.get());
         group = static_cast<Group*>(modelWithEffects.get());
     }
-    for (unsigned i = 0; i < animation_nodes.size(); ++i)
+
+    for (unsigned i = 0; i < animation_nodes.size(); ++i) {
+        if (previewMode && animation_nodes[i]->hasChild("nopreview")) {
+            PropertyList names(animation_nodes[i]->getChildren("object-name"));
+            for (unsigned int n=0; n<names.size(); ++n) {
+                removeNamedNode(group, names[n]->getStringValue());
+            } // of object-names in the animation
+            continue;
+        }
+        
         /// OSGFIXME: duh, why not only model?????
         SGAnimation::animate(group.get(), animation_nodes[i], prop_root,
-                             options.get());
-
+                             options.get(), path.str(), i);
+    }
+    
     if (!needTransform && group->getNumChildren() < 2) {
         model = group->getChild(0);
         group->removeChild(model.get());