]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGLightAnimation.cxx
Remove ambient component of additional lights
[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/scene/material/EffectGeode.hxx>
30 #include <simgear/scene/material/Technique.hxx>
31 #include <simgear/scene/material/Pass.hxx>
32 #include <simgear/scene/util/CopyOp.hxx>
33 #include <simgear/scene/util/OsgMath.hxx>
34 #include <boost/scoped_array.hpp>
35 #include <boost/foreach.hpp>
36
37 typedef std::map<string, osg::ref_ptr<simgear::Effect> > EffectMap;
38 static EffectMap lightEffectMap;
39
40 #define GET_COLOR_VALUE(n) \
41     SGVec4d( getConfig()->getDoubleValue(n "/r"), \
42                 getConfig()->getDoubleValue(n "/g"), \
43                 getConfig()->getDoubleValue(n "/b"), \
44                 getConfig()->getDoubleValue(n "/a") )
45
46 class SGLightAnimation::UpdateCallback : public osg::NodeCallback {
47 public:
48     UpdateCallback(string k, const SGExpressiond* v, SGVec4d d, SGVec4d s) :
49         _key(k),
50         _diffuse(d),
51         _specular(s),
52         _animationValue(v)
53     {
54         _prev_values[k] = -1;
55     }
56     virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
57     {
58         double dim = _animationValue->getValue();
59         if (dim != _prev_values[_key]) {
60             _prev_values[_key] = dim;
61
62             EffectMap::iterator iter = lightEffectMap.find(_key);
63             if (iter != lightEffectMap.end()) {
64                 simgear::Effect* effect = iter->second;
65                 SGPropertyNode* params = effect->parametersProp;
66                 params->getNode("diffuse")->setValue(_diffuse * dim);
67                 params->getNode("specular")->setValue(_specular * dim);
68                 BOOST_FOREACH(osg::ref_ptr<simgear::Technique>& technique, effect->techniques)
69                 {
70                     BOOST_FOREACH(osg::ref_ptr<simgear::Pass>& pass, technique->passes)
71                     {
72                         osg::Uniform* dif = pass->getUniform("Diffuse");
73                         if (dif)
74                             dif->set(toOsg(_diffuse) * dim);
75                         osg::Uniform* spe = pass->getUniform("Specular");
76                         if (spe)
77                             spe->set(toOsg(_specular) * dim);
78                     }
79                 }
80             }
81         }
82         traverse(node, nv);
83     }
84 public:
85     string _key;
86     SGVec4d _diffuse;
87     SGVec4d _specular;
88     SGSharedPtr<SGExpressiond const> _animationValue;
89
90     typedef std::map<string, double> PrevValueMap;
91     static PrevValueMap _prev_values;
92 };
93 SGLightAnimation::UpdateCallback::PrevValueMap SGLightAnimation::UpdateCallback::_prev_values;
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     _diffuse = GET_COLOR_VALUE("diffuse");
106     _specular = GET_COLOR_VALUE("specular");
107     _attenuation = SGVec3d( getConfig()->getDoubleValue("attenuation/c"), getConfig()->getDoubleValue("attenuation/l"), getConfig()->getDoubleValue("attenuation/q") );
108     _exponent = getConfig()->getDoubleValue("exponent");
109     _cutoff = getConfig()->getDoubleValue("cutoff");
110     _near = getConfig()->getDoubleValue("near-m");
111     _far = getConfig()->getDoubleValue("far-m");
112     _key = path + ";" + boost::lexical_cast<string>( i );
113
114     SGConstPropertyNode_ptr dim_factor = configNode->getChild("dim-factor");
115     if (dim_factor.valid()) {
116         _animationValue = read_value(dim_factor, modelRoot, "", 0, 1);
117     }
118 }
119
120 osg::Group*
121 SGLightAnimation::createAnimationGroup(osg::Group& parent)
122 {
123     osg::Group* grp = new osg::Group;
124     grp->setName("light animation node");
125     if (_animationValue.valid())
126         grp->setUpdateCallback(new UpdateCallback(_key, _animationValue, _diffuse, _specular));
127     parent.addChild(grp);
128     grp->setNodeMask( simgear::MODELLIGHT_BIT );
129     return grp;
130 }
131
132 void
133 SGLightAnimation::install(osg::Node& node)
134 {
135     SGAnimation::install(node);
136
137     if (_light_type == "spot") {
138
139         simgear::Effect* effect = 0;
140         EffectMap::iterator iter = lightEffectMap.find(_key);
141         if (iter == lightEffectMap.end()) {
142             SGPropertyNode_ptr effectProp = new SGPropertyNode;
143             makeChild(effectProp, "name")->setStringValue(_key);
144             makeChild(effectProp, "inherits-from")->setStringValue("Effects/light-spot");
145             double dim = 1.0;
146             if (_animationValue.valid())
147                 dim = _animationValue->getValue();
148
149             SGPropertyNode* params = makeChild(effectProp, "parameters");
150             params->getNode("position",true)->setValue(SGVec4d(_position.x(),_position.y(),_position.z(),1.0));
151             params->getNode("direction",true)->setValue(SGVec4d(_direction.x(),_direction.y(),_direction.z(),0.0));
152             params->getNode("diffuse",true)->setValue(_diffuse * dim);
153             params->getNode("specular",true)->setValue(_specular * dim);
154             params->getNode("attenuation",true)->setValue(_attenuation);
155             params->getNode("exponent",true)->setValue(_exponent);
156             params->getNode("cutoff",true)->setValue(_cutoff);
157             params->getNode("cosCutoff",true)->setValue( cos(_cutoff*SG_DEGREES_TO_RADIANS) );
158             params->getNode("near",true)->setValue(_near);
159             params->getNode("far",true)->setValue(_far);
160
161             effect = simgear::makeEffect(effectProp, true);
162             lightEffectMap.insert(EffectMap::value_type(_key, effect));
163         } else {
164             effect = iter->second.get();
165         }
166
167         node.setNodeMask( simgear::MODELLIGHT_BIT );
168         simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>(&node);
169         if (geode == 0) {
170             osg::Group* grp = node.asGroup();
171             if (grp != 0) {
172                 for (size_t i=0; i<grp->getNumChildren(); ++i) {
173                     geode = dynamic_cast<simgear::EffectGeode*>(grp->getChild(i));
174                     if (geode)
175                         geode->setEffect(effect);
176                 }
177             }
178         }
179     }
180     else if (_light_type == "point") {
181
182         simgear::Effect* effect = 0;
183         EffectMap::iterator iter = lightEffectMap.find(_key);
184         if (iter == lightEffectMap.end()) {
185             SGPropertyNode_ptr effectProp = new SGPropertyNode;
186             makeChild(effectProp, "name")->setStringValue(_key);
187             makeChild(effectProp, "inherits-from")->setStringValue("Effects/light-point");
188             double dim = 1.0;
189             if (_animationValue.valid())
190                 dim = _animationValue->getValue();
191
192             SGPropertyNode* params = makeChild(effectProp, "parameters");
193             params->getNode("position",true)->setValue(SGVec4d(_position.x(),_position.y(),_position.z(),1.0));
194             params->getNode("diffuse",true)->setValue(_diffuse * dim);
195             params->getNode("specular",true)->setValue(_specular * dim);
196             params->getNode("attenuation",true)->setValue(_attenuation);
197             params->getNode("near",true)->setValue(_near);
198             params->getNode("far",true)->setValue(_far);
199
200             effect = simgear::makeEffect(effectProp, true);
201             lightEffectMap.insert(EffectMap::value_type(_key, effect));
202         } else {
203             effect = iter->second.get();
204         }
205
206         node.setNodeMask( simgear::MODELLIGHT_BIT );
207         simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>(&node);
208         if (geode == 0) {
209             osg::Group* grp = node.asGroup();
210             if (grp != 0) {
211                 for (size_t i=0; i<grp->getNumChildren(); ++i) {
212                     geode = dynamic_cast<simgear::EffectGeode*>(grp->getChild(i));
213                     if (geode)
214                         geode->setEffect(effect);
215                 }
216             }
217         }
218     }
219 }