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 <simgear/math/point3d.hxx>
21 #include <simgear/props/props.hxx>
22 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/scene/model/persparam.hxx>
30 // Don't pull in the headers, since we don't need them here.
33 class SGPersonalityBranch;
36 // Has anyone done anything *really* stupid, like making min and max macros?
46 //////////////////////////////////////////////////////////////////////
48 //////////////////////////////////////////////////////////////////////
51 * Abstract base class for all animations.
53 class SGAnimation : public ssgBase
56 enum PersonalityVar { INIT_SPIN, LAST_TIME_SEC_SPIN, FACTOR_SPIN,
58 INIT_TIMED, LAST_TIME_SEC_TIMED, TOTAL_DURATION_SEC_TIMED,
59 BRANCH_DURATION_SEC_TIMED, STEP_TIMED,
60 INIT_TRANSLATE, FACTOR_TRANSLATE, OFFSET_TRANSLATE,
61 INIT_BLEND, FACTOR_BLEND, OFFSET_BLEND,
62 INIT_SCALE, X_FACTOR_SCALE, Y_FACTOR_SCALE, Z_FACTOR_SCALE,
63 X_OFFSET_SCALE, Y_OFFSET_SCALE, Z_OFFSET_SCALE };
65 SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch);
67 virtual ~SGAnimation ();
70 * Get the SSG branch holding the animation.
72 virtual ssgBranch * getBranch () { return _branch; }
75 * Initialize the animation, after children have been added.
80 * Update the animation.
85 * Restore the state after the animation.
87 virtual void restore();
90 * Set the value of sim_time_sec. This needs to be called every
91 * frame in order for the time based animations to work correctly.
93 static void set_sim_time_sec( double val ) { sim_time_sec = val; }
96 * Current personality branch : enable animation to behave differently
99 static SGPersonalityBranch *current_object;
101 int get_animation_type(void) { return animation_type; }
105 static double sim_time_sec;
116 class SGNullAnimation : public SGAnimation
119 SGNullAnimation (SGPropertyNode_ptr props);
120 virtual ~SGNullAnimation ();
125 * A range, or level-of-detail (LOD) animation.
127 class SGRangeAnimation : public SGAnimation
130 SGRangeAnimation (SGPropertyNode *prop_root,
131 SGPropertyNode_ptr props);
132 virtual ~SGRangeAnimation ();
133 virtual int update();
135 SGPropertyNode_ptr _min_prop;
136 SGPropertyNode_ptr _max_prop;
141 SGCondition * _condition;
146 * Animation to turn and face the screen.
148 class SGBillboardAnimation : public SGAnimation
151 SGBillboardAnimation (SGPropertyNode_ptr props);
152 virtual ~SGBillboardAnimation ();
157 * Animation to select alternative versions of the same object.
159 class SGSelectAnimation : public SGAnimation
162 SGSelectAnimation( SGPropertyNode *prop_root,
163 SGPropertyNode_ptr props );
164 virtual ~SGSelectAnimation ();
165 virtual int update();
167 SGCondition * _condition;
172 * Animation to spin an object around a center point.
174 * This animation rotates at a specific velocity.
176 class SGSpinAnimation : public SGAnimation
179 SGSpinAnimation( SGPropertyNode *prop_root,
180 SGPropertyNode_ptr props,
181 double sim_time_sec );
182 virtual ~SGSpinAnimation ();
183 virtual int update();
185 bool _use_personality;
186 SGPropertyNode_ptr _prop;
187 SGPersonalityParameter<double> _factor;
188 SGPersonalityParameter<double> _position_deg;
189 double _last_time_sec;
193 SGCondition * _condition;
198 * Animation to draw objects for a specific amount of time each.
200 class SGTimedAnimation : public SGAnimation
203 SGTimedAnimation (SGPropertyNode_ptr props);
204 virtual ~SGTimedAnimation ();
206 virtual int update();
208 bool _use_personality;
209 double _duration_sec;
210 double _last_time_sec;
211 double _total_duration_sec;
213 struct DurationSpec {
214 DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
215 DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
218 vector<DurationSpec> _branch_duration_specs;
219 vector<double> _branch_duration_sec;
224 * Animation to rotate an object around a center point.
226 * This animation rotates to a specific position.
228 class SGRotateAnimation : public SGAnimation
231 SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
232 virtual ~SGRotateAnimation ();
233 virtual int update();
235 SGPropertyNode_ptr _prop;
238 SGInterpTable * _table;
243 double _position_deg;
247 SGCondition * _condition;
252 * Animation to slide along an axis.
254 class SGTranslateAnimation : public SGAnimation
257 SGTranslateAnimation( SGPropertyNode *prop_root,
258 SGPropertyNode_ptr props );
259 virtual ~SGTranslateAnimation ();
260 virtual int update();
262 bool _use_personality;
263 SGPropertyNode_ptr _prop;
264 SGPersonalityParameter<double> _offset_m;
265 SGPersonalityParameter<double> _factor;
266 SGInterpTable * _table;
274 SGCondition * _condition;
278 * Animation to blend an object.
280 class SGBlendAnimation : public SGAnimation
283 SGBlendAnimation( SGPropertyNode *prop_root,
284 SGPropertyNode_ptr props );
285 virtual ~SGBlendAnimation ();
286 virtual int update();
288 bool _use_personality;
289 SGPropertyNode_ptr _prop;
290 SGInterpTable * _table;
292 SGPersonalityParameter<double> _offset;
293 SGPersonalityParameter<double> _factor;
301 * Animation to scale an object.
303 class SGScaleAnimation : public SGAnimation
306 SGScaleAnimation( SGPropertyNode *prop_root,
307 SGPropertyNode_ptr props );
308 virtual ~SGScaleAnimation ();
309 virtual int update();
311 bool _use_personality;
312 SGPropertyNode_ptr _prop;
313 SGPersonalityParameter<double> _x_factor;
314 SGPersonalityParameter<double> _y_factor;
315 SGPersonalityParameter<double> _z_factor;
316 SGPersonalityParameter<double> _x_offset;
317 SGPersonalityParameter<double> _y_offset;
318 SGPersonalityParameter<double> _z_offset;
319 SGInterpTable * _table;
339 * Animation to rotate texture mappings around a center point.
341 * This animation rotates to a specific position.
343 class SGTexRotateAnimation : public SGAnimation
346 SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
347 virtual ~SGTexRotateAnimation ();
348 virtual int update();
350 SGPropertyNode_ptr _prop;
353 SGInterpTable * _table;
358 double _position_deg;
362 SGCondition * _condition;
367 * Animation to slide texture mappings along an axis.
369 class SGTexTranslateAnimation : public SGAnimation
372 SGTexTranslateAnimation( SGPropertyNode *prop_root,
373 SGPropertyNode_ptr props );
374 virtual ~SGTexTranslateAnimation ();
375 virtual int update();
377 SGPropertyNode_ptr _prop;
382 SGInterpTable * _table;
390 SGCondition * _condition;
396 * Classes for handling multiple types of Texture translations on one object
399 class SGTexMultipleAnimation : public SGAnimation
402 SGTexMultipleAnimation( SGPropertyNode *prop_root,
403 SGPropertyNode_ptr props );
404 virtual ~SGTexMultipleAnimation ();
405 virtual int update();
410 SGPropertyNode_ptr prop;
411 int subtype; // 0=translation, 1=rotation
416 SGInterpTable * table;
426 SGPropertyNode_ptr _prop;
427 TexTransform* _transform;
433 * An "animation" to enable the alpha test
435 class SGAlphaTestAnimation : public SGAnimation
438 SGAlphaTestAnimation(SGPropertyNode_ptr props);
439 virtual ~SGAlphaTestAnimation ();
442 void setAlphaClampToBranch(ssgBranch *b, float clamp);
448 * An "animation" to modify material properties
450 class SGMaterialAnimation : public SGAnimation
453 SGMaterialAnimation(SGPropertyNode *prop_root, SGPropertyNode_ptr props,
454 const SGPath &texpath);
455 virtual ~SGMaterialAnimation() {}
457 virtual int update();
470 float red, green, blue;
473 SGPropertyNode_ptr red_prop;
474 SGPropertyNode_ptr green_prop;
475 SGPropertyNode_ptr blue_prop;
476 SGPropertyNode_ptr factor_prop;
477 SGPropertyNode_ptr offset_prop;
479 inline bool dirty() {
480 return red >= 0.0 || green >= 0.0 || blue >= 0.0;
483 return red_prop || green_prop || blue_prop
484 || factor_prop || offset_prop;
486 inline bool operator!=(ColorSpec& a) {
487 return red != a.red || green != a.green || blue != a.blue
488 || factor != a.factor || offset != a.offset;
491 v[0] = clamp(red * factor + offset);
492 v[1] = clamp(green * factor + offset);
493 v[2] = clamp(blue * factor + offset);
497 inline float clamp(float val) {
498 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0 : val;
507 SGPropertyNode_ptr value_prop;
508 SGPropertyNode_ptr factor_prop;
509 SGPropertyNode_ptr offset_prop;
510 inline bool dirty() { return value >= 0.0; }
511 inline bool live() { return value_prop || factor_prop || offset_prop; }
512 inline bool operator!=(PropSpec& a) {
513 return value != a.value || factor != a.factor || offset != a.offset;
516 SGCondition *_condition;
517 bool _last_condition;
518 SGPropertyNode_ptr _prop_root;
520 SGPath _texture_base;
523 ssgSimpleState* _cached_material;
524 ssgSimpleState* _cloned_material;
527 unsigned _static_update;
535 float _thresh; // alpha_clamp (see man glAlphaFunc)
538 SGPropertyNode_ptr _shi_prop;
539 SGPropertyNode_ptr _thresh_prop;
540 SGPropertyNode_ptr _tex_prop;
542 void cloneMaterials(ssgBranch *b);
543 void setMaterialBranch(ssgBranch *b);
544 void initColorGroup(SGPropertyNode_ptr, ColorSpec *, int flag);
545 void updateColorGroup(ColorSpec *, int flag);
546 inline float clamp(float val, float min = 0.0, float max = 1.0) {
547 return val < min ? min : val > max ? max : val;
549 const char *path(const char *rel) {
550 return (_tmpstr = _prop_base + rel).c_str();
556 * An "animation" that compute a scale according to
557 * the angle between an axis and the view direction
559 class SGFlashAnimation : public SGAnimation
562 SGFlashAnimation(SGPropertyNode_ptr props);
563 virtual ~SGFlashAnimation ();
565 static void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
566 void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
569 sgVec3 _axis, _center;
570 float _power, _factor, _offset, _min_v, _max_v;
576 * An animation that compute a scale according to
577 * the distance from a point and the viewer
579 class SGDistScaleAnimation : public SGAnimation
582 SGDistScaleAnimation(SGPropertyNode_ptr props);
583 virtual ~SGDistScaleAnimation ();
585 static void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
586 void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
590 float _factor, _offset, _min_v, _max_v;
591 bool _has_min, _has_max;
592 SGInterpTable * _table;
596 * An animation to tell wich objects don't cast shadows.
598 class SGShadowAnimation : public SGAnimation
601 SGShadowAnimation ( SGPropertyNode *prop_root,
602 SGPropertyNode_ptr props );
603 virtual ~SGShadowAnimation ();
604 virtual int update();
605 bool get_condition_value(void);
607 SGCondition * _condition;
608 bool _condition_value;
612 + * An "animation" that replace fixed opengl pipeline by shaders
614 class SGShaderAnimation : public SGAnimation
617 SGShaderAnimation ( SGPropertyNode *prop_root,
618 SGPropertyNode_ptr props );
619 virtual ~SGShaderAnimation ();
621 virtual int update();
622 bool get_condition_value(void);
624 SGCondition * _condition;
625 bool _condition_value;
632 SGPropertyNode_ptr _factor_prop;
634 SGPropertyNode_ptr _speed_prop;
635 ssgTexture *_effectTexture;
636 unsigned char *_textureData;
637 GLint _texWidth, _texHeight;
642 #endif // _SG_ANIMATION_HXX