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;
82 std::list<std::string> _objectNames;
84 void installInGroup(const std::string& name, osg::Group& group,
85 osg::ref_ptr<osg::Group>& animationGroup);
87 class RemoveModeVisitor;
88 class RemoveAttributeVisitor;
89 class RemoveTextureModeVisitor;
90 class RemoveTextureAttributeVisitor;
91 class BinToInheritVisitor;
92 class DrawableCloneVisitor;
96 SGSharedPtr<SGPropertyNode const> _configNode;
97 SGPropertyNode* _modelRoot;
99 std::list<osg::ref_ptr<osg::Node> > _installedAnimations;
104 //////////////////////////////////////////////////////////////////////
105 // Null animation installer
106 //////////////////////////////////////////////////////////////////////
108 class SGGroupAnimation : public SGAnimation {
110 SGGroupAnimation(const SGPropertyNode*, SGPropertyNode*);
111 virtual osg::Group* createAnimationGroup(osg::Group& parent);
115 //////////////////////////////////////////////////////////////////////
116 // Translate animation installer
117 //////////////////////////////////////////////////////////////////////
119 class SGTranslateAnimation : public SGAnimation {
121 SGTranslateAnimation(const SGPropertyNode* configNode,
122 SGPropertyNode* modelRoot);
123 virtual osg::Group* createAnimationGroup(osg::Group& parent);
125 class UpdateCallback;
126 SGSharedPtr<const SGCondition> _condition;
127 SGSharedPtr<const SGExpressiond> _animationValue;
129 double _initialValue;
133 //////////////////////////////////////////////////////////////////////
134 // Rotate/Spin animation installer
135 //////////////////////////////////////////////////////////////////////
137 class SGRotateAnimation : public SGAnimation {
139 SGRotateAnimation(const SGPropertyNode* configNode,
140 SGPropertyNode* modelRoot);
141 virtual osg::Group* createAnimationGroup(osg::Group& parent);
143 SGSharedPtr<const SGCondition> _condition;
144 SGSharedPtr<const SGExpressiond> _animationValue;
147 double _initialValue;
152 //////////////////////////////////////////////////////////////////////
153 // Scale animation installer
154 //////////////////////////////////////////////////////////////////////
156 class SGScaleAnimation : public SGAnimation {
158 SGScaleAnimation(const SGPropertyNode* configNode,
159 SGPropertyNode* modelRoot);
160 virtual osg::Group* createAnimationGroup(osg::Group& parent);
162 class UpdateCallback;
163 SGSharedPtr<const SGCondition> _condition;
164 SGSharedPtr<const SGExpressiond> _animationValue[3];
165 SGVec3d _initialValue;
170 //////////////////////////////////////////////////////////////////////
171 // dist scale animation installer
172 //////////////////////////////////////////////////////////////////////
174 class SGDistScaleAnimation : public SGAnimation {
176 SGDistScaleAnimation(const SGPropertyNode* configNode,
177 SGPropertyNode* modelRoot);
178 virtual osg::Group* createAnimationGroup(osg::Group& parent);
183 //////////////////////////////////////////////////////////////////////
184 // dist scale animation installer
185 //////////////////////////////////////////////////////////////////////
187 class SGFlashAnimation : public SGAnimation {
189 SGFlashAnimation(const SGPropertyNode* configNode,
190 SGPropertyNode* modelRoot);
191 virtual osg::Group* createAnimationGroup(osg::Group& parent);
197 //////////////////////////////////////////////////////////////////////
198 // dist scale animation installer
199 //////////////////////////////////////////////////////////////////////
201 class SGBillboardAnimation : public SGAnimation {
203 SGBillboardAnimation(const SGPropertyNode* configNode,
204 SGPropertyNode* modelRoot);
205 virtual osg::Group* createAnimationGroup(osg::Group& parent);
210 //////////////////////////////////////////////////////////////////////
211 // Range animation installer
212 //////////////////////////////////////////////////////////////////////
214 class SGRangeAnimation : public SGAnimation {
216 SGRangeAnimation(const SGPropertyNode* configNode,
217 SGPropertyNode* modelRoot);
218 virtual osg::Group* createAnimationGroup(osg::Group& parent);
220 class UpdateCallback;
221 SGSharedPtr<const SGCondition> _condition;
222 SGSharedPtr<const SGExpressiond> _minAnimationValue;
223 SGSharedPtr<const SGExpressiond> _maxAnimationValue;
224 SGVec2d _initialValue;
228 //////////////////////////////////////////////////////////////////////
229 // Select animation installer
230 //////////////////////////////////////////////////////////////////////
232 class SGSelectAnimation : public SGAnimation {
234 SGSelectAnimation(const SGPropertyNode* configNode,
235 SGPropertyNode* modelRoot);
236 virtual osg::Group* createAnimationGroup(osg::Group& parent);
240 //////////////////////////////////////////////////////////////////////
241 // Alpha test animation installer
242 //////////////////////////////////////////////////////////////////////
244 class SGAlphaTestAnimation : public SGAnimation {
246 SGAlphaTestAnimation(const SGPropertyNode* configNode,
247 SGPropertyNode* modelRoot);
248 virtual void install(osg::Node& node);
252 //////////////////////////////////////////////////////////////////////
253 // Blend animation installer
254 //////////////////////////////////////////////////////////////////////
256 class SGBlendAnimation : public SGAnimation {
258 SGBlendAnimation(const SGPropertyNode* configNode,
259 SGPropertyNode* modelRoot);
260 virtual osg::Group* createAnimationGroup(osg::Group& parent);
261 virtual void install(osg::Node& node);
264 class UpdateCallback;
265 SGSharedPtr<SGExpressiond> _animationValue;
269 //////////////////////////////////////////////////////////////////////
270 // Timed animation installer
271 //////////////////////////////////////////////////////////////////////
273 class SGTimedAnimation : public SGAnimation {
275 SGTimedAnimation(const SGPropertyNode* configNode,
276 SGPropertyNode* modelRoot);
277 virtual osg::Group* createAnimationGroup(osg::Group& parent);
279 class UpdateCallback;
283 //////////////////////////////////////////////////////////////////////
284 // Shadow animation installer
285 //////////////////////////////////////////////////////////////////////
287 class SGShadowAnimation : public SGAnimation {
289 SGShadowAnimation(const SGPropertyNode* configNode,
290 SGPropertyNode* modelRoot);
291 virtual osg::Group* createAnimationGroup(osg::Group& parent);
293 class UpdateCallback;
297 //////////////////////////////////////////////////////////////////////
298 // TextureTransform animation
299 //////////////////////////////////////////////////////////////////////
301 class SGTexTransformAnimation : public SGAnimation {
303 SGTexTransformAnimation(const SGPropertyNode* configNode,
304 SGPropertyNode* modelRoot);
305 virtual osg::Group* createAnimationGroup(osg::Group& parent);
310 class UpdateCallback;
311 void appendTexTranslate(const SGPropertyNode* config,
312 UpdateCallback* updateCallback);
313 void appendTexRotate(const SGPropertyNode* config,
314 UpdateCallback* updateCallback);
318 //////////////////////////////////////////////////////////////////////
320 //////////////////////////////////////////////////////////////////////
322 class SGShaderAnimation : public SGAnimation {
324 SGShaderAnimation(const SGPropertyNode* configNode,
325 SGPropertyNode* modelRoot,
326 const osgDB::Options* options);
327 virtual osg::Group* createAnimationGroup(osg::Group& parent);
329 class UpdateCallback;
330 osg::ref_ptr<osg::Texture2D> _effect_texture;
333 //////////////////////////////////////////////////////////////////////
335 //////////////////////////////////////////////////////////////////////
337 class SGLightAnimation : public SGAnimation {
339 SGLightAnimation(const SGPropertyNode* configNode,
340 SGPropertyNode* modelRoot,
341 const osgDB::Options* options,
342 const string &path, int i);
343 virtual osg::Group* createAnimationGroup(osg::Group& parent);
344 virtual void install(osg::Node& node);
352 SGVec3d _attenuation;
358 class UpdateCallback;
359 friend class UpdateCallback;
360 SGSharedPtr<SGExpressiond> _animationValue;
361 osg::ref_ptr<const osgDB::Options> _options;
364 #endif // _SG_ANIMATION_HXX