]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/animation.hxx
Jim Wilson:
[simgear.git] / simgear / scene / model / animation.hxx
1
2 // animation.hxx - classes to manage model animation.
3 // Written by David Megginson, started 2002.
4 //
5 // This file is in the Public Domain, and comes with no warranty.
6
7 #ifndef _SG_ANIMATION_HXX
8 #define _SG_ANIMATION_HXX 1
9
10 #ifndef __cplusplus
11 # error This library requires C++
12 #endif
13
14 #include <vector>
15 #include <map>
16
17 SG_USING_STD(vector);
18 SG_USING_STD(map);
19
20 #include <plib/sg.h>
21 #include <plib/ssg.h>
22
23 #include <simgear/math/point3d.hxx>
24 #include <simgear/props/props.hxx>
25
26
27 // Don't pull in the headers, since we don't need them here.
28 class SGInterpTable;
29 class SGCondition;
30 class SGPersonalityBranch;
31
32
33 // Has anyone done anything *really* stupid, like making min and max macros?
34 #ifdef min
35 #undef min
36 #endif
37 #ifdef max
38 #undef max
39 #endif
40
41
42 \f
43 //////////////////////////////////////////////////////////////////////
44 // Animation classes
45 //////////////////////////////////////////////////////////////////////
46
47 /**
48  * Abstract base class for all animations.
49  */
50 class SGAnimation :  public ssgBase
51 {
52 public:
53   enum PersonalityVar { INIT_SPIN, LAST_TIME_SEC_SPIN, FACTOR_SPIN,
54                         POSITION_DEG_SPIN, INIT_TIMED, LAST_TIME_SEC_TIMED,
55                         TOTAL_DURATION_SEC_TIMED, BRANCH_DURATION_SEC_TIMED,
56                         STEP_TIMED };
57
58   SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch);
59
60   virtual ~SGAnimation ();
61
62   /**
63    * Get the SSG branch holding the animation.
64    */
65   virtual ssgBranch * getBranch () { return _branch; }
66
67   /**
68    * Initialize the animation, after children have been added.
69    */
70   virtual void init ();
71
72   /**
73    * Update the animation.
74    */
75   virtual int update();
76
77   /**
78    * Restore the state after the animation.
79    */
80   virtual void restore();
81
82   /**
83    * Set the value of sim_time_sec.  This needs to be called every
84    * frame in order for the time based animations to work correctly.
85    */
86   static void set_sim_time_sec( double val ) { sim_time_sec = val; }
87
88   /**
89    * Current personality branch : enable animation to behave differently
90    * for similar objects
91    */
92   static SGPersonalityBranch *current_object;
93
94 protected:
95
96   static double sim_time_sec;
97
98   ssgBranch * _branch;
99
100 };
101
102
103 /**
104  * A no-op animation.
105  */
106 class SGNullAnimation : public SGAnimation
107 {
108 public:
109   SGNullAnimation (SGPropertyNode_ptr props);
110   virtual ~SGNullAnimation ();
111 };
112
113
114 /**
115  * A range, or level-of-detail (LOD) animation.
116  */
117 class SGRangeAnimation : public SGAnimation
118 {
119 public:
120   SGRangeAnimation (SGPropertyNode *prop_root,
121                     SGPropertyNode_ptr props);
122   virtual ~SGRangeAnimation ();
123   virtual int update();
124 private:
125   SGPropertyNode_ptr _min_prop;
126   SGPropertyNode_ptr _max_prop;
127   float _min;
128   float _max;
129   float _min_factor;
130   float _max_factor;
131   SGCondition * _condition;
132 };
133
134
135 /**
136  * Animation to turn and face the screen.
137  */
138 class SGBillboardAnimation : public SGAnimation
139 {
140 public:
141   SGBillboardAnimation (SGPropertyNode_ptr props);
142   virtual ~SGBillboardAnimation ();
143 };
144
145
146 /**
147  * Animation to select alternative versions of the same object.
148  */
149 class SGSelectAnimation : public SGAnimation
150 {
151 public:
152   SGSelectAnimation( SGPropertyNode *prop_root,
153                    SGPropertyNode_ptr props );
154   virtual ~SGSelectAnimation ();
155   virtual int update();
156 private:
157   SGCondition * _condition;
158 };
159
160
161 /**
162  * Animation to spin an object around a center point.
163  *
164  * This animation rotates at a specific velocity.
165  */
166 class SGSpinAnimation : public SGAnimation
167 {
168 public:
169   SGSpinAnimation( SGPropertyNode *prop_root,
170                  SGPropertyNode_ptr props,
171                  double sim_time_sec );
172   virtual ~SGSpinAnimation ();
173   virtual int update();
174 private:
175   bool _use_personality;
176   SGPropertyNode_ptr _prop;
177   double _factor;
178   double _factor_min;
179   double _factor_max;
180   double _position_deg;
181   double _position_deg_min;
182   double _position_deg_max;
183   double _last_time_sec;
184   sgMat4 _matrix;
185   sgVec3 _center;
186   sgVec3 _axis;
187   SGCondition * _condition;
188 };
189
190
191 /**
192  * Animation to draw objects for a specific amount of time each.
193  */
194 class SGTimedAnimation : public SGAnimation
195 {
196 public:
197     SGTimedAnimation (SGPropertyNode_ptr props);
198     virtual ~SGTimedAnimation ();
199     virtual void init();
200     virtual int update();
201 private:
202     bool _use_personality;
203     double _duration_sec;
204     double _last_time_sec;
205     double _total_duration_sec;
206     int _step;
207     struct DurationSpec {
208         DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
209         DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
210         double _min, _max;
211     };
212     vector<DurationSpec> _branch_duration_specs;
213     vector<double> _branch_duration_sec;
214 };
215
216
217 /**
218  * Animation to rotate an object around a center point.
219  *
220  * This animation rotates to a specific position.
221  */
222 class SGRotateAnimation : public SGAnimation
223 {
224 public:
225   SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
226   virtual ~SGRotateAnimation ();
227   virtual int update();
228 private:
229   SGPropertyNode_ptr _prop;
230   double _offset_deg;
231   double _factor;
232   SGInterpTable * _table;
233   bool _has_min;
234   double _min_deg;
235   bool _has_max;
236   double _max_deg;
237   double _position_deg;
238   sgMat4 _matrix;
239   sgVec3 _center;
240   sgVec3 _axis;
241   SGCondition * _condition;
242 };
243
244
245 /**
246  * Animation to slide along an axis.
247  */
248 class SGTranslateAnimation : public SGAnimation
249 {
250 public:
251   SGTranslateAnimation( SGPropertyNode *prop_root,
252                       SGPropertyNode_ptr props );
253   virtual ~SGTranslateAnimation ();
254   virtual int update();
255 private:
256   SGPropertyNode_ptr _prop;
257   double _offset_m;
258   double _factor;
259   SGInterpTable * _table;
260   bool _has_min;
261   double _min_m;
262   bool _has_max;
263   double _max_m;
264   double _position_m;
265   sgMat4 _matrix;
266   sgVec3 _axis;
267   SGCondition * _condition;
268 };
269
270 /**
271  * Animation to blend an object.
272  */
273 class SGBlendAnimation : public SGAnimation
274 {
275 public:
276   SGBlendAnimation( SGPropertyNode *prop_root,
277                       SGPropertyNode_ptr props );
278   virtual ~SGBlendAnimation ();
279   virtual int update();
280 private:
281   SGPropertyNode_ptr _prop;
282   SGInterpTable * _table;
283   double _prev_value;
284   double _offset;
285   double _factor;
286   bool _has_min;
287   double _min;
288   bool _has_max;
289   double _max;
290 };
291
292 /**
293  * Animation to scale an object.
294  */
295 class SGScaleAnimation : public SGAnimation
296 {
297 public:
298   SGScaleAnimation( SGPropertyNode *prop_root,
299                         SGPropertyNode_ptr props );
300   virtual ~SGScaleAnimation ();
301   virtual int update();
302 private:
303   SGPropertyNode_ptr _prop;
304   double _x_factor;
305   double _y_factor;
306   double _z_factor;
307   double _x_offset;
308   double _y_offset;
309   double _z_offset;
310   SGInterpTable * _table;
311   bool _has_min_x;
312   bool _has_min_y;
313   bool _has_min_z;
314   double _min_x;
315   double _min_y;
316   double _min_z;
317   bool _has_max_x;
318   bool _has_max_y;
319   bool _has_max_z;
320   double _max_x;
321   double _max_y;
322   double _max_z;
323   double _x_scale;
324   double _y_scale;
325   double _z_scale;
326   sgMat4 _matrix;
327 };
328
329 /**
330  * Animation to rotate texture mappings around a center point.
331  *
332  * This animation rotates to a specific position.
333  */
334 class SGTexRotateAnimation : public SGAnimation
335 {
336 public:
337   SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
338   virtual ~SGTexRotateAnimation ();
339   virtual int update();
340 private:
341   SGPropertyNode_ptr _prop;
342   double _offset_deg;
343   double _factor;
344   SGInterpTable * _table;
345   bool _has_min;
346   double _min_deg;
347   bool _has_max;
348   double _max_deg;
349   double _position_deg;
350   sgMat4 _matrix;
351   sgVec3 _center;
352   sgVec3 _axis;
353 };
354
355
356 /**
357  * Animation to slide texture mappings along an axis.
358  */
359 class SGTexTranslateAnimation : public SGAnimation
360 {
361 public:
362   SGTexTranslateAnimation( SGPropertyNode *prop_root,
363                       SGPropertyNode_ptr props );
364   virtual ~SGTexTranslateAnimation ();
365   virtual int update();
366 private:
367   SGPropertyNode_ptr _prop;
368   double _offset;
369   double _factor;
370   double _step;
371   double _scroll;
372   SGInterpTable * _table;
373   bool _has_min;
374   double _min;
375   bool _has_max;
376   double _max;
377   double _position;
378   sgMat4 _matrix;
379   sgVec3 _axis;
380 };
381
382
383
384 /**
385  * Classes for handling multiple types of Texture translations on one object
386  */
387
388 class SGTexMultipleAnimation : public SGAnimation
389 {
390 public:
391   SGTexMultipleAnimation( SGPropertyNode *prop_root,
392                       SGPropertyNode_ptr props );
393   virtual ~SGTexMultipleAnimation ();
394   virtual int update();
395 private:
396   class TexTransform
397     {
398     public:
399     SGPropertyNode_ptr prop;
400     int subtype; //  0=translation, 1=rotation
401     double offset;
402     double factor;
403     double step;
404     double scroll;
405     SGInterpTable * table;
406     bool has_min;
407     double min;
408     bool has_max;
409     double max;
410     double position;
411     sgMat4 matrix;
412     sgVec3 center;
413     sgVec3 axis;
414   };
415   SGPropertyNode_ptr _prop;
416   TexTransform* _transform;
417   int _num_transforms;
418 };
419
420
421 /**
422  * An "animation" to enable the alpha test 
423  */
424 class SGAlphaTestAnimation : public SGAnimation
425 {
426 public:
427   SGAlphaTestAnimation(SGPropertyNode_ptr props);
428   virtual ~SGAlphaTestAnimation ();
429   virtual void init();
430 private:
431   void setAlphaClampToBranch(ssgBranch *b, float clamp);
432   float _alpha_clamp;
433 };
434
435
436 /**
437  * An "animation" to modify emissive values on leaf nodes
438  */
439 class SGEmissionAnimation : public SGAnimation
440 {
441 public:
442   SGEmissionAnimation(SGPropertyNode *prop_root, SGPropertyNode_ptr props);
443   virtual ~SGEmissionAnimation ();
444   virtual void init();
445   virtual int update();
446 private:
447   SGPropertyNode_ptr _prop;
448   ssgSimpleState* _cached_material;
449   ssgSimpleState* _cloned_material;
450   void cloneMaterials(ssgBranch *b);
451   void setEmissionBranch(ssgBranch *b, float color0, float color1, float color2);
452   float _color0;
453   float _color1;
454   float _color2;
455   float _old_brightness;
456 };
457
458
459 /**
460  * An "animation" that compute a scale according to 
461  * the angle between an axis and the view direction
462  */
463 class SGFlashAnimation : public SGAnimation
464 {
465 public:
466   SGFlashAnimation(SGPropertyNode_ptr props);
467   virtual ~SGFlashAnimation ();
468
469   static void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
470   void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
471
472 private:
473   sgVec3 _axis, _center;
474   float _power, _factor, _offset, _min_v, _max_v;
475   bool _two_sides;
476 };
477
478
479 /**
480  * An animation that compute a scale according to 
481  * the distance from a point and the viewer
482  */
483 class SGDistScaleAnimation : public SGAnimation
484 {
485 public:
486   SGDistScaleAnimation(SGPropertyNode_ptr props);
487   virtual ~SGDistScaleAnimation ();
488
489   static void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
490   void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
491
492 private:
493   sgVec3 _center;
494   float _factor, _offset, _min_v, _max_v;
495   bool _has_min, _has_max;
496   SGInterpTable * _table;
497 };
498
499
500 #endif // _SG_ANIMATION_HXX