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;
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 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 float clamp(float val) {
488 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0 : val;
491 SGCondition *_condition;
495 ssgSimpleState* _cached_material;
496 ssgSimpleState* _cloned_material;
506 float _thresh; // alpha_clamp (see man glAlphaFunc)
508 SGPropertyNode_ptr _shi_prop;
509 SGPropertyNode_ptr _trans_prop;
510 SGPropertyNode_ptr _thresh_prop;
511 SGPropertyNode_ptr _tex_prop;
513 void cloneMaterials(ssgBranch *b);
514 void setMaterialBranch(ssgBranch *b);
515 float clamp(float val, float min = 0.0, float max = 1.0) {
516 return val < min ? min : val > max ? max : val;
522 * An "animation" that compute a scale according to
523 * the angle between an axis and the view direction
525 class SGFlashAnimation : public SGAnimation
528 SGFlashAnimation(SGPropertyNode_ptr props);
529 virtual ~SGFlashAnimation ();
531 static void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
532 void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
535 sgVec3 _axis, _center;
536 float _power, _factor, _offset, _min_v, _max_v;
542 * An animation that compute a scale according to
543 * the distance from a point and the viewer
545 class SGDistScaleAnimation : public SGAnimation
548 SGDistScaleAnimation(SGPropertyNode_ptr props);
549 virtual ~SGDistScaleAnimation ();
551 static void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
552 void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
556 float _factor, _offset, _min_v, _max_v;
557 bool _has_min, _has_max;
558 SGInterpTable * _table;
562 #endif // _SG_ANIMATION_HXX