]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/animation.cxx
faec6334f9751a4319eea14c6caeaa61d2cb0c00
[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 #ifdef HAVE_CONFIG_H
7 #  include <simgear_config.h>
8 #endif
9
10 #include <string.h>             // for strcmp()
11 #include <math.h>
12
13 #include <plib/sg.h>
14 #include <plib/ssg.h>
15 #include <plib/ul.h>
16
17 #include <simgear/math/interpolater.hxx>
18 #include <simgear/props/condition.hxx>
19 #include <simgear/props/props.hxx>
20 #include <simgear/math/sg_random.h>
21
22 #include "animation.hxx"
23 #include "custtrans.hxx"
24 #include "personality.hxx"
25
26 \f
27 ////////////////////////////////////////////////////////////////////////
28 // Static utility functions.
29 ////////////////////////////////////////////////////////////////////////
30
31 /**
32  * Set up the transform matrix for a spin or rotation.
33  */
34 static void
35 set_rotation (sgMat4 &matrix, double position_deg,
36               sgVec3 &center, sgVec3 &axis)
37 {
38  float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
39  
40  float s = (float) sin ( temp_angle ) ;
41  float c = (float) cos ( temp_angle ) ;
42  float t = SG_ONE - c ;
43
44  // axis was normalized at load time 
45  // hint to the compiler to put these into FP registers
46  float x = axis[0];
47  float y = axis[1];
48  float z = axis[2];
49
50  matrix[0][0] = t * x * x + c ;
51  matrix[0][1] = t * y * x - s * z ;
52  matrix[0][2] = t * z * x + s * y ;
53  matrix[0][3] = SG_ZERO;
54  
55  matrix[1][0] = t * x * y + s * z ;
56  matrix[1][1] = t * y * y + c ;
57  matrix[1][2] = t * z * y - s * x ;
58  matrix[1][3] = SG_ZERO;
59  
60  matrix[2][0] = t * x * z - s * y ;
61  matrix[2][1] = t * y * z + s * x ;
62  matrix[2][2] = t * z * z + c ;
63  matrix[2][3] = SG_ZERO;
64
65   // hint to the compiler to put these into FP registers
66  x = center[0];
67  y = center[1];
68  z = center[2];
69  
70  matrix[3][0] = x - x*matrix[0][0] - y*matrix[1][0] - z*matrix[2][0];
71  matrix[3][1] = y - x*matrix[0][1] - y*matrix[1][1] - z*matrix[2][1];
72  matrix[3][2] = z - x*matrix[0][2] - y*matrix[1][2] - z*matrix[2][2];
73  matrix[3][3] = SG_ONE;
74 }
75
76 /**
77  * Set up the transform matrix for a translation.
78  */
79 static void
80 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
81 {
82   sgVec3 xyz;
83   sgScaleVec3(xyz, axis, position_m);
84   sgMakeTransMat4(matrix, xyz);
85 }
86
87 /**
88  * Set up the transform matrix for a scale operation.
89  */
90 static void
91 set_scale (sgMat4 &matrix, double x, double y, double z)
92 {
93   sgMakeIdentMat4( matrix );
94   matrix[0][0] = x;
95   matrix[1][1] = y;
96   matrix[2][2] = z;
97 }
98
99 /**
100  * Recursively process all kids to change the alpha values
101  */
102 static void
103 change_alpha( ssgBase *_branch, float _blend )
104 {
105   int i;
106
107   for (i = 0; i < ((ssgBranch *)_branch)->getNumKids(); i++)
108     change_alpha( ((ssgBranch *)_branch)->getKid(i), _blend );
109
110   if ( !_branch->isAKindOf(ssgTypeLeaf())
111        && !_branch->isAKindOf(ssgTypeVtxTable())
112        && !_branch->isAKindOf(ssgTypeVTable()) )
113     return;
114
115   int num_colors = ((ssgLeaf *)_branch)->getNumColours();
116 // unsigned int select_ = (_blend == 1.0) ? false : true;
117
118   for (i = 0; i < num_colors; i++)
119   {
120 //    ((ssgSelector *)_branch)->select( select_ );
121     float *color =  ((ssgLeaf *)_branch)->getColour(i);
122     color[3] = _blend;
123   }
124 }
125
126 /**
127  * Modify property value by step and scroll settings in texture translations
128  */
129 static double
130 apply_mods(double property, double step, double scroll)
131 {
132
133   double modprop;
134   if(step > 0) {
135     double scrollval = 0.0;
136     if(scroll > 0) {
137       // calculate scroll amount (for odometer like movement)
138       double remainder  =  step - fmod(fabs(property), step);
139       if (remainder < scroll) {
140         scrollval = (scroll - remainder) / scroll * step;
141       }
142     }
143   // apply stepping of input value
144   if(property > 0) 
145      modprop = ((floor(property/step) * step) + scrollval);
146   else
147      modprop = ((ceil(property/step) * step) + scrollval);
148   } else {
149      modprop = property;
150   }
151   return modprop;
152
153 }
154
155 /**
156  * Read an interpolation table from properties.
157  */
158 static SGInterpTable *
159 read_interpolation_table (SGPropertyNode_ptr props)
160 {
161   SGPropertyNode_ptr table_node = props->getNode("interpolation");
162   if (table_node != 0) {
163     SGInterpTable * table = new SGInterpTable();
164     vector<SGPropertyNode_ptr> entries = table_node->getChildren("entry");
165     for (unsigned int i = 0; i < entries.size(); i++)
166       table->addEntry(entries[i]->getDoubleValue("ind", 0.0),
167                       entries[i]->getDoubleValue("dep", 0.0));
168     return table;
169   } else {
170     return 0;
171   }
172 }
173
174
175 \f
176 ////////////////////////////////////////////////////////////////////////
177 // Implementation of SGAnimation
178 ////////////////////////////////////////////////////////////////////////
179
180 // Initialize the static data member
181 double SGAnimation::sim_time_sec = 0.0;
182 SGPersonalityBranch *SGAnimation::current_object = 0;
183
184 SGAnimation::SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch)
185     : _branch(branch),
186     animation_type(0)
187 {
188     _branch->setName(props->getStringValue("name", 0));
189     if ( props->getBoolValue( "enable-hot", true ) ) {
190         _branch->setTraversalMaskBits( SSGTRAV_HOT );
191     } else {
192         _branch->clrTraversalMaskBits( SSGTRAV_HOT );
193     }
194 }
195
196 SGAnimation::~SGAnimation ()
197 {
198 }
199
200 void
201 SGAnimation::init ()
202 {
203 }
204
205 int
206 SGAnimation::update()
207 {
208     return 1;
209 }
210
211 void
212 SGAnimation::restore()
213 {
214 }
215
216
217 \f
218 ////////////////////////////////////////////////////////////////////////
219 // Implementation of SGNullAnimation
220 ////////////////////////////////////////////////////////////////////////
221
222 SGNullAnimation::SGNullAnimation (SGPropertyNode_ptr props)
223   : SGAnimation(props, new ssgBranch)
224 {
225 }
226
227 SGNullAnimation::~SGNullAnimation ()
228 {
229 }
230
231
232 \f
233 ////////////////////////////////////////////////////////////////////////
234 // Implementation of SGRangeAnimation
235 ////////////////////////////////////////////////////////////////////////
236
237 SGRangeAnimation::SGRangeAnimation (SGPropertyNode *prop_root,
238                                     SGPropertyNode_ptr props)
239   : SGAnimation(props, new ssgRangeSelector),
240     _min(0.0), _max(0.0), _min_factor(1.0), _max_factor(1.0),
241     _condition(0)
242 {
243     SGPropertyNode_ptr node = props->getChild("condition");
244     if (node != 0)
245        _condition = sgReadCondition(prop_root, node);
246
247     float ranges[2];
248
249     node = props->getChild( "min-factor" );
250     if (node != 0) {
251        _min_factor = props->getFloatValue("min-factor", 1.0);
252     }
253     node = props->getChild( "max-factor" );
254     if (node != 0) {
255        _max_factor = props->getFloatValue("max-factor", 1.0);
256     }
257     node = props->getChild( "min-property" );
258     if (node != 0) {
259        _min_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
260        ranges[0] = _min_prop->getFloatValue() * _min_factor;
261     } else {
262        _min = props->getFloatValue("min-m", 0);
263        ranges[0] = _min * _min_factor;
264     }
265     node = props->getChild( "max-property" );
266     if (node != 0) {
267        _max_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
268        ranges[1] = _max_prop->getFloatValue() * _max_factor;
269     } else {
270        _max = props->getFloatValue("max-m", 0);
271        ranges[1] = _max * _max_factor;
272     }
273     ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
274 }
275
276 SGRangeAnimation::~SGRangeAnimation ()
277 {
278 }
279
280 int
281 SGRangeAnimation::update()
282 {
283   float ranges[2];
284   if ( _condition == 0 || _condition->test() ) {
285     if (_min_prop != 0) {
286       ranges[0] = _min_prop->getFloatValue() * _min_factor;
287     } else {
288       ranges[0] = _min * _min_factor;
289     }
290     if (_max_prop != 0) {
291       ranges[1] = _max_prop->getFloatValue() * _max_factor;
292     } else {
293       ranges[1] = _max * _max_factor;
294     }
295   } else {
296     ranges[0] = 0.f;
297     ranges[1] = 1000000000.f;
298   }
299   ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
300   return 2;
301 }
302
303
304 \f
305 ////////////////////////////////////////////////////////////////////////
306 // Implementation of SGBillboardAnimation
307 ////////////////////////////////////////////////////////////////////////
308
309 SGBillboardAnimation::SGBillboardAnimation (SGPropertyNode_ptr props)
310     : SGAnimation(props, new ssgCutout(props->getBoolValue("spherical", true)))
311 {
312 }
313
314 SGBillboardAnimation::~SGBillboardAnimation ()
315 {
316 }
317
318
319 \f
320 ////////////////////////////////////////////////////////////////////////
321 // Implementation of SGSelectAnimation
322 ////////////////////////////////////////////////////////////////////////
323
324 SGSelectAnimation::SGSelectAnimation( SGPropertyNode *prop_root,
325                                   SGPropertyNode_ptr props )
326   : SGAnimation(props, new ssgSelector),
327     _condition(0)
328 {
329   SGPropertyNode_ptr node = props->getChild("condition");
330   if (node != 0)
331     _condition = sgReadCondition(prop_root, node);
332 }
333
334 SGSelectAnimation::~SGSelectAnimation ()
335 {
336   delete _condition;
337 }
338
339 int
340 SGSelectAnimation::update()
341 {
342   if (_condition != 0 && _condition->test()) 
343       ((ssgSelector *)_branch)->select(0xffff);
344   else
345       ((ssgSelector *)_branch)->select(0x0000);
346   return 2;
347 }
348
349
350 \f
351 ////////////////////////////////////////////////////////////////////////
352 // Implementation of SGSpinAnimation
353 ////////////////////////////////////////////////////////////////////////
354
355 SGSpinAnimation::SGSpinAnimation( SGPropertyNode *prop_root,
356                               SGPropertyNode_ptr props,
357                               double sim_time_sec )
358   : SGAnimation(props, new ssgTransform),
359     _use_personality( props->getBoolValue("use-personality",false) ),
360     _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
361     _last_time_sec( sim_time_sec ),
362     _condition(0)
363 {
364     SGPropertyNode_ptr node = props->getChild("condition");
365     if (node != 0)
366         _condition = sgReadCondition(prop_root, node);
367
368     _center[0] = 0;
369     _center[1] = 0;
370     _center[2] = 0;
371     if (props->hasValue("axis/x1-m")) {
372         double x1,y1,z1,x2,y2,z2;
373         x1 = props->getFloatValue("axis/x1-m");
374         y1 = props->getFloatValue("axis/y1-m");
375         z1 = props->getFloatValue("axis/z1-m");
376         x2 = props->getFloatValue("axis/x2-m");
377         y2 = props->getFloatValue("axis/y2-m");
378         z2 = props->getFloatValue("axis/z2-m");
379         _center[0] = (x1+x2)/2;
380         _center[1]= (y1+y2)/2;
381         _center[2] = (z1+z2)/2;
382         float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
383         _axis[0] = (x2-x1)/vector_length;
384         _axis[1] = (y2-y1)/vector_length;
385         _axis[2] = (z2-z1)/vector_length;
386     } else {
387        _axis[0] = props->getFloatValue("axis/x", 0);
388        _axis[1] = props->getFloatValue("axis/y", 0);
389        _axis[2] = props->getFloatValue("axis/z", 0);
390     }
391     if (props->hasValue("center/x-m")) {
392        _center[0] = props->getFloatValue("center/x-m", 0);
393        _center[1] = props->getFloatValue("center/y-m", 0);
394        _center[2] = props->getFloatValue("center/z-m", 0);
395     }
396     sgNormalizeVec3(_axis);
397
398     //_factor(props->getDoubleValue("factor", 1.0)),
399     _factor = 1.0;
400     _factor_min = 1.0;
401     _factor_max = 1.0;
402     SGPropertyNode_ptr factor_n = props->getNode( "factor" );
403     if ( factor_n != 0 ) {
404        SGPropertyNode_ptr rand_n = factor_n->getNode( "random" );
405        if ( rand_n != 0 ) {
406           _factor_min = rand_n->getDoubleValue( "min", 0.0 );
407           _factor_max = rand_n->getDoubleValue( "max", 1.0 );
408           _factor = _factor_min + sg_random() * ( _factor_max - _factor_min );
409        } else {
410           _factor = _factor_min = _factor_max = props->getDoubleValue("factor", 1.0);
411        }
412     }
413     //_position_deg(props->getDoubleValue("starting-position-deg", 0)),
414     _position_deg = 0.0;
415     _position_deg_min = 0.0;
416     _position_deg_max = 0.0;
417     SGPropertyNode_ptr position_deg_n = props->getNode( "starting-position-deg" );
418     if ( position_deg_n != 0 ) {
419        SGPropertyNode_ptr rand_n = position_deg_n->getNode( "random" );
420        if ( rand_n != 0 ) {
421           _position_deg_min = rand_n->getDoubleValue( "min", 0.0 );
422           _position_deg_max = rand_n->getDoubleValue( "max", 1.0 );
423           _position_deg = _position_deg_min + sg_random() * ( _position_deg_max - _position_deg_min );
424        } else {
425           _position_deg = _position_deg_min = _position_deg_max = 
426                   props->getDoubleValue("starting-position-deg", 1.0);
427        }
428     }
429 }
430
431 SGSpinAnimation::~SGSpinAnimation ()
432 {
433 }
434
435 int
436 SGSpinAnimation::update()
437 {
438   if ( _condition == 0 || _condition->test() ) {
439     double dt;
440     float velocity_rpms;
441     if ( _use_personality ) {
442       SGPersonalityBranch *key = current_object;
443       if ( !key->getIntValue( this, INIT_SPIN ) ) {
444         double v = _factor_min + sg_random() * ( _factor_max - _factor_min );
445         key->setDoubleValue( v, this, FACTOR_SPIN );
446
447         key->setDoubleValue( sim_time_sec, this, LAST_TIME_SEC_SPIN );
448         key->setIntValue( 1, this, INIT_SPIN );
449
450         v = _position_deg_min + sg_random() * ( _position_deg_max - _position_deg_min );
451         key->setDoubleValue( v, this, POSITION_DEG_SPIN );
452       }
453
454       _factor = key->getDoubleValue( this, FACTOR_SPIN );
455       _position_deg = key->getDoubleValue( this, POSITION_DEG_SPIN );
456       _last_time_sec = key->getDoubleValue( this, LAST_TIME_SEC_SPIN );
457       dt = sim_time_sec - _last_time_sec;
458       _last_time_sec = sim_time_sec;
459       key->setDoubleValue( _last_time_sec, this, LAST_TIME_SEC_SPIN );
460
461       velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
462       _position_deg += (dt * velocity_rpms * 360);
463       while (_position_deg < 0)
464          _position_deg += 360.0;
465       while (_position_deg >= 360.0)
466          _position_deg -= 360.0;
467       key->setDoubleValue( _position_deg, this, POSITION_DEG_SPIN );
468     } else {
469       dt = sim_time_sec - _last_time_sec;
470       _last_time_sec = sim_time_sec;
471
472       velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
473       _position_deg += (dt * velocity_rpms * 360);
474       while (_position_deg < 0)
475          _position_deg += 360.0;
476       while (_position_deg >= 360.0)
477          _position_deg -= 360.0;
478     }
479
480     set_rotation(_matrix, _position_deg, _center, _axis);
481     ((ssgTransform *)_branch)->setTransform(_matrix);
482   }
483   return 1;
484 }
485
486
487 \f
488 ////////////////////////////////////////////////////////////////////////
489 // Implementation of SGTimedAnimation
490 ////////////////////////////////////////////////////////////////////////
491
492 SGTimedAnimation::SGTimedAnimation (SGPropertyNode_ptr props)
493   : SGAnimation(props, new ssgSelector),
494     _use_personality( props->getBoolValue("use-personality",false) ),
495     _duration_sec(props->getDoubleValue("duration-sec", 1.0)),
496     _last_time_sec( sim_time_sec ),
497     _total_duration_sec( 0 ),
498     _step( 0 )
499     
500 {
501     vector<SGPropertyNode_ptr> nodes = props->getChildren( "branch-duration-sec" );
502     size_t nb = nodes.size();
503     for ( size_t i = 0; i < nb; i++ ) {
504         size_t ind = nodes[ i ]->getIndex();
505         while ( ind >= _branch_duration_specs.size() ) {
506             _branch_duration_specs.push_back( DurationSpec( _duration_sec ) );
507         }
508         SGPropertyNode_ptr rNode = nodes[ i ]->getChild("random");
509         if ( rNode == 0 ) {
510             _branch_duration_specs[ ind ] = DurationSpec( nodes[ i ]->getDoubleValue() );
511         } else {
512             _branch_duration_specs[ ind ] = DurationSpec( rNode->getDoubleValue( "min", 0.0 ),
513                                                           rNode->getDoubleValue( "max", 1.0 ) );
514         }
515     }
516 }
517
518 SGTimedAnimation::~SGTimedAnimation ()
519 {
520 }
521
522 void
523 SGTimedAnimation::init()
524 {
525     if ( !_use_personality ) {
526         for ( int i = 0; i < getBranch()->getNumKids(); i++ ) {
527             double v;
528             if ( i < (int)_branch_duration_specs.size() ) {
529                 DurationSpec &sp = _branch_duration_specs[ i ];
530                 v = sp._min + sg_random() * ( sp._max - sp._min );
531             } else {
532                 v = _duration_sec;
533             }
534             _branch_duration_sec.push_back( v );
535             _total_duration_sec += v;
536         }
537         // Sanity check : total duration shouldn't equal zero
538         if ( _total_duration_sec < 0.01 ) {
539             _total_duration_sec = 0.01;
540         }
541     }
542     ((ssgSelector *)getBranch())->selectStep(_step);
543 }
544
545 int
546 SGTimedAnimation::update()
547 {
548     if ( _use_personality ) {
549         SGPersonalityBranch *key = current_object;
550         if ( !key->getIntValue( this, INIT_TIMED ) ) {
551             double total = 0;
552             double offset = 1.0;
553             for ( size_t i = 0; i < _branch_duration_specs.size(); i++ ) {
554                 DurationSpec &sp = _branch_duration_specs[ i ];
555                 double v = sp._min + sg_random() * ( sp._max - sp._min );
556                 key->setDoubleValue( v, this, BRANCH_DURATION_SEC_TIMED, i );
557                 if ( i == 0 )
558                     offset = v;
559                 total += v;
560             }
561             // Sanity check : total duration shouldn't equal zero
562             if ( total < 0.01 ) {
563                 total = 0.01;
564             }
565             offset *= sg_random();
566             key->setDoubleValue( sim_time_sec - offset, this, LAST_TIME_SEC_TIMED );
567             key->setDoubleValue( total, this, TOTAL_DURATION_SEC_TIMED );
568             key->setIntValue( 0, this, STEP_TIMED );
569             key->setIntValue( 1, this, INIT_TIMED );
570         }
571
572         _step = key->getIntValue( this, STEP_TIMED );
573         _last_time_sec = key->getDoubleValue( this, LAST_TIME_SEC_TIMED );
574         _total_duration_sec = key->getDoubleValue( this, TOTAL_DURATION_SEC_TIMED );
575         while ( ( sim_time_sec - _last_time_sec ) >= _total_duration_sec ) {
576             _last_time_sec += _total_duration_sec;
577         }
578         double duration = _duration_sec;
579         if ( _step < (int)_branch_duration_specs.size() ) {
580             duration = key->getDoubleValue( this, BRANCH_DURATION_SEC_TIMED, _step );
581         }
582         if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
583             _last_time_sec += duration;
584             _step += 1;
585             if ( _step >= getBranch()->getNumKids() )
586                 _step = 0;
587         }
588         ((ssgSelector *)getBranch())->selectStep( _step );
589         key->setDoubleValue( _last_time_sec, this, LAST_TIME_SEC_TIMED );
590         key->setIntValue( _step, this, STEP_TIMED );
591     } else {
592         while ( ( sim_time_sec - _last_time_sec ) >= _total_duration_sec ) {
593             _last_time_sec += _total_duration_sec;
594         }
595         double duration = _duration_sec;
596         if ( _step < (int)_branch_duration_sec.size() ) {
597             duration = _branch_duration_sec[ _step ];
598         }
599         if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
600             _last_time_sec += duration;
601             _step += 1;
602             if ( _step >= getBranch()->getNumKids() )
603                 _step = 0;
604             ((ssgSelector *)getBranch())->selectStep( _step );
605         }
606     }
607     return 1;
608 }
609
610
611 \f
612 ////////////////////////////////////////////////////////////////////////
613 // Implementation of SGRotateAnimation
614 ////////////////////////////////////////////////////////////////////////
615
616 SGRotateAnimation::SGRotateAnimation( SGPropertyNode *prop_root,
617                                   SGPropertyNode_ptr props )
618     : SGAnimation(props, new ssgTransform),
619       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
620       _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
621       _factor(props->getDoubleValue("factor", 1.0)),
622       _table(read_interpolation_table(props)),
623       _has_min(props->hasValue("min-deg")),
624       _min_deg(props->getDoubleValue("min-deg")),
625       _has_max(props->hasValue("max-deg")),
626       _max_deg(props->getDoubleValue("max-deg")),
627       _position_deg(props->getDoubleValue("starting-position-deg", 0)),
628       _condition(0)
629 {
630     SGPropertyNode_ptr node = props->getChild("condition");
631     if (node != 0)
632       _condition = sgReadCondition(prop_root, node);
633
634     _center[0] = 0;
635     _center[1] = 0;
636     _center[2] = 0;
637     if (props->hasValue("axis/x") || props->hasValue("axis/y") || props->hasValue("axis/z")) {
638        _axis[0] = props->getFloatValue("axis/x", 0);
639        _axis[1] = props->getFloatValue("axis/y", 0);
640        _axis[2] = props->getFloatValue("axis/z", 0);
641     } else {
642         double x1,y1,z1,x2,y2,z2;
643         x1 = props->getFloatValue("axis/x1-m", 0);
644         y1 = props->getFloatValue("axis/y1-m", 0);
645         z1 = props->getFloatValue("axis/z1-m", 0);
646         x2 = props->getFloatValue("axis/x2-m", 0);
647         y2 = props->getFloatValue("axis/y2-m", 0);
648         z2 = props->getFloatValue("axis/z2-m", 0);
649         _center[0] = (x1+x2)/2;
650         _center[1]= (y1+y2)/2;
651         _center[2] = (z1+z2)/2;
652         float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
653         _axis[0] = (x2-x1)/vector_length;
654         _axis[1] = (y2-y1)/vector_length;
655         _axis[2] = (z2-z1)/vector_length;
656     }
657     if (props->hasValue("center/x-m") || props->hasValue("center/y-m")
658             || props->hasValue("center/z-m")) {
659         _center[0] = props->getFloatValue("center/x-m", 0);
660         _center[1] = props->getFloatValue("center/y-m", 0);
661         _center[2] = props->getFloatValue("center/z-m", 0);
662     }
663     sgNormalizeVec3(_axis);
664 }
665
666 SGRotateAnimation::~SGRotateAnimation ()
667 {
668   delete _table;
669 }
670
671 int
672 SGRotateAnimation::update()
673 {
674   if (_condition == 0 || _condition->test()) {
675     if (_table == 0) {
676       _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
677       if (_has_min && _position_deg < _min_deg)
678         _position_deg = _min_deg;
679       if (_has_max && _position_deg > _max_deg)
680         _position_deg = _max_deg;
681     } else {
682       _position_deg = _table->interpolate(_prop->getDoubleValue());
683     }
684     set_rotation(_matrix, _position_deg, _center, _axis);
685     ((ssgTransform *)_branch)->setTransform(_matrix);
686   }
687   return 2;
688 }
689
690 \f
691 ////////////////////////////////////////////////////////////////////////
692 // Implementation of SGBlendAnimation
693 ////////////////////////////////////////////////////////////////////////
694
695 SGBlendAnimation::SGBlendAnimation( SGPropertyNode *prop_root,
696                                         SGPropertyNode_ptr props )
697   : SGAnimation(props, new ssgTransform),
698     _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
699     _table(read_interpolation_table(props)),
700     _prev_value(1.0),
701     _offset(props->getDoubleValue("offset", 0.0)),
702     _factor(props->getDoubleValue("factor", 1.0)),
703     _has_min(props->hasValue("min")),
704     _min(props->getDoubleValue("min", 0.0)),
705     _has_max(props->hasValue("max")),
706     _max(props->getDoubleValue("max", 1.0))
707 {
708 }
709
710 SGBlendAnimation::~SGBlendAnimation ()
711 {
712     delete _table;
713 }
714
715 int
716 SGBlendAnimation::update()
717 {
718   double _blend;
719
720   if (_table == 0) {
721     _blend = 1.0 - (_prop->getDoubleValue() * _factor + _offset);
722
723     if (_has_min && (_blend < _min))
724       _blend = _min;
725     if (_has_max && (_blend > _max))
726       _blend = _max;
727   } else {
728     _blend = _table->interpolate(_prop->getDoubleValue());
729   }
730
731   if (_blend != _prev_value) {
732     _prev_value = _blend;
733     change_alpha( _branch, _blend );
734   }
735   return 1;
736 }
737
738
739 \f
740 ////////////////////////////////////////////////////////////////////////
741 // Implementation of SGTranslateAnimation
742 ////////////////////////////////////////////////////////////////////////
743
744 SGTranslateAnimation::SGTranslateAnimation( SGPropertyNode *prop_root,
745                                         SGPropertyNode_ptr props )
746   : SGAnimation(props, new ssgTransform),
747       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
748     _offset_m(props->getDoubleValue("offset-m", 0.0)),
749     _factor(props->getDoubleValue("factor", 1.0)),
750     _table(read_interpolation_table(props)),
751     _has_min(props->hasValue("min-m")),
752     _min_m(props->getDoubleValue("min-m")),
753     _has_max(props->hasValue("max-m")),
754     _max_m(props->getDoubleValue("max-m")),
755     _position_m(props->getDoubleValue("starting-position-m", 0)),
756     _condition(0)
757 {
758   SGPropertyNode_ptr node = props->getChild("condition");
759   if (node != 0)
760     _condition = sgReadCondition(prop_root, node);
761
762   _axis[0] = props->getFloatValue("axis/x", 0);
763   _axis[1] = props->getFloatValue("axis/y", 0);
764   _axis[2] = props->getFloatValue("axis/z", 0);
765   sgNormalizeVec3(_axis);
766 }
767
768 SGTranslateAnimation::~SGTranslateAnimation ()
769 {
770   delete _table;
771 }
772
773 int
774 SGTranslateAnimation::update()
775 {
776   if (_condition == 0 || _condition->test()) {
777     if (_table == 0) {
778       _position_m = (_prop->getDoubleValue() * _factor) + _offset_m;
779       if (_has_min && _position_m < _min_m)
780         _position_m = _min_m;
781       if (_has_max && _position_m > _max_m)
782         _position_m = _max_m;
783     } else {
784       _position_m = _table->interpolate(_prop->getDoubleValue());
785     }
786     set_translation(_matrix, _position_m, _axis);
787     ((ssgTransform *)_branch)->setTransform(_matrix);
788   }
789   return 2;
790 }
791
792
793 \f
794 ////////////////////////////////////////////////////////////////////////
795 // Implementation of SGScaleAnimation
796 ////////////////////////////////////////////////////////////////////////
797
798 SGScaleAnimation::SGScaleAnimation( SGPropertyNode *prop_root,
799                                         SGPropertyNode_ptr props )
800   : SGAnimation(props, new ssgTransform),
801       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
802     _x_factor(props->getDoubleValue("x-factor", 1.0)),
803     _y_factor(props->getDoubleValue("y-factor", 1.0)),
804     _z_factor(props->getDoubleValue("z-factor", 1.0)),
805     _x_offset(props->getDoubleValue("x-offset", 1.0)),
806     _y_offset(props->getDoubleValue("y-offset", 1.0)),
807     _z_offset(props->getDoubleValue("z-offset", 1.0)),
808     _table(read_interpolation_table(props)),
809     _has_min_x(props->hasValue("x-min")),
810     _has_min_y(props->hasValue("y-min")),
811     _has_min_z(props->hasValue("z-min")),
812     _min_x(props->getDoubleValue("x-min")),
813     _min_y(props->getDoubleValue("y-min")),
814     _min_z(props->getDoubleValue("z-min")),
815     _has_max_x(props->hasValue("x-max")),
816     _has_max_y(props->hasValue("y-max")),
817     _has_max_z(props->hasValue("z-max")),
818     _max_x(props->getDoubleValue("x-max")),
819     _max_y(props->getDoubleValue("y-max")),
820     _max_z(props->getDoubleValue("z-max"))
821 {
822 }
823
824 SGScaleAnimation::~SGScaleAnimation ()
825 {
826   delete _table;
827 }
828
829 int
830 SGScaleAnimation::update()
831 {
832   if (_table == 0) {
833       _x_scale = _prop->getDoubleValue() * _x_factor + _x_offset;
834     if (_has_min_x && _x_scale < _min_x)
835       _x_scale = _min_x;
836     if (_has_max_x && _x_scale > _max_x)
837       _x_scale = _max_x;
838   } else {
839     _x_scale = _table->interpolate(_prop->getDoubleValue());
840   }
841
842   if (_table == 0) {
843     _y_scale = _prop->getDoubleValue() * _y_factor + _y_offset;
844     if (_has_min_y && _y_scale < _min_y)
845       _y_scale = _min_y;
846     if (_has_max_y && _y_scale > _max_y)
847       _y_scale = _max_y;
848   } else {
849     _y_scale = _table->interpolate(_prop->getDoubleValue());
850   }
851
852   if (_table == 0) {
853     _z_scale = _prop->getDoubleValue() * _z_factor + _z_offset;
854     if (_has_min_z && _z_scale < _min_z)
855       _z_scale = _min_z;
856     if (_has_max_z && _z_scale > _max_z)
857       _z_scale = _max_z;
858   } else {
859     _z_scale = _table->interpolate(_prop->getDoubleValue());
860   }
861
862   set_scale(_matrix, _x_scale, _y_scale, _z_scale );
863   ((ssgTransform *)_branch)->setTransform(_matrix);
864   return 2;
865 }
866
867
868 ////////////////////////////////////////////////////////////////////////
869 // Implementation of SGTexRotateAnimation
870 ////////////////////////////////////////////////////////////////////////
871
872 SGTexRotateAnimation::SGTexRotateAnimation( SGPropertyNode *prop_root,
873                                   SGPropertyNode_ptr props )
874     : SGAnimation(props, new ssgTexTrans),
875       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
876       _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
877       _factor(props->getDoubleValue("factor", 1.0)),
878       _table(read_interpolation_table(props)),
879       _has_min(props->hasValue("min-deg")),
880       _min_deg(props->getDoubleValue("min-deg")),
881       _has_max(props->hasValue("max-deg")),
882       _max_deg(props->getDoubleValue("max-deg")),
883       _position_deg(props->getDoubleValue("starting-position-deg", 0))
884 {
885   _center[0] = props->getFloatValue("center/x", 0);
886   _center[1] = props->getFloatValue("center/y", 0);
887   _center[2] = props->getFloatValue("center/z", 0);
888   _axis[0] = props->getFloatValue("axis/x", 0);
889   _axis[1] = props->getFloatValue("axis/y", 0);
890   _axis[2] = props->getFloatValue("axis/z", 0);
891   sgNormalizeVec3(_axis);
892 }
893
894 SGTexRotateAnimation::~SGTexRotateAnimation ()
895 {
896   delete _table;
897 }
898
899 int
900 SGTexRotateAnimation::update()
901 {
902   if (_table == 0) {
903    _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
904    if (_has_min && _position_deg < _min_deg)
905      _position_deg = _min_deg;
906    if (_has_max && _position_deg > _max_deg)
907      _position_deg = _max_deg;
908   } else {
909     _position_deg = _table->interpolate(_prop->getDoubleValue());
910   }
911   set_rotation(_matrix, _position_deg, _center, _axis);
912   ((ssgTexTrans *)_branch)->setTransform(_matrix);
913   return 2;
914 }
915
916
917 ////////////////////////////////////////////////////////////////////////
918 // Implementation of SGTexTranslateAnimation
919 ////////////////////////////////////////////////////////////////////////
920
921 SGTexTranslateAnimation::SGTexTranslateAnimation( SGPropertyNode *prop_root,
922                                         SGPropertyNode_ptr props )
923   : SGAnimation(props, new ssgTexTrans),
924       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
925     _offset(props->getDoubleValue("offset", 0.0)),
926     _factor(props->getDoubleValue("factor", 1.0)),
927     _step(props->getDoubleValue("step",0.0)),
928     _scroll(props->getDoubleValue("scroll",0.0)),
929     _table(read_interpolation_table(props)),
930     _has_min(props->hasValue("min")),
931     _min(props->getDoubleValue("min")),
932     _has_max(props->hasValue("max")),
933     _max(props->getDoubleValue("max")),
934     _position(props->getDoubleValue("starting-position", 0))
935 {
936   _axis[0] = props->getFloatValue("axis/x", 0);
937   _axis[1] = props->getFloatValue("axis/y", 0);
938   _axis[2] = props->getFloatValue("axis/z", 0);
939   sgNormalizeVec3(_axis);
940 }
941
942 SGTexTranslateAnimation::~SGTexTranslateAnimation ()
943 {
944   delete _table;
945 }
946
947 int
948 SGTexTranslateAnimation::update()
949 {
950   if (_table == 0) {
951     _position = (apply_mods(_prop->getDoubleValue(), _step, _scroll) + _offset) * _factor;
952     if (_has_min && _position < _min)
953       _position = _min;
954     if (_has_max && _position > _max)
955       _position = _max;
956   } else {
957     _position = _table->interpolate(apply_mods(_prop->getDoubleValue(), _step, _scroll));
958   }
959   set_translation(_matrix, _position, _axis);
960   ((ssgTexTrans *)_branch)->setTransform(_matrix);
961   return 2;
962 }
963
964
965 ////////////////////////////////////////////////////////////////////////
966 // Implementation of SGTexMultipleAnimation
967 ////////////////////////////////////////////////////////////////////////
968
969 SGTexMultipleAnimation::SGTexMultipleAnimation( SGPropertyNode *prop_root,
970                                         SGPropertyNode_ptr props )
971   : SGAnimation(props, new ssgTexTrans),
972       _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true))
973 {
974   unsigned int i;
975   // Load animations
976   vector<SGPropertyNode_ptr> transform_nodes = props->getChildren("transform");
977   _transform = new TexTransform [transform_nodes.size()];
978   _num_transforms = 0;
979   for (i = 0; i < transform_nodes.size(); i++) {
980     SGPropertyNode_ptr transform_props = transform_nodes[i];
981
982     if (!strcmp("textranslate",transform_props->getStringValue("subtype", 0))) {
983
984       // transform is a translation
985       _transform[i].subtype = 0;
986
987       _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
988
989       _transform[i].offset = transform_props->getDoubleValue("offset", 0.0);
990       _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
991       _transform[i].step = transform_props->getDoubleValue("step",0.0);
992       _transform[i].scroll = transform_props->getDoubleValue("scroll",0.0);
993       _transform[i].table = read_interpolation_table(transform_props);
994       _transform[i].has_min = transform_props->hasValue("min");
995       _transform[i].min = transform_props->getDoubleValue("min");
996       _transform[i].has_max = transform_props->hasValue("max");
997       _transform[i].max = transform_props->getDoubleValue("max");
998       _transform[i].position = transform_props->getDoubleValue("starting-position", 0);
999
1000       _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
1001       _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
1002       _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
1003       sgNormalizeVec3(_transform[i].axis);
1004       _num_transforms++;
1005     } else if (!strcmp("texrotate",transform_nodes[i]->getStringValue("subtype", 0))) {
1006
1007       // transform is a rotation
1008       _transform[i].subtype = 1;
1009
1010       _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
1011       _transform[i].offset = transform_props->getDoubleValue("offset-deg", 0.0);
1012       _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
1013       _transform[i].table = read_interpolation_table(transform_props);
1014       _transform[i].has_min = transform_props->hasValue("min-deg");
1015       _transform[i].min = transform_props->getDoubleValue("min-deg");
1016       _transform[i].has_max = transform_props->hasValue("max-deg");
1017       _transform[i].max = transform_props->getDoubleValue("max-deg");
1018       _transform[i].position = transform_props->getDoubleValue("starting-position-deg", 0);
1019
1020       _transform[i].center[0] = transform_props->getFloatValue("center/x", 0);
1021       _transform[i].center[1] = transform_props->getFloatValue("center/y", 0);
1022       _transform[i].center[2] = transform_props->getFloatValue("center/z", 0);
1023       _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
1024       _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
1025       _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
1026       sgNormalizeVec3(_transform[i].axis);
1027       _num_transforms++;
1028     }
1029   }
1030 }
1031
1032 SGTexMultipleAnimation::~SGTexMultipleAnimation ()
1033 {
1034    delete [] _transform;
1035 }
1036
1037 int
1038 SGTexMultipleAnimation::update()
1039 {
1040   int i;
1041   sgMat4 tmatrix;
1042   sgMakeIdentMat4(tmatrix);
1043   for (i = 0; i < _num_transforms; i++) {
1044
1045     if(_transform[i].subtype == 0) {
1046
1047       // subtype 0 is translation
1048       if (_transform[i].table == 0) {
1049         _transform[i].position = (apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll) + _transform[i].offset) * _transform[i].factor;
1050         if (_transform[i].has_min && _transform[i].position < _transform[i].min)
1051           _transform[i].position = _transform[i].min;
1052         if (_transform[i].has_max && _transform[i].position > _transform[i].max)
1053           _transform[i].position = _transform[i].max;
1054       } else {
1055          _transform[i].position = _transform[i].table->interpolate(apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll));
1056       }
1057       set_translation(_transform[i].matrix, _transform[i].position, _transform[i].axis);
1058       sgPreMultMat4(tmatrix, _transform[i].matrix);
1059
1060     } else if (_transform[i].subtype == 1) {
1061
1062       // subtype 1 is rotation
1063
1064       if (_transform[i].table == 0) {
1065         _transform[i].position = _transform[i].prop->getDoubleValue() * _transform[i].factor + _transform[i].offset;
1066         if (_transform[i].has_min && _transform[i].position < _transform[i].min)
1067          _transform[i].position = _transform[i].min;
1068        if (_transform[i].has_max && _transform[i].position > _transform[i].max)
1069          _transform[i].position = _transform[i].max;
1070      } else {
1071         _transform[i].position = _transform[i].table->interpolate(_transform[i].prop->getDoubleValue());
1072       }
1073       set_rotation(_transform[i].matrix, _transform[i].position, _transform[i].center, _transform[i].axis);
1074       sgPreMultMat4(tmatrix, _transform[i].matrix);
1075     }
1076   }
1077   ((ssgTexTrans *)_branch)->setTransform(tmatrix);
1078   return 2;
1079 }
1080
1081
1082 \f
1083 ////////////////////////////////////////////////////////////////////////
1084 // Implementation of SGAlphaTestAnimation
1085 ////////////////////////////////////////////////////////////////////////
1086
1087 SGAlphaTestAnimation::SGAlphaTestAnimation(SGPropertyNode_ptr props)
1088   : SGAnimation(props, new ssgBranch)
1089 {
1090   _alpha_clamp = props->getFloatValue("alpha-factor", 0.0);
1091 }
1092
1093 SGAlphaTestAnimation::~SGAlphaTestAnimation ()
1094 {
1095 }
1096
1097 void SGAlphaTestAnimation::init()
1098 {
1099   setAlphaClampToBranch(_branch,_alpha_clamp);
1100 }
1101
1102 void SGAlphaTestAnimation::setAlphaClampToBranch(ssgBranch *b, float clamp)
1103 {
1104   int nb = b->getNumKids();
1105   for (int i = 0; i<nb; i++) {
1106     ssgEntity *e = b->getKid(i);
1107     if (e->isAKindOf(ssgTypeLeaf())) {
1108       ssgSimpleState*s = (ssgSimpleState*)((ssgLeaf*)e)->getState();
1109       s->enable( GL_ALPHA_TEST );
1110       s->setAlphaClamp( clamp );
1111     } else if (e->isAKindOf(ssgTypeBranch())) {
1112       setAlphaClampToBranch( (ssgBranch*)e, clamp );
1113     }
1114   }
1115 }
1116
1117
1118 \f
1119 ////////////////////////////////////////////////////////////////////////
1120 // Implementation of SGMaterialAnimation
1121 ////////////////////////////////////////////////////////////////////////
1122
1123 SGMaterialAnimation::SGMaterialAnimation( SGPropertyNode *prop_root,
1124         SGPropertyNode_ptr props, const SGPath &texture_path)
1125     : SGAnimation(props, new ssgBranch),
1126     _prop_root(prop_root),
1127     _last_condition(false),
1128     _prop_base(""),
1129     _texture_base(texture_path),
1130     _cached_material(0),
1131     _cloned_material(0),
1132     _read(0),
1133     _update(0),
1134     _global(props->getBoolValue("global", false))
1135 {
1136     SGPropertyNode_ptr n;
1137     n = props->getChild("condition");
1138     _condition = n ? sgReadCondition(_prop_root, n) : 0;
1139     n = props->getChild("property-base");
1140     if (n) {
1141         _prop_base = n->getStringValue();
1142         if (!_prop_base.empty() && _prop_base.end()[-1] != '/')
1143             _prop_base += '/';
1144     }
1145
1146     initColorGroup(props->getChild("diffuse"), &_diff, DIFFUSE);
1147     initColorGroup(props->getChild("ambient"), &_amb, AMBIENT);
1148     initColorGroup(props->getChild("emission"), &_emis, EMISSION);
1149     initColorGroup(props->getChild("specular"), &_spec, SPECULAR);
1150
1151     _shi = props->getFloatValue("shininess", -1.0);
1152     if (_shi >= 0.0)
1153         _update |= SHININESS;
1154
1155     SGPropertyNode_ptr group = props->getChild("transparency");
1156     if (group) {
1157         _trans.value = group->getFloatValue("alpha", -1.0);
1158         _trans.factor = group->getFloatValue("factor", 1.0);
1159         _trans.offset = group->getFloatValue("offset", 0.0);
1160         _trans.min = group->getFloatValue("min", 0.0);
1161         if (_trans.min < 0.0)
1162             _trans.min = 0.0;
1163         _trans.max = group->getFloatValue("max", 1.0);
1164         if (_trans.max > 1.0)
1165             _trans.max = 1.0;
1166         if (_trans.dirty())
1167             _update |= TRANSPARENCY;
1168
1169         n = group->getChild("alpha-prop");
1170         _trans.value_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1171         n = group->getChild("factor-prop");
1172         _trans.factor_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1173         n = group->getChild("offset-prop");
1174         _trans.offset_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1175         if (_trans.live())
1176             _read |= TRANSPARENCY;
1177     }
1178
1179     _thresh = props->getFloatValue("threshold", -1.0);
1180     if (_thresh >= 0.0)
1181         _update |= THRESHOLD;
1182
1183     string _texture_str = props->getStringValue("texture", "");
1184     if (!_texture_str.empty()) {
1185         _texture = _texture_base;
1186         _texture.append(_texture_str);
1187         _update |= TEXTURE;
1188     }
1189
1190     n = props->getChild("shininess-prop");
1191     _shi_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1192     n = props->getChild("threshold-prop");
1193     _thresh_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1194     n = props->getChild("texture-prop");
1195     _tex_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1196
1197     _static_update = _update;
1198 }
1199
1200 void SGMaterialAnimation::initColorGroup(SGPropertyNode_ptr group, ColorSpec *col, int flag)
1201 {
1202     if (!group)
1203         return;
1204
1205     col->red = group->getFloatValue("red", -1.0);
1206     col->green = group->getFloatValue("green", -1.0);
1207     col->blue = group->getFloatValue("blue", -1.0);
1208     col->factor = group->getFloatValue("factor", 1.0);
1209     col->offset = group->getFloatValue("offset", 0.0);
1210     if (col->dirty())
1211         _update |= flag;
1212
1213     SGPropertyNode *n;
1214     n = group->getChild("red-prop");
1215     col->red_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1216     n = group->getChild("green-prop");
1217     col->green_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1218     n = group->getChild("blue-prop");
1219     col->blue_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1220     n = group->getChild("factor-prop");
1221     col->factor_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1222     n = group->getChild("offset-prop");
1223     col->offset_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1224     if (col->live())
1225         _read |= flag;
1226 }
1227
1228 void SGMaterialAnimation::init()
1229 {
1230     if (!_global)
1231         cloneMaterials(_branch);
1232 }
1233
1234 int SGMaterialAnimation::update()
1235 {
1236     if (_condition) {
1237         bool cond = _condition->test();
1238         if (cond && !_last_condition)
1239             _update |= _static_update;
1240
1241         _last_condition = cond;
1242         if (!cond)
1243             return 2;
1244     }
1245
1246     if (_read & DIFFUSE)
1247         updateColorGroup(&_diff, DIFFUSE);
1248     if (_read & AMBIENT)
1249         updateColorGroup(&_amb, AMBIENT);
1250     if (_read & EMISSION)
1251         updateColorGroup(&_emis, EMISSION);
1252     if (_read & SPECULAR)
1253         updateColorGroup(&_spec, SPECULAR);
1254
1255     float f;
1256     if (_shi_prop) {
1257         f = _shi;
1258         _shi = _shi_prop->getFloatValue();
1259         if (_shi != f)
1260             _update |= SHININESS;
1261     }
1262     if (_read & TRANSPARENCY) {
1263         PropSpec tmp = _trans;
1264         if (_trans.value_prop)
1265             _trans.value = _trans.value_prop->getFloatValue();
1266         if (_trans.factor_prop)
1267             _trans.factor = _trans.factor_prop->getFloatValue();
1268         if (_trans.offset_prop)
1269             _trans.offset = _trans.offset_prop->getFloatValue();
1270         if (_trans != tmp)
1271             _update |= TRANSPARENCY;
1272     }
1273     if (_thresh_prop) {
1274         f = _thresh;
1275         _thresh = _thresh_prop->getFloatValue();
1276         if (_thresh != f)
1277             _update |= THRESHOLD;
1278     }
1279     if (_tex_prop) {
1280         string t = _tex_prop->getStringValue();
1281         if (!t.empty() && t != _texture_str) {
1282             _texture_str = t;
1283             _texture = _texture_base;
1284             _texture.append(t);
1285             _update |= TEXTURE;
1286         }
1287     }
1288     if (_update) {
1289         setMaterialBranch(_branch);
1290         _update = 0;
1291     }
1292     return 2;
1293 }
1294
1295 void SGMaterialAnimation::updateColorGroup(ColorSpec *col, int flag)
1296 {
1297     ColorSpec tmp = *col;
1298     if (col->red_prop)
1299         col->red = col->red_prop->getFloatValue();
1300     if (col->green_prop)
1301         col->green = col->green_prop->getFloatValue();
1302     if (col->blue_prop)
1303         col->blue = col->blue_prop->getFloatValue();
1304     if (col->factor_prop)
1305         col->factor = col->factor_prop->getFloatValue();
1306     if (col->offset_prop)
1307         col->offset = col->offset_prop->getFloatValue();
1308     if (*col != tmp)
1309         _update |= flag;
1310 }
1311
1312 void SGMaterialAnimation::cloneMaterials(ssgBranch *b)
1313 {
1314     for (int i = 0; i < b->getNumKids(); i++)
1315         cloneMaterials((ssgBranch *)b->getKid(i));
1316
1317     if (!b->isAKindOf(ssgTypeLeaf()) || !((ssgLeaf *)b)->hasState())
1318         return;
1319
1320     ssgSimpleState *s = (ssgSimpleState *)((ssgLeaf *)b)->getState();
1321     if (!_cached_material || _cached_material != s) {
1322         _cached_material = s;
1323         _cloned_material = (ssgSimpleState *)s->clone(SSG_CLONE_STATE);
1324     }
1325     ((ssgLeaf *)b)->setState(_cloned_material);
1326 }
1327
1328 void SGMaterialAnimation::setMaterialBranch(ssgBranch *b)
1329 {
1330     for (int i = 0; i < b->getNumKids(); i++)
1331         setMaterialBranch((ssgBranch *)b->getKid(i));
1332
1333     if (!b->isAKindOf(ssgTypeLeaf()) || !((ssgLeaf *)b)->hasState())
1334         return;
1335
1336     ssgSimpleState *s = (ssgSimpleState *)((ssgLeaf *)b)->getState();
1337
1338     if (_update & DIFFUSE) {
1339         float *v = _diff.rgba();
1340         SGfloat alpha = s->getMaterial(GL_DIFFUSE)[3];
1341         s->setColourMaterial(GL_DIFFUSE);
1342         s->enable(GL_COLOR_MATERIAL);
1343         s->setMaterial(GL_DIFFUSE, v[0], v[1], v[2], alpha);
1344         s->disable(GL_COLOR_MATERIAL);
1345     }
1346     if (_update & AMBIENT) {
1347         s->setColourMaterial(GL_AMBIENT);
1348         s->enable(GL_COLOR_MATERIAL);
1349         s->setMaterial(GL_AMBIENT, _amb.rgba());
1350         s->disable(GL_COLOR_MATERIAL);
1351     }
1352     if (_update & EMISSION)
1353         s->setMaterial(GL_EMISSION, _emis.rgba());
1354     if (_update & SPECULAR)
1355         s->setMaterial(GL_SPECULAR, _spec.rgba());
1356     if (_update & SHININESS)
1357         s->setShininess(clamp(_shi, 0.0, 128.0));
1358     if (_update & TRANSPARENCY) {
1359         SGfloat *v = s->getMaterial(GL_DIFFUSE);
1360         float trans = _trans.value * _trans.factor + _trans.offset;
1361         trans = trans < _trans.min ? _trans.min : trans > _trans.max ? _trans.max : trans;
1362         s->setMaterial(GL_DIFFUSE, v[0], v[1], v[2], trans);
1363     }
1364     if (_update & THRESHOLD)
1365         s->setAlphaClamp(clamp(_thresh));
1366     if (_update & TEXTURE)
1367         s->setTexture(_texture.c_str());
1368     if (_update & (TEXTURE|TRANSPARENCY)) {
1369         SGfloat alpha = s->getMaterial(GL_DIFFUSE)[3];
1370         ssgTexture *tex = s->getTexture();
1371         if ((tex && tex->hasAlpha()) || alpha < 0.999) {
1372             s->setColourMaterial(GL_DIFFUSE);
1373             s->enable(GL_COLOR_MATERIAL);
1374             s->enable(GL_BLEND);
1375             s->enable(GL_ALPHA_TEST);
1376             s->setTranslucent();
1377             s->disable(GL_COLOR_MATERIAL);
1378         } else {
1379             s->disable(GL_BLEND);
1380             s->disable(GL_ALPHA_TEST);
1381             s->setOpaque();
1382         }
1383     }
1384     s->force();
1385 }
1386
1387
1388 \f
1389 ////////////////////////////////////////////////////////////////////////
1390 // Implementation of SGFlashAnimation
1391 ////////////////////////////////////////////////////////////////////////
1392 SGFlashAnimation::SGFlashAnimation(SGPropertyNode_ptr props)
1393   : SGAnimation( props, new SGCustomTransform )
1394 {
1395   _axis[0] = props->getFloatValue("axis/x", 0);
1396   _axis[1] = props->getFloatValue("axis/y", 0);
1397   _axis[2] = props->getFloatValue("axis/z", 1);
1398
1399   _center[0] = props->getFloatValue("center/x-m", 0);
1400   _center[1] = props->getFloatValue("center/y-m", 0);
1401   _center[2] = props->getFloatValue("center/z-m", 0);
1402
1403   _offset = props->getFloatValue("offset", 0.0);
1404   _factor = props->getFloatValue("factor", 1.0);
1405   _power = props->getFloatValue("power", 1.0);
1406   _two_sides = props->getBoolValue("two-sides", false);
1407
1408   _min_v = props->getFloatValue("min", 0.0);
1409   _max_v = props->getFloatValue("max", 1.0);
1410
1411   ((SGCustomTransform *)_branch)->setTransCallback( &SGFlashAnimation::flashCallback, this );
1412 }
1413
1414 SGFlashAnimation::~SGFlashAnimation()
1415 {
1416 }
1417
1418 void SGFlashAnimation::flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d )
1419 {
1420   ((SGFlashAnimation *)d)->flashCallback( r, f, m );
1421 }
1422
1423 void SGFlashAnimation::flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m )
1424 {
1425   sgVec3 transformed_axis;
1426   sgXformVec3( transformed_axis, _axis, m );
1427   sgNormalizeVec3( transformed_axis );
1428
1429   sgVec3 view;
1430   sgFullXformPnt3( view, _center, m );
1431   sgNormalizeVec3( view );
1432
1433   float cos_angle = -sgScalarProductVec3( transformed_axis, view );
1434   float scale_factor = 0.f;
1435   if ( _two_sides && cos_angle < 0 )
1436     scale_factor = _factor * (float)pow( -cos_angle, _power ) + _offset;
1437   else if ( cos_angle > 0 )
1438     scale_factor = _factor * (float)pow( cos_angle, _power ) + _offset;
1439
1440   if ( scale_factor < _min_v )
1441       scale_factor = _min_v;
1442   if ( scale_factor > _max_v )
1443       scale_factor = _max_v;
1444
1445   sgMat4 transform;
1446   sgMakeIdentMat4( transform );
1447   transform[0][0] = scale_factor;
1448   transform[1][1] = scale_factor;
1449   transform[2][2] = scale_factor;
1450   transform[3][0] = _center[0] * ( 1 - scale_factor );
1451   transform[3][1] = _center[1] * ( 1 - scale_factor );
1452   transform[3][2] = _center[2] * ( 1 - scale_factor );
1453
1454   sgCopyMat4( r, m );
1455   sgPreMultMat4( r, transform );
1456 }
1457
1458
1459 \f
1460 ////////////////////////////////////////////////////////////////////////
1461 // Implementation of SGDistScaleAnimation
1462 ////////////////////////////////////////////////////////////////////////
1463 SGDistScaleAnimation::SGDistScaleAnimation(SGPropertyNode_ptr props)
1464   : SGAnimation( props, new SGCustomTransform ),
1465     _factor(props->getFloatValue("factor", 1.0)),
1466     _offset(props->getFloatValue("offset", 0.0)),
1467     _min_v(props->getFloatValue("min", 0.0)),
1468     _max_v(props->getFloatValue("max", 1.0)),
1469     _has_min(props->hasValue("min")),
1470     _has_max(props->hasValue("max")),
1471     _table(read_interpolation_table(props))
1472 {
1473   _center[0] = props->getFloatValue("center/x-m", 0);
1474   _center[1] = props->getFloatValue("center/y-m", 0);
1475   _center[2] = props->getFloatValue("center/z-m", 0);
1476
1477   ((SGCustomTransform *)_branch)->setTransCallback( &SGDistScaleAnimation::distScaleCallback, this );
1478 }
1479
1480 SGDistScaleAnimation::~SGDistScaleAnimation()
1481 {
1482 }
1483
1484 void SGDistScaleAnimation::distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d )
1485 {
1486   ((SGDistScaleAnimation *)d)->distScaleCallback( r, f, m );
1487 }
1488
1489 void SGDistScaleAnimation::distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m )
1490 {
1491   sgVec3 view;
1492   sgFullXformPnt3( view, _center, m );
1493
1494   float scale_factor = sgLengthVec3( view );
1495   if (_table == 0) {
1496     scale_factor = _factor * scale_factor + _offset;
1497     if ( _has_min && scale_factor < _min_v )
1498       scale_factor = _min_v;
1499     if ( _has_max && scale_factor > _max_v )
1500       scale_factor = _max_v;
1501   } else {
1502     scale_factor = _table->interpolate( scale_factor );
1503   }
1504
1505   sgMat4 transform;
1506   sgMakeIdentMat4( transform );
1507   transform[0][0] = scale_factor;
1508   transform[1][1] = scale_factor;
1509   transform[2][2] = scale_factor;
1510   transform[3][0] = _center[0] * ( 1 - scale_factor );
1511   transform[3][1] = _center[1] * ( 1 - scale_factor );
1512   transform[3][2] = _center[2] * ( 1 - scale_factor );
1513
1514   sgCopyMat4( r, m );
1515   sgPreMultMat4( r, transform );
1516 }
1517
1518 ////////////////////////////////////////////////////////////////////////
1519 // Implementation of SGShadowAnimation
1520 ////////////////////////////////////////////////////////////////////////
1521
1522 SGShadowAnimation::SGShadowAnimation ( SGPropertyNode *prop_root,
1523                    SGPropertyNode_ptr props )
1524   : SGAnimation(props, new ssgBranch),
1525     _condition(0),
1526         _condition_value(true)
1527 {
1528         animation_type = 1;
1529         SGPropertyNode_ptr node = props->getChild("condition");
1530         if (node != 0) {
1531                 _condition = sgReadCondition(prop_root, node);
1532                 _condition_value = false;
1533         }
1534 }
1535
1536 SGShadowAnimation::~SGShadowAnimation ()
1537 {
1538         delete _condition;
1539 }
1540
1541 int
1542 SGShadowAnimation::update()
1543 {
1544         if (_condition)
1545                 _condition_value = _condition->test();
1546         return 2;
1547 }
1548
1549 bool SGShadowAnimation::get_condition_value(void) {
1550         return _condition_value;
1551 }
1552
1553 // end of animation.cxx