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;
357 SGCondition * _condition;
362 * Animation to slide texture mappings along an axis.
364 class SGTexTranslateAnimation : public SGAnimation
367 SGTexTranslateAnimation( SGPropertyNode *prop_root,
368 SGPropertyNode_ptr props );
369 virtual ~SGTexTranslateAnimation ();
370 virtual int update();
372 SGPropertyNode_ptr _prop;
377 SGInterpTable * _table;
385 SGCondition * _condition;
391 * Classes for handling multiple types of Texture translations on one object
394 class SGTexMultipleAnimation : public SGAnimation
397 SGTexMultipleAnimation( SGPropertyNode *prop_root,
398 SGPropertyNode_ptr props );
399 virtual ~SGTexMultipleAnimation ();
400 virtual int update();
405 SGPropertyNode_ptr prop;
406 int subtype; // 0=translation, 1=rotation
411 SGInterpTable * table;
421 SGPropertyNode_ptr _prop;
422 TexTransform* _transform;
428 * An "animation" to enable the alpha test
430 class SGAlphaTestAnimation : public SGAnimation
433 SGAlphaTestAnimation(SGPropertyNode_ptr props);
434 virtual ~SGAlphaTestAnimation ();
437 void setAlphaClampToBranch(ssgBranch *b, float clamp);
443 * An "animation" to modify material properties
445 class SGMaterialAnimation : public SGAnimation
448 SGMaterialAnimation(SGPropertyNode *prop_root, SGPropertyNode_ptr props,
449 const SGPath &texpath);
450 virtual ~SGMaterialAnimation() {}
452 virtual int update();
465 float red, green, blue;
468 SGPropertyNode_ptr red_prop;
469 SGPropertyNode_ptr green_prop;
470 SGPropertyNode_ptr blue_prop;
471 SGPropertyNode_ptr factor_prop;
472 SGPropertyNode_ptr offset_prop;
474 inline bool dirty() {
475 return red >= 0.0 || green >= 0.0 || blue >= 0.0;
478 return red_prop || green_prop || blue_prop
479 || factor_prop || offset_prop;
481 inline bool operator!=(ColorSpec& a) {
482 return red != a.red || green != a.green || blue != a.blue
483 || factor != a.factor || offset != a.offset;
486 v[0] = clamp(red * factor + offset);
487 v[1] = clamp(green * factor + offset);
488 v[2] = clamp(blue * factor + offset);
492 inline float clamp(float val) {
493 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0 : val;
502 SGPropertyNode_ptr value_prop;
503 SGPropertyNode_ptr factor_prop;
504 SGPropertyNode_ptr offset_prop;
505 inline bool dirty() { return value >= 0.0; }
506 inline bool live() { return value_prop || factor_prop || offset_prop; }
507 inline bool operator!=(PropSpec& a) {
508 return value != a.value || factor != a.factor || offset != a.offset;
511 SGCondition *_condition;
512 bool _last_condition;
513 SGPropertyNode_ptr _prop_root;
515 SGPath _texture_base;
518 ssgSimpleState* _cached_material;
519 ssgSimpleState* _cloned_material;
522 unsigned _static_update;
530 float _thresh; // alpha_clamp (see man glAlphaFunc)
533 SGPropertyNode_ptr _shi_prop;
534 SGPropertyNode_ptr _thresh_prop;
535 SGPropertyNode_ptr _tex_prop;
537 void cloneMaterials(ssgBranch *b);
538 void setMaterialBranch(ssgBranch *b);
539 void initColorGroup(SGPropertyNode_ptr, ColorSpec *, int flag);
540 void updateColorGroup(ColorSpec *, int flag);
541 inline float clamp(float val, float min = 0.0, float max = 1.0) {
542 return val < min ? min : val > max ? max : val;
544 const char *path(const char *rel) {
545 return (_tmpstr = _prop_base + rel).c_str();
551 * An "animation" that compute a scale according to
552 * the angle between an axis and the view direction
554 class SGFlashAnimation : public SGAnimation
557 SGFlashAnimation(SGPropertyNode_ptr props);
558 virtual ~SGFlashAnimation ();
560 static void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
561 void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
564 sgVec3 _axis, _center;
565 float _power, _factor, _offset, _min_v, _max_v;
571 * An animation that compute a scale according to
572 * the distance from a point and the viewer
574 class SGDistScaleAnimation : public SGAnimation
577 SGDistScaleAnimation(SGPropertyNode_ptr props);
578 virtual ~SGDistScaleAnimation ();
580 static void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
581 void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
585 float _factor, _offset, _min_v, _max_v;
586 bool _has_min, _has_max;
587 SGInterpTable * _table;
591 * An animation to tell wich objects don't cast shadows.
593 class SGShadowAnimation : public SGAnimation
596 SGShadowAnimation ( SGPropertyNode *prop_root,
597 SGPropertyNode_ptr props );
598 virtual ~SGShadowAnimation ();
599 virtual int update();
600 bool get_condition_value(void);
602 SGCondition * _condition;
603 bool _condition_value;
607 + * An "animation" that replace fixed opengl pipeline by shaders
609 class SGShaderAnimation : public SGAnimation
612 SGShaderAnimation ( SGPropertyNode *prop_root,
613 SGPropertyNode_ptr props );
614 virtual ~SGShaderAnimation ();
616 virtual int update();
617 bool get_condition_value(void);
619 SGCondition * _condition;
620 bool _condition_value;
627 SGPropertyNode_ptr _factor_prop;
629 SGPropertyNode_ptr _speed_prop;
630 ssgTexture *_effectTexture;
631 unsigned char *_textureData;
632 GLint _texWidth, _texHeight;
637 #endif // _SG_ANIMATION_HXX