]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/animation.hxx
Frederic Bouvier:
[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
54   SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch);
55
56   virtual ~SGAnimation ();
57
58   /**
59    * Get the SSG branch holding the animation.
60    */
61   virtual ssgBranch * getBranch () { return _branch; }
62
63   /**
64    * Initialize the animation, after children have been added.
65    */
66   virtual void init ();
67
68   /**
69    * Update the animation.
70    */
71   virtual int update();
72
73   /**
74    * Restore the state after the animation.
75    */
76   virtual void restore();
77
78   /**
79    * Set the value of sim_time_sec.  This needs to be called every
80    * frame in order for the time based animations to work correctly.
81    */
82   static void set_sim_time_sec( double val ) { sim_time_sec = val; }
83
84   /**
85    * Current personality branch : enable animation to behave differently
86    * for similar objects
87    */
88   static SGPersonalityBranch *current_object;
89
90 protected:
91
92   static double sim_time_sec;
93
94   ssgBranch * _branch;
95
96 };
97
98
99 /**
100  * A no-op animation.
101  */
102 class SGNullAnimation : public SGAnimation
103 {
104 public:
105   SGNullAnimation (SGPropertyNode_ptr props);
106   virtual ~SGNullAnimation ();
107 };
108
109
110 /**
111  * A range, or level-of-detail (LOD) animation.
112  */
113 class SGRangeAnimation : public SGAnimation
114 {
115 public:
116   SGRangeAnimation (SGPropertyNode *prop_root,
117                     SGPropertyNode_ptr props);
118   virtual ~SGRangeAnimation ();
119   virtual int update();
120 private:
121   SGPropertyNode_ptr _min_prop;
122   SGPropertyNode_ptr _max_prop;
123   float _min;
124   float _max;
125   float _min_factor;
126   float _max_factor;
127   SGCondition * _condition;
128 };
129
130
131 /**
132  * Animation to turn and face the screen.
133  */
134 class SGBillboardAnimation : public SGAnimation
135 {
136 public:
137   SGBillboardAnimation (SGPropertyNode_ptr props);
138   virtual ~SGBillboardAnimation ();
139 };
140
141
142 /**
143  * Animation to select alternative versions of the same object.
144  */
145 class SGSelectAnimation : public SGAnimation
146 {
147 public:
148   SGSelectAnimation( SGPropertyNode *prop_root,
149                    SGPropertyNode_ptr props );
150   virtual ~SGSelectAnimation ();
151   virtual int update();
152 private:
153   SGCondition * _condition;
154 };
155
156
157 /**
158  * Animation to spin an object around a center point.
159  *
160  * This animation rotates at a specific velocity.
161  */
162 class SGSpinAnimation : public SGAnimation
163 {
164 public:
165   SGSpinAnimation( SGPropertyNode *prop_root,
166                  SGPropertyNode_ptr props,
167                  double sim_time_sec );
168   virtual ~SGSpinAnimation ();
169   virtual int update();
170 private:
171   SGPropertyNode_ptr _prop;
172   double _factor;
173   double _position_deg;
174   double _last_time_sec;
175   sgMat4 _matrix;
176   sgVec3 _center;
177   sgVec3 _axis;
178   SGCondition * _condition;
179 };
180
181
182 /**
183  * Animation to draw objects for a specific amount of time each.
184  */
185 class SGTimedAnimation : public SGAnimation
186 {
187 public:
188     SGTimedAnimation (SGPropertyNode_ptr props);
189     virtual ~SGTimedAnimation ();
190     virtual int update();
191 private:
192     bool _use_personality;
193     enum PersonalityVar { INIT, LAST_TIME_SEC, TOTAL_DURATION_SEC, BRANCH_DURATION_SEC, STEP };
194     double _duration_sec;
195     double _last_time_sec;
196     double _total_duration_sec;
197     int _step;
198     struct DurationSpec {
199         DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
200         DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
201         double _min, _max;
202     };
203     vector<DurationSpec> _branch_duration_specs;
204     vector<double> _branch_duration_sec;
205 };
206
207
208 /**
209  * Animation to rotate an object around a center point.
210  *
211  * This animation rotates to a specific position.
212  */
213 class SGRotateAnimation : public SGAnimation
214 {
215 public:
216   SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
217   virtual ~SGRotateAnimation ();
218   virtual int update();
219 private:
220   SGPropertyNode_ptr _prop;
221   double _offset_deg;
222   double _factor;
223   SGInterpTable * _table;
224   bool _has_min;
225   double _min_deg;
226   bool _has_max;
227   double _max_deg;
228   double _position_deg;
229   sgMat4 _matrix;
230   sgVec3 _center;
231   sgVec3 _axis;
232   SGCondition * _condition;
233 };
234
235
236 /**
237  * Animation to slide along an axis.
238  */
239 class SGTranslateAnimation : public SGAnimation
240 {
241 public:
242   SGTranslateAnimation( SGPropertyNode *prop_root,
243                       SGPropertyNode_ptr props );
244   virtual ~SGTranslateAnimation ();
245   virtual int update();
246 private:
247   SGPropertyNode_ptr _prop;
248   double _offset_m;
249   double _factor;
250   SGInterpTable * _table;
251   bool _has_min;
252   double _min_m;
253   bool _has_max;
254   double _max_m;
255   double _position_m;
256   sgMat4 _matrix;
257   sgVec3 _axis;
258   SGCondition * _condition;
259 };
260
261 /**
262  * Animation to blend an object.
263  */
264 class SGBlendAnimation : public SGAnimation
265 {
266 public:
267   SGBlendAnimation( SGPropertyNode *prop_root,
268                       SGPropertyNode_ptr props );
269   virtual ~SGBlendAnimation ();
270   virtual int update();
271 private:
272   SGPropertyNode_ptr _prop;
273   SGInterpTable * _table;
274   double _prev_value;
275   double _offset;
276   double _factor;
277   bool _has_min;
278   double _min;
279   bool _has_max;
280   double _max;
281 };
282
283 /**
284  * Animation to scale an object.
285  */
286 class SGScaleAnimation : public SGAnimation
287 {
288 public:
289   SGScaleAnimation( SGPropertyNode *prop_root,
290                         SGPropertyNode_ptr props );
291   virtual ~SGScaleAnimation ();
292   virtual int update();
293 private:
294   SGPropertyNode_ptr _prop;
295   double _x_factor;
296   double _y_factor;
297   double _z_factor;
298   double _x_offset;
299   double _y_offset;
300   double _z_offset;
301   SGInterpTable * _table;
302   bool _has_min_x;
303   bool _has_min_y;
304   bool _has_min_z;
305   double _min_x;
306   double _min_y;
307   double _min_z;
308   bool _has_max_x;
309   bool _has_max_y;
310   bool _has_max_z;
311   double _max_x;
312   double _max_y;
313   double _max_z;
314   double _x_scale;
315   double _y_scale;
316   double _z_scale;
317   sgMat4 _matrix;
318 };
319
320 /**
321  * Animation to rotate texture mappings around a center point.
322  *
323  * This animation rotates to a specific position.
324  */
325 class SGTexRotateAnimation : public SGAnimation
326 {
327 public:
328   SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
329   virtual ~SGTexRotateAnimation ();
330   virtual int update();
331 private:
332   SGPropertyNode_ptr _prop;
333   double _offset_deg;
334   double _factor;
335   SGInterpTable * _table;
336   bool _has_min;
337   double _min_deg;
338   bool _has_max;
339   double _max_deg;
340   double _position_deg;
341   sgMat4 _matrix;
342   sgVec3 _center;
343   sgVec3 _axis;
344 };
345
346
347 /**
348  * Animation to slide texture mappings along an axis.
349  */
350 class SGTexTranslateAnimation : public SGAnimation
351 {
352 public:
353   SGTexTranslateAnimation( SGPropertyNode *prop_root,
354                       SGPropertyNode_ptr props );
355   virtual ~SGTexTranslateAnimation ();
356   virtual int update();
357 private:
358   SGPropertyNode_ptr _prop;
359   double _offset;
360   double _factor;
361   double _step;
362   double _scroll;
363   SGInterpTable * _table;
364   bool _has_min;
365   double _min;
366   bool _has_max;
367   double _max;
368   double _position;
369   sgMat4 _matrix;
370   sgVec3 _axis;
371 };
372
373
374
375 /**
376  * Classes for handling multiple types of Texture translations on one object
377  */
378
379 class SGTexMultipleAnimation : public SGAnimation
380 {
381 public:
382   SGTexMultipleAnimation( SGPropertyNode *prop_root,
383                       SGPropertyNode_ptr props );
384   virtual ~SGTexMultipleAnimation ();
385   virtual int update();
386 private:
387   class TexTransform
388     {
389     public:
390     SGPropertyNode_ptr prop;
391     int subtype; //  0=translation, 1=rotation
392     double offset;
393     double factor;
394     double step;
395     double scroll;
396     SGInterpTable * table;
397     bool has_min;
398     double min;
399     bool has_max;
400     double max;
401     double position;
402     sgMat4 matrix;
403     sgVec3 center;
404     sgVec3 axis;
405   };
406   SGPropertyNode_ptr _prop;
407   TexTransform* _transform;
408   int _num_transforms;
409 };
410
411
412 /**
413  * An "animation" to enable the alpha test 
414  */
415 class SGAlphaTestAnimation : public SGAnimation
416 {
417 public:
418   SGAlphaTestAnimation(SGPropertyNode_ptr props);
419   virtual ~SGAlphaTestAnimation ();
420   virtual void init();
421 private:
422   void setAlphaClampToBranch(ssgBranch *b, float clamp);
423   float _alpha_clamp;
424 };
425
426
427 /**
428  * An "animation" that compute a scale according to 
429  * the angle between an axis and the view direction
430  */
431 class SGFlashAnimation : public SGAnimation
432 {
433 public:
434   SGFlashAnimation(SGPropertyNode_ptr props);
435   virtual ~SGFlashAnimation ();
436
437   static void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
438   void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
439
440 private:
441   sgVec3 _axis, _center;
442   float _power, _factor, _offset, _min_v, _max_v;
443   bool _two_sides;
444 };
445
446
447 /**
448  * An animation that compute a scale according to 
449  * the distance from a point and the viewer
450  */
451 class SGDistScaleAnimation : public SGAnimation
452 {
453 public:
454   SGDistScaleAnimation(SGPropertyNode_ptr props);
455   virtual ~SGDistScaleAnimation ();
456
457   static void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
458   void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
459
460 private:
461   sgVec3 _center;
462   float _factor, _offset, _min_v, _max_v;
463   bool _has_min, _has_max;
464   SGInterpTable * _table;
465 };
466
467
468 #endif // _SG_ANIMATION_HXX