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 std::string& name,
72 const std::string& suffix = "",
73 const SGVec3d& def = SGVec3d::zeros() ) const;
74 void readRotationCenterAndAxis(SGVec3d& center, SGVec3d& axis) const;
76 SGExpressiond* readOffsetValue(const char* tag_name) const;
78 void removeMode(osg::Node& node, osg::StateAttribute::GLMode mode);
79 void removeAttribute(osg::Node& node, osg::StateAttribute::Type type);
80 void removeTextureMode(osg::Node& node, unsigned unit,
81 osg::StateAttribute::GLMode mode);
82 void removeTextureAttribute(osg::Node& node, unsigned unit,
83 osg::StateAttribute::Type type);
84 void setRenderBinToInherit(osg::Node& node);
85 void cloneDrawables(osg::Node& node);
87 std::string getType() const
88 { return std::string(_configNode->getStringValue("type", "")); }
90 const SGPropertyNode* getConfig() const
91 { return _configNode; }
92 SGPropertyNode* getModelRoot() const
93 { return _modelRoot; }
95 const SGCondition* getCondition() const;
97 std::list<std::string> _objectNames;
99 void installInGroup(const std::string& name, osg::Group& group,
100 osg::ref_ptr<osg::Group>& animationGroup);
102 class RemoveModeVisitor;
103 class RemoveAttributeVisitor;
104 class RemoveTextureModeVisitor;
105 class RemoveTextureAttributeVisitor;
106 class BinToInheritVisitor;
107 class DrawableCloneVisitor;
111 SGSharedPtr<SGPropertyNode const> _configNode;
112 SGPropertyNode* _modelRoot;
114 std::list<osg::ref_ptr<osg::Node> > _installedAnimations;
119 //////////////////////////////////////////////////////////////////////
120 // Null animation installer
121 //////////////////////////////////////////////////////////////////////
123 class SGGroupAnimation : public SGAnimation {
125 SGGroupAnimation(const SGPropertyNode*, SGPropertyNode*);
126 virtual osg::Group* createAnimationGroup(osg::Group& parent);
130 //////////////////////////////////////////////////////////////////////
131 // Translate animation installer
132 //////////////////////////////////////////////////////////////////////
134 class SGTranslateAnimation : public SGAnimation {
136 SGTranslateAnimation(const SGPropertyNode* configNode,
137 SGPropertyNode* modelRoot);
138 virtual osg::Group* createAnimationGroup(osg::Group& parent);
140 class UpdateCallback;
141 SGSharedPtr<const SGCondition> _condition;
142 SGSharedPtr<const SGExpressiond> _animationValue;
144 double _initialValue;
148 //////////////////////////////////////////////////////////////////////
149 // Rotate/Spin animation installer
150 //////////////////////////////////////////////////////////////////////
152 class SGRotateAnimation : public SGAnimation {
154 SGRotateAnimation(const SGPropertyNode* configNode,
155 SGPropertyNode* modelRoot);
156 virtual osg::Group* createAnimationGroup(osg::Group& parent);
158 SGSharedPtr<const SGCondition> _condition;
159 SGSharedPtr<const SGExpressiond> _animationValue;
162 double _initialValue;
167 //////////////////////////////////////////////////////////////////////
168 // Scale animation installer
169 //////////////////////////////////////////////////////////////////////
171 class SGScaleAnimation : public SGAnimation {
173 SGScaleAnimation(const SGPropertyNode* configNode,
174 SGPropertyNode* modelRoot);
175 virtual osg::Group* createAnimationGroup(osg::Group& parent);
177 class UpdateCallback;
178 SGSharedPtr<const SGCondition> _condition;
179 SGSharedPtr<const SGExpressiond> _animationValue[3];
180 SGVec3d _initialValue;
185 //////////////////////////////////////////////////////////////////////
186 // dist scale animation installer
187 //////////////////////////////////////////////////////////////////////
189 class SGDistScaleAnimation : public SGAnimation {
191 SGDistScaleAnimation(const SGPropertyNode* configNode,
192 SGPropertyNode* modelRoot);
193 virtual osg::Group* createAnimationGroup(osg::Group& parent);
198 //////////////////////////////////////////////////////////////////////
199 // dist scale animation installer
200 //////////////////////////////////////////////////////////////////////
202 class SGFlashAnimation : public SGAnimation {
204 SGFlashAnimation(const SGPropertyNode* configNode,
205 SGPropertyNode* modelRoot);
206 virtual osg::Group* createAnimationGroup(osg::Group& parent);
212 //////////////////////////////////////////////////////////////////////
213 // dist scale animation installer
214 //////////////////////////////////////////////////////////////////////
216 class SGBillboardAnimation : public SGAnimation {
218 SGBillboardAnimation(const SGPropertyNode* configNode,
219 SGPropertyNode* modelRoot);
220 virtual osg::Group* createAnimationGroup(osg::Group& parent);
225 //////////////////////////////////////////////////////////////////////
226 // Range animation installer
227 //////////////////////////////////////////////////////////////////////
229 class SGRangeAnimation : public SGAnimation {
231 SGRangeAnimation(const SGPropertyNode* configNode,
232 SGPropertyNode* modelRoot);
233 virtual osg::Group* createAnimationGroup(osg::Group& parent);
235 class UpdateCallback;
236 SGSharedPtr<const SGCondition> _condition;
237 SGSharedPtr<const SGExpressiond> _minAnimationValue;
238 SGSharedPtr<const SGExpressiond> _maxAnimationValue;
239 SGVec2d _initialValue;
243 //////////////////////////////////////////////////////////////////////
244 // Select animation installer
245 //////////////////////////////////////////////////////////////////////
247 class SGSelectAnimation : public SGAnimation {
249 SGSelectAnimation(const SGPropertyNode* configNode,
250 SGPropertyNode* modelRoot);
251 virtual osg::Group* createAnimationGroup(osg::Group& parent);
255 //////////////////////////////////////////////////////////////////////
256 // Alpha test animation installer
257 //////////////////////////////////////////////////////////////////////
259 class SGAlphaTestAnimation : public SGAnimation {
261 SGAlphaTestAnimation(const SGPropertyNode* configNode,
262 SGPropertyNode* modelRoot);
263 virtual void install(osg::Node& node);
267 //////////////////////////////////////////////////////////////////////
268 // Blend animation installer
269 //////////////////////////////////////////////////////////////////////
271 class SGBlendAnimation : public SGAnimation {
273 SGBlendAnimation(const SGPropertyNode* configNode,
274 SGPropertyNode* modelRoot);
275 virtual osg::Group* createAnimationGroup(osg::Group& parent);
276 virtual void install(osg::Node& node);
279 class UpdateCallback;
280 SGSharedPtr<SGExpressiond> _animationValue;
284 //////////////////////////////////////////////////////////////////////
285 // Timed animation installer
286 //////////////////////////////////////////////////////////////////////
288 class SGTimedAnimation : public SGAnimation {
290 SGTimedAnimation(const SGPropertyNode* configNode,
291 SGPropertyNode* modelRoot);
292 virtual osg::Group* createAnimationGroup(osg::Group& parent);
294 class UpdateCallback;
298 //////////////////////////////////////////////////////////////////////
299 // Shadow animation installer
300 //////////////////////////////////////////////////////////////////////
302 class SGShadowAnimation : public SGAnimation {
304 SGShadowAnimation(const SGPropertyNode* configNode,
305 SGPropertyNode* modelRoot);
306 virtual osg::Group* createAnimationGroup(osg::Group& parent);
308 class UpdateCallback;
312 //////////////////////////////////////////////////////////////////////
313 // TextureTransform animation
314 //////////////////////////////////////////////////////////////////////
316 class SGTexTransformAnimation : public SGAnimation {
318 SGTexTransformAnimation(const SGPropertyNode* configNode,
319 SGPropertyNode* modelRoot);
320 virtual osg::Group* createAnimationGroup(osg::Group& parent);
325 class UpdateCallback;
326 void appendTexTranslate(const SGPropertyNode* config,
327 UpdateCallback* updateCallback);
328 void appendTexRotate(const SGPropertyNode* config,
329 UpdateCallback* updateCallback);
333 //////////////////////////////////////////////////////////////////////
335 //////////////////////////////////////////////////////////////////////
337 class SGShaderAnimation : public SGAnimation {
339 SGShaderAnimation(const SGPropertyNode* configNode,
340 SGPropertyNode* modelRoot,
341 const osgDB::Options* options);
342 virtual osg::Group* createAnimationGroup(osg::Group& parent);
344 class UpdateCallback;
345 osg::ref_ptr<osg::Texture2D> _effect_texture;
348 //////////////////////////////////////////////////////////////////////
350 //////////////////////////////////////////////////////////////////////
352 class SGLightAnimation : public SGAnimation {
354 SGLightAnimation(const SGPropertyNode* configNode,
355 SGPropertyNode* modelRoot,
356 const osgDB::Options* options,
357 const std::string &path, int i);
358 virtual osg::Group* createAnimationGroup(osg::Group& parent);
359 virtual void install(osg::Node& node);
361 std::string _light_type;
367 SGVec3d _attenuation;
373 class UpdateCallback;
374 friend class UpdateCallback;
375 SGSharedPtr<SGExpressiond> _animationValue;
376 osg::ref_ptr<const osgDB::Options> _options;
379 #endif // _SG_ANIMATION_HXX