]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/animation.hxx
Solve the endless loop problem for the DC-3 and prevent a potential segmentation...
[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 void init();
191     virtual int update();
192 private:
193     bool _use_personality;
194     enum PersonalityVar { INIT, LAST_TIME_SEC, TOTAL_DURATION_SEC, BRANCH_DURATION_SEC, STEP };
195     double _duration_sec;
196     double _last_time_sec;
197     double _total_duration_sec;
198     int _step;
199     struct DurationSpec {
200         DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
201         DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
202         double _min, _max;
203     };
204     vector<DurationSpec> _branch_duration_specs;
205     vector<double> _branch_duration_sec;
206 };
207
208
209 /**
210  * Animation to rotate an object around a center point.
211  *
212  * This animation rotates to a specific position.
213  */
214 class SGRotateAnimation : public SGAnimation
215 {
216 public:
217   SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
218   virtual ~SGRotateAnimation ();
219   virtual int update();
220 private:
221   SGPropertyNode_ptr _prop;
222   double _offset_deg;
223   double _factor;
224   SGInterpTable * _table;
225   bool _has_min;
226   double _min_deg;
227   bool _has_max;
228   double _max_deg;
229   double _position_deg;
230   sgMat4 _matrix;
231   sgVec3 _center;
232   sgVec3 _axis;
233   SGCondition * _condition;
234 };
235
236
237 /**
238  * Animation to slide along an axis.
239  */
240 class SGTranslateAnimation : public SGAnimation
241 {
242 public:
243   SGTranslateAnimation( SGPropertyNode *prop_root,
244                       SGPropertyNode_ptr props );
245   virtual ~SGTranslateAnimation ();
246   virtual int update();
247 private:
248   SGPropertyNode_ptr _prop;
249   double _offset_m;
250   double _factor;
251   SGInterpTable * _table;
252   bool _has_min;
253   double _min_m;
254   bool _has_max;
255   double _max_m;
256   double _position_m;
257   sgMat4 _matrix;
258   sgVec3 _axis;
259   SGCondition * _condition;
260 };
261
262 /**
263  * Animation to blend an object.
264  */
265 class SGBlendAnimation : public SGAnimation
266 {
267 public:
268   SGBlendAnimation( SGPropertyNode *prop_root,
269                       SGPropertyNode_ptr props );
270   virtual ~SGBlendAnimation ();
271   virtual int update();
272 private:
273   SGPropertyNode_ptr _prop;
274   SGInterpTable * _table;
275   double _prev_value;
276   double _offset;
277   double _factor;
278   bool _has_min;
279   double _min;
280   bool _has_max;
281   double _max;
282 };
283
284 /**
285  * Animation to scale an object.
286  */
287 class SGScaleAnimation : public SGAnimation
288 {
289 public:
290   SGScaleAnimation( SGPropertyNode *prop_root,
291                         SGPropertyNode_ptr props );
292   virtual ~SGScaleAnimation ();
293   virtual int update();
294 private:
295   SGPropertyNode_ptr _prop;
296   double _x_factor;
297   double _y_factor;
298   double _z_factor;
299   double _x_offset;
300   double _y_offset;
301   double _z_offset;
302   SGInterpTable * _table;
303   bool _has_min_x;
304   bool _has_min_y;
305   bool _has_min_z;
306   double _min_x;
307   double _min_y;
308   double _min_z;
309   bool _has_max_x;
310   bool _has_max_y;
311   bool _has_max_z;
312   double _max_x;
313   double _max_y;
314   double _max_z;
315   double _x_scale;
316   double _y_scale;
317   double _z_scale;
318   sgMat4 _matrix;
319 };
320
321 /**
322  * Animation to rotate texture mappings around a center point.
323  *
324  * This animation rotates to a specific position.
325  */
326 class SGTexRotateAnimation : public SGAnimation
327 {
328 public:
329   SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
330   virtual ~SGTexRotateAnimation ();
331   virtual int update();
332 private:
333   SGPropertyNode_ptr _prop;
334   double _offset_deg;
335   double _factor;
336   SGInterpTable * _table;
337   bool _has_min;
338   double _min_deg;
339   bool _has_max;
340   double _max_deg;
341   double _position_deg;
342   sgMat4 _matrix;
343   sgVec3 _center;
344   sgVec3 _axis;
345 };
346
347
348 /**
349  * Animation to slide texture mappings along an axis.
350  */
351 class SGTexTranslateAnimation : public SGAnimation
352 {
353 public:
354   SGTexTranslateAnimation( SGPropertyNode *prop_root,
355                       SGPropertyNode_ptr props );
356   virtual ~SGTexTranslateAnimation ();
357   virtual int update();
358 private:
359   SGPropertyNode_ptr _prop;
360   double _offset;
361   double _factor;
362   double _step;
363   double _scroll;
364   SGInterpTable * _table;
365   bool _has_min;
366   double _min;
367   bool _has_max;
368   double _max;
369   double _position;
370   sgMat4 _matrix;
371   sgVec3 _axis;
372 };
373
374
375
376 /**
377  * Classes for handling multiple types of Texture translations on one object
378  */
379
380 class SGTexMultipleAnimation : public SGAnimation
381 {
382 public:
383   SGTexMultipleAnimation( SGPropertyNode *prop_root,
384                       SGPropertyNode_ptr props );
385   virtual ~SGTexMultipleAnimation ();
386   virtual int update();
387 private:
388   class TexTransform
389     {
390     public:
391     SGPropertyNode_ptr prop;
392     int subtype; //  0=translation, 1=rotation
393     double offset;
394     double factor;
395     double step;
396     double scroll;
397     SGInterpTable * table;
398     bool has_min;
399     double min;
400     bool has_max;
401     double max;
402     double position;
403     sgMat4 matrix;
404     sgVec3 center;
405     sgVec3 axis;
406   };
407   SGPropertyNode_ptr _prop;
408   TexTransform* _transform;
409   int _num_transforms;
410 };
411
412
413 /**
414  * An "animation" to enable the alpha test 
415  */
416 class SGAlphaTestAnimation : public SGAnimation
417 {
418 public:
419   SGAlphaTestAnimation(SGPropertyNode_ptr props);
420   virtual ~SGAlphaTestAnimation ();
421   virtual void init();
422 private:
423   void setAlphaClampToBranch(ssgBranch *b, float clamp);
424   float _alpha_clamp;
425 };
426
427
428 /**
429  * An "animation" that compute a scale according to 
430  * the angle between an axis and the view direction
431  */
432 class SGFlashAnimation : public SGAnimation
433 {
434 public:
435   SGFlashAnimation(SGPropertyNode_ptr props);
436   virtual ~SGFlashAnimation ();
437
438   static void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
439   void flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
440
441 private:
442   sgVec3 _axis, _center;
443   float _power, _factor, _offset, _min_v, _max_v;
444   bool _two_sides;
445 };
446
447
448 /**
449  * An animation that compute a scale according to 
450  * the distance from a point and the viewer
451  */
452 class SGDistScaleAnimation : public SGAnimation
453 {
454 public:
455   SGDistScaleAnimation(SGPropertyNode_ptr props);
456   virtual ~SGDistScaleAnimation ();
457
458   static void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d );
459   void distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m );
460
461 private:
462   sgVec3 _center;
463   float _factor, _offset, _min_v, _max_v;
464   bool _has_min, _has_max;
465   SGInterpTable * _table;
466 };
467
468
469 #endif // _SG_ANIMATION_HXX