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 SGVec3d readTranslateAxis(const SGPropertyNode* configNode);
41 * Base class for animation installers
43 class SGAnimation : protected osg::NodeVisitor {
45 SGAnimation(const SGPropertyNode* configNode, SGPropertyNode* modelRoot);
46 virtual ~SGAnimation();
48 static bool animate(osg::Node* node, const SGPropertyNode* configNode,
49 SGPropertyNode* modelRoot,
50 const osgDB::Options* options,
51 const string &path, int i);
54 void apply(osg::Node* node);
56 virtual void install(osg::Node& node);
57 virtual osg::Group* createAnimationGroup(osg::Group& parent);
59 virtual void apply(osg::Group& group);
62 * Read a 3d vector from the configuration property node.
64 * Reads values from @a name/[xyz]@a prefix and defaults to the according
65 * value of @a def for each value which is not set.
67 * @param name Name of the root node containing all coordinates
68 * @param suffix Suffix appended to each child node (x,y,z)
69 * @param def Vector containing default values
71 SGVec3d readVec3( const std::string& name,
72 const std::string& suffix = "",
73 const SGVec3d& def = SGVec3d::zeros() ) const;
74 void readRotationCenterAndAxis(SGVec3d& center, SGVec3d& axis) const;
76 void removeMode(osg::Node& node, osg::StateAttribute::GLMode mode);
77 void removeAttribute(osg::Node& node, osg::StateAttribute::Type type);
78 void removeTextureMode(osg::Node& node, unsigned unit,
79 osg::StateAttribute::GLMode mode);
80 void removeTextureAttribute(osg::Node& node, unsigned unit,
81 osg::StateAttribute::Type type);
82 void setRenderBinToInherit(osg::Node& node);
83 void cloneDrawables(osg::Node& node);
85 std::string getType() const
86 { return std::string(_configNode->getStringValue("type", "")); }
88 const SGPropertyNode* getConfig() const
89 { return _configNode; }
90 SGPropertyNode* getModelRoot() const
91 { return _modelRoot; }
93 const SGCondition* getCondition() const;
95 std::list<std::string> _objectNames;
97 void installInGroup(const std::string& name, osg::Group& group,
98 osg::ref_ptr<osg::Group>& animationGroup);
100 class RemoveModeVisitor;
101 class RemoveAttributeVisitor;
102 class RemoveTextureModeVisitor;
103 class RemoveTextureAttributeVisitor;
104 class BinToInheritVisitor;
105 class DrawableCloneVisitor;
109 SGSharedPtr<SGPropertyNode const> _configNode;
110 SGPropertyNode* _modelRoot;
112 std::list<osg::ref_ptr<osg::Node> > _installedAnimations;
117 //////////////////////////////////////////////////////////////////////
118 // Null animation installer
119 //////////////////////////////////////////////////////////////////////
121 class SGGroupAnimation : public SGAnimation {
123 SGGroupAnimation(const SGPropertyNode*, SGPropertyNode*);
124 virtual osg::Group* createAnimationGroup(osg::Group& parent);
128 //////////////////////////////////////////////////////////////////////
129 // Translate animation installer
130 //////////////////////////////////////////////////////////////////////
132 class SGTranslateAnimation : public SGAnimation {
134 SGTranslateAnimation(const SGPropertyNode* configNode,
135 SGPropertyNode* modelRoot);
136 virtual osg::Group* createAnimationGroup(osg::Group& parent);
138 class UpdateCallback;
139 SGSharedPtr<const SGCondition> _condition;
140 SGSharedPtr<const SGExpressiond> _animationValue;
142 double _initialValue;
146 //////////////////////////////////////////////////////////////////////
147 // Rotate/Spin animation installer
148 //////////////////////////////////////////////////////////////////////
150 class SGRotateAnimation : public SGAnimation {
152 SGRotateAnimation(const SGPropertyNode* configNode,
153 SGPropertyNode* modelRoot);
154 virtual osg::Group* createAnimationGroup(osg::Group& parent);
156 SGSharedPtr<const SGCondition> _condition;
157 SGSharedPtr<const SGExpressiond> _animationValue;
160 double _initialValue;
165 //////////////////////////////////////////////////////////////////////
166 // Scale animation installer
167 //////////////////////////////////////////////////////////////////////
169 class SGScaleAnimation : public SGAnimation {
171 SGScaleAnimation(const SGPropertyNode* configNode,
172 SGPropertyNode* modelRoot);
173 virtual osg::Group* createAnimationGroup(osg::Group& parent);
175 class UpdateCallback;
176 SGSharedPtr<const SGCondition> _condition;
177 SGSharedPtr<const SGExpressiond> _animationValue[3];
178 SGVec3d _initialValue;
183 //////////////////////////////////////////////////////////////////////
184 // dist scale animation installer
185 //////////////////////////////////////////////////////////////////////
187 class SGDistScaleAnimation : public SGAnimation {
189 SGDistScaleAnimation(const SGPropertyNode* configNode,
190 SGPropertyNode* modelRoot);
191 virtual osg::Group* createAnimationGroup(osg::Group& parent);
196 //////////////////////////////////////////////////////////////////////
197 // dist scale animation installer
198 //////////////////////////////////////////////////////////////////////
200 class SGFlashAnimation : public SGAnimation {
202 SGFlashAnimation(const SGPropertyNode* configNode,
203 SGPropertyNode* modelRoot);
204 virtual osg::Group* createAnimationGroup(osg::Group& parent);
210 //////////////////////////////////////////////////////////////////////
211 // dist scale animation installer
212 //////////////////////////////////////////////////////////////////////
214 class SGBillboardAnimation : public SGAnimation {
216 SGBillboardAnimation(const SGPropertyNode* configNode,
217 SGPropertyNode* modelRoot);
218 virtual osg::Group* createAnimationGroup(osg::Group& parent);
223 //////////////////////////////////////////////////////////////////////
224 // Range animation installer
225 //////////////////////////////////////////////////////////////////////
227 class SGRangeAnimation : public SGAnimation {
229 SGRangeAnimation(const SGPropertyNode* configNode,
230 SGPropertyNode* modelRoot);
231 virtual osg::Group* createAnimationGroup(osg::Group& parent);
233 class UpdateCallback;
234 SGSharedPtr<const SGCondition> _condition;
235 SGSharedPtr<const SGExpressiond> _minAnimationValue;
236 SGSharedPtr<const SGExpressiond> _maxAnimationValue;
237 SGVec2d _initialValue;
241 //////////////////////////////////////////////////////////////////////
242 // Select animation installer
243 //////////////////////////////////////////////////////////////////////
245 class SGSelectAnimation : public SGAnimation {
247 SGSelectAnimation(const SGPropertyNode* configNode,
248 SGPropertyNode* modelRoot);
249 virtual osg::Group* createAnimationGroup(osg::Group& parent);
253 //////////////////////////////////////////////////////////////////////
254 // Alpha test animation installer
255 //////////////////////////////////////////////////////////////////////
257 class SGAlphaTestAnimation : public SGAnimation {
259 SGAlphaTestAnimation(const SGPropertyNode* configNode,
260 SGPropertyNode* modelRoot);
261 virtual void install(osg::Node& node);
265 //////////////////////////////////////////////////////////////////////
266 // Blend animation installer
267 //////////////////////////////////////////////////////////////////////
269 class SGBlendAnimation : public SGAnimation {
271 SGBlendAnimation(const SGPropertyNode* configNode,
272 SGPropertyNode* modelRoot);
273 virtual osg::Group* createAnimationGroup(osg::Group& parent);
274 virtual void install(osg::Node& node);
277 class UpdateCallback;
278 SGSharedPtr<SGExpressiond> _animationValue;
282 //////////////////////////////////////////////////////////////////////
283 // Timed animation installer
284 //////////////////////////////////////////////////////////////////////
286 class SGTimedAnimation : public SGAnimation {
288 SGTimedAnimation(const SGPropertyNode* configNode,
289 SGPropertyNode* modelRoot);
290 virtual osg::Group* createAnimationGroup(osg::Group& parent);
292 class UpdateCallback;
296 //////////////////////////////////////////////////////////////////////
297 // Shadow animation installer
298 //////////////////////////////////////////////////////////////////////
300 class SGShadowAnimation : public SGAnimation {
302 SGShadowAnimation(const SGPropertyNode* configNode,
303 SGPropertyNode* modelRoot);
304 virtual osg::Group* createAnimationGroup(osg::Group& parent);
306 class UpdateCallback;
310 //////////////////////////////////////////////////////////////////////
311 // TextureTransform animation
312 //////////////////////////////////////////////////////////////////////
314 class SGTexTransformAnimation : public SGAnimation {
316 SGTexTransformAnimation(const SGPropertyNode* configNode,
317 SGPropertyNode* modelRoot);
318 virtual osg::Group* createAnimationGroup(osg::Group& parent);
323 class UpdateCallback;
324 void appendTexTranslate(const SGPropertyNode* config,
325 UpdateCallback* updateCallback);
326 void appendTexRotate(const SGPropertyNode* config,
327 UpdateCallback* updateCallback);
331 //////////////////////////////////////////////////////////////////////
333 //////////////////////////////////////////////////////////////////////
335 class SGShaderAnimation : public SGAnimation {
337 SGShaderAnimation(const SGPropertyNode* configNode,
338 SGPropertyNode* modelRoot,
339 const osgDB::Options* options);
340 virtual osg::Group* createAnimationGroup(osg::Group& parent);
342 class UpdateCallback;
343 osg::ref_ptr<osg::Texture2D> _effect_texture;
346 //////////////////////////////////////////////////////////////////////
348 //////////////////////////////////////////////////////////////////////
350 class SGLightAnimation : public SGAnimation {
352 SGLightAnimation(const SGPropertyNode* configNode,
353 SGPropertyNode* modelRoot,
354 const osgDB::Options* options,
355 const string &path, int i);
356 virtual osg::Group* createAnimationGroup(osg::Group& parent);
357 virtual void install(osg::Node& node);
365 SGVec3d _attenuation;
371 class UpdateCallback;
372 friend class UpdateCallback;
373 SGSharedPtr<SGExpressiond> _animationValue;
374 osg::ref_ptr<const osgDB::Options> _options;
377 #endif // _SG_ANIMATION_HXX