2 // animation.hxx - classes to manage model animation.
3 // Written by David Megginson, started 2002.
5 // This file is in the Public Domain, and comes with no warranty.
7 #ifndef _SG_ANIMATION_HXX
8 #define _SG_ANIMATION_HXX 1
11 # error This library requires C++
14 #include <osg/ref_ptr>
17 #include <osg/NodeVisitor>
18 #include <osg/Texture2D>
19 #include <osgDB/ReaderWriter>
21 #include <simgear/scene/util/SGNodeMasks.hxx>
22 #include <simgear/props/props.hxx>
23 #include <simgear/props/condition.hxx>
24 #include <simgear/structure/SGExpression.hxx>
26 // Has anyone done anything *really* stupid, like making min and max macros?
35 read_value(const SGPropertyNode* configNode, SGPropertyNode* modelRoot,
36 const char* unit, double defMin, double defMax);
38 void readRotationCenterAndAxis(const SGPropertyNode* configNode, SGVec3d& center, SGVec3d& axis);
39 SGVec3d readTranslateAxis(const SGPropertyNode* configNode);
41 //////////////////////////////////////////////////////////////////////
42 // Base class for animation installers
43 //////////////////////////////////////////////////////////////////////
45 class SGAnimation : protected osg::NodeVisitor {
47 SGAnimation(const SGPropertyNode* configNode, SGPropertyNode* modelRoot);
48 virtual ~SGAnimation();
50 static bool animate(osg::Node* node, const SGPropertyNode* configNode,
51 SGPropertyNode* modelRoot,
52 const osgDB::Options* options,
53 const string &path, int i);
56 void apply(osg::Node* node);
58 virtual void install(osg::Node& node);
59 virtual osg::Group* createAnimationGroup(osg::Group& parent);
61 virtual void apply(osg::Group& group);
63 void removeMode(osg::Node& node, osg::StateAttribute::GLMode mode);
64 void removeAttribute(osg::Node& node, osg::StateAttribute::Type type);
65 void removeTextureMode(osg::Node& node, unsigned unit,
66 osg::StateAttribute::GLMode mode);
67 void removeTextureAttribute(osg::Node& node, unsigned unit,
68 osg::StateAttribute::Type type);
69 void setRenderBinToInherit(osg::Node& node);
70 void cloneDrawables(osg::Node& node);
72 std::string getType() const
73 { return std::string(_configNode->getStringValue("type", "")); }
75 const SGPropertyNode* getConfig() const
76 { return _configNode; }
77 SGPropertyNode* getModelRoot() const
78 { return _modelRoot; }
80 const SGCondition* getCondition() const;
83 void installInGroup(const std::string& name, osg::Group& group,
84 osg::ref_ptr<osg::Group>& animationGroup);
86 class RemoveModeVisitor;
87 class RemoveAttributeVisitor;
88 class RemoveTextureModeVisitor;
89 class RemoveTextureAttributeVisitor;
90 class BinToInheritVisitor;
91 class DrawableCloneVisitor;
95 SGSharedPtr<SGPropertyNode const> _configNode;
96 SGPropertyNode* _modelRoot;
97 std::list<std::string> _objectNames;
98 std::list<osg::ref_ptr<osg::Node> > _installedAnimations;
103 //////////////////////////////////////////////////////////////////////
104 // Null animation installer
105 //////////////////////////////////////////////////////////////////////
107 class SGGroupAnimation : public SGAnimation {
109 SGGroupAnimation(const SGPropertyNode*, SGPropertyNode*);
110 virtual osg::Group* createAnimationGroup(osg::Group& parent);
114 //////////////////////////////////////////////////////////////////////
115 // Translate animation installer
116 //////////////////////////////////////////////////////////////////////
118 class SGTranslateAnimation : public SGAnimation {
120 SGTranslateAnimation(const SGPropertyNode* configNode,
121 SGPropertyNode* modelRoot);
122 virtual osg::Group* createAnimationGroup(osg::Group& parent);
124 class UpdateCallback;
125 SGSharedPtr<const SGCondition> _condition;
126 SGSharedPtr<const SGExpressiond> _animationValue;
128 double _initialValue;
132 //////////////////////////////////////////////////////////////////////
133 // Rotate/Spin animation installer
134 //////////////////////////////////////////////////////////////////////
136 class SGRotateAnimation : public SGAnimation {
138 SGRotateAnimation(const SGPropertyNode* configNode,
139 SGPropertyNode* modelRoot);
140 virtual osg::Group* createAnimationGroup(osg::Group& parent);
142 SGSharedPtr<const SGCondition> _condition;
143 SGSharedPtr<const SGExpressiond> _animationValue;
146 double _initialValue;
151 //////////////////////////////////////////////////////////////////////
152 // Scale animation installer
153 //////////////////////////////////////////////////////////////////////
155 class SGScaleAnimation : public SGAnimation {
157 SGScaleAnimation(const SGPropertyNode* configNode,
158 SGPropertyNode* modelRoot);
159 virtual osg::Group* createAnimationGroup(osg::Group& parent);
161 class UpdateCallback;
162 SGSharedPtr<const SGCondition> _condition;
163 SGSharedPtr<const SGExpressiond> _animationValue[3];
164 SGVec3d _initialValue;
169 //////////////////////////////////////////////////////////////////////
170 // dist scale animation installer
171 //////////////////////////////////////////////////////////////////////
173 class SGDistScaleAnimation : public SGAnimation {
175 SGDistScaleAnimation(const SGPropertyNode* configNode,
176 SGPropertyNode* modelRoot);
177 virtual osg::Group* createAnimationGroup(osg::Group& parent);
182 //////////////////////////////////////////////////////////////////////
183 // dist scale animation installer
184 //////////////////////////////////////////////////////////////////////
186 class SGFlashAnimation : public SGAnimation {
188 SGFlashAnimation(const SGPropertyNode* configNode,
189 SGPropertyNode* modelRoot);
190 virtual osg::Group* createAnimationGroup(osg::Group& parent);
196 //////////////////////////////////////////////////////////////////////
197 // dist scale animation installer
198 //////////////////////////////////////////////////////////////////////
200 class SGBillboardAnimation : public SGAnimation {
202 SGBillboardAnimation(const SGPropertyNode* configNode,
203 SGPropertyNode* modelRoot);
204 virtual osg::Group* createAnimationGroup(osg::Group& parent);
209 //////////////////////////////////////////////////////////////////////
210 // Range animation installer
211 //////////////////////////////////////////////////////////////////////
213 class SGRangeAnimation : public SGAnimation {
215 SGRangeAnimation(const SGPropertyNode* configNode,
216 SGPropertyNode* modelRoot);
217 virtual osg::Group* createAnimationGroup(osg::Group& parent);
219 class UpdateCallback;
220 SGSharedPtr<const SGCondition> _condition;
221 SGSharedPtr<const SGExpressiond> _minAnimationValue;
222 SGSharedPtr<const SGExpressiond> _maxAnimationValue;
223 SGVec2d _initialValue;
227 //////////////////////////////////////////////////////////////////////
228 // Select animation installer
229 //////////////////////////////////////////////////////////////////////
231 class SGSelectAnimation : public SGAnimation {
233 SGSelectAnimation(const SGPropertyNode* configNode,
234 SGPropertyNode* modelRoot);
235 virtual osg::Group* createAnimationGroup(osg::Group& parent);
239 //////////////////////////////////////////////////////////////////////
240 // Alpha test animation installer
241 //////////////////////////////////////////////////////////////////////
243 class SGAlphaTestAnimation : public SGAnimation {
245 SGAlphaTestAnimation(const SGPropertyNode* configNode,
246 SGPropertyNode* modelRoot);
247 virtual void install(osg::Node& node);
251 //////////////////////////////////////////////////////////////////////
252 // Blend animation installer
253 //////////////////////////////////////////////////////////////////////
255 class SGBlendAnimation : public SGAnimation {
257 SGBlendAnimation(const SGPropertyNode* configNode,
258 SGPropertyNode* modelRoot);
259 virtual osg::Group* createAnimationGroup(osg::Group& parent);
260 virtual void install(osg::Node& node);
263 class UpdateCallback;
264 SGSharedPtr<SGExpressiond> _animationValue;
268 //////////////////////////////////////////////////////////////////////
269 // Timed animation installer
270 //////////////////////////////////////////////////////////////////////
272 class SGTimedAnimation : public SGAnimation {
274 SGTimedAnimation(const SGPropertyNode* configNode,
275 SGPropertyNode* modelRoot);
276 virtual osg::Group* createAnimationGroup(osg::Group& parent);
278 class UpdateCallback;
282 //////////////////////////////////////////////////////////////////////
283 // Shadow animation installer
284 //////////////////////////////////////////////////////////////////////
286 class SGShadowAnimation : public SGAnimation {
288 SGShadowAnimation(const SGPropertyNode* configNode,
289 SGPropertyNode* modelRoot);
290 virtual osg::Group* createAnimationGroup(osg::Group& parent);
292 class UpdateCallback;
296 //////////////////////////////////////////////////////////////////////
297 // TextureTransform animation
298 //////////////////////////////////////////////////////////////////////
300 class SGTexTransformAnimation : public SGAnimation {
302 SGTexTransformAnimation(const SGPropertyNode* configNode,
303 SGPropertyNode* modelRoot);
304 virtual osg::Group* createAnimationGroup(osg::Group& parent);
309 class UpdateCallback;
310 void appendTexTranslate(const SGPropertyNode* config,
311 UpdateCallback* updateCallback);
312 void appendTexRotate(const SGPropertyNode* config,
313 UpdateCallback* updateCallback);
317 //////////////////////////////////////////////////////////////////////
319 //////////////////////////////////////////////////////////////////////
321 class SGShaderAnimation : public SGAnimation {
323 SGShaderAnimation(const SGPropertyNode* configNode,
324 SGPropertyNode* modelRoot,
325 const osgDB::Options* options);
326 virtual osg::Group* createAnimationGroup(osg::Group& parent);
328 class UpdateCallback;
329 osg::ref_ptr<osg::Texture2D> _effect_texture;
332 //////////////////////////////////////////////////////////////////////
334 //////////////////////////////////////////////////////////////////////
336 class SGLightAnimation : public SGAnimation {
338 SGLightAnimation(const SGPropertyNode* configNode,
339 SGPropertyNode* modelRoot,
340 const osgDB::Options* options,
341 const string &path, int i);
342 virtual osg::Group* createAnimationGroup(osg::Group& parent);
343 virtual void install(osg::Node& node);
351 SGVec3d _attenuation;
357 class UpdateCallback;
358 friend class UpdateCallback;
359 SGSharedPtr<SGExpressiond> _animationValue;
360 osg::ref_ptr<const osgDB::Options> _options;
363 #endif // _SG_ANIMATION_HXX