]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGLightAnimation.cxx
bvh: Implement paging for osg derived bvh trees.
[simgear.git] / simgear / scene / model / SGLightAnimation.cxx
1 // Copyright (C) 2011 Frederic Bouvier (fredfgfs01@free.fr)
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21
22 #include <boost/lexical_cast.hpp>
23
24 #include "animation.hxx"
25 #include "ConditionNode.hxx"
26
27 #include <osg/Geometry>
28 #include <osg/MatrixTransform>
29 #include <simgear/props/vectorPropTemplates.hxx>
30 #include <simgear/scene/material/EffectGeode.hxx>
31 #include <simgear/scene/material/Technique.hxx>
32 #include <simgear/scene/material/Pass.hxx>
33 #include <simgear/scene/util/CopyOp.hxx>
34 #include <simgear/scene/util/OsgMath.hxx>
35 #include <boost/scoped_array.hpp>
36 #include <boost/foreach.hpp>
37
38 typedef std::map<string, osg::observer_ptr<simgear::Effect> > EffectMap;
39 static EffectMap lightEffectMap;
40
41 #define GET_COLOR_VALUE(n) \
42     SGVec4d( getConfig()->getDoubleValue(n "/r"), \
43                 getConfig()->getDoubleValue(n "/g"), \
44                 getConfig()->getDoubleValue(n "/b"), \
45                 getConfig()->getDoubleValue(n "/a") )
46
47 class SGLightAnimation::UpdateCallback : public osg::NodeCallback {
48 public:
49     UpdateCallback(const SGExpressiond* v, SGVec4d a, SGVec4d d, SGVec4d s) :
50         _ambient(a),
51         _diffuse(d),
52         _specular(s),
53         _animationValue(v)
54     {
55         _prev_value = -1;
56     }
57     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
58     {
59         double dim = _animationValue->getValue();
60         if (dim != _prev_value) {
61             _prev_value = dim;
62             simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>( node );
63             if (geode != 0) {
64                 osg::ref_ptr<simgear::Effect> effect = geode->getEffect();
65
66                 SGPropertyNode* params = effect->parametersProp;
67                 params->getNode("ambient")->setValue(_ambient * dim);
68                 params->getNode("diffuse")->setValue(_diffuse * dim);
69                 params->getNode("specular")->setValue(_specular * dim);
70                 BOOST_FOREACH(osg::ref_ptr<simgear::Technique>& technique, effect->techniques) {
71                     BOOST_FOREACH(osg::ref_ptr<simgear::Pass>& pass, technique->passes) {
72                         osg::Uniform* amb = pass->getUniform("Ambient");
73                         if (amb)
74                             amb->set(osg::Vec4f(_ambient.x() * dim, _ambient.y() * dim, _ambient.z() * dim, _ambient.w() * dim));
75                         osg::Uniform* dif = pass->getUniform("Diffuse");
76                         if (dif)
77                             dif->set(osg::Vec4f(_diffuse.x() * dim, _diffuse.y() * dim, _diffuse.z() * dim, _diffuse.w() * dim));
78                         osg::Uniform* spe = pass->getUniform("Specular");
79                         if (spe)
80                             spe->set(osg::Vec4f(_specular.x() * dim, _specular.y() * dim, _specular.z() * dim, _specular.w() * dim));
81                     }
82                 }
83             }
84         }
85         traverse(node, nv);
86     }
87 public:
88     SGVec4d _ambient;
89     SGVec4d _diffuse;
90     SGVec4d _specular;
91     SGSharedPtr<SGExpressiond const> _animationValue;
92     double _prev_value;
93 };
94
95 SGLightAnimation::SGLightAnimation(const SGPropertyNode* configNode,
96                                    SGPropertyNode* modelRoot,
97                                    const string &path, int i) :
98     SGAnimation(configNode, modelRoot)
99 {
100     _light_type = getConfig()->getStringValue("light-type");
101     _position = SGVec3d( getConfig()->getDoubleValue("position/x"), getConfig()->getDoubleValue("position/y"), getConfig()->getDoubleValue("position/z") );
102     _direction = SGVec3d( getConfig()->getDoubleValue("direction/x"), getConfig()->getDoubleValue("direction/y"), getConfig()->getDoubleValue("direction/z") );
103     double l = length(_direction);
104     if (l > 0.001) _direction /= l;
105     _ambient = GET_COLOR_VALUE("ambient");
106     _diffuse = GET_COLOR_VALUE("diffuse");
107     _specular = GET_COLOR_VALUE("specular");
108     _attenuation = SGVec3d( getConfig()->getDoubleValue("attenuation/c"), getConfig()->getDoubleValue("attenuation/l"), getConfig()->getDoubleValue("attenuation/q") );
109     _exponent = getConfig()->getDoubleValue("exponent");
110     _cutoff = getConfig()->getDoubleValue("cutoff");
111     _near = getConfig()->getDoubleValue("near-m");
112     _far = getConfig()->getDoubleValue("far-m");
113     _key = path + ";" + boost::lexical_cast<string>( i );
114
115     SGConstPropertyNode_ptr dim_factor = configNode->getChild("dim-factor");
116     if (dim_factor.valid()) {
117         _animationValue = read_value(dim_factor, modelRoot, "", 0, 1);
118     }
119 }
120
121 osg::Group*
122 SGLightAnimation::createAnimationGroup(osg::Group& parent)
123 {
124     osg::Group* grp = new osg::Group;
125     grp->setName("light animation node");
126     parent.addChild(grp);
127     grp->setNodeMask( simgear::MODELLIGHT_BIT );
128     return grp;
129 }
130
131 void
132 SGLightAnimation::install(osg::Node& node)
133 {
134     SGAnimation::install(node);
135
136     bool cacheEffect = false;
137     osg::ref_ptr<simgear::Effect> effect;
138     EffectMap::iterator iter = lightEffectMap.end();
139     if (!_animationValue.valid()) { // Effects with animated properties should be singletons
140         iter = lightEffectMap.find(_key);
141         cacheEffect = true;
142     }
143
144     if (iter == lightEffectMap.end() || !iter->second.lock(effect)) {
145         SGPropertyNode_ptr effectProp = new SGPropertyNode;
146         if (_light_type == "spot") {
147             makeChild(effectProp, "name")->setStringValue(_key);
148             makeChild(effectProp, "inherits-from")->setStringValue("Effects/light-spot");
149             double dim = 1.0;
150             if (_animationValue.valid())
151                 dim = _animationValue->getValue();
152
153             SGPropertyNode* params = makeChild(effectProp, "parameters");
154             params->getNode("position",true)->setValue(SGVec4d(_position.x(),_position.y(),_position.z(),1.0));
155             params->getNode("direction",true)->setValue(SGVec4d(_direction.x(),_direction.y(),_direction.z(),0.0));
156             params->getNode("ambient",true)->setValue(_ambient * dim);
157             params->getNode("diffuse",true)->setValue(_diffuse * dim);
158             params->getNode("specular",true)->setValue(_specular * dim);
159             params->getNode("attenuation",true)->setValue(_attenuation);
160             params->getNode("exponent",true)->setValue(_exponent);
161             params->getNode("cutoff",true)->setValue(_cutoff);
162             params->getNode("cosCutoff",true)->setValue( cos(_cutoff*SG_DEGREES_TO_RADIANS) );
163             params->getNode("near",true)->setValue(_near);
164             params->getNode("far",true)->setValue(_far);
165         }
166         else if (_light_type == "point") {
167             makeChild(effectProp, "name")->setStringValue(_key);
168             makeChild(effectProp, "inherits-from")->setStringValue("Effects/light-point");
169             double dim = 1.0;
170             if (_animationValue.valid())
171                 dim = _animationValue->getValue();
172
173             SGPropertyNode* params = makeChild(effectProp, "parameters");
174             params->getNode("position",true)->setValue(SGVec4d(_position.x(),_position.y(),_position.z(),1.0));
175             params->getNode("ambient",true)->setValue(_ambient * dim);
176             params->getNode("diffuse",true)->setValue(_diffuse * dim);
177             params->getNode("specular",true)->setValue(_specular * dim);
178             params->getNode("attenuation",true)->setValue(_attenuation);
179             params->getNode("near",true)->setValue(_near);
180             params->getNode("far",true)->setValue(_far);
181         } else {
182             return;
183         }
184
185         effect = simgear::makeEffect(effectProp, true);
186         if (iter == lightEffectMap.end() && cacheEffect)
187             lightEffectMap.insert(EffectMap::value_type(_key, effect));
188         else if (cacheEffect)
189             iter->second = effect;
190     } else {
191         effect = iter->second.get();
192     }
193
194     node.setNodeMask( simgear::MODELLIGHT_BIT );
195     simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>(&node);
196     if (geode == 0) {
197         osg::Group* grp = node.asGroup();
198         if (grp != 0) {
199             for (size_t i=0; i<grp->getNumChildren(); ++i) {
200                 geode = dynamic_cast<simgear::EffectGeode*>(grp->getChild(i));
201                 if (geode) {
202                     geode->setNodeMask( simgear::MODELLIGHT_BIT );
203                     geode->setEffect(effect);
204                 }
205             }
206         }
207     }
208
209     if (geode != 0 && _animationValue.valid())
210         geode->setUpdateCallback(new UpdateCallback(_animationValue, _ambient, _diffuse, _specular));
211 }