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