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++
20 #include <osg/ref_ptr>
21 #include <osg/AlphaFunc>
23 #include <osg/Material>
25 #include <osg/NodeCallback>
26 #include <osg/NodeVisitor>
27 #include <osg/StateSet>
28 #include <osg/Texture2D>
31 #include <simgear/props/props.hxx>
32 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/scene/model/persparam.hxx>
35 #include <simgear/scene/util/SGNodeMasks.hxx>
40 // Don't pull in the headers, since we don't need them here.
43 class SGPersonalityBranch;
45 // Has anyone done anything *really* stupid, like making min and max macros?
55 //////////////////////////////////////////////////////////////////////
57 //////////////////////////////////////////////////////////////////////
60 * Abstract base class for all animations.
62 class SGAnimation : public osg::NodeCallback
65 SGAnimation (SGPropertyNode_ptr props, osg::Group * branch);
67 virtual ~SGAnimation ();
70 * Get the SSG branch holding the animation.
72 virtual osg::Group * getBranch () { return _branch; }
75 * Initialize the animation, after children have been added.
80 * Update the animation.
82 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
85 * Restore the state after the animation.
87 virtual void restore();
89 int get_animation_type(void) { return animation_type; }
102 class SGNullAnimation : public SGAnimation
105 SGNullAnimation (SGPropertyNode_ptr props);
106 virtual ~SGNullAnimation ();
111 * A range, or level-of-detail (LOD) animation.
113 class SGRangeAnimation : public SGAnimation
116 SGRangeAnimation (SGPropertyNode *prop_root,
117 SGPropertyNode_ptr props);
118 virtual ~SGRangeAnimation ();
119 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
121 SGPropertyNode_ptr _min_prop;
122 SGPropertyNode_ptr _max_prop;
127 SGCondition * _condition;
132 * Animation to turn and face the screen.
134 class SGBillboardAnimation : public SGAnimation
137 SGBillboardAnimation (SGPropertyNode_ptr props);
138 virtual ~SGBillboardAnimation ();
143 * Animation to select alternative versions of the same object.
145 class SGSelectAnimation : public SGAnimation
148 SGSelectAnimation( SGPropertyNode *prop_root,
149 SGPropertyNode_ptr props );
150 virtual ~SGSelectAnimation ();
151 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
153 SGCondition * _condition;
158 * Animation to spin an object around a center point.
160 * This animation rotates at a specific velocity.
162 class SGSpinAnimation : public SGAnimation
165 SGSpinAnimation( SGPropertyNode *prop_root,
166 SGPropertyNode_ptr props,
167 double sim_time_sec );
168 virtual ~SGSpinAnimation ();
169 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
171 bool _use_personality;
172 SGPropertyNode_ptr _prop;
173 SGPersonalityParameter<double> _factor;
174 SGPersonalityParameter<double> _position_deg;
175 double _last_time_sec;
178 SGCondition * _condition;
183 * Animation to draw objects for a specific amount of time each.
185 class SGTimedAnimation : public SGAnimation
188 SGTimedAnimation (SGPropertyNode_ptr props);
189 virtual ~SGTimedAnimation ();
191 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
193 bool _use_personality;
194 double _duration_sec;
195 double _last_time_sec;
196 double _total_duration_sec;
198 struct DurationSpec {
199 DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
200 DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
203 vector<DurationSpec> _branch_duration_specs;
204 vector<double> _branch_duration_sec;
209 * Animation to rotate an object around a center point.
211 * This animation rotates to a specific position.
213 class SGRotateAnimation : public SGAnimation
216 SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
217 virtual ~SGRotateAnimation ();
218 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
220 SGPropertyNode_ptr _prop;
223 SGInterpTable * _table;
228 double _position_deg;
231 SGCondition * _condition;
236 * Animation to slide along an axis.
238 class SGTranslateAnimation : public SGAnimation
241 SGTranslateAnimation( SGPropertyNode *prop_root,
242 SGPropertyNode_ptr props );
243 virtual ~SGTranslateAnimation ();
244 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
246 bool _use_personality;
247 SGPropertyNode_ptr _prop;
248 SGPersonalityParameter<double> _offset_m;
249 SGPersonalityParameter<double> _factor;
250 SGInterpTable * _table;
257 SGCondition * _condition;
261 * Animation to blend an object.
263 class SGBlendAnimation : public SGAnimation
266 SGBlendAnimation( SGPropertyNode *prop_root,
267 SGPropertyNode_ptr props );
268 virtual ~SGBlendAnimation ();
269 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
271 bool _use_personality;
272 SGPropertyNode_ptr _prop;
273 SGInterpTable * _table;
275 SGPersonalityParameter<double> _offset;
276 SGPersonalityParameter<double> _factor;
282 * Animation to scale an object.
284 class SGScaleAnimation : public SGAnimation
287 SGScaleAnimation( SGPropertyNode *prop_root,
288 SGPropertyNode_ptr props );
289 virtual ~SGScaleAnimation ();
290 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
292 bool _use_personality;
293 SGPropertyNode_ptr _prop;
294 SGPersonalityParameter<double> _x_factor;
295 SGPersonalityParameter<double> _y_factor;
296 SGPersonalityParameter<double> _z_factor;
297 SGPersonalityParameter<double> _x_offset;
298 SGPersonalityParameter<double> _y_offset;
299 SGPersonalityParameter<double> _z_offset;
300 SGInterpTable * _table;
319 * Animation to rotate texture mappings around a center point.
321 * This animation rotates to a specific position.
323 class SGTexRotateAnimation : public SGAnimation
326 SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
327 virtual ~SGTexRotateAnimation ();
328 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
330 SGPropertyNode_ptr _prop;
333 SGInterpTable * _table;
338 double _position_deg;
341 SGCondition * _condition;
342 osg::ref_ptr<osg::TexMat> _texMat;
347 * Animation to slide texture mappings along an axis.
349 class SGTexTranslateAnimation : public SGAnimation
352 SGTexTranslateAnimation( SGPropertyNode *prop_root,
353 SGPropertyNode_ptr props );
354 virtual ~SGTexTranslateAnimation ();
355 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
357 SGPropertyNode_ptr _prop;
362 SGInterpTable * _table;
369 SGCondition * _condition;
370 osg::ref_ptr<osg::TexMat> _texMat;
376 * Classes for handling multiple types of Texture translations on one object
379 class SGTexMultipleAnimation : public SGAnimation
382 SGTexMultipleAnimation( SGPropertyNode *prop_root,
383 SGPropertyNode_ptr props );
384 virtual ~SGTexMultipleAnimation ();
385 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
390 SGPropertyNode_ptr prop;
391 int subtype; // 0=translation, 1=rotation
396 SGInterpTable * table;
405 SGPropertyNode_ptr _prop;
406 TexTransform* _transform;
408 osg::ref_ptr<osg::TexMat> _texMat;
413 * An "animation" to enable the alpha test
415 class SGAlphaTestAnimation : public SGAnimation
418 SGAlphaTestAnimation(SGPropertyNode_ptr props);
419 virtual ~SGAlphaTestAnimation ();
427 * An "animation" to modify material properties
429 class SGMaterialAnimation : public SGAnimation
432 SGMaterialAnimation(SGPropertyNode *prop_root, SGPropertyNode_ptr props,
433 const SGPath &texpath);
434 virtual ~SGMaterialAnimation() {}
436 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
449 float red, green, blue;
452 SGPropertyNode_ptr red_prop;
453 SGPropertyNode_ptr green_prop;
454 SGPropertyNode_ptr blue_prop;
455 SGPropertyNode_ptr factor_prop;
456 SGPropertyNode_ptr offset_prop;
458 inline bool dirty() {
459 return red >= 0.0 || green >= 0.0 || blue >= 0.0;
462 return red_prop || green_prop || blue_prop
463 || factor_prop || offset_prop;
465 inline bool operator!=(ColorSpec& a) {
466 return red != a.red || green != a.green || blue != a.blue
467 || factor != a.factor || offset != a.offset;
470 v[0] = clamp(red * factor + offset);
471 v[1] = clamp(green * factor + offset);
472 v[2] = clamp(blue * factor + offset);
476 inline float clamp(float val) {
477 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0 : val;
486 SGPropertyNode_ptr value_prop;
487 SGPropertyNode_ptr factor_prop;
488 SGPropertyNode_ptr offset_prop;
489 inline bool dirty() { return value >= 0.0; }
490 inline bool live() { return value_prop || factor_prop || offset_prop; }
491 inline bool operator!=(PropSpec& a) {
492 return value != a.value || factor != a.factor || offset != a.offset;
495 SGCondition *_condition;
496 bool _last_condition;
497 SGPropertyNode_ptr _prop_root;
499 SGPath _texture_base;
504 unsigned _static_update;
512 float _thresh; // alpha_clamp (see man glAlphaFunc)
515 SGPropertyNode_ptr _shi_prop;
516 SGPropertyNode_ptr _thresh_prop;
517 SGPropertyNode_ptr _tex_prop;
518 std::vector<osg::ref_ptr<osg::Material> > _materialList;
519 osg::ref_ptr<osg::AlphaFunc> _alphaFunc;
520 osg::ref_ptr<osg::Texture2D> _texture2D;
522 void cloneMaterials(osg::Group *b);
523 void setMaterialBranch(osg::Group *b);
524 void initColorGroup(SGPropertyNode_ptr, ColorSpec *, int flag);
525 void updateColorGroup(ColorSpec *, int flag);
526 inline float clamp(float val, float min = 0.0, float max = 1.0) {
527 return val < min ? min : val > max ? max : val;
529 const char *path(const char *rel) {
530 return (_tmpstr = _prop_base + rel).c_str();
536 * An "animation" that compute a scale according to
537 * the angle between an axis and the view direction
539 class SGFlashAnimation : public SGAnimation
542 SGFlashAnimation(SGPropertyNode_ptr props);
543 virtual ~SGFlashAnimation ();
548 * An animation that compute a scale according to
549 * the distance from a point and the viewer
551 class SGDistScaleAnimation : public SGAnimation
554 SGDistScaleAnimation(SGPropertyNode_ptr props);
555 virtual ~SGDistScaleAnimation ();
559 * An animation to tell wich objects don't cast shadows.
561 class SGShadowAnimation : public SGAnimation
564 SGShadowAnimation ( SGPropertyNode *prop_root,
565 SGPropertyNode_ptr props );
566 virtual ~SGShadowAnimation ();
567 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
568 bool get_condition_value(void);
570 SGCondition * _condition;
571 bool _condition_value;
575 + * An "animation" that replace fixed opengl pipeline by shaders
577 class SGShaderAnimation : public SGAnimation
580 SGShaderAnimation ( SGPropertyNode *prop_root,
581 SGPropertyNode_ptr props );
582 virtual ~SGShaderAnimation ();
584 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
585 bool get_condition_value(void);
587 SGCondition * _condition;
588 bool _condition_value;
591 osg::Vec4 _param_color;
595 SGPropertyNode_ptr _factor_prop;
598 SGPropertyNode_ptr _speed_prop;
599 osg::ref_ptr<osg::Texture2D> _effectTexture;
600 unsigned char *_textureData;
601 GLint _texWidth, _texHeight;
606 #endif // _SG_ANIMATION_HXX