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 "animation.hxx"
23 #include "ConditionNode.hxx"
25 #include <osg/Geometry>
26 #include <osg/MatrixTransform>
27 #include <simgear/scene/material/EffectGeode.hxx>
28 #include <boost/scoped_array.hpp>
30 SGLightAnimation::SGLightAnimation(const SGPropertyNode* configNode,
31 SGPropertyNode* modelRoot) :
32 SGAnimation(configNode, modelRoot)
37 SGLightAnimation::createAnimationGroup(osg::Group& parent)
39 osg::MatrixTransform* grp = new osg::MatrixTransform;
40 osg::Vec3 position( getConfig()->getDoubleValue("position/x-m"),
41 getConfig()->getDoubleValue("position/y-m"),
42 getConfig()->getDoubleValue("position/z-m") );
43 grp->setMatrix(osg::Matrix::translate(position));
45 grp->setNodeMask( simgear::MODELLIGHT_BIT );
50 SGLightAnimation::install(osg::Node& node)
52 SGAnimation::install(node);
54 std::string light_type = getConfig()->getStringValue("light-type");
55 if (light_type == "spot") {
56 osg::Vec3 axis( getConfig()->getDoubleValue("axis/x"),
57 getConfig()->getDoubleValue("axis/y"),
58 getConfig()->getDoubleValue("axis/z") );
59 double l = axis.length();
60 if (l < 0.001) return;
63 osg::Vec3 p1( axis.z(), axis.x(), axis.y() ),
67 float n = getConfig()->getFloatValue("near-m"),
68 f = getConfig()->getFloatValue("far-m"),
69 a1 = getConfig()->getFloatValue("angle-inner-deg"),
70 a2 = getConfig()->getFloatValue("angle-outer-deg"),
71 r1 = n * tan( a2 * SG_DEGREES_TO_RADIANS ),
72 r2 = f * tan( a2 * SG_DEGREES_TO_RADIANS );
74 osg::Geometry* cone = new osg::Geometry;
75 cone->setUseDisplayList(false);
76 osg::Vec3Array* vertices = new osg::Vec3Array(36);
77 (*vertices)[0] = osg::Vec3(0.0,0.0,0.0);
78 for (int i=0; i<16; ++i) {
79 (*vertices)[16-i] = axis*f + p1*r2*cos( i * 2 * M_PI / 16 ) + p2*r2*sin( i * 2 * M_PI / 16 );
81 (*vertices)[17] = (*vertices)[1];
84 (*vertices)[18] = axis*f;
85 for (int i=0; i<16; ++i) {
86 (*vertices)[19+i] = axis*f + p1*r2*cos( i * 2 * M_PI / 16 ) + p2*r2*sin( i * 2 * M_PI / 16 );
88 (*vertices)[35] = (*vertices)[19];
90 osg::Vec4Array* colours = new osg::Vec4Array(1);
91 (*colours)[0].set( getConfig()->getFloatValue("color/red"),
92 getConfig()->getFloatValue("color/green"),
93 getConfig()->getFloatValue("color/blue"),
95 cone->setColorArray(colours);
96 cone->setColorBinding(osg::Geometry::BIND_OVERALL);
98 osg::Vec3Array* normals = new osg::Vec3Array(1);
100 cone->setNormalArray(normals);
101 cone->setNormalBinding(osg::Geometry::BIND_OVERALL);
103 cone->setVertexArray(vertices);
104 cone->addPrimitiveSet( new osg::DrawArrays( GL_TRIANGLE_FAN, 0, 18 ) );
105 cone->addPrimitiveSet( new osg::DrawArrays( GL_TRIANGLE_FAN, 18, 18 ) );
107 simgear::EffectGeode* geode = new simgear::EffectGeode;
108 geode->addDrawable( cone );
110 node.asGroup()->addChild( geode );
111 simgear::Effect *effect = simgear::makeEffect("Effects/light-spot", true);
113 effect->parametersProp->setFloatValue("inner-angle",getConfig()->getFloatValue("angle-inner-deg"));
114 effect->parametersProp->setFloatValue("outer-angle",getConfig()->getFloatValue("angle-outer-deg"));
115 geode->setEffect(effect);