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 std::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 SGPropertyNode& cfg,
72 const std::string& name,
73 const std::string& suffix = "",
74 const SGVec3d& def = SGVec3d::zeros() ) const;
76 SGVec3d readVec3( const std::string& name,
77 const std::string& suffix = "",
78 const SGVec3d& def = SGVec3d::zeros() ) const;
80 void readRotationCenterAndAxis(SGVec3d& center, SGVec3d& axis) const;
82 SGExpressiond* readOffsetValue(const char* tag_name) const;
84 void removeMode(osg::Node& node, osg::StateAttribute::GLMode mode);
85 void removeAttribute(osg::Node& node, osg::StateAttribute::Type type);
86 void removeTextureMode(osg::Node& node, unsigned unit,
87 osg::StateAttribute::GLMode mode);
88 void removeTextureAttribute(osg::Node& node, unsigned unit,
89 osg::StateAttribute::Type type);
90 void setRenderBinToInherit(osg::Node& node);
91 void cloneDrawables(osg::Node& node);
93 std::string getType() const
94 { return std::string(_configNode->getStringValue("type", "")); }
96 const SGPropertyNode* getConfig() const
97 { return _configNode; }
98 SGPropertyNode* getModelRoot() const
99 { return _modelRoot; }
101 const SGCondition* getCondition() const;
103 std::list<std::string> _objectNames;
105 void installInGroup(const std::string& name, osg::Group& group,
106 osg::ref_ptr<osg::Group>& animationGroup);
108 class RemoveModeVisitor;
109 class RemoveAttributeVisitor;
110 class RemoveTextureModeVisitor;
111 class RemoveTextureAttributeVisitor;
112 class BinToInheritVisitor;
113 class DrawableCloneVisitor;
117 SGSharedPtr<SGPropertyNode const> _configNode;
118 SGPropertyNode* _modelRoot;
120 std::list<osg::ref_ptr<osg::Node> > _installedAnimations;
125 //////////////////////////////////////////////////////////////////////
126 // Null animation installer
127 //////////////////////////////////////////////////////////////////////
129 class SGGroupAnimation : public SGAnimation {
131 SGGroupAnimation(const SGPropertyNode*, SGPropertyNode*);
132 virtual osg::Group* createAnimationGroup(osg::Group& parent);
136 //////////////////////////////////////////////////////////////////////
137 // Translate animation installer
138 //////////////////////////////////////////////////////////////////////
140 class SGTranslateAnimation : public SGAnimation {
142 SGTranslateAnimation(const SGPropertyNode* configNode,
143 SGPropertyNode* modelRoot);
144 virtual osg::Group* createAnimationGroup(osg::Group& parent);
146 class UpdateCallback;
147 SGSharedPtr<const SGCondition> _condition;
148 SGSharedPtr<const SGExpressiond> _animationValue;
150 double _initialValue;
154 //////////////////////////////////////////////////////////////////////
155 // Rotate/Spin animation installer
156 //////////////////////////////////////////////////////////////////////
158 class SGRotateAnimation : public SGAnimation {
160 SGRotateAnimation(const SGPropertyNode* configNode,
161 SGPropertyNode* modelRoot);
162 virtual osg::Group* createAnimationGroup(osg::Group& parent);
164 SGSharedPtr<const SGCondition> _condition;
165 SGSharedPtr<const SGExpressiond> _animationValue;
168 double _initialValue;
173 //////////////////////////////////////////////////////////////////////
174 // Scale animation installer
175 //////////////////////////////////////////////////////////////////////
177 class SGScaleAnimation : public SGAnimation {
179 SGScaleAnimation(const SGPropertyNode* configNode,
180 SGPropertyNode* modelRoot);
181 virtual osg::Group* createAnimationGroup(osg::Group& parent);
183 class UpdateCallback;
184 SGSharedPtr<const SGCondition> _condition;
185 SGSharedPtr<const SGExpressiond> _animationValue[3];
186 SGVec3d _initialValue;
191 //////////////////////////////////////////////////////////////////////
192 // dist scale animation installer
193 //////////////////////////////////////////////////////////////////////
195 class SGDistScaleAnimation : public SGAnimation {
197 SGDistScaleAnimation(const SGPropertyNode* configNode,
198 SGPropertyNode* modelRoot);
199 virtual osg::Group* createAnimationGroup(osg::Group& parent);
204 //////////////////////////////////////////////////////////////////////
205 // dist scale animation installer
206 //////////////////////////////////////////////////////////////////////
208 class SGFlashAnimation : public SGAnimation {
210 SGFlashAnimation(const SGPropertyNode* configNode,
211 SGPropertyNode* modelRoot);
212 virtual osg::Group* createAnimationGroup(osg::Group& parent);
218 //////////////////////////////////////////////////////////////////////
219 // dist scale animation installer
220 //////////////////////////////////////////////////////////////////////
222 class SGBillboardAnimation : public SGAnimation {
224 SGBillboardAnimation(const SGPropertyNode* configNode,
225 SGPropertyNode* modelRoot);
226 virtual osg::Group* createAnimationGroup(osg::Group& parent);
231 //////////////////////////////////////////////////////////////////////
232 // Range animation installer
233 //////////////////////////////////////////////////////////////////////
235 class SGRangeAnimation : public SGAnimation {
237 SGRangeAnimation(const SGPropertyNode* configNode,
238 SGPropertyNode* modelRoot);
239 virtual osg::Group* createAnimationGroup(osg::Group& parent);
241 class UpdateCallback;
242 SGSharedPtr<const SGCondition> _condition;
243 SGSharedPtr<const SGExpressiond> _minAnimationValue;
244 SGSharedPtr<const SGExpressiond> _maxAnimationValue;
245 SGVec2d _initialValue;
249 //////////////////////////////////////////////////////////////////////
250 // Select animation installer
251 //////////////////////////////////////////////////////////////////////
253 class SGSelectAnimation : public SGAnimation {
255 SGSelectAnimation(const SGPropertyNode* configNode,
256 SGPropertyNode* modelRoot);
257 virtual osg::Group* createAnimationGroup(osg::Group& parent);
261 //////////////////////////////////////////////////////////////////////
262 // Alpha test animation installer
263 //////////////////////////////////////////////////////////////////////
265 class SGAlphaTestAnimation : public SGAnimation {
267 SGAlphaTestAnimation(const SGPropertyNode* configNode,
268 SGPropertyNode* modelRoot);
269 virtual void install(osg::Node& node);
273 //////////////////////////////////////////////////////////////////////
274 // Blend animation installer
275 //////////////////////////////////////////////////////////////////////
277 class SGBlendAnimation : public SGAnimation {
279 SGBlendAnimation(const SGPropertyNode* configNode,
280 SGPropertyNode* modelRoot);
281 virtual osg::Group* createAnimationGroup(osg::Group& parent);
282 virtual void install(osg::Node& node);
285 class UpdateCallback;
286 SGSharedPtr<SGExpressiond> _animationValue;
290 //////////////////////////////////////////////////////////////////////
291 // Timed animation installer
292 //////////////////////////////////////////////////////////////////////
294 class SGTimedAnimation : public SGAnimation {
296 SGTimedAnimation(const SGPropertyNode* configNode,
297 SGPropertyNode* modelRoot);
298 virtual osg::Group* createAnimationGroup(osg::Group& parent);
300 class UpdateCallback;
304 //////////////////////////////////////////////////////////////////////
305 // Shadow animation installer
306 //////////////////////////////////////////////////////////////////////
308 class SGShadowAnimation : public SGAnimation {
310 SGShadowAnimation(const SGPropertyNode* configNode,
311 SGPropertyNode* modelRoot);
312 virtual osg::Group* createAnimationGroup(osg::Group& parent);
314 class UpdateCallback;
318 //////////////////////////////////////////////////////////////////////
319 // TextureTransform animation
320 //////////////////////////////////////////////////////////////////////
322 class SGTexTransformAnimation : public SGAnimation {
324 SGTexTransformAnimation(const SGPropertyNode* configNode,
325 SGPropertyNode* modelRoot);
326 virtual osg::Group* createAnimationGroup(osg::Group& parent);
332 class UpdateCallback;
334 SGExpressiond* readValue( const SGPropertyNode& cfg,
335 const std::string& suffix = "" );
337 void appendTexTranslate( const SGPropertyNode& cfg,
338 UpdateCallback* updateCallback);
339 void appendTexRotate( const SGPropertyNode& cfg,
340 UpdateCallback* updateCallback);
341 void appendTexTrapezoid( const SGPropertyNode& cfg,
342 UpdateCallback* updateCallback);
346 //////////////////////////////////////////////////////////////////////
348 //////////////////////////////////////////////////////////////////////
350 class SGShaderAnimation : public SGAnimation {
352 SGShaderAnimation(const SGPropertyNode* configNode,
353 SGPropertyNode* modelRoot,
354 const osgDB::Options* options);
355 virtual osg::Group* createAnimationGroup(osg::Group& parent);
357 class UpdateCallback;
358 osg::ref_ptr<osg::Texture2D> _effect_texture;
361 //////////////////////////////////////////////////////////////////////
363 //////////////////////////////////////////////////////////////////////
365 class SGLightAnimation : public SGAnimation {
367 SGLightAnimation(const SGPropertyNode* configNode,
368 SGPropertyNode* modelRoot,
369 const osgDB::Options* options,
370 const std::string &path, int i);
371 virtual osg::Group* createAnimationGroup(osg::Group& parent);
372 virtual void install(osg::Node& node);
374 std::string _light_type;
380 SGVec3d _attenuation;
386 class UpdateCallback;
387 friend class UpdateCallback;
388 SGSharedPtr<SGExpressiond> _animationValue;
389 osg::ref_ptr<const osgDB::Options> _options;
392 #endif // _SG_ANIMATION_HXX