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;
97 static double sim_time_sec;
107 class SGNullAnimation : public SGAnimation
110 SGNullAnimation (SGPropertyNode_ptr props);
111 virtual ~SGNullAnimation ();
116 * A range, or level-of-detail (LOD) animation.
118 class SGRangeAnimation : public SGAnimation
121 SGRangeAnimation (SGPropertyNode *prop_root,
122 SGPropertyNode_ptr props);
123 virtual ~SGRangeAnimation ();
124 virtual int update();
126 SGPropertyNode_ptr _min_prop;
127 SGPropertyNode_ptr _max_prop;
132 SGCondition * _condition;
137 * Animation to turn and face the screen.
139 class SGBillboardAnimation : public SGAnimation
142 SGBillboardAnimation (SGPropertyNode_ptr props);
143 virtual ~SGBillboardAnimation ();
148 * Animation to select alternative versions of the same object.
150 class SGSelectAnimation : public SGAnimation
153 SGSelectAnimation( SGPropertyNode *prop_root,
154 SGPropertyNode_ptr props );
155 virtual ~SGSelectAnimation ();
156 virtual int update();
158 SGCondition * _condition;
163 * Animation to spin an object around a center point.
165 * This animation rotates at a specific velocity.
167 class SGSpinAnimation : public SGAnimation
170 SGSpinAnimation( SGPropertyNode *prop_root,
171 SGPropertyNode_ptr props,
172 double sim_time_sec );
173 virtual ~SGSpinAnimation ();
174 virtual int update();
176 bool _use_personality;
177 SGPropertyNode_ptr _prop;
181 double _position_deg;
182 double _position_deg_min;
183 double _position_deg_max;
184 double _last_time_sec;
188 SGCondition * _condition;
193 * Animation to draw objects for a specific amount of time each.
195 class SGTimedAnimation : public SGAnimation
198 SGTimedAnimation (SGPropertyNode_ptr props);
199 virtual ~SGTimedAnimation ();
201 virtual int update();
203 bool _use_personality;
204 double _duration_sec;
205 double _last_time_sec;
206 double _total_duration_sec;
208 struct DurationSpec {
209 DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
210 DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
213 vector<DurationSpec> _branch_duration_specs;
214 vector<double> _branch_duration_sec;
219 * Animation to rotate an object around a center point.
221 * This animation rotates to a specific position.
223 class SGRotateAnimation : public SGAnimation
226 SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
227 virtual ~SGRotateAnimation ();
228 virtual int update();
230 SGPropertyNode_ptr _prop;
233 SGInterpTable * _table;
238 double _position_deg;
242 SGCondition * _condition;
247 * Animation to slide along an axis.
249 class SGTranslateAnimation : public SGAnimation
252 SGTranslateAnimation( SGPropertyNode *prop_root,
253 SGPropertyNode_ptr props );
254 virtual ~SGTranslateAnimation ();
255 virtual int update();
257 SGPropertyNode_ptr _prop;
260 SGInterpTable * _table;
268 SGCondition * _condition;
272 * Animation to blend an object.
274 class SGBlendAnimation : public SGAnimation
277 SGBlendAnimation( SGPropertyNode *prop_root,
278 SGPropertyNode_ptr props );
279 virtual ~SGBlendAnimation ();
280 virtual int update();
282 SGPropertyNode_ptr _prop;
283 SGInterpTable * _table;
294 * Animation to scale an object.
296 class SGScaleAnimation : public SGAnimation
299 SGScaleAnimation( SGPropertyNode *prop_root,
300 SGPropertyNode_ptr props );
301 virtual ~SGScaleAnimation ();
302 virtual int update();
304 SGPropertyNode_ptr _prop;
311 SGInterpTable * _table;
331 * Animation to rotate texture mappings around a center point.
333 * This animation rotates to a specific position.
335 class SGTexRotateAnimation : public SGAnimation
338 SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
339 virtual ~SGTexRotateAnimation ();
340 virtual int update();
342 SGPropertyNode_ptr _prop;
345 SGInterpTable * _table;
350 double _position_deg;
358 * Animation to slide texture mappings along an axis.
360 class SGTexTranslateAnimation : public SGAnimation
363 SGTexTranslateAnimation( SGPropertyNode *prop_root,
364 SGPropertyNode_ptr props );
365 virtual ~SGTexTranslateAnimation ();
366 virtual int update();
368 SGPropertyNode_ptr _prop;
373 SGInterpTable * _table;
386 * Classes for handling multiple types of Texture translations on one object
389 class SGTexMultipleAnimation : public SGAnimation
392 SGTexMultipleAnimation( SGPropertyNode *prop_root,
393 SGPropertyNode_ptr props );
394 virtual ~SGTexMultipleAnimation ();
395 virtual int update();
400 SGPropertyNode_ptr prop;
401 int subtype; // 0=translation, 1=rotation
406 SGInterpTable * table;
416 SGPropertyNode_ptr _prop;
417 TexTransform* _transform;
423 * An "animation" to enable the alpha test
425 class SGAlphaTestAnimation : public SGAnimation
428 SGAlphaTestAnimation(SGPropertyNode_ptr props);
429 virtual ~SGAlphaTestAnimation ();
432 void setAlphaClampToBranch(ssgBranch *b, float clamp);
438 * An "animation" to modify material properties
440 class SGMaterialAnimation : public SGAnimation
443 SGMaterialAnimation(SGPropertyNode *prop_root, SGPropertyNode_ptr props,
444 const SGPath &texpath);
445 virtual ~SGMaterialAnimation() {}
447 virtual int update();
460 float red, green, blue;
463 SGPropertyNode_ptr red_prop;
464 SGPropertyNode_ptr green_prop;
465 SGPropertyNode_ptr blue_prop;
466 SGPropertyNode_ptr factor_prop;
467 SGPropertyNode_ptr offset_prop;
469 inline bool dirty() {
470 return red >= 0.0 || green >= 0.0 || blue >= 0.0;
473 return red_prop || green_prop || blue_prop
474 || factor_prop || offset_prop;
476 inline bool operator!=(ColorSpec& a) {
477 return red != a.red || green != a.green || blue != a.blue
478 || factor != a.factor || offset != a.offset;
481 v[0] = clamp(red * factor + offset);
482 v[1] = clamp(green * factor + offset);
483 v[2] = clamp(blue * factor + offset);
487 inline float clamp(float val) {
488 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0 : val;
497 SGPropertyNode_ptr value_prop;
498 SGPropertyNode_ptr factor_prop;
499 SGPropertyNode_ptr offset_prop;
500 inline bool dirty() { return value >= 0.0; }
501 inline bool live() { return value_prop || factor_prop || offset_prop; }
502 inline bool operator!=(PropSpec& a) {
503 return value != a.value || factor != a.factor || offset != a.offset;
506 SGCondition *_condition;
507 SGPropertyNode *_prop_root;
509 SGPath _texture_base;
512 ssgSimpleState* _cached_material;
513 ssgSimpleState* _cloned_material;
523 float _thresh; // alpha_clamp (see man glAlphaFunc)
526 SGPropertyNode_ptr _shi_prop;
527 SGPropertyNode_ptr _thresh_prop;
528 SGPropertyNode_ptr _tex_prop;
530 void cloneMaterials(ssgBranch *b);
531 void setMaterialBranch(ssgBranch *b);
532 void initColorGroup(SGPropertyNode_ptr, ColorSpec *, int flag);
533 void updateColorGroup(ColorSpec *, int flag);
534 inline float clamp(float val, float min = 0.0, float max = 1.0) {
535 return val < min ? min : val > max ? max : val;
537 const char *path(const char *rel) {
538 return (_tmpstr = _prop_base + rel).c_str();
544 * An "animation" that compute a scale according to
545 * the angle between an axis and the view direction
547 class SGFlashAnimation : public SGAnimation
550 SGFlashAnimation(SGPropertyNode_ptr props);
551 virtual ~SGFlashAnimation ();
553 static void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
554 void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
557 sgVec3 _axis, _center;
558 float _power, _factor, _offset, _min_v, _max_v;
564 * An animation that compute a scale according to
565 * the distance from a point and the viewer
567 class SGDistScaleAnimation : public SGAnimation
570 SGDistScaleAnimation(SGPropertyNode_ptr props);
571 virtual ~SGDistScaleAnimation ();
573 static void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
574 void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
578 float _factor, _offset, _min_v, _max_v;
579 bool _has_min, _has_max;
580 SGInterpTable * _table;
584 #endif // _SG_ANIMATION_HXX