]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/SGLightAnimation.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / model / SGLightAnimation.cxx
index 930d2db26302052f8de25267ca950bbb0a0cbc62..b049c15d2a13e8f925a5d741f8158a4b272effe4 100644 (file)
 
 #include <osg/Geometry>
 #include <osg/MatrixTransform>
+#include <simgear/props/vectorPropTemplates.hxx>
 #include <simgear/scene/material/EffectGeode.hxx>
 #include <simgear/scene/material/Technique.hxx>
 #include <simgear/scene/material/Pass.hxx>
 #include <simgear/scene/util/CopyOp.hxx>
 #include <simgear/scene/util/OsgMath.hxx>
+#include <simgear/scene/util/SGReaderWriterOptions.hxx>
 #include <boost/scoped_array.hpp>
 #include <boost/foreach.hpp>
 
-typedef std::map<string, osg::ref_ptr<simgear::Effect> > EffectMap;
+typedef std::map<std::string, osg::observer_ptr<simgear::Effect> > EffectMap;
 static EffectMap lightEffectMap;
 
 #define GET_COLOR_VALUE(n) \
@@ -45,38 +47,38 @@ static EffectMap lightEffectMap;
 
 class SGLightAnimation::UpdateCallback : public osg::NodeCallback {
 public:
-    UpdateCallback(string k, const SGExpressiond* v, SGVec4d a, SGVec4d d, SGVec4d s) :
-        _key(k),
+    UpdateCallback(const SGExpressiond* v, SGVec4d a, SGVec4d d, SGVec4d s) :
         _ambient(a),
         _diffuse(d),
         _specular(s),
         _animationValue(v)
     {
-        _prev_values[k] = -1;
+        _prev_value = -1;
     }
     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
     {
         double dim = _animationValue->getValue();
-        if (dim != _prev_values[_key]) {
-            _prev_values[_key] = dim;
+        if (dim != _prev_value) {
+            _prev_value = dim;
+            simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>( node );
+            if (geode != 0) {
+                osg::ref_ptr<simgear::Effect> effect = geode->getEffect();
 
-            EffectMap::iterator iter = lightEffectMap.find(_key);
-            if (iter != lightEffectMap.end()) {
-                simgear::Effect* effect = iter->second;
                 SGPropertyNode* params = effect->parametersProp;
                 params->getNode("ambient")->setValue(_ambient * dim);
                 params->getNode("diffuse")->setValue(_diffuse * dim);
                 params->getNode("specular")->setValue(_specular * dim);
-                BOOST_FOREACH(osg::ref_ptr<simgear::Technique>& technique, effect->techniques)
-                {
-                    BOOST_FOREACH(osg::ref_ptr<simgear::Pass>& pass, technique->passes)
-                    {
+                BOOST_FOREACH(osg::ref_ptr<simgear::Technique>& technique, effect->techniques) {
+                    BOOST_FOREACH(osg::ref_ptr<simgear::Pass>& pass, technique->passes) {
                         osg::Uniform* amb = pass->getUniform("Ambient");
-                        amb->set(toOsg(_ambient) * dim);
+                        if (amb)
+                            amb->set(osg::Vec4f(_ambient.x() * dim, _ambient.y() * dim, _ambient.z() * dim, _ambient.w() * dim));
                         osg::Uniform* dif = pass->getUniform("Diffuse");
-                        dif->set(toOsg(_diffuse) * dim);
+                        if (dif)
+                            dif->set(osg::Vec4f(_diffuse.x() * dim, _diffuse.y() * dim, _diffuse.z() * dim, _diffuse.w() * dim));
                         osg::Uniform* spe = pass->getUniform("Specular");
-                        spe->set(toOsg(_specular) * dim);
+                        if (spe)
+                            spe->set(osg::Vec4f(_specular.x() * dim, _specular.y() * dim, _specular.z() * dim, _specular.w() * dim));
                     }
                 }
             }
@@ -84,21 +86,19 @@ public:
         traverse(node, nv);
     }
 public:
-    string _key;
     SGVec4d _ambient;
     SGVec4d _diffuse;
     SGVec4d _specular;
     SGSharedPtr<SGExpressiond const> _animationValue;
-
-    typedef std::map<string, double> PrevValueMap;
-    static PrevValueMap _prev_values;
+    double _prev_value;
 };
-SGLightAnimation::UpdateCallback::PrevValueMap SGLightAnimation::UpdateCallback::_prev_values;
 
 SGLightAnimation::SGLightAnimation(const SGPropertyNode* configNode,
                                    SGPropertyNode* modelRoot,
-                                   const string &path, int i) :
-    SGAnimation(configNode, modelRoot)
+                                   const osgDB::Options* options,
+                                   const std::string &path, int i) :
+    SGAnimation(configNode, modelRoot),
+    _options(options)
 {
     _light_type = getConfig()->getStringValue("light-type");
     _position = SGVec3d( getConfig()->getDoubleValue("position/x"), getConfig()->getDoubleValue("position/y"), getConfig()->getDoubleValue("position/z") );
@@ -113,7 +113,7 @@ SGLightAnimation::SGLightAnimation(const SGPropertyNode* configNode,
     _cutoff = getConfig()->getDoubleValue("cutoff");
     _near = getConfig()->getDoubleValue("near-m");
     _far = getConfig()->getDoubleValue("far-m");
-    _key = path + ";" + boost::lexical_cast<string>( i );
+    _key = path + ";" + boost::lexical_cast<std::string>( i );
 
     SGConstPropertyNode_ptr dim_factor = configNode->getChild("dim-factor");
     if (dim_factor.valid()) {
@@ -126,8 +126,6 @@ SGLightAnimation::createAnimationGroup(osg::Group& parent)
 {
     osg::Group* grp = new osg::Group;
     grp->setName("light animation node");
-    if (_animationValue.valid())
-        grp->setUpdateCallback(new UpdateCallback(_key, _animationValue, _ambient, _diffuse, _specular));
     parent.addChild(grp);
     grp->setNodeMask( simgear::MODELLIGHT_BIT );
     return grp;
@@ -138,12 +136,18 @@ SGLightAnimation::install(osg::Node& node)
 {
     SGAnimation::install(node);
 
-    if (_light_type == "spot") {
+    bool cacheEffect = false;
+    osg::ref_ptr<simgear::Effect> effect;
+    EffectMap::iterator iter = lightEffectMap.end();
+    if (!_animationValue.valid()) { // Effects with animated properties should be singletons
+        iter = lightEffectMap.find(_key);
+        cacheEffect = true;
+    }
 
-        simgear::Effect* effect = 0;
-        EffectMap::iterator iter = lightEffectMap.find(_key);
-        if (iter == lightEffectMap.end()) {
-            SGPropertyNode_ptr effectProp = new SGPropertyNode;
+    if (iter == lightEffectMap.end() || !iter->second.lock(effect)) {
+        SGPropertyNode_ptr effectProp = new SGPropertyNode;
+        if (_light_type == "spot") {
+            makeChild(effectProp, "name")->setStringValue(_key);
             makeChild(effectProp, "inherits-from")->setStringValue("Effects/light-spot");
             double dim = 1.0;
             if (_animationValue.valid())
@@ -161,32 +165,9 @@ SGLightAnimation::install(osg::Node& node)
             params->getNode("cosCutoff",true)->setValue( cos(_cutoff*SG_DEGREES_TO_RADIANS) );
             params->getNode("near",true)->setValue(_near);
             params->getNode("far",true)->setValue(_far);
-
-            effect = simgear::makeEffect(effectProp, true);
-            lightEffectMap.insert(EffectMap::value_type(_key, effect));
-        } else {
-            effect = iter->second.get();
-        }
-
-        node.setNodeMask( simgear::MODELLIGHT_BIT );
-        simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>(&node);
-        if (geode == 0) {
-            osg::Group* grp = node.asGroup();
-            if (grp != 0) {
-                for (size_t i=0; i<grp->getNumChildren(); ++i) {
-                    geode = dynamic_cast<simgear::EffectGeode*>(grp->getChild(i));
-                    if (geode)
-                        geode->setEffect(effect);
-                }
-            }
         }
-    }
-    else if (_light_type == "point") {
-
-        simgear::Effect* effect = 0;
-        EffectMap::iterator iter = lightEffectMap.find(_key);
-        if (iter == lightEffectMap.end()) {
-            SGPropertyNode_ptr effectProp = new SGPropertyNode;
+        else if (_light_type == "point") {
+            makeChild(effectProp, "name")->setStringValue(_key);
             makeChild(effectProp, "inherits-from")->setStringValue("Effects/light-point");
             double dim = 1.0;
             if (_animationValue.valid())
@@ -200,24 +181,34 @@ SGLightAnimation::install(osg::Node& node)
             params->getNode("attenuation",true)->setValue(_attenuation);
             params->getNode("near",true)->setValue(_near);
             params->getNode("far",true)->setValue(_far);
-
-            effect = simgear::makeEffect(effectProp, true);
-            lightEffectMap.insert(EffectMap::value_type(_key, effect));
         } else {
-            effect = iter->second.get();
+            return;
         }
 
-        node.setNodeMask( simgear::MODELLIGHT_BIT );
-        simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>(&node);
-        if (geode == 0) {
-            osg::Group* grp = node.asGroup();
-            if (grp != 0) {
-                for (size_t i=0; i<grp->getNumChildren(); ++i) {
-                    geode = dynamic_cast<simgear::EffectGeode*>(grp->getChild(i));
-                    if (geode)
-                        geode->setEffect(effect);
+        effect = simgear::makeEffect(effectProp, true, dynamic_cast<const simgear::SGReaderWriterOptions*>(_options.get()));
+        if (iter == lightEffectMap.end() && cacheEffect)
+            lightEffectMap.insert(EffectMap::value_type(_key, effect));
+        else if (cacheEffect)
+            iter->second = effect;
+    } else {
+        effect = iter->second.get();
+    }
+
+    node.setNodeMask( simgear::MODELLIGHT_BIT );
+    simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>(&node);
+    if (geode == 0) {
+        osg::Group* grp = node.asGroup();
+        if (grp != 0) {
+            for (size_t i=0; i<grp->getNumChildren(); ++i) {
+                geode = dynamic_cast<simgear::EffectGeode*>(grp->getChild(i));
+                if (geode) {
+                    geode->setNodeMask( simgear::MODELLIGHT_BIT );
+                    geode->setEffect(effect);
                 }
             }
         }
     }
+
+    if (geode != 0 && _animationValue.valid())
+        geode->setUpdateCallback(new UpdateCallback(_animationValue, _ambient, _diffuse, _specular));
 }