]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/animation.cxx
68cd908f888852dcc112fc9c3f91bbf886168d54
[simgear.git] / simgear / scene / model / animation.cxx
1 // animation.cxx - 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 #include <math.h>
9
10 #include <plib/sg.h>
11 #include <plib/ssg.h>
12 #include <plib/ul.h>
13
14 #include <simgear/math/interpolater.hxx>
15 #include <simgear/props/condition.hxx>
16 #include <simgear/props/props.hxx>
17 #include <simgear/math/sg_random.h>
18
19 #include "animation.hxx"
20 #include "custtrans.hxx"
21 #include "personality.hxx"
22
23 \f
24 ////////////////////////////////////////////////////////////////////////
25 // Static utility functions.
26 ////////////////////////////////////////////////////////////////////////
27
28 /**
29  * Set up the transform matrix for a spin or rotation.
30  */
31 static void
32 set_rotation (sgMat4 &matrix, double position_deg,
33               sgVec3 &center, sgVec3 &axis)
34 {
35  float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
36  
37  float s = (float) sin ( temp_angle ) ;
38  float c = (float) cos ( temp_angle ) ;
39  float t = SG_ONE - c ;
40
41  // axis was normalized at load time 
42  // hint to the compiler to put these into FP registers
43  float x = axis[0];
44  float y = axis[1];
45  float z = axis[2];
46
47  matrix[0][0] = t * x * x + c ;
48  matrix[0][1] = t * y * x - s * z ;
49  matrix[0][2] = t * z * x + s * y ;
50  matrix[0][3] = SG_ZERO;
51  
52  matrix[1][0] = t * x * y + s * z ;
53  matrix[1][1] = t * y * y + c ;
54  matrix[1][2] = t * z * y - s * x ;
55  matrix[1][3] = SG_ZERO;
56  
57  matrix[2][0] = t * x * z - s * y ;
58  matrix[2][1] = t * y * z + s * x ;
59  matrix[2][2] = t * z * z + c ;
60  matrix[2][3] = SG_ZERO;
61
62   // hint to the compiler to put these into FP registers
63  x = center[0];
64  y = center[1];
65  z = center[2];
66  
67  matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
68  matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
69  matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
70  matrix[3][3] = SG_ONE;
71 }
72
73 /**
74  * Set up the transform matrix for a translation.
75  */
76 static void
77 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
78 {
79   sgVec3 xyz;
80   sgScaleVec3(xyz, axis, position_m);
81   sgMakeTransMat4(matrix, xyz);
82 }
83
84 /**
85  * Set up the transform matrix for a scale operation.
86  */
87 static void
88 set_scale (sgMat4 &matrix, double x, double y, double z)
89 {
90   sgMakeIdentMat4( matrix );
91   matrix[0][0] = x;
92   matrix[1][1] = y;
93   matrix[2][2] = z;
94 }
95
96 /**
97  * Recursively process all kids to change the alpha values
98  */
99 static void
100 change_alpha( ssgBase *_branch, float _blend )
101 {
102   int i;
103
104   for (i = 0; i < ((ssgBranch *)_branch)->getNumKids(); i++)
105     change_alpha( ((ssgBranch *)_branch)->getKid(i), _blend );
106
107   if ( !_branch->isAKindOf(ssgTypeLeaf())
108        && !_branch->isAKindOf(ssgTypeVtxTable())
109        && !_branch->isAKindOf(ssgTypeVTable()) )
110     return;
111
112   int num_colors = ((ssgLeaf *)_branch)->getNumColours();
113 // unsigned int select_ = (_blend == 1.0) ? false : true;
114
115   for (i = 0; i < num_colors; i++)
116   {
117 //    ((ssgSelector *)_branch)->select( select_ );
118     float *color =  ((ssgLeaf *)_branch)->getColour(i);
119     color[3] = _blend;
120   }
121 }
122
123 /**
124  * Modify property value by step and scroll settings in texture translations
125  */
126 static double
127 apply_mods(double property, double step, double scroll)
128 {
129
130   double modprop;
131   if(step > 0) {
132     double scrollval = 0.0;
133     if(scroll > 0) {
134       // calculate scroll amount (for odometer like movement)
135       double remainder  =  step - fmod(fabs(property), step);
136       if (remainder < scroll) {
137         scrollval = (scroll - remainder) / scroll * step;
138       }
139     }
140   // apply stepping of input value
141   if(property > 0) 
142      modprop = ((floor(property/step) * step) + scrollval);
143   else
144      modprop = ((ceil(property/step) * step) + scrollval);
145   } else {
146      modprop = property;
147   }
148   return modprop;
149
150 }
151
152 /**
153  * Read an interpolation table from properties.
154  */
155 static SGInterpTable *
156 read_interpolation_table (SGPropertyNode_ptr props)
157 {
158   SGPropertyNode_ptr table_node = props->getNode("interpolation");
159   if (table_node != 0) {
160     SGInterpTable * table = new SGInterpTable();
161     vector<SGPropertyNode_ptr> entries = table_node->getChildren("entry");
162     for (unsigned int i = 0; i < entries.size(); i++)
163       table->addEntry(entries[i]->getDoubleValue("ind", 0.0),
164                       entries[i]->getDoubleValue("dep", 0.0));
165     return table;
166   } else {
167     return 0;
168   }
169 }
170
171
172 \f
173 ////////////////////////////////////////////////////////////////////////
174 // Implementation of SGAnimation
175 ////////////////////////////////////////////////////////////////////////
176
177 // Initialize the static data member
178 double SGAnimation::sim_time_sec = 0.0;
179 SGPersonalityBranch *SGAnimation::current_object = 0;
180
181 SGAnimation::SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch)
182     : _branch(branch)
183 {
184     _branch->setName(props->getStringValue("name", 0));
185 }
186
187 SGAnimation::~SGAnimation ()
188 {
189 }
190
191 void
192 SGAnimation::init ()
193 {
194 }
195
196 int
197 SGAnimation::update()
198 {
199     return 1;
200 }
201
202 void
203 SGAnimation::restore()
204 {
205 }
206
207
208 \f
209 ////////////////////////////////////////////////////////////////////////
210 // Implementation of SGNullAnimation
211 ////////////////////////////////////////////////////////////////////////
212
213 SGNullAnimation::SGNullAnimation (SGPropertyNode_ptr props)
214   : SGAnimation(props, new ssgBranch)
215 {
216 }
217
218 SGNullAnimation::~SGNullAnimation ()
219 {
220 }
221
222
223 \f
224 ////////////////////////////////////////////////////////////////////////
225 // Implementation of SGRangeAnimation
226 ////////////////////////////////////////////////////////////////////////
227
228 SGRangeAnimation::SGRangeAnimation (SGPropertyNode *prop_root,
229                                     SGPropertyNode_ptr props)
230   : SGAnimation(props, new ssgRangeSelector),
231     _min(0.0), _max(0.0), _min_factor(1.0), _max_factor(1.0),
232     _condition(0)
233 {
234     SGPropertyNode_ptr node = props->getChild("condition");
235     if (node != 0)
236        _condition = sgReadCondition(prop_root, node);
237
238     float ranges[2];
239
240     node = props->getChild( "min-factor" );
241     if (node != 0) {
242        _min_factor = props->getFloatValue("min-factor", 1.0);
243     }
244     node = props->getChild( "max-factor" );
245     if (node != 0) {
246        _max_factor = props->getFloatValue("max-factor", 1.0);
247     }
248     node = props->getChild( "min-property" );
249     if (node != 0) {
250        _min_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
251        ranges[0] = _min_prop->getFloatValue() * _min_factor;
252     } else {
253        _min = props->getFloatValue("min-m", 0);
254        ranges[0] = _min * _min_factor;
255     }
256     node = props->getChild( "max-property" );
257     if (node != 0) {
258        _max_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
259        ranges[1] = _max_prop->getFloatValue() * _max_factor;
260     } else {
261        _max = props->getFloatValue("max-m", 0);
262        ranges[1] = _max * _max_factor;
263     }
264     ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
265 }
266
267 SGRangeAnimation::~SGRangeAnimation ()
268 {
269 }
270
271 int
272 SGRangeAnimation::update()
273 {
274   float ranges[2];
275   if ( _condition == 0 || _condition->test() ) {
276     if (_min_prop != 0) {
277       ranges[0] = _min_prop->getFloatValue() * _min_factor;
278     } else {
279       ranges[0] = _min * _min_factor;
280     }
281     if (_max_prop != 0) {
282       ranges[1] = _max_prop->getFloatValue() * _max_factor;
283     } else {
284       ranges[1] = _max * _max_factor;
285     }
286   } else {
287     ranges[0] = 0.f;
288     ranges[1] = 1000000000.f;
289   }
290   ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
291   return 1;
292 }
293
294
295 \f
296 ////////////////////////////////////////////////////////////////////////
297 // Implementation of SGBillboardAnimation
298 ////////////////////////////////////////////////////////////////////////
299
300 SGBillboardAnimation::SGBillboardAnimation (SGPropertyNode_ptr props)
301     : SGAnimation(props, new ssgCutout(props->getBoolValue("spherical", true)))
302 {
303 }
304
305 SGBillboardAnimation::~SGBillboardAnimation ()
306 {
307 }
308
309
310 \f
311 ////////////////////////////////////////////////////////////////////////
312 // Implementation of SGSelectAnimation
313 ////////////////////////////////////////////////////////////////////////
314
315 SGSelectAnimation::SGSelectAnimation( SGPropertyNode *prop_root,
316                                   SGPropertyNode_ptr props )
317   : SGAnimation(props, new ssgSelector),
318     _condition(0)
319 {
320   SGPropertyNode_ptr node = props->getChild("condition");
321   if (node != 0)
322     _condition = sgReadCondition(prop_root, node);
323 }
324
325 SGSelectAnimation::~SGSelectAnimation ()
326 {
327   delete _condition;
328 }
329
330 int
331 SGSelectAnimation::update()
332 {
333   if (_condition != 0 && _condition->test()) 
334       ((ssgSelector *)_branch)->select(0xffff);
335   else
336       ((ssgSelector *)_branch)->select(0x0000);
337   return 1;
338 }
339
340
341 \f
342 ////////////////////////////////////////////////////////////////////////
343 // Implementation of SGSpinAnimation
344 ////////////////////////////////////////////////////////////////////////
345
346 SGSpinAnimation::SGSpinAnimation( SGPropertyNode *prop_root,
347                               SGPropertyNode_ptr props,
348                               double sim_time_sec )
349   : SGAnimation(props, new ssgTransform),
350     _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
351     _factor(props->getDoubleValue("factor", 1.0)),
352     _position_deg(props->getDoubleValue("starting-position-deg", 0)),
353     _last_time_sec( sim_time_sec ),
354     _condition(0)
355 {
356     SGPropertyNode_ptr node = props->getChild("condition");
357     if (node != 0)
358         _condition = sgReadCondition(prop_root, node);
359
360     _center[0] = 0;
361     _center[1] = 0;
362     _center[2] = 0;
363     if (props->hasValue("axis/x1-m")) {
364         double x1,y1,z1,x2,y2,z2;
365         x1 = props->getFloatValue("axis/x1-m");
366         y1 = props->getFloatValue("axis/y1-m");
367         z1 = props->getFloatValue("axis/z1-m");
368         x2 = props->getFloatValue("axis/x2-m");
369         y2 = props->getFloatValue("axis/y2-m");
370         z2 = props->getFloatValue("axis/z2-m");
371         _center[0] = (x1+x2)/2;
372         _center[1]= (y1+y2)/2;
373         _center[2] = (z1+z2)/2;
374         float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
375         _axis[0] = (x2-x1)/vector_length;
376         _axis[1] = (y2-y1)/vector_length;
377         _axis[2] = (z2-z1)/vector_length;
378     } else {
379        _axis[0] = props->getFloatValue("axis/x", 0);
380        _axis[1] = props->getFloatValue("axis/y", 0);
381        _axis[2] = props->getFloatValue("axis/z", 0);
382     }
383     if (props->hasValue("center/x-m")) {
384        _center[0] = props->getFloatValue("center/x-m", 0);
385        _center[1] = props->getFloatValue("center/y-m", 0);
386        _center[2] = props->getFloatValue("center/z-m", 0);
387     }
388     sgNormalizeVec3(_axis);
389 }
390
391 SGSpinAnimation::~SGSpinAnimation ()
392 {
393 }
394
395 int
396 SGSpinAnimation::update()
397 {
398   if ( _condition == 0 || _condition->test() ) {
399     double dt = sim_time_sec - _last_time_sec;
400     _last_time_sec = sim_time_sec;
401
402     float velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
403     _position_deg += (dt * velocity_rpms * 360);
404     while (_position_deg < 0)
405         _position_deg += 360.0;
406     while (_position_deg >= 360.0)
407         _position_deg -= 360.0;
408     set_rotation(_matrix, _position_deg, _center, _axis);
409     ((ssgTransform *)_branch)->setTransform(_matrix);
410   }
411   return 1;
412 }
413
414
415 \f
416 ////////////////////////////////////////////////////////////////////////
417 // Implementation of SGTimedAnimation
418 ////////////////////////////////////////////////////////////////////////
419
420 SGTimedAnimation::SGTimedAnimation (SGPropertyNode_ptr props)
421   : SGAnimation(props, new ssgSelector),
422     _use_personality( props->getBoolValue("use-personality",false) ),
423     _duration_sec(props->getDoubleValue("duration-sec", 1.0)),
424     _last_time_sec( sim_time_sec ),
425     _total_duration_sec( 0 ),
426     _step( 0 )
427     
428 {
429     vector<SGPropertyNode_ptr> nodes = props->getChildren( "branch-duration-sec" );
430     size_t nb = nodes.size();
431     for ( size_t i = 0; i < nb; i++ ) {
432         size_t ind = nodes[ i ]->getIndex();
433         while ( ind >= _branch_duration_specs.size() ) {
434             _branch_duration_specs.push_back( DurationSpec( _duration_sec ) );
435         }
436         SGPropertyNode_ptr rNode = nodes[ i ]->getChild("random");
437         if ( rNode == 0 ) {
438             _branch_duration_specs[ ind ] = DurationSpec( nodes[ i ]->getDoubleValue() );
439         } else {
440             _branch_duration_specs[ ind ] = DurationSpec( rNode->getDoubleValue( "min", 0.0 ),
441                                                           rNode->getDoubleValue( "max", 1.0 ) );
442         }
443     }
444     if ( !_use_personality ) {
445         for ( size_t i = 0; i < _branch_duration_specs.size(); i++ ) {
446             DurationSpec &sp = _branch_duration_specs[ i ];
447             double v = sp._min + sg_random() * ( sp._max - sp._min );
448             _branch_duration_sec.push_back( v );
449             _total_duration_sec += v;
450         }
451     }
452     ((ssgSelector *)getBranch())->selectStep(_step);
453 }
454
455 SGTimedAnimation::~SGTimedAnimation ()
456 {
457 }
458
459 int
460 SGTimedAnimation::update()
461 {
462     if ( _use_personality ) {
463         SGPersonalityBranch *key = current_object;
464         if ( !key->getIntValue( this, INIT ) ) {
465             double total = 0;
466             double offset = 1.0;
467             for ( size_t i = 0; i < _branch_duration_specs.size(); i++ ) {
468                 DurationSpec &sp = _branch_duration_specs[ i ];
469                 double v = sp._min + sg_random() * ( sp._max - sp._min );
470                 key->setDoubleValue( v, this, BRANCH_DURATION_SEC, i );
471                 if ( i == 0 )
472                     offset = v;
473                 total += v;
474             }
475             offset *= sg_random();
476             key->setDoubleValue( sim_time_sec - offset, this, LAST_TIME_SEC );
477             key->setDoubleValue( total, this, TOTAL_DURATION_SEC );
478             key->setIntValue( 0, this, STEP );
479             key->setIntValue( 1, this, INIT );
480         }
481
482         _step = key->getIntValue( this, STEP );
483         _last_time_sec = key->getDoubleValue( this, LAST_TIME_SEC );
484         _total_duration_sec = key->getDoubleValue( this, TOTAL_DURATION_SEC );
485         while ( ( sim_time_sec - _last_time_sec ) >= _total_duration_sec ) {
486             _last_time_sec += _total_duration_sec;
487         }
488         double duration = _duration_sec;
489         if ( _step < (int)_branch_duration_specs.size() ) {
490             duration = key->getDoubleValue( this, BRANCH_DURATION_SEC, _step );
491         }
492         if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
493             _last_time_sec += duration;
494             _step += 1;
495             if ( _step >= getBranch()->getNumKids() )
496                 _step = 0;
497         }
498         ((ssgSelector *)getBranch())->selectStep( _step );
499         key->setDoubleValue( _last_time_sec, this, LAST_TIME_SEC );
500         key->setIntValue( _step, this, STEP );
501     } else {
502         while ( ( sim_time_sec - _last_time_sec ) >= _total_duration_sec ) {
503             _last_time_sec += _total_duration_sec;
504         }
505         double duration = _duration_sec;
506         if ( _step < (int)_branch_duration_sec.size() ) {
507             duration = _branch_duration_sec[ _step ];
508         }
509         if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
510             _last_time_sec += duration;
511             _step += 1;
512             if ( _step >= getBranch()->getNumKids() )
513                 _step = 0;
514             ((ssgSelector *)getBranch())->selectStep( _step );
515         }
516     }
517     return 1;
518 }
519
520
521 \f
522 ////////////////////////////////////////////////////////////////////////
523 // Implementation of SGRotateAnimation
524 ////////////////////////////////////////////////////////////////////////
525
526 SGRotateAnimation::SGRotateAnimation( SGPropertyNode *prop_root,
527                                   SGPropertyNode_ptr props )
528     : SGAnimation(props, new ssgTransform),
529       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
530       _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
531       _factor(props->getDoubleValue("factor", 1.0)),
532       _table(read_interpolation_table(props)),
533       _has_min(props->hasValue("min-deg")),
534       _min_deg(props->getDoubleValue("min-deg")),
535       _has_max(props->hasValue("max-deg")),
536       _max_deg(props->getDoubleValue("max-deg")),
537       _position_deg(props->getDoubleValue("starting-position-deg", 0)),
538       _condition(0)
539 {
540     SGPropertyNode_ptr node = props->getChild("condition");
541     if (node != 0)
542       _condition = sgReadCondition(prop_root, node);
543
544     _center[0] = 0;
545     _center[1] = 0;
546     _center[2] = 0;
547     if (props->hasValue("axis/x1-m")) {
548         double x1,y1,z1,x2,y2,z2;
549         x1 = props->getFloatValue("axis/x1-m");
550         y1 = props->getFloatValue("axis/y1-m");
551         z1 = props->getFloatValue("axis/z1-m");
552         x2 = props->getFloatValue("axis/x2-m");
553         y2 = props->getFloatValue("axis/y2-m");
554         z2 = props->getFloatValue("axis/z2-m");
555         _center[0] = (x1+x2)/2;
556         _center[1]= (y1+y2)/2;
557         _center[2] = (z1+z2)/2;
558         float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
559         _axis[0] = (x2-x1)/vector_length;
560         _axis[1] = (y2-y1)/vector_length;
561         _axis[2] = (z2-z1)/vector_length;
562     } else {
563        _axis[0] = props->getFloatValue("axis/x", 0);
564        _axis[1] = props->getFloatValue("axis/y", 0);
565        _axis[2] = props->getFloatValue("axis/z", 0);
566     }
567     if (props->hasValue("center/x-m")) {
568        _center[0] = props->getFloatValue("center/x-m", 0);
569        _center[1] = props->getFloatValue("center/y-m", 0);
570        _center[2] = props->getFloatValue("center/z-m", 0);
571     }
572     sgNormalizeVec3(_axis);
573 }
574
575 SGRotateAnimation::~SGRotateAnimation ()
576 {
577   delete _table;
578 }
579
580 int
581 SGRotateAnimation::update()
582 {
583   if (_condition == 0 || _condition->test()) {
584     if (_table == 0) {
585       _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
586       if (_has_min && _position_deg < _min_deg)
587         _position_deg = _min_deg;
588       if (_has_max && _position_deg > _max_deg)
589         _position_deg = _max_deg;
590     } else {
591       _position_deg = _table->interpolate(_prop->getDoubleValue());
592     }
593     set_rotation(_matrix, _position_deg, _center, _axis);
594     ((ssgTransform *)_branch)->setTransform(_matrix);
595   }
596   return 1;
597 }
598
599 \f
600 ////////////////////////////////////////////////////////////////////////
601 // Implementation of SGBlendAnimation
602 ////////////////////////////////////////////////////////////////////////
603
604 SGBlendAnimation::SGBlendAnimation( SGPropertyNode *prop_root,
605                                         SGPropertyNode_ptr props )
606   : SGAnimation(props, new ssgTransform),
607     _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
608     _table(read_interpolation_table(props)),
609     _prev_value(1.0),
610     _offset(props->getDoubleValue("offset", 0.0)),
611     _factor(props->getDoubleValue("factor", 1.0)),
612     _has_min(props->hasValue("min")),
613     _min(props->getDoubleValue("min", 0.0)),
614     _has_max(props->hasValue("max")),
615     _max(props->getDoubleValue("max", 1.0))
616 {
617 }
618
619 SGBlendAnimation::~SGBlendAnimation ()
620 {
621     delete _table;
622 }
623
624 int
625 SGBlendAnimation::update()
626 {
627   double _blend;
628
629   if (_table == 0) {
630     _blend = 1.0 - (_prop->getDoubleValue() * _factor + _offset);
631
632     if (_has_min && (_blend < _min))
633       _blend = _min;
634     if (_has_max && (_blend > _max))
635       _blend = _max;
636   } else {
637     _blend = _table->interpolate(_prop->getDoubleValue());
638   }
639
640   if (_blend != _prev_value) {
641     _prev_value = _blend;
642     change_alpha( _branch, _blend );
643   }
644   return 1;
645 }
646
647
648 \f
649 ////////////////////////////////////////////////////////////////////////
650 // Implementation of SGTranslateAnimation
651 ////////////////////////////////////////////////////////////////////////
652
653 SGTranslateAnimation::SGTranslateAnimation( SGPropertyNode *prop_root,
654                                         SGPropertyNode_ptr props )
655   : SGAnimation(props, new ssgTransform),
656       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
657     _offset_m(props->getDoubleValue("offset-m", 0.0)),
658     _factor(props->getDoubleValue("factor", 1.0)),
659     _table(read_interpolation_table(props)),
660     _has_min(props->hasValue("min-m")),
661     _min_m(props->getDoubleValue("min-m")),
662     _has_max(props->hasValue("max-m")),
663     _max_m(props->getDoubleValue("max-m")),
664     _position_m(props->getDoubleValue("starting-position-m", 0)),
665     _condition(0)
666 {
667   SGPropertyNode_ptr node = props->getChild("condition");
668   if (node != 0)
669     _condition = sgReadCondition(prop_root, node);
670
671   _axis[0] = props->getFloatValue("axis/x", 0);
672   _axis[1] = props->getFloatValue("axis/y", 0);
673   _axis[2] = props->getFloatValue("axis/z", 0);
674   sgNormalizeVec3(_axis);
675 }
676
677 SGTranslateAnimation::~SGTranslateAnimation ()
678 {
679   delete _table;
680 }
681
682 int
683 SGTranslateAnimation::update()
684 {
685   if (_condition == 0 || _condition->test()) {
686     if (_table == 0) {
687       _position_m = (_prop->getDoubleValue() + _offset_m) * _factor;
688       if (_has_min && _position_m < _min_m)
689         _position_m = _min_m;
690       if (_has_max && _position_m > _max_m)
691         _position_m = _max_m;
692     } else {
693       _position_m = _table->interpolate(_prop->getDoubleValue());
694     }
695     set_translation(_matrix, _position_m, _axis);
696     ((ssgTransform *)_branch)->setTransform(_matrix);
697   }
698   return 1;
699 }
700
701
702 \f
703 ////////////////////////////////////////////////////////////////////////
704 // Implementation of SGScaleAnimation
705 ////////////////////////////////////////////////////////////////////////
706
707 SGScaleAnimation::SGScaleAnimation( SGPropertyNode *prop_root,
708                                         SGPropertyNode_ptr props )
709   : SGAnimation(props, new ssgTransform),
710       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
711     _x_factor(props->getDoubleValue("x-factor", 1.0)),
712     _y_factor(props->getDoubleValue("y-factor", 1.0)),
713     _z_factor(props->getDoubleValue("z-factor", 1.0)),
714     _x_offset(props->getDoubleValue("x-offset", 1.0)),
715     _y_offset(props->getDoubleValue("y-offset", 1.0)),
716     _z_offset(props->getDoubleValue("z-offset", 1.0)),
717     _table(read_interpolation_table(props)),
718     _has_min_x(props->hasValue("x-min")),
719     _has_min_y(props->hasValue("y-min")),
720     _has_min_z(props->hasValue("z-min")),
721     _min_x(props->getDoubleValue("x-min")),
722     _min_y(props->getDoubleValue("y-min")),
723     _min_z(props->getDoubleValue("z-min")),
724     _has_max_x(props->hasValue("x-max")),
725     _has_max_y(props->hasValue("y-max")),
726     _has_max_z(props->hasValue("z-max")),
727     _max_x(props->getDoubleValue("x-max")),
728     _max_y(props->getDoubleValue("y-max")),
729     _max_z(props->getDoubleValue("z-max"))
730 {
731 }
732
733 SGScaleAnimation::~SGScaleAnimation ()
734 {
735   delete _table;
736 }
737
738 int
739 SGScaleAnimation::update()
740 {
741   if (_table == 0) {
742       _x_scale = _prop->getDoubleValue() * _x_factor + _x_offset;
743     if (_has_min_x && _x_scale < _min_x)
744       _x_scale = _min_x;
745     if (_has_max_x && _x_scale > _max_x)
746       _x_scale = _max_x;
747   } else {
748     _x_scale = _table->interpolate(_prop->getDoubleValue());
749   }
750
751   if (_table == 0) {
752     _y_scale = _prop->getDoubleValue() * _y_factor + _y_offset;
753     if (_has_min_y && _y_scale < _min_y)
754       _y_scale = _min_y;
755     if (_has_max_y && _y_scale > _max_y)
756       _y_scale = _max_y;
757   } else {
758     _y_scale = _table->interpolate(_prop->getDoubleValue());
759   }
760
761   if (_table == 0) {
762     _z_scale = _prop->getDoubleValue() * _z_factor + _z_offset;
763     if (_has_min_z && _z_scale < _min_z)
764       _z_scale = _min_z;
765     if (_has_max_z && _z_scale > _max_z)
766       _z_scale = _max_z;
767   } else {
768     _z_scale = _table->interpolate(_prop->getDoubleValue());
769   }
770
771   set_scale(_matrix, _x_scale, _y_scale, _z_scale );
772   ((ssgTransform *)_branch)->setTransform(_matrix);
773   return 1;
774 }
775
776
777 ////////////////////////////////////////////////////////////////////////
778 // Implementation of SGTexRotateAnimation
779 ////////////////////////////////////////////////////////////////////////
780
781 SGTexRotateAnimation::SGTexRotateAnimation( SGPropertyNode *prop_root,
782                                   SGPropertyNode_ptr props )
783     : SGAnimation(props, new ssgTexTrans),
784       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
785       _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
786       _factor(props->getDoubleValue("factor", 1.0)),
787       _table(read_interpolation_table(props)),
788       _has_min(props->hasValue("min-deg")),
789       _min_deg(props->getDoubleValue("min-deg")),
790       _has_max(props->hasValue("max-deg")),
791       _max_deg(props->getDoubleValue("max-deg")),
792       _position_deg(props->getDoubleValue("starting-position-deg", 0))
793 {
794   _center[0] = props->getFloatValue("center/x", 0);
795   _center[1] = props->getFloatValue("center/y", 0);
796   _center[2] = props->getFloatValue("center/z", 0);
797   _axis[0] = props->getFloatValue("axis/x", 0);
798   _axis[1] = props->getFloatValue("axis/y", 0);
799   _axis[2] = props->getFloatValue("axis/z", 0);
800   sgNormalizeVec3(_axis);
801 }
802
803 SGTexRotateAnimation::~SGTexRotateAnimation ()
804 {
805   delete _table;
806 }
807
808 int
809 SGTexRotateAnimation::update()
810 {
811   if (_table == 0) {
812    _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
813    if (_has_min && _position_deg < _min_deg)
814      _position_deg = _min_deg;
815    if (_has_max && _position_deg > _max_deg)
816      _position_deg = _max_deg;
817   } else {
818     _position_deg = _table->interpolate(_prop->getDoubleValue());
819   }
820   set_rotation(_matrix, _position_deg, _center, _axis);
821   ((ssgTexTrans *)_branch)->setTransform(_matrix);
822   return 1;
823 }
824
825
826 ////////////////////////////////////////////////////////////////////////
827 // Implementation of SGTexTranslateAnimation
828 ////////////////////////////////////////////////////////////////////////
829
830 SGTexTranslateAnimation::SGTexTranslateAnimation( SGPropertyNode *prop_root,
831                                         SGPropertyNode_ptr props )
832   : SGAnimation(props, new ssgTexTrans),
833       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
834     _offset(props->getDoubleValue("offset", 0.0)),
835     _factor(props->getDoubleValue("factor", 1.0)),
836     _step(props->getDoubleValue("step",0.0)),
837     _scroll(props->getDoubleValue("scroll",0.0)),
838     _table(read_interpolation_table(props)),
839     _has_min(props->hasValue("min")),
840     _min(props->getDoubleValue("min")),
841     _has_max(props->hasValue("max")),
842     _max(props->getDoubleValue("max")),
843     _position(props->getDoubleValue("starting-position", 0))
844 {
845   _axis[0] = props->getFloatValue("axis/x", 0);
846   _axis[1] = props->getFloatValue("axis/y", 0);
847   _axis[2] = props->getFloatValue("axis/z", 0);
848   sgNormalizeVec3(_axis);
849 }
850
851 SGTexTranslateAnimation::~SGTexTranslateAnimation ()
852 {
853   delete _table;
854 }
855
856 int
857 SGTexTranslateAnimation::update()
858 {
859   if (_table == 0) {
860     _position = (apply_mods(_prop->getDoubleValue(), _step, _scroll) + _offset) * _factor;
861     if (_has_min && _position < _min)
862       _position = _min;
863     if (_has_max && _position > _max)
864       _position = _max;
865   } else {
866     _position = _table->interpolate(apply_mods(_prop->getDoubleValue(), _step, _scroll));
867   }
868   set_translation(_matrix, _position, _axis);
869   ((ssgTexTrans *)_branch)->setTransform(_matrix);
870   return 1;
871 }
872
873
874 ////////////////////////////////////////////////////////////////////////
875 // Implementation of SGTexMultipleAnimation
876 ////////////////////////////////////////////////////////////////////////
877
878 SGTexMultipleAnimation::SGTexMultipleAnimation( SGPropertyNode *prop_root,
879                                         SGPropertyNode_ptr props )
880   : SGAnimation(props, new ssgTexTrans),
881       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true))
882 {
883   unsigned int i;
884   // Load animations
885   vector<SGPropertyNode_ptr> transform_nodes = props->getChildren("transform");
886   _transform = new TexTransform [transform_nodes.size()];
887   _num_transforms = 0;
888   for (i = 0; i < transform_nodes.size(); i++) {
889     SGPropertyNode_ptr transform_props = transform_nodes[i];
890
891     if (!strcmp("textranslate",transform_props->getStringValue("subtype", 0))) {
892
893       // transform is a translation
894       _transform[i].subtype = 0;
895
896       _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
897
898       _transform[i].offset = transform_props->getDoubleValue("offset", 0.0);
899       _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
900       _transform[i].step = transform_props->getDoubleValue("step",0.0);
901       _transform[i].scroll = transform_props->getDoubleValue("scroll",0.0);
902       _transform[i].table = read_interpolation_table(transform_props);
903       _transform[i].has_min = transform_props->hasValue("min");
904       _transform[i].min = transform_props->getDoubleValue("min");
905       _transform[i].has_max = transform_props->hasValue("max");
906       _transform[i].max = transform_props->getDoubleValue("max");
907       _transform[i].position = transform_props->getDoubleValue("starting-position", 0);
908
909       _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
910       _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
911       _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
912       sgNormalizeVec3(_transform[i].axis);
913       _num_transforms++;
914     } else if (!strcmp("texrotate",transform_nodes[i]->getStringValue("subtype", 0))) {
915
916       // transform is a rotation
917       _transform[i].subtype = 1;
918
919       _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
920       _transform[i].offset = transform_props->getDoubleValue("offset-deg", 0.0);
921       _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
922       _transform[i].table = read_interpolation_table(transform_props);
923       _transform[i].has_min = transform_props->hasValue("min-deg");
924       _transform[i].min = transform_props->getDoubleValue("min-deg");
925       _transform[i].has_max = transform_props->hasValue("max-deg");
926       _transform[i].max = transform_props->getDoubleValue("max-deg");
927       _transform[i].position = transform_props->getDoubleValue("starting-position-deg", 0);
928
929       _transform[i].center[0] = transform_props->getFloatValue("center/x", 0);
930       _transform[i].center[1] = transform_props->getFloatValue("center/y", 0);
931       _transform[i].center[2] = transform_props->getFloatValue("center/z", 0);
932       _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
933       _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
934       _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
935       sgNormalizeVec3(_transform[i].axis);
936       _num_transforms++;
937     }
938   }
939 }
940
941 SGTexMultipleAnimation::~SGTexMultipleAnimation ()
942 {
943   // delete _table;
944   delete _transform;
945 }
946
947 int
948 SGTexMultipleAnimation::update()
949 {
950   int i;
951   sgMat4 tmatrix;
952   sgMakeIdentMat4(tmatrix);
953   for (i = 0; i < _num_transforms; i++) {
954
955     if(_transform[i].subtype == 0) {
956
957       // subtype 0 is translation
958       if (_transform[i].table == 0) {
959         _transform[i].position = (apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll) + _transform[i].offset) * _transform[i].factor;
960         if (_transform[i].has_min && _transform[i].position < _transform[i].min)
961           _transform[i].position = _transform[i].min;
962         if (_transform[i].has_max && _transform[i].position > _transform[i].max)
963           _transform[i].position = _transform[i].max;
964       } else {
965          _transform[i].position = _transform[i].table->interpolate(apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll));
966       }
967       set_translation(_transform[i].matrix, _transform[i].position, _transform[i].axis);
968       sgPreMultMat4(tmatrix, _transform[i].matrix);
969
970     } else if (_transform[i].subtype == 1) {
971
972       // subtype 1 is rotation
973
974       if (_transform[i].table == 0) {
975         _transform[i].position = _transform[i].prop->getDoubleValue() * _transform[i].factor + _transform[i].offset;
976         if (_transform[i].has_min && _transform[i].position < _transform[i].min)
977          _transform[i].position = _transform[i].min;
978        if (_transform[i].has_max && _transform[i].position > _transform[i].max)
979          _transform[i].position = _transform[i].max;
980      } else {
981         _transform[i].position = _transform[i].table->interpolate(_transform[i].prop->getDoubleValue());
982       }
983       set_rotation(_transform[i].matrix, _transform[i].position, _transform[i].center, _transform[i].axis);
984       sgPreMultMat4(tmatrix, _transform[i].matrix);
985     }
986   }
987   ((ssgTexTrans *)_branch)->setTransform(tmatrix);
988   return 1;
989 }
990
991
992 \f
993 ////////////////////////////////////////////////////////////////////////
994 // Implementation of SGAlphaTestAnimation
995 ////////////////////////////////////////////////////////////////////////
996
997 SGAlphaTestAnimation::SGAlphaTestAnimation(SGPropertyNode_ptr props)
998   : SGAnimation(props, new ssgBranch)
999 {
1000   _alpha_clamp = props->getFloatValue("alpha-factor", 0.0);
1001 }
1002
1003 SGAlphaTestAnimation::~SGAlphaTestAnimation ()
1004 {
1005 }
1006
1007 void SGAlphaTestAnimation::init()
1008 {
1009   setAlphaClampToBranch(_branch,_alpha_clamp);
1010 }
1011
1012 void SGAlphaTestAnimation::setAlphaClampToBranch(ssgBranch *b, float clamp)
1013 {
1014   int nb = b->getNumKids();
1015   for (int i = 0; i<nb; i++) {
1016     ssgEntity *e = b->getKid(i);
1017     if (e->isAKindOf(ssgTypeLeaf())) {
1018       ssgSimpleState*s = (ssgSimpleState*)((ssgLeaf*)e)->getState();
1019       s->enable( GL_ALPHA_TEST );
1020       s->setAlphaClamp( clamp );
1021     } else if (e->isAKindOf(ssgTypeBranch())) {
1022       setAlphaClampToBranch( (ssgBranch*)e, clamp );
1023     }
1024   }
1025 }
1026
1027
1028 \f
1029 ////////////////////////////////////////////////////////////////////////
1030 // Implementation of SGFlashAnimation
1031 ////////////////////////////////////////////////////////////////////////
1032 SGFlashAnimation::SGFlashAnimation(SGPropertyNode_ptr props)
1033   : SGAnimation( props, new SGCustomTransform )
1034 {
1035   _axis[0] = props->getFloatValue("axis/x", 0);
1036   _axis[1] = props->getFloatValue("axis/y", 0);
1037   _axis[2] = props->getFloatValue("axis/z", 1);
1038
1039   _center[0] = props->getFloatValue("center/x-m", 0);
1040   _center[1] = props->getFloatValue("center/y-m", 0);
1041   _center[2] = props->getFloatValue("center/z-m", 0);
1042
1043   _offset = props->getFloatValue("offset", 0.0);
1044   _factor = props->getFloatValue("factor", 1.0);
1045   _power = props->getFloatValue("power", 1.0);
1046   _two_sides = props->getBoolValue("two-sides", false);
1047
1048   _min_v = props->getFloatValue("min", 0.0);
1049   _max_v = props->getFloatValue("max", 1.0);
1050
1051   ((SGCustomTransform *)_branch)->setTransCallback( &SGFlashAnimation::flashCallback, this );
1052 }
1053
1054 SGFlashAnimation::~SGFlashAnimation()
1055 {
1056 }
1057
1058 void SGFlashAnimation::flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d )
1059 {
1060   ((SGFlashAnimation *)d)->flashCallback( r, f, m );
1061 }
1062
1063 void SGFlashAnimation::flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m )
1064 {
1065   sgVec3 transformed_axis;
1066   sgXformVec3( transformed_axis, _axis, m );
1067   sgNormalizeVec3( transformed_axis );
1068
1069   sgVec3 view;
1070   sgFullXformPnt3( view, _center, m );
1071   sgNormalizeVec3( view );
1072
1073   float cos_angle = -sgScalarProductVec3( transformed_axis, view );
1074   float scale_factor = 0.f;
1075   if ( _two_sides && cos_angle < 0 )
1076     scale_factor = _factor * (float)pow( -cos_angle, _power ) + _offset;
1077   else if ( cos_angle > 0 )
1078     scale_factor = _factor * (float)pow( cos_angle, _power ) + _offset;
1079
1080   if ( scale_factor < _min_v )
1081       scale_factor = _min_v;
1082   if ( scale_factor > _max_v )
1083       scale_factor = _max_v;
1084
1085   sgMat4 transform;
1086   sgMakeIdentMat4( transform );
1087   transform[0][0] = scale_factor;
1088   transform[1][1] = scale_factor;
1089   transform[2][2] = scale_factor;
1090   transform[3][0] = _center[0] * ( 1 - scale_factor );
1091   transform[3][1] = _center[1] * ( 1 - scale_factor );
1092   transform[3][2] = _center[2] * ( 1 - scale_factor );
1093
1094   sgCopyMat4( r, m );
1095   sgPreMultMat4( r, transform );
1096 }
1097
1098
1099 \f
1100 ////////////////////////////////////////////////////////////////////////
1101 // Implementation of SGDistScaleAnimation
1102 ////////////////////////////////////////////////////////////////////////
1103 SGDistScaleAnimation::SGDistScaleAnimation(SGPropertyNode_ptr props)
1104   : SGAnimation( props, new SGCustomTransform ),
1105     _factor(props->getFloatValue("factor", 1.0)),
1106     _offset(props->getFloatValue("offset", 0.0)),
1107     _min_v(props->getFloatValue("min", 0.0)),
1108     _max_v(props->getFloatValue("max", 1.0)),
1109     _has_min(props->hasValue("min")),
1110     _has_max(props->hasValue("max")),
1111     _table(read_interpolation_table(props))
1112 {
1113   _center[0] = props->getFloatValue("center/x-m", 0);
1114   _center[1] = props->getFloatValue("center/y-m", 0);
1115   _center[2] = props->getFloatValue("center/z-m", 0);
1116
1117   ((SGCustomTransform *)_branch)->setTransCallback( &SGDistScaleAnimation::distScaleCallback, this );
1118 }
1119
1120 SGDistScaleAnimation::~SGDistScaleAnimation()
1121 {
1122 }
1123
1124 void SGDistScaleAnimation::distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d )
1125 {
1126   ((SGDistScaleAnimation *)d)->distScaleCallback( r, f, m );
1127 }
1128
1129 void SGDistScaleAnimation::distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m )
1130 {
1131   sgVec3 view;
1132   sgFullXformPnt3( view, _center, m );
1133
1134   float scale_factor = sgLengthVec3( view );
1135   if (_table == 0) {
1136     scale_factor = _factor * scale_factor + _offset;
1137     if ( _has_min && scale_factor < _min_v )
1138       scale_factor = _min_v;
1139     if ( _has_max && scale_factor > _max_v )
1140       scale_factor = _max_v;
1141   } else {
1142     scale_factor = _table->interpolate( scale_factor );
1143   }
1144
1145   sgMat4 transform;
1146   sgMakeIdentMat4( transform );
1147   transform[0][0] = scale_factor;
1148   transform[1][1] = scale_factor;
1149   transform[2][2] = scale_factor;
1150   transform[3][0] = _center[0] * ( 1 - scale_factor );
1151   transform[3][1] = _center[1] * ( 1 - scale_factor );
1152   transform[3][2] = _center[2] * ( 1 - scale_factor );
1153
1154   sgCopyMat4( r, m );
1155   sgPreMultMat4( r, transform );
1156 }
1157
1158 // end of animation.cxx