1 // Copyright (C) 2011 Frederic Bouvier (fredfgfs01@free.fr)
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.
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.
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.
19 # include <simgear_config.h>
22 #include <boost/lexical_cast.hpp>
24 #include "animation.hxx"
25 #include "ConditionNode.hxx"
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 <simgear/scene/util/SGReaderWriterOptions.hxx>
36 #include <boost/scoped_array.hpp>
37 #include <boost/foreach.hpp>
39 typedef std::map<std::string, osg::observer_ptr<simgear::Effect> > EffectMap;
40 static EffectMap lightEffectMap;
42 #define GET_COLOR_VALUE(n) \
43 SGVec4d( getConfig()->getDoubleValue(n "/r"), \
44 getConfig()->getDoubleValue(n "/g"), \
45 getConfig()->getDoubleValue(n "/b"), \
46 getConfig()->getDoubleValue(n "/a") )
48 class SGLightAnimation::UpdateCallback : public osg::NodeCallback {
50 UpdateCallback(const SGExpressiond* v, SGVec4d a, SGVec4d d, SGVec4d s) :
58 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
60 double dim = _animationValue->getValue();
61 if (dim != _prev_value) {
63 simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>( node );
65 osg::ref_ptr<simgear::Effect> effect = geode->getEffect();
67 SGPropertyNode* params = effect->parametersProp;
68 params->getNode("ambient")->setValue(_ambient * dim);
69 params->getNode("diffuse")->setValue(_diffuse * dim);
70 params->getNode("specular")->setValue(_specular * dim);
71 BOOST_FOREACH(osg::ref_ptr<simgear::Technique>& technique, effect->techniques) {
72 BOOST_FOREACH(osg::ref_ptr<simgear::Pass>& pass, technique->passes) {
73 osg::Uniform* amb = pass->getUniform("Ambient");
75 amb->set(osg::Vec4f(_ambient.x() * dim, _ambient.y() * dim, _ambient.z() * dim, _ambient.w() * dim));
76 osg::Uniform* dif = pass->getUniform("Diffuse");
78 dif->set(osg::Vec4f(_diffuse.x() * dim, _diffuse.y() * dim, _diffuse.z() * dim, _diffuse.w() * dim));
79 osg::Uniform* spe = pass->getUniform("Specular");
81 spe->set(osg::Vec4f(_specular.x() * dim, _specular.y() * dim, _specular.z() * dim, _specular.w() * dim));
92 SGSharedPtr<SGExpressiond const> _animationValue;
96 SGLightAnimation::SGLightAnimation(const SGPropertyNode* configNode,
97 SGPropertyNode* modelRoot,
98 const osgDB::Options* options,
99 const std::string &path, int i) :
100 SGAnimation(configNode, modelRoot),
103 _light_type = getConfig()->getStringValue("light-type");
104 _position = SGVec3d( getConfig()->getDoubleValue("position/x"), getConfig()->getDoubleValue("position/y"), getConfig()->getDoubleValue("position/z") );
105 _direction = SGVec3d( getConfig()->getDoubleValue("direction/x"), getConfig()->getDoubleValue("direction/y"), getConfig()->getDoubleValue("direction/z") );
106 double l = length(_direction);
107 if (l > 0.001) _direction /= l;
108 _ambient = GET_COLOR_VALUE("ambient");
109 _diffuse = GET_COLOR_VALUE("diffuse");
110 _specular = GET_COLOR_VALUE("specular");
111 _attenuation = SGVec3d( getConfig()->getDoubleValue("attenuation/c"), getConfig()->getDoubleValue("attenuation/l"), getConfig()->getDoubleValue("attenuation/q") );
112 _exponent = getConfig()->getDoubleValue("exponent");
113 _cutoff = getConfig()->getDoubleValue("cutoff");
114 _near = getConfig()->getDoubleValue("near-m");
115 _far = getConfig()->getDoubleValue("far-m");
116 _key = path + ";" + boost::lexical_cast<std::string>( i );
118 SGConstPropertyNode_ptr dim_factor = configNode->getChild("dim-factor");
119 if (dim_factor.valid()) {
120 _animationValue = read_value(dim_factor, modelRoot, "", 0, 1);
125 SGLightAnimation::createAnimationGroup(osg::Group& parent)
127 osg::Group* grp = new osg::Group;
128 grp->setName("light animation node");
129 parent.addChild(grp);
130 grp->setNodeMask( simgear::MODELLIGHT_BIT );
135 SGLightAnimation::install(osg::Node& node)
137 SGAnimation::install(node);
139 bool cacheEffect = false;
140 osg::ref_ptr<simgear::Effect> effect;
141 EffectMap::iterator iter = lightEffectMap.end();
142 if (!_animationValue.valid()) { // Effects with animated properties should be singletons
143 iter = lightEffectMap.find(_key);
147 if (iter == lightEffectMap.end() || !iter->second.lock(effect)) {
148 SGPropertyNode_ptr effectProp = new SGPropertyNode;
149 if (_light_type == "spot") {
150 makeChild(effectProp, "name")->setStringValue(_key);
151 makeChild(effectProp, "inherits-from")->setStringValue("Effects/light-spot");
153 if (_animationValue.valid())
154 dim = _animationValue->getValue();
156 SGPropertyNode* params = makeChild(effectProp, "parameters");
157 params->getNode("position",true)->setValue(SGVec4d(_position.x(),_position.y(),_position.z(),1.0));
158 params->getNode("direction",true)->setValue(SGVec4d(_direction.x(),_direction.y(),_direction.z(),0.0));
159 params->getNode("ambient",true)->setValue(_ambient * dim);
160 params->getNode("diffuse",true)->setValue(_diffuse * dim);
161 params->getNode("specular",true)->setValue(_specular * dim);
162 params->getNode("attenuation",true)->setValue(_attenuation);
163 params->getNode("exponent",true)->setValue(_exponent);
164 params->getNode("cutoff",true)->setValue(_cutoff);
165 params->getNode("cosCutoff",true)->setValue( cos(_cutoff*SG_DEGREES_TO_RADIANS) );
166 params->getNode("near",true)->setValue(_near);
167 params->getNode("far",true)->setValue(_far);
169 else if (_light_type == "point") {
170 makeChild(effectProp, "name")->setStringValue(_key);
171 makeChild(effectProp, "inherits-from")->setStringValue("Effects/light-point");
173 if (_animationValue.valid())
174 dim = _animationValue->getValue();
176 SGPropertyNode* params = makeChild(effectProp, "parameters");
177 params->getNode("position",true)->setValue(SGVec4d(_position.x(),_position.y(),_position.z(),1.0));
178 params->getNode("ambient",true)->setValue(_ambient * dim);
179 params->getNode("diffuse",true)->setValue(_diffuse * dim);
180 params->getNode("specular",true)->setValue(_specular * dim);
181 params->getNode("attenuation",true)->setValue(_attenuation);
182 params->getNode("near",true)->setValue(_near);
183 params->getNode("far",true)->setValue(_far);
188 effect = simgear::makeEffect(effectProp, true, dynamic_cast<const simgear::SGReaderWriterOptions*>(_options.get()));
189 if (iter == lightEffectMap.end() && cacheEffect)
190 lightEffectMap.insert(EffectMap::value_type(_key, effect));
191 else if (cacheEffect)
192 iter->second = effect;
194 effect = iter->second.get();
197 node.setNodeMask( simgear::MODELLIGHT_BIT );
198 simgear::EffectGeode* geode = dynamic_cast<simgear::EffectGeode*>(&node);
200 osg::Group* grp = node.asGroup();
202 for (size_t i=0; i<grp->getNumChildren(); ++i) {
203 geode = dynamic_cast<simgear::EffectGeode*>(grp->getChild(i));
205 geode->setNodeMask( simgear::MODELLIGHT_BIT );
206 geode->setEffect(effect);
212 if (geode != 0 && _animationValue.valid())
213 geode->setUpdateCallback(new UpdateCallback(_animationValue, _ambient, _diffuse, _specular));