]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/model.cxx
Improved tile cache priority scheme.
[simgear.git] / simgear / scene / model / model.cxx
index 5672ece6b810bf1940d035ae0b9b7442c832a342..648b670aaf7ee4ad3032b0fb8989464f52d20027 100644 (file)
@@ -9,6 +9,8 @@
 
 #include <utility>
 
+#include <boost/foreach.hpp>
+
 #include <osg/ref_ptr>
 #include <osgDB/FileNameUtils>
 #include <osgDB/FileUtils>
 #include <osgDB/ReadFile>
 #include <osgDB/SharedStateManager>
 
+#include <simgear/math/SGMath.hxx>
+#include <simgear/scene/material/Effect.hxx>
+#include <simgear/scene/material/EffectGeode.hxx>
 #include <simgear/scene/util/SGSceneFeatures.hxx>
 #include <simgear/scene/util/SGSceneUserData.hxx>
+#include <simgear/scene/util/CopyOp.hxx>
+#include <simgear/scene/util/SplicingVisitor.hxx>
+
 
 #include <simgear/structure/exception.hxx>
+#include <simgear/structure/Singleton.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/props/props_io.hxx>
 #include <simgear/props/condition.hxx>
 
+#include "SGReaderWriterXMLOptions.hxx"
 #include "model.hxx"
 
 using std::vector;
@@ -67,21 +77,23 @@ SGLoadTexture2D(bool staticTexture, const std::string& path,
 
 namespace simgear
 {
+using namespace std;
 using namespace osg;
+using simgear::CopyOp;
 
 Node* copyModel(Node* model)
 {
-    CopyOp::CopyFlags flags = CopyOp::DEEP_COPY_ALL;
-    flags &= ~CopyOp::DEEP_COPY_TEXTURES;
-    flags &= ~CopyOp::DEEP_COPY_IMAGES;
-    flags &= ~CopyOp::DEEP_COPY_STATESETS;
-    flags &= ~CopyOp::DEEP_COPY_STATEATTRIBUTES;
-    flags &= ~CopyOp::DEEP_COPY_ARRAYS;
-    flags &= ~CopyOp::DEEP_COPY_PRIMITIVES;
-    // This will preserve display lists ...
-    flags &= ~CopyOp::DEEP_COPY_DRAWABLES;
-    flags &= ~CopyOp::DEEP_COPY_SHAPES;
-    return static_cast<Node*>(model->clone(CopyOp(flags)));
+    const CopyOp::CopyFlags flags = (CopyOp::DEEP_COPY_ALL
+                                     & ~CopyOp::DEEP_COPY_TEXTURES
+                                     & ~CopyOp::DEEP_COPY_IMAGES
+                                     & ~CopyOp::DEEP_COPY_STATESETS
+                                     & ~CopyOp::DEEP_COPY_STATEATTRIBUTES
+                                     & ~CopyOp::DEEP_COPY_ARRAYS
+                                     & ~CopyOp::DEEP_COPY_PRIMITIVES
+                                     // This will preserve display lists ...
+                                     & ~CopyOp::DEEP_COPY_DRAWABLES
+                                     & ~CopyOp::DEEP_COPY_SHAPES);
+    return (CopyOp(flags))(model);
 }
 
 TextureUpdateVisitor::TextureUpdateVisitor(const osgDB::FilePathList& pathList) :
@@ -191,5 +203,140 @@ void UserDataCopyVisitor::apply(Node& node)
     node.traverse(*this);
 }
 
+namespace
+{
+class MakeEffectVisitor : public SplicingVisitor
+{
+public:
+    typedef std::map<string, SGPropertyNode_ptr> EffectMap;
+    using SplicingVisitor::apply;
+    MakeEffectVisitor(const SGReaderWriterXMLOptions* options = 0)
+        : _options(options)
+    {
+    }
+    virtual void apply(osg::Group& node);
+    virtual void apply(osg::Geode& geode);
+    EffectMap& getEffectMap() { return _effectMap; }
+    const EffectMap& getEffectMap() const { return _effectMap; }
+    void setDefaultEffect(SGPropertyNode* effect)
+    {
+        _currentEffectParent = effect;
+    }
+    SGPropertyNode* getDefaultEffect() { return _currentEffectParent; }
+protected:
+    EffectMap _effectMap;
+    SGPropertyNode_ptr _currentEffectParent;
+    osg::ref_ptr<const SGReaderWriterXMLOptions> _options;
+};
+
+void MakeEffectVisitor::apply(osg::Group& node)
+{
+    SGPropertyNode_ptr savedEffectRoot;
+    const string& nodeName = node.getName();
+    bool restoreEffect = false;
+    if (!nodeName.empty()) {
+        EffectMap::iterator eitr = _effectMap.find(nodeName);
+        if (eitr != _effectMap.end()) {
+            savedEffectRoot = _currentEffectParent;
+            _currentEffectParent = eitr->second;
+            restoreEffect = true;
+        }
+    }
+    SplicingVisitor::apply(node);
+    // If a new node was created, copy the user data too.
+    ref_ptr<SGSceneUserData> userData = SGSceneUserData::getSceneUserData(&node);
+    if (userData.valid() && _childStack.back().back().get() != &node)
+        _childStack.back().back()->setUserData(new SGSceneUserData(*userData));
+    if (restoreEffect)
+        _currentEffectParent = savedEffectRoot;
+}
+
+void MakeEffectVisitor::apply(osg::Geode& geode)
+{
+    if (pushNode(getNewNode(geode)))
+        return;
+    osg::StateSet* ss = geode.getStateSet();
+    if (!ss) {
+        pushNode(&geode);
+        return;
+    }
+    SGPropertyNode_ptr ssRoot = new SGPropertyNode;
+    makeParametersFromStateSet(ssRoot, ss);
+    SGPropertyNode_ptr effectRoot = new SGPropertyNode;
+    effect::mergePropertyTrees(effectRoot, ssRoot, _currentEffectParent);
+    Effect* effect = makeEffect(effectRoot, true, _options.get());
+    EffectGeode* eg = dynamic_cast<EffectGeode*>(&geode);
+    if (eg) {
+        eg->setEffect(effect);
+    } else {
+        eg = new EffectGeode;
+        eg->setEffect(effect);
+        ref_ptr<SGSceneUserData> userData = SGSceneUserData::getSceneUserData(&geode);
+        if (userData.valid())
+            eg->setUserData(new SGSceneUserData(*userData));
+        for (unsigned i = 0; i < geode.getNumDrawables(); ++i) {
+            osg::Drawable *drawable = geode.getDrawable(i);
+            eg->addDrawable(drawable);
+
+            // Generate tangent vectors etc if needed
+            osg::Geometry *geom = dynamic_cast<osg::Geometry*>(drawable);
+            if(geom) eg->runGenerators(geom);
+        }
+    }
+    pushResultNode(&geode, eg);
+
+}
+
+}
+
+namespace
+{
+class DefaultEffect : public simgear::Singleton<DefaultEffect>
+{
+public:
+    DefaultEffect()
+    {
+        _effect = new SGPropertyNode;
+        makeChild(_effect.ptr(), "inherits-from")
+            ->setStringValue("Effects/model-default");
+    }
+    virtual ~DefaultEffect() {}
+    SGPropertyNode* getEffect() { return _effect.ptr(); }
+protected:
+    SGPropertyNode_ptr _effect;
+};
+}
+
+ref_ptr<Node> instantiateEffects(osg::Node* modelGroup,
+                                 PropertyList& effectProps,
+                                 const SGReaderWriterXMLOptions* options)
+{
+    SGPropertyNode_ptr defaultEffectPropRoot;
+    MakeEffectVisitor visitor(options);
+    MakeEffectVisitor::EffectMap& emap = visitor.getEffectMap();
+    for (PropertyList::iterator itr = effectProps.begin(),
+             end = effectProps.end();
+         itr != end;
+        ++itr)
+    {
+        SGPropertyNode_ptr configNode = *itr;
+        std::vector<SGPropertyNode_ptr> objectNames =
+            configNode->getChildren("object-name");
+        SGPropertyNode* defaultNode = configNode->getChild("default");
+        if (defaultNode && defaultNode->getValue<bool>())
+            defaultEffectPropRoot = configNode;
+        BOOST_FOREACH(SGPropertyNode_ptr objNameNode, objectNames) {
+            emap.insert(make_pair(objNameNode->getStringValue(), configNode));
+        }
+        configNode->removeChild("default");
+        configNode->removeChildren("object-name");
+    }
+    if (!defaultEffectPropRoot)
+        defaultEffectPropRoot = DefaultEffect::instance()->getEffect();
+    visitor.setDefaultEffect(defaultEffectPropRoot.ptr());
+    modelGroup->accept(visitor);
+    osg::NodeList& result = visitor.getResults();
+    return ref_ptr<Node>(result[0].get());
+}
 }
 // end of model.cxx