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++
23 #include <simgear/math/point3d.hxx>
24 #include <simgear/props/props.hxx>
25 #include <simgear/misc/sg_path.hxx>
28 // Don't pull in the headers, since we don't need them here.
31 class SGPersonalityBranch;
34 // Has anyone done anything *really* stupid, like making min and max macros?
44 //////////////////////////////////////////////////////////////////////
46 //////////////////////////////////////////////////////////////////////
49 * Abstract base class for all animations.
51 class SGAnimation : public ssgBase
54 enum PersonalityVar { INIT_SPIN, LAST_TIME_SEC_SPIN, FACTOR_SPIN,
55 POSITION_DEG_SPIN, INIT_TIMED, LAST_TIME_SEC_TIMED,
56 TOTAL_DURATION_SEC_TIMED, BRANCH_DURATION_SEC_TIMED,
59 SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch);
61 virtual ~SGAnimation ();
64 * Get the SSG branch holding the animation.
66 virtual ssgBranch * getBranch () { return _branch; }
69 * Initialize the animation, after children have been added.
74 * Update the animation.
79 * Restore the state after the animation.
81 virtual void restore();
84 * Set the value of sim_time_sec. This needs to be called every
85 * frame in order for the time based animations to work correctly.
87 static void set_sim_time_sec( double val ) { sim_time_sec = val; }
90 * Current personality branch : enable animation to behave differently
93 static SGPersonalityBranch *current_object;
95 int get_animation_type(void) { return animation_type; }
99 static double sim_time_sec;
110 class SGNullAnimation : public SGAnimation
113 SGNullAnimation (SGPropertyNode_ptr props);
114 virtual ~SGNullAnimation ();
119 * A range, or level-of-detail (LOD) animation.
121 class SGRangeAnimation : public SGAnimation
124 SGRangeAnimation (SGPropertyNode *prop_root,
125 SGPropertyNode_ptr props);
126 virtual ~SGRangeAnimation ();
127 virtual int update();
129 SGPropertyNode_ptr _min_prop;
130 SGPropertyNode_ptr _max_prop;
135 SGCondition * _condition;
140 * Animation to turn and face the screen.
142 class SGBillboardAnimation : public SGAnimation
145 SGBillboardAnimation (SGPropertyNode_ptr props);
146 virtual ~SGBillboardAnimation ();
151 * Animation to select alternative versions of the same object.
153 class SGSelectAnimation : public SGAnimation
156 SGSelectAnimation( SGPropertyNode *prop_root,
157 SGPropertyNode_ptr props );
158 virtual ~SGSelectAnimation ();
159 virtual int update();
161 SGCondition * _condition;
166 * Animation to spin an object around a center point.
168 * This animation rotates at a specific velocity.
170 class SGSpinAnimation : public SGAnimation
173 SGSpinAnimation( SGPropertyNode *prop_root,
174 SGPropertyNode_ptr props,
175 double sim_time_sec );
176 virtual ~SGSpinAnimation ();
177 virtual int update();
179 bool _use_personality;
180 SGPropertyNode_ptr _prop;
184 double _position_deg;
185 double _position_deg_min;
186 double _position_deg_max;
187 double _last_time_sec;
191 SGCondition * _condition;
196 * Animation to draw objects for a specific amount of time each.
198 class SGTimedAnimation : public SGAnimation
201 SGTimedAnimation (SGPropertyNode_ptr props);
202 virtual ~SGTimedAnimation ();
204 virtual int update();
206 bool _use_personality;
207 double _duration_sec;
208 double _last_time_sec;
209 double _total_duration_sec;
211 struct DurationSpec {
212 DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
213 DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
216 vector<DurationSpec> _branch_duration_specs;
217 vector<double> _branch_duration_sec;
222 * Animation to rotate an object around a center point.
224 * This animation rotates to a specific position.
226 class SGRotateAnimation : public SGAnimation
229 SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
230 virtual ~SGRotateAnimation ();
231 virtual int update();
233 SGPropertyNode_ptr _prop;
236 SGInterpTable * _table;
241 double _position_deg;
245 SGCondition * _condition;
250 * Animation to slide along an axis.
252 class SGTranslateAnimation : public SGAnimation
255 SGTranslateAnimation( SGPropertyNode *prop_root,
256 SGPropertyNode_ptr props );
257 virtual ~SGTranslateAnimation ();
258 virtual int update();
260 SGPropertyNode_ptr _prop;
263 SGInterpTable * _table;
271 SGCondition * _condition;
275 * Animation to blend an object.
277 class SGBlendAnimation : public SGAnimation
280 SGBlendAnimation( SGPropertyNode *prop_root,
281 SGPropertyNode_ptr props );
282 virtual ~SGBlendAnimation ();
283 virtual int update();
285 SGPropertyNode_ptr _prop;
286 SGInterpTable * _table;
297 * Animation to scale an object.
299 class SGScaleAnimation : public SGAnimation
302 SGScaleAnimation( SGPropertyNode *prop_root,
303 SGPropertyNode_ptr props );
304 virtual ~SGScaleAnimation ();
305 virtual int update();
307 SGPropertyNode_ptr _prop;
314 SGInterpTable * _table;
334 * Animation to rotate texture mappings around a center point.
336 * This animation rotates to a specific position.
338 class SGTexRotateAnimation : public SGAnimation
341 SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
342 virtual ~SGTexRotateAnimation ();
343 virtual int update();
345 SGPropertyNode_ptr _prop;
348 SGInterpTable * _table;
353 double _position_deg;
361 * Animation to slide texture mappings along an axis.
363 class SGTexTranslateAnimation : public SGAnimation
366 SGTexTranslateAnimation( SGPropertyNode *prop_root,
367 SGPropertyNode_ptr props );
368 virtual ~SGTexTranslateAnimation ();
369 virtual int update();
371 SGPropertyNode_ptr _prop;
376 SGInterpTable * _table;
389 * Classes for handling multiple types of Texture translations on one object
392 class SGTexMultipleAnimation : public SGAnimation
395 SGTexMultipleAnimation( SGPropertyNode *prop_root,
396 SGPropertyNode_ptr props );
397 virtual ~SGTexMultipleAnimation ();
398 virtual int update();
403 SGPropertyNode_ptr prop;
404 int subtype; // 0=translation, 1=rotation
409 SGInterpTable * table;
419 SGPropertyNode_ptr _prop;
420 TexTransform* _transform;
426 * An "animation" to enable the alpha test
428 class SGAlphaTestAnimation : public SGAnimation
431 SGAlphaTestAnimation(SGPropertyNode_ptr props);
432 virtual ~SGAlphaTestAnimation ();
435 void setAlphaClampToBranch(ssgBranch *b, float clamp);
441 * An "animation" to modify material properties
443 class SGMaterialAnimation : public SGAnimation
446 SGMaterialAnimation(SGPropertyNode *prop_root, SGPropertyNode_ptr props,
447 const SGPath &texpath);
448 virtual ~SGMaterialAnimation() {}
450 virtual int update();
463 float red, green, blue;
466 SGPropertyNode_ptr red_prop;
467 SGPropertyNode_ptr green_prop;
468 SGPropertyNode_ptr blue_prop;
469 SGPropertyNode_ptr factor_prop;
470 SGPropertyNode_ptr offset_prop;
472 inline bool dirty() {
473 return red >= 0.0 || green >= 0.0 || blue >= 0.0;
476 return red_prop || green_prop || blue_prop
477 || factor_prop || offset_prop;
479 inline bool operator!=(ColorSpec& a) {
480 return red != a.red || green != a.green || blue != a.blue
481 || factor != a.factor || offset != a.offset;
484 v[0] = clamp(red * factor + offset);
485 v[1] = clamp(green * factor + offset);
486 v[2] = clamp(blue * factor + offset);
490 inline float clamp(float val) {
491 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0 : val;
500 SGPropertyNode_ptr value_prop;
501 SGPropertyNode_ptr factor_prop;
502 SGPropertyNode_ptr offset_prop;
503 inline bool dirty() { return value >= 0.0; }
504 inline bool live() { return value_prop || factor_prop || offset_prop; }
505 inline bool operator!=(PropSpec& a) {
506 return value != a.value || factor != a.factor || offset != a.offset;
509 SGCondition *_condition;
510 SGPropertyNode *_prop_root;
512 SGPath _texture_base;
515 ssgSimpleState* _cached_material;
516 ssgSimpleState* _cloned_material;
526 float _thresh; // alpha_clamp (see man glAlphaFunc)
529 SGPropertyNode_ptr _shi_prop;
530 SGPropertyNode_ptr _thresh_prop;
531 SGPropertyNode_ptr _tex_prop;
533 void cloneMaterials(ssgBranch *b);
534 void setMaterialBranch(ssgBranch *b);
535 void initColorGroup(SGPropertyNode_ptr, ColorSpec *, int flag);
536 void updateColorGroup(ColorSpec *, int flag);
537 inline float clamp(float val, float min = 0.0, float max = 1.0) {
538 return val < min ? min : val > max ? max : val;
540 const char *path(const char *rel) {
541 return (_tmpstr = _prop_base + rel).c_str();
547 * An "animation" that compute a scale according to
548 * the angle between an axis and the view direction
550 class SGFlashAnimation : public SGAnimation
553 SGFlashAnimation(SGPropertyNode_ptr props);
554 virtual ~SGFlashAnimation ();
556 static void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
557 void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
560 sgVec3 _axis, _center;
561 float _power, _factor, _offset, _min_v, _max_v;
567 * An animation that compute a scale according to
568 * the distance from a point and the viewer
570 class SGDistScaleAnimation : public SGAnimation
573 SGDistScaleAnimation(SGPropertyNode_ptr props);
574 virtual ~SGDistScaleAnimation ();
576 static void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
577 void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
581 float _factor, _offset, _min_v, _max_v;
582 bool _has_min, _has_max;
583 SGInterpTable * _table;
587 * An animation to tell wich objects don't cast shadows.
589 class SGShadowAnimation : public SGAnimation
592 SGShadowAnimation ( SGPropertyNode *prop_root,
593 SGPropertyNode_ptr props );
594 virtual ~SGShadowAnimation ();
595 virtual int update();
596 bool get_condition_value(void);
598 SGCondition * _condition;
599 bool _condition_value;
603 #endif // _SG_ANIMATION_HXX