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>
22 #include <osg/ColorMatrix>
24 #include <osg/Material>
26 #include <osg/NodeCallback>
27 #include <osg/NodeVisitor>
28 #include <osg/StateSet>
29 #include <osg/Texture2D>
32 #include <simgear/props/props.hxx>
33 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/scene/model/persparam.hxx>
36 #include <simgear/scene/util/SGNodeMasks.hxx>
41 // Don't pull in the headers, since we don't need them here.
44 class SGPersonalityBranch;
46 // Has anyone done anything *really* stupid, like making min and max macros?
56 //////////////////////////////////////////////////////////////////////
58 //////////////////////////////////////////////////////////////////////
61 * Abstract base class for all animations.
63 class SGAnimation : public osg::NodeCallback
66 enum PersonalityVar { INIT_SPIN, LAST_TIME_SEC_SPIN, FACTOR_SPIN,
68 INIT_TIMED, LAST_TIME_SEC_TIMED, TOTAL_DURATION_SEC_TIMED,
69 BRANCH_DURATION_SEC_TIMED, STEP_TIMED,
70 INIT_TRANSLATE, FACTOR_TRANSLATE, OFFSET_TRANSLATE,
71 INIT_BLEND, FACTOR_BLEND, OFFSET_BLEND,
72 INIT_SCALE, X_FACTOR_SCALE, Y_FACTOR_SCALE, Z_FACTOR_SCALE,
73 X_OFFSET_SCALE, Y_OFFSET_SCALE, Z_OFFSET_SCALE };
75 SGAnimation (SGPropertyNode_ptr props, osg::Group * branch);
77 virtual ~SGAnimation ();
80 * Get the SSG branch holding the animation.
82 virtual osg::Group * getBranch () { return _branch; }
85 * Initialize the animation, after children have been added.
90 * Update the animation.
92 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
94 // note, callback is responsible for scenegraph traversal so
95 // should always include call traverse(node,nv) to ensure
96 // that the rest of cullbacks and the scene graph are traversed.
100 virtual int update();
103 * Restore the state after the animation.
105 virtual void restore();
108 * Set the value of sim_time_sec. This needs to be called every
109 * frame in order for the time based animations to work correctly.
111 static void set_sim_time_sec( double val ) { sim_time_sec = val; }
114 * Current personality branch : enable animation to behave differently
115 * for similar objects
117 static SGPersonalityBranch *current_object;
119 int get_animation_type(void) { return animation_type; }
123 static double sim_time_sec;
134 class SGNullAnimation : public SGAnimation
137 SGNullAnimation (SGPropertyNode_ptr props);
138 virtual ~SGNullAnimation ();
143 * A range, or level-of-detail (LOD) animation.
145 class SGRangeAnimation : public SGAnimation
148 SGRangeAnimation (SGPropertyNode *prop_root,
149 SGPropertyNode_ptr props);
150 virtual ~SGRangeAnimation ();
151 virtual int update();
153 SGPropertyNode_ptr _min_prop;
154 SGPropertyNode_ptr _max_prop;
159 SGCondition * _condition;
164 * Animation to turn and face the screen.
166 class SGBillboardAnimation : public SGAnimation
169 SGBillboardAnimation (SGPropertyNode_ptr props);
170 virtual ~SGBillboardAnimation ();
175 * Animation to select alternative versions of the same object.
177 class SGSelectAnimation : public SGAnimation
180 SGSelectAnimation( SGPropertyNode *prop_root,
181 SGPropertyNode_ptr props );
182 virtual ~SGSelectAnimation ();
183 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
185 SGCondition * _condition;
190 * Animation to spin an object around a center point.
192 * This animation rotates at a specific velocity.
194 class SGSpinAnimation : public SGAnimation
197 SGSpinAnimation( SGPropertyNode *prop_root,
198 SGPropertyNode_ptr props,
199 double sim_time_sec );
200 virtual ~SGSpinAnimation ();
201 virtual int update();
203 bool _use_personality;
204 SGPropertyNode_ptr _prop;
205 SGPersonalityParameter<double> _factor;
206 SGPersonalityParameter<double> _position_deg;
207 double _last_time_sec;
210 SGCondition * _condition;
215 * Animation to draw objects for a specific amount of time each.
217 class SGTimedAnimation : public SGAnimation
220 SGTimedAnimation (SGPropertyNode_ptr props);
221 virtual ~SGTimedAnimation ();
223 virtual int update();
225 bool _use_personality;
226 double _duration_sec;
227 double _last_time_sec;
228 double _total_duration_sec;
230 struct DurationSpec {
231 DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
232 DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
235 vector<DurationSpec> _branch_duration_specs;
236 vector<double> _branch_duration_sec;
241 * Animation to rotate an object around a center point.
243 * This animation rotates to a specific position.
245 class SGRotateAnimation : public SGAnimation
248 SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
249 virtual ~SGRotateAnimation ();
250 virtual int update();
252 SGPropertyNode_ptr _prop;
255 SGInterpTable * _table;
260 double _position_deg;
263 SGCondition * _condition;
268 * Animation to slide along an axis.
270 class SGTranslateAnimation : public SGAnimation
273 SGTranslateAnimation( SGPropertyNode *prop_root,
274 SGPropertyNode_ptr props );
275 virtual ~SGTranslateAnimation ();
276 virtual int update();
278 bool _use_personality;
279 SGPropertyNode_ptr _prop;
280 SGPersonalityParameter<double> _offset_m;
281 SGPersonalityParameter<double> _factor;
282 SGInterpTable * _table;
289 SGCondition * _condition;
293 * Animation to blend an object.
295 class SGBlendAnimation : public SGAnimation
298 SGBlendAnimation( SGPropertyNode *prop_root,
299 SGPropertyNode_ptr props );
300 virtual ~SGBlendAnimation ();
301 virtual int update();
303 bool _use_personality;
304 SGPropertyNode_ptr _prop;
305 SGInterpTable * _table;
307 SGPersonalityParameter<double> _offset;
308 SGPersonalityParameter<double> _factor;
313 osg::ref_ptr<osg::ColorMatrix> _colorMatrix;
317 * Animation to scale an object.
319 class SGScaleAnimation : public SGAnimation
322 SGScaleAnimation( SGPropertyNode *prop_root,
323 SGPropertyNode_ptr props );
324 virtual ~SGScaleAnimation ();
325 virtual int update();
327 bool _use_personality;
328 SGPropertyNode_ptr _prop;
329 SGPersonalityParameter<double> _x_factor;
330 SGPersonalityParameter<double> _y_factor;
331 SGPersonalityParameter<double> _z_factor;
332 SGPersonalityParameter<double> _x_offset;
333 SGPersonalityParameter<double> _y_offset;
334 SGPersonalityParameter<double> _z_offset;
335 SGInterpTable * _table;
354 * Animation to rotate texture mappings around a center point.
356 * This animation rotates to a specific position.
358 class SGTexRotateAnimation : public SGAnimation
361 SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
362 virtual ~SGTexRotateAnimation ();
363 virtual int update();
365 SGPropertyNode_ptr _prop;
368 SGInterpTable * _table;
373 double _position_deg;
376 SGCondition * _condition;
377 osg::ref_ptr<osg::TexMat> _texMat;
382 * Animation to slide texture mappings along an axis.
384 class SGTexTranslateAnimation : public SGAnimation
387 SGTexTranslateAnimation( SGPropertyNode *prop_root,
388 SGPropertyNode_ptr props );
389 virtual ~SGTexTranslateAnimation ();
390 virtual int update();
392 SGPropertyNode_ptr _prop;
397 SGInterpTable * _table;
404 SGCondition * _condition;
405 osg::ref_ptr<osg::TexMat> _texMat;
411 * Classes for handling multiple types of Texture translations on one object
414 class SGTexMultipleAnimation : public SGAnimation
417 SGTexMultipleAnimation( SGPropertyNode *prop_root,
418 SGPropertyNode_ptr props );
419 virtual ~SGTexMultipleAnimation ();
420 virtual int update();
425 SGPropertyNode_ptr prop;
426 int subtype; // 0=translation, 1=rotation
431 SGInterpTable * table;
440 SGPropertyNode_ptr _prop;
441 TexTransform* _transform;
443 osg::ref_ptr<osg::TexMat> _texMat;
448 * An "animation" to enable the alpha test
450 class SGAlphaTestAnimation : public SGAnimation
453 SGAlphaTestAnimation(SGPropertyNode_ptr props);
454 virtual ~SGAlphaTestAnimation ();
462 * An "animation" to modify material properties
464 class SGMaterialAnimation : public SGAnimation
467 SGMaterialAnimation(SGPropertyNode *prop_root, SGPropertyNode_ptr props,
468 const SGPath &texpath);
469 virtual ~SGMaterialAnimation() {}
471 virtual int update();
484 float red, green, blue;
487 SGPropertyNode_ptr red_prop;
488 SGPropertyNode_ptr green_prop;
489 SGPropertyNode_ptr blue_prop;
490 SGPropertyNode_ptr factor_prop;
491 SGPropertyNode_ptr offset_prop;
493 inline bool dirty() {
494 return red >= 0.0 || green >= 0.0 || blue >= 0.0;
497 return red_prop || green_prop || blue_prop
498 || factor_prop || offset_prop;
500 inline bool operator!=(ColorSpec& a) {
501 return red != a.red || green != a.green || blue != a.blue
502 || factor != a.factor || offset != a.offset;
505 v[0] = clamp(red * factor + offset);
506 v[1] = clamp(green * factor + offset);
507 v[2] = clamp(blue * factor + offset);
511 inline float clamp(float val) {
512 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0 : val;
521 SGPropertyNode_ptr value_prop;
522 SGPropertyNode_ptr factor_prop;
523 SGPropertyNode_ptr offset_prop;
524 inline bool dirty() { return value >= 0.0; }
525 inline bool live() { return value_prop || factor_prop || offset_prop; }
526 inline bool operator!=(PropSpec& a) {
527 return value != a.value || factor != a.factor || offset != a.offset;
530 SGCondition *_condition;
531 bool _last_condition;
532 SGPropertyNode_ptr _prop_root;
534 SGPath _texture_base;
539 unsigned _static_update;
547 float _thresh; // alpha_clamp (see man glAlphaFunc)
550 SGPropertyNode_ptr _shi_prop;
551 SGPropertyNode_ptr _thresh_prop;
552 SGPropertyNode_ptr _tex_prop;
553 std::vector<osg::Material*> _materialList;
554 osg::ref_ptr<osg::AlphaFunc> _alphaFunc;
555 osg::ref_ptr<osg::Texture2D> _texture2D;
557 void cloneMaterials(osg::Group *b);
558 void setMaterialBranch(osg::Group *b);
559 void initColorGroup(SGPropertyNode_ptr, ColorSpec *, int flag);
560 void updateColorGroup(ColorSpec *, int flag);
561 inline float clamp(float val, float min = 0.0, float max = 1.0) {
562 return val < min ? min : val > max ? max : val;
564 const char *path(const char *rel) {
565 return (_tmpstr = _prop_base + rel).c_str();
571 * An "animation" that compute a scale according to
572 * the angle between an axis and the view direction
574 class SGFlashAnimation : public SGAnimation
577 SGFlashAnimation(SGPropertyNode_ptr props);
578 virtual ~SGFlashAnimation ();
583 * An animation that compute a scale according to
584 * the distance from a point and the viewer
586 class SGDistScaleAnimation : public SGAnimation
589 SGDistScaleAnimation(SGPropertyNode_ptr props);
590 virtual ~SGDistScaleAnimation ();
594 * An animation to tell wich objects don't cast shadows.
596 class SGShadowAnimation : public SGAnimation
599 SGShadowAnimation ( SGPropertyNode *prop_root,
600 SGPropertyNode_ptr props );
601 virtual ~SGShadowAnimation ();
602 virtual int update();
603 bool get_condition_value(void);
605 SGCondition * _condition;
606 bool _condition_value;
610 + * An "animation" that replace fixed opengl pipeline by shaders
612 class SGShaderAnimation : public SGAnimation
615 SGShaderAnimation ( SGPropertyNode *prop_root,
616 SGPropertyNode_ptr props );
617 virtual ~SGShaderAnimation ();
619 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
620 bool get_condition_value(void);
622 SGCondition * _condition;
623 bool _condition_value;
626 osg::Vec4 _param_color;
630 SGPropertyNode_ptr _factor_prop;
633 SGPropertyNode_ptr _speed_prop;
634 osg::ref_ptr<osg::Texture2D> _effectTexture;
635 unsigned char *_textureData;
636 GLint _texWidth, _texHeight;
641 #endif // _SG_ANIMATION_HXX