]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/animation.cxx
a258a1c726d250db8f5e7497f1c5c776afeb5762
[simgear.git] / simgear / scene / model / animation.cxx
1 // animation.hxx - classes to manage model animation.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6
7 #include <string.h>             // for strcmp()
8
9 #include <plib/sg.h>
10 #include <plib/ssg.h>
11 #include <plib/ul.h>
12
13 #include <simgear/math/interpolater.hxx>
14 #include <simgear/props/condition.hxx>
15 #include <simgear/props/props.hxx>
16
17 #include "animation.hxx"
18
19
20 \f
21 ////////////////////////////////////////////////////////////////////////
22 // Static utility functions.
23 ////////////////////////////////////////////////////////////////////////
24
25 /**
26  * Set up the transform matrix for a spin or rotation.
27  */
28 static void
29 set_rotation (sgMat4 &matrix, double position_deg,
30               sgVec3 &center, sgVec3 &axis)
31 {
32  float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
33  
34  float s = (float) sin ( temp_angle ) ;
35  float c = (float) cos ( temp_angle ) ;
36  float t = SG_ONE - c ;
37
38  // axis was normalized at load time 
39  // hint to the compiler to put these into FP registers
40  float x = axis[0];
41  float y = axis[1];
42  float z = axis[2];
43
44  matrix[0][0] = t * x * x + c ;
45  matrix[0][1] = t * y * x - s * z ;
46  matrix[0][2] = t * z * x + s * y ;
47  matrix[0][3] = SG_ZERO;
48  
49  matrix[1][0] = t * x * y + s * z ;
50  matrix[1][1] = t * y * y + c ;
51  matrix[1][2] = t * z * y - s * x ;
52  matrix[1][3] = SG_ZERO;
53  
54  matrix[2][0] = t * x * z - s * y ;
55  matrix[2][1] = t * y * z + s * x ;
56  matrix[2][2] = t * z * z + c ;
57  matrix[2][3] = SG_ZERO;
58
59   // hint to the compiler to put these into FP registers
60  x = center[0];
61  y = center[1];
62  z = center[2];
63  
64  matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
65  matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
66  matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
67  matrix[3][3] = SG_ONE;
68 }
69
70 /**
71  * Set up the transform matrix for a translation.
72  */
73 static void
74 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
75 {
76   sgVec3 xyz;
77   sgScaleVec3(xyz, axis, position_m);
78   sgMakeTransMat4(matrix, xyz);
79 }
80
81
82 /**
83  * Read an interpolation table from properties.
84  */
85 static SGInterpTable *
86 read_interpolation_table (SGPropertyNode_ptr props)
87 {
88   SGPropertyNode_ptr table_node = props->getNode("interpolation");
89   if (table_node != 0) {
90     SGInterpTable * table = new SGInterpTable();
91     vector<SGPropertyNode_ptr> entries = table_node->getChildren("entry");
92     for (unsigned int i = 0; i < entries.size(); i++)
93       table->addEntry(entries[i]->getDoubleValue("ind", 0.0),
94                       entries[i]->getDoubleValue("dep", 0.0));
95     return table;
96   } else {
97     return 0;
98   }
99 }
100
101
102 \f
103 ////////////////////////////////////////////////////////////////////////
104 // Implementation of SGAnimation
105 ////////////////////////////////////////////////////////////////////////
106
107 // Initialize the static data member
108 double SGAnimation::sim_time_sec = 0.0;
109
110 SGAnimation::SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch)
111     : _branch(branch)
112 {
113     _branch->setName(props->getStringValue("name", 0));
114 }
115
116 SGAnimation::~SGAnimation ()
117 {
118 }
119
120 void
121 SGAnimation::init ()
122 {
123 }
124
125 void
126 SGAnimation::update()
127 {
128 }
129
130
131 \f
132 ////////////////////////////////////////////////////////////////////////
133 // Implementation of SGNullAnimation
134 ////////////////////////////////////////////////////////////////////////
135
136 SGNullAnimation::SGNullAnimation (SGPropertyNode_ptr props)
137   : SGAnimation(props, new ssgBranch)
138 {
139 }
140
141 SGNullAnimation::~SGNullAnimation ()
142 {
143 }
144
145
146 \f
147 ////////////////////////////////////////////////////////////////////////
148 // Implementation of SGRangeAnimation
149 ////////////////////////////////////////////////////////////////////////
150
151 SGRangeAnimation::SGRangeAnimation (SGPropertyNode_ptr props)
152   : SGAnimation(props, new ssgRangeSelector)
153 {
154     float ranges[] = { props->getFloatValue("min-m", 0),
155                        props->getFloatValue("max-m", 5000) };
156     ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
157                        
158 }
159
160 SGRangeAnimation::~SGRangeAnimation ()
161 {
162 }
163
164
165 \f
166 ////////////////////////////////////////////////////////////////////////
167 // Implementation of SGBillboardAnimation
168 ////////////////////////////////////////////////////////////////////////
169
170 SGBillboardAnimation::SGBillboardAnimation (SGPropertyNode_ptr props)
171     : SGAnimation(props, new ssgCutout(props->getBoolValue("spherical", true)))
172 {
173 }
174
175 SGBillboardAnimation::~SGBillboardAnimation ()
176 {
177 }
178
179
180 \f
181 ////////////////////////////////////////////////////////////////////////
182 // Implementation of SGSelectAnimation
183 ////////////////////////////////////////////////////////////////////////
184
185 SGSelectAnimation::SGSelectAnimation( SGPropertyNode *prop_root,
186                                   SGPropertyNode_ptr props )
187   : SGAnimation(props, new ssgSelector),
188     _condition(0)
189 {
190   SGPropertyNode_ptr node = props->getChild("condition");
191   if (node != 0)
192     _condition = sgReadCondition(prop_root, node);
193 }
194
195 SGSelectAnimation::~SGSelectAnimation ()
196 {
197   delete _condition;
198 }
199
200 void
201 SGSelectAnimation::update()
202 {
203   if (_condition != 0 && _condition->test()) 
204       ((ssgSelector *)_branch)->select(0xffff);
205   else
206       ((ssgSelector *)_branch)->select(0x0000);
207 }
208
209
210 \f
211 ////////////////////////////////////////////////////////////////////////
212 // Implementation of SGSpinAnimation
213 ////////////////////////////////////////////////////////////////////////
214
215 SGSpinAnimation::SGSpinAnimation( SGPropertyNode *prop_root,
216                               SGPropertyNode_ptr props,
217                               double sim_time_sec )
218   : SGAnimation(props, new ssgTransform),
219     _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
220     _factor(props->getDoubleValue("factor", 1.0)),
221     _position_deg(props->getDoubleValue("starting-position-deg", 0)),
222     _last_time_sec( sim_time_sec )
223 {
224     _center[0] = props->getFloatValue("center/x-m", 0);
225     _center[1] = props->getFloatValue("center/y-m", 0);
226     _center[2] = props->getFloatValue("center/z-m", 0);
227     _axis[0] = props->getFloatValue("axis/x", 0);
228     _axis[1] = props->getFloatValue("axis/y", 0);
229     _axis[2] = props->getFloatValue("axis/z", 0);
230     sgNormalizeVec3(_axis);
231 }
232
233 SGSpinAnimation::~SGSpinAnimation ()
234 {
235 }
236
237 void
238 SGSpinAnimation::update()
239 {
240   double dt = sim_time_sec - _last_time_sec;
241   _last_time_sec = sim_time_sec;
242
243   float velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
244   _position_deg += (dt * velocity_rpms * 360);
245   while (_position_deg < 0)
246     _position_deg += 360.0;
247   while (_position_deg >= 360.0)
248     _position_deg -= 360.0;
249   set_rotation(_matrix, _position_deg, _center, _axis);
250   ((ssgTransform *)_branch)->setTransform(_matrix);
251 }
252
253
254 \f
255 ////////////////////////////////////////////////////////////////////////
256 // Implementation of SGTimedAnimation
257 ////////////////////////////////////////////////////////////////////////
258
259 SGTimedAnimation::SGTimedAnimation (SGPropertyNode_ptr props)
260   : SGAnimation(props, new ssgSelector),
261     _duration_sec(props->getDoubleValue("duration-sec", 1.0)),
262     _last_time_sec(0),
263     _step(-1)
264 {
265 }
266
267 SGTimedAnimation::~SGTimedAnimation ()
268 {
269 }
270
271 void
272 SGTimedAnimation::update()
273 {
274     if ((sim_time_sec - _last_time_sec) >= _duration_sec) {
275         _last_time_sec = sim_time_sec;
276         _step++;
277         if (_step >= getBranch()->getNumKids())
278             _step = 0;
279         ((ssgSelector *)getBranch())->selectStep(_step);
280     }
281 }
282
283
284 \f
285 ////////////////////////////////////////////////////////////////////////
286 // Implementation of SGRotateAnimation
287 ////////////////////////////////////////////////////////////////////////
288
289 SGRotateAnimation::SGRotateAnimation( SGPropertyNode *prop_root,
290                                   SGPropertyNode_ptr props )
291     : SGAnimation(props, new ssgTransform),
292       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
293       _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
294       _factor(props->getDoubleValue("factor", 1.0)),
295       _table(read_interpolation_table(props)),
296       _has_min(props->hasValue("min-deg")),
297       _min_deg(props->getDoubleValue("min-deg")),
298       _has_max(props->hasValue("max-deg")),
299       _max_deg(props->getDoubleValue("max-deg")),
300       _position_deg(props->getDoubleValue("starting-position-deg", 0))
301 {
302   _center[0] = props->getFloatValue("center/x-m", 0);
303   _center[1] = props->getFloatValue("center/y-m", 0);
304   _center[2] = props->getFloatValue("center/z-m", 0);
305   _axis[0] = props->getFloatValue("axis/x", 0);
306   _axis[1] = props->getFloatValue("axis/y", 0);
307   _axis[2] = props->getFloatValue("axis/z", 0);
308   sgNormalizeVec3(_axis);
309 }
310
311 SGRotateAnimation::~SGRotateAnimation ()
312 {
313   delete _table;
314 }
315
316 void
317 SGRotateAnimation::update()
318 {
319   if (_table == 0) {
320    _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
321    if (_has_min && _position_deg < _min_deg)
322      _position_deg = _min_deg;
323    if (_has_max && _position_deg > _max_deg)
324      _position_deg = _max_deg;
325   } else {
326     _position_deg = _table->interpolate(_prop->getDoubleValue());
327   }
328   set_rotation(_matrix, _position_deg, _center, _axis);
329   ((ssgTransform *)_branch)->setTransform(_matrix);
330 }
331
332
333 \f
334 ////////////////////////////////////////////////////////////////////////
335 // Implementation of SGTranslateAnimation
336 ////////////////////////////////////////////////////////////////////////
337
338 SGTranslateAnimation::SGTranslateAnimation( SGPropertyNode *prop_root,
339                                         SGPropertyNode_ptr props )
340   : SGAnimation(props, new ssgTransform),
341       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
342     _offset_m(props->getDoubleValue("offset-m", 0.0)),
343     _factor(props->getDoubleValue("factor", 1.0)),
344     _table(read_interpolation_table(props)),
345     _has_min(props->hasValue("min-m")),
346     _min_m(props->getDoubleValue("min-m")),
347     _has_max(props->hasValue("max-m")),
348     _max_m(props->getDoubleValue("max-m")),
349     _position_m(props->getDoubleValue("starting-position-m", 0))
350 {
351   _axis[0] = props->getFloatValue("axis/x", 0);
352   _axis[1] = props->getFloatValue("axis/y", 0);
353   _axis[2] = props->getFloatValue("axis/z", 0);
354   sgNormalizeVec3(_axis);
355 }
356
357 SGTranslateAnimation::~SGTranslateAnimation ()
358 {
359   delete _table;
360 }
361
362 void
363 SGTranslateAnimation::update()
364 {
365   if (_table == 0) {
366     _position_m = (_prop->getDoubleValue() + _offset_m) * _factor;
367     if (_has_min && _position_m < _min_m)
368       _position_m = _min_m;
369     if (_has_max && _position_m > _max_m)
370       _position_m = _max_m;
371   } else {
372     _position_m = _table->interpolate(_prop->getDoubleValue());
373   }
374   set_translation(_matrix, _position_m, _axis);
375   ((ssgTransform *)_branch)->setTransform(_matrix);
376 }
377
378
379 // end of animation.cxx