]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/SGLightAnimation.cxx
Implement a cache of light effects
[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 <boost/scoped_array.hpp>
31 #include <simgear/scene/util/CopyOp.hxx>
32
33 typedef std::map<string, osg::ref_ptr<simgear::Effect> > EffectMap;
34 static EffectMap lightEffectMap;
35
36 #define GET_COLOR_VALUE(n) \
37     SGVec4d( getConfig()->getDoubleValue(n "/r"), \
38                 getConfig()->getDoubleValue(n "/g"), \
39                 getConfig()->getDoubleValue(n "/b"), \
40                 getConfig()->getDoubleValue(n "/a") )
41
42 SGLightAnimation::SGLightAnimation(const SGPropertyNode* configNode,
43                                    SGPropertyNode* modelRoot,
44                                    const string &path, int i) :
45     SGAnimation(configNode, modelRoot)
46 {
47     _position = SGVec3d( getConfig()->getDoubleValue("position/x"), getConfig()->getDoubleValue("position/y"), getConfig()->getDoubleValue("position/z") );
48     _direction = SGVec3d( getConfig()->getDoubleValue("direction/x"), getConfig()->getDoubleValue("direction/y"), getConfig()->getDoubleValue("direction/z") );
49     double l = length(_direction);
50     if (l > 0.001) _direction /= l;
51     _ambient = GET_COLOR_VALUE("ambient");
52     _diffuse = GET_COLOR_VALUE("diffuse");
53     _specular = GET_COLOR_VALUE("specular");
54     _attenuation = SGVec3d( getConfig()->getDoubleValue("attenuation/c"), getConfig()->getDoubleValue("attenuation/l"), getConfig()->getDoubleValue("attenuation/q") );
55     _exponent = getConfig()->getDoubleValue("exponent");
56     _cutoff = getConfig()->getDoubleValue("cutoff");
57     _near = getConfig()->getDoubleValue("near-m");
58     _far = getConfig()->getDoubleValue("far-m");
59     _key = path + ";" + boost::lexical_cast<string>( i );
60 }
61
62 osg::Group*
63 SGLightAnimation::createAnimationGroup(osg::Group& parent)
64 {
65     osg::MatrixTransform* grp = new osg::MatrixTransform;
66     grp->setMatrix(osg::Matrix::translate(toOsg(_position)));
67     parent.addChild(grp);
68     grp->setNodeMask( simgear::MODELLIGHT_BIT );
69     return grp;
70 }
71
72 void
73 SGLightAnimation::install(osg::Node& node)
74 {
75     SGAnimation::install(node);
76
77     std::string light_type = getConfig()->getStringValue("light-type");
78     if (light_type == "spot") {
79
80         SGVec3d p1( _direction.z(), _direction.x(), _direction.y() ),
81         p2 = cross( _direction, p1 );
82         p1 = cross( p2, _direction );
83
84         float r2 = _far * tan( _cutoff * SG_DEGREES_TO_RADIANS );
85
86         osg::Geometry* cone = new osg::Geometry;
87 cone->setUseDisplayList(false);
88         osg::Vec3Array* vertices = new osg::Vec3Array(34);
89         (*vertices)[0] = osg::Vec3(0.0,0.0,0.0);
90         for (int i=0; i<16; ++i) {
91             (*vertices)[16-i]    = toOsg(_direction)*_far + toOsg(p1)*r2*cos( i * 2 * M_PI / 16 ) + toOsg(p2)*r2*sin( i * 2 * M_PI / 16 );
92         }
93         (*vertices)[17] = (*vertices)[1];
94
95         // Bottom
96         for (int i=0; i<16; ++i) {
97             (*vertices)[18+i]    = toOsg(_direction)*_far + toOsg(p1)*r2*cos( i * 2 * M_PI / 16 ) + toOsg(p2)*r2*sin( i * 2 * M_PI / 16 );
98         }
99
100         osg::Vec4Array* colours = new osg::Vec4Array(1);
101         (*colours)[0] = toOsg(_diffuse);
102         cone->setColorArray(colours);
103         cone->setColorBinding(osg::Geometry::BIND_OVERALL);
104
105         osg::Vec3Array* normals = new osg::Vec3Array(1);
106         (*normals)[0] = toOsg(_direction);
107         cone->setNormalArray(normals);
108         cone->setNormalBinding(osg::Geometry::BIND_OVERALL);
109
110         cone->setVertexArray(vertices);
111         cone->addPrimitiveSet( new osg::DrawArrays( GL_TRIANGLE_FAN, 0, 18 ) );
112         cone->addPrimitiveSet( new osg::DrawArrays( GL_TRIANGLE_FAN, 18, 16 ) );
113
114         simgear::EffectGeode* geode = new simgear::EffectGeode;
115         geode->addDrawable( cone );
116
117         node.asGroup()->addChild( geode );
118
119         simgear::Effect* effect = 0;
120         EffectMap::iterator iter = lightEffectMap.find(_key);
121         if (iter == lightEffectMap.end()) {
122             SGPropertyNode_ptr effectProp = new SGPropertyNode;
123             makeChild(effectProp, "inherits-from")->setStringValue("Effects/light-spot");
124             SGPropertyNode* params = makeChild(effectProp, "parameters");
125             params->getNode("light-spot/position",true)->setValue(SGVec4d(_position.x(),_position.y(),_position.z(),1.0));
126             params->getNode("light-spot/direction",true)->setValue(SGVec4d(_direction.x(),_direction.y(),_direction.z(),0.0));
127             params->getNode("light-spot/ambient",true)->setValue(_ambient);
128             params->getNode("light-spot/diffuse",true)->setValue(_diffuse);
129             params->getNode("light-spot/specular",true)->setValue(_specular);
130             params->getNode("light-spot/attenuation",true)->setValue(_attenuation);
131             params->getNode("light-spot/exponent",true)->setValue(_exponent);
132             params->getNode("light-spot/cutoff",true)->setValue(_cutoff);
133             params->getNode("light-spot/cosCutoff",true)->setValue( cos(_cutoff*SG_DEGREES_TO_RADIANS) );
134             params->getNode("light-spot/near",true)->setValue(_near);
135             params->getNode("light-spot/far",true)->setValue(_far);
136
137             effect = simgear::makeEffect(effectProp, true);
138             lightEffectMap.insert(EffectMap::value_type(_key, effect));
139         } else {
140             effect = iter->second.get();
141         }
142         geode->setEffect(effect);
143     }
144 }