1 // animation.cxx - classes to manage model animation.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
7 #include <string.h> // for strcmp()
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>
19 #include "animation.hxx"
20 #include "custtrans.hxx"
21 #include "personality.hxx"
24 ////////////////////////////////////////////////////////////////////////
25 // Static utility functions.
26 ////////////////////////////////////////////////////////////////////////
29 * Set up the transform matrix for a spin or rotation.
32 set_rotation (sgMat4 &matrix, double position_deg,
33 sgVec3 ¢er, sgVec3 &axis)
35 float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
37 float s = (float) sin ( temp_angle ) ;
38 float c = (float) cos ( temp_angle ) ;
39 float t = SG_ONE - c ;
41 // axis was normalized at load time
42 // hint to the compiler to put these into FP registers
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;
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;
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;
62 // hint to the compiler to put these into FP registers
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;
74 * Set up the transform matrix for a translation.
77 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
80 sgScaleVec3(xyz, axis, position_m);
81 sgMakeTransMat4(matrix, xyz);
85 * Set up the transform matrix for a scale operation.
88 set_scale (sgMat4 &matrix, double x, double y, double z)
90 sgMakeIdentMat4( matrix );
97 * Recursively process all kids to change the alpha values
100 change_alpha( ssgBase *_branch, float _blend )
104 for (i = 0; i < ((ssgBranch *)_branch)->getNumKids(); i++)
105 change_alpha( ((ssgBranch *)_branch)->getKid(i), _blend );
107 if ( !_branch->isAKindOf(ssgTypeLeaf())
108 && !_branch->isAKindOf(ssgTypeVtxTable())
109 && !_branch->isAKindOf(ssgTypeVTable()) )
112 int num_colors = ((ssgLeaf *)_branch)->getNumColours();
113 // unsigned int select_ = (_blend == 1.0) ? false : true;
115 for (i = 0; i < num_colors; i++)
117 // ((ssgSelector *)_branch)->select( select_ );
118 float *color = ((ssgLeaf *)_branch)->getColour(i);
124 * Modify property value by step and scroll settings in texture translations
127 apply_mods(double property, double step, double scroll)
132 double scrollval = 0.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;
140 // apply stepping of input value
142 modprop = ((floor(property/step) * step) + scrollval);
144 modprop = ((ceil(property/step) * step) + scrollval);
153 * Read an interpolation table from properties.
155 static SGInterpTable *
156 read_interpolation_table (SGPropertyNode_ptr props)
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));
173 ////////////////////////////////////////////////////////////////////////
174 // Implementation of SGAnimation
175 ////////////////////////////////////////////////////////////////////////
177 // Initialize the static data member
178 double SGAnimation::sim_time_sec = 0.0;
179 SGPersonalityBranch *SGAnimation::current_object = 0;
181 SGAnimation::SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch)
184 _branch->setName(props->getStringValue("name", 0));
185 if ( props->getBoolValue( "enable-hot", true ) ) {
186 _branch->setTraversalMaskBits( SSGTRAV_HOT );
188 _branch->clrTraversalMaskBits( SSGTRAV_HOT );
192 SGAnimation::~SGAnimation ()
202 SGAnimation::update()
208 SGAnimation::restore()
214 ////////////////////////////////////////////////////////////////////////
215 // Implementation of SGNullAnimation
216 ////////////////////////////////////////////////////////////////////////
218 SGNullAnimation::SGNullAnimation (SGPropertyNode_ptr props)
219 : SGAnimation(props, new ssgBranch)
223 SGNullAnimation::~SGNullAnimation ()
229 ////////////////////////////////////////////////////////////////////////
230 // Implementation of SGRangeAnimation
231 ////////////////////////////////////////////////////////////////////////
233 SGRangeAnimation::SGRangeAnimation (SGPropertyNode *prop_root,
234 SGPropertyNode_ptr props)
235 : SGAnimation(props, new ssgRangeSelector),
236 _min(0.0), _max(0.0), _min_factor(1.0), _max_factor(1.0),
239 SGPropertyNode_ptr node = props->getChild("condition");
241 _condition = sgReadCondition(prop_root, node);
245 node = props->getChild( "min-factor" );
247 _min_factor = props->getFloatValue("min-factor", 1.0);
249 node = props->getChild( "max-factor" );
251 _max_factor = props->getFloatValue("max-factor", 1.0);
253 node = props->getChild( "min-property" );
255 _min_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
256 ranges[0] = _min_prop->getFloatValue() * _min_factor;
258 _min = props->getFloatValue("min-m", 0);
259 ranges[0] = _min * _min_factor;
261 node = props->getChild( "max-property" );
263 _max_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
264 ranges[1] = _max_prop->getFloatValue() * _max_factor;
266 _max = props->getFloatValue("max-m", 0);
267 ranges[1] = _max * _max_factor;
269 ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
272 SGRangeAnimation::~SGRangeAnimation ()
277 SGRangeAnimation::update()
280 if ( _condition == 0 || _condition->test() ) {
281 if (_min_prop != 0) {
282 ranges[0] = _min_prop->getFloatValue() * _min_factor;
284 ranges[0] = _min * _min_factor;
286 if (_max_prop != 0) {
287 ranges[1] = _max_prop->getFloatValue() * _max_factor;
289 ranges[1] = _max * _max_factor;
293 ranges[1] = 1000000000.f;
295 ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
301 ////////////////////////////////////////////////////////////////////////
302 // Implementation of SGBillboardAnimation
303 ////////////////////////////////////////////////////////////////////////
305 SGBillboardAnimation::SGBillboardAnimation (SGPropertyNode_ptr props)
306 : SGAnimation(props, new ssgCutout(props->getBoolValue("spherical", true)))
310 SGBillboardAnimation::~SGBillboardAnimation ()
316 ////////////////////////////////////////////////////////////////////////
317 // Implementation of SGSelectAnimation
318 ////////////////////////////////////////////////////////////////////////
320 SGSelectAnimation::SGSelectAnimation( SGPropertyNode *prop_root,
321 SGPropertyNode_ptr props )
322 : SGAnimation(props, new ssgSelector),
325 SGPropertyNode_ptr node = props->getChild("condition");
327 _condition = sgReadCondition(prop_root, node);
330 SGSelectAnimation::~SGSelectAnimation ()
336 SGSelectAnimation::update()
338 if (_condition != 0 && _condition->test())
339 ((ssgSelector *)_branch)->select(0xffff);
341 ((ssgSelector *)_branch)->select(0x0000);
347 ////////////////////////////////////////////////////////////////////////
348 // Implementation of SGSpinAnimation
349 ////////////////////////////////////////////////////////////////////////
351 SGSpinAnimation::SGSpinAnimation( SGPropertyNode *prop_root,
352 SGPropertyNode_ptr props,
353 double sim_time_sec )
354 : SGAnimation(props, new ssgTransform),
355 _use_personality( props->getBoolValue("use-personality",false) ),
356 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
357 _last_time_sec( sim_time_sec ),
360 SGPropertyNode_ptr node = props->getChild("condition");
362 _condition = sgReadCondition(prop_root, node);
367 if (props->hasValue("axis/x1-m")) {
368 double x1,y1,z1,x2,y2,z2;
369 x1 = props->getFloatValue("axis/x1-m");
370 y1 = props->getFloatValue("axis/y1-m");
371 z1 = props->getFloatValue("axis/z1-m");
372 x2 = props->getFloatValue("axis/x2-m");
373 y2 = props->getFloatValue("axis/y2-m");
374 z2 = props->getFloatValue("axis/z2-m");
375 _center[0] = (x1+x2)/2;
376 _center[1]= (y1+y2)/2;
377 _center[2] = (z1+z2)/2;
378 float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
379 _axis[0] = (x2-x1)/vector_length;
380 _axis[1] = (y2-y1)/vector_length;
381 _axis[2] = (z2-z1)/vector_length;
383 _axis[0] = props->getFloatValue("axis/x", 0);
384 _axis[1] = props->getFloatValue("axis/y", 0);
385 _axis[2] = props->getFloatValue("axis/z", 0);
387 if (props->hasValue("center/x-m")) {
388 _center[0] = props->getFloatValue("center/x-m", 0);
389 _center[1] = props->getFloatValue("center/y-m", 0);
390 _center[2] = props->getFloatValue("center/z-m", 0);
392 sgNormalizeVec3(_axis);
394 //_factor(props->getDoubleValue("factor", 1.0)),
398 SGPropertyNode_ptr factor_n = props->getNode( "factor" );
399 if ( factor_n != 0 ) {
400 SGPropertyNode_ptr rand_n = factor_n->getNode( "random" );
402 _factor_min = rand_n->getDoubleValue( "min", 0.0 );
403 _factor_max = rand_n->getDoubleValue( "max", 1.0 );
404 _factor = _factor_min + sg_random() * ( _factor_max - _factor_min );
406 _factor = _factor_min = _factor_max = props->getDoubleValue("factor", 1.0);
409 //_position_deg(props->getDoubleValue("starting-position-deg", 0)),
410 _position_deg_min = 0.0;
411 _position_deg_max = 0.0;
412 SGPropertyNode_ptr position_deg_n = props->getNode( "starting-position-deg" );
413 if ( position_deg_n != 0 ) {
414 SGPropertyNode_ptr rand_n = position_deg_n->getNode( "random" );
416 _position_deg_min = rand_n->getDoubleValue( "min", 0.0 );
417 _position_deg_max = rand_n->getDoubleValue( "max", 1.0 );
418 _position_deg = _position_deg_min + sg_random() * ( _position_deg_max - _position_deg_min );
420 _position_deg = _position_deg_min = _position_deg_max =
421 props->getDoubleValue("starting-position-deg", 1.0);
426 SGSpinAnimation::~SGSpinAnimation ()
431 SGSpinAnimation::update()
433 if ( _condition == 0 || _condition->test() ) {
436 if ( _use_personality ) {
437 SGPersonalityBranch *key = current_object;
438 if ( !key->getIntValue( this, INIT_SPIN ) ) {
439 double v = _factor_min + sg_random() * ( _factor_max - _factor_min );
440 key->setDoubleValue( v, this, FACTOR_SPIN );
442 key->setDoubleValue( sim_time_sec, this, LAST_TIME_SEC_SPIN );
443 key->setIntValue( 1, this, INIT_SPIN );
445 v = _position_deg_min + sg_random() * ( _position_deg_max - _position_deg_min );
446 key->setDoubleValue( v, this, POSITION_DEG_SPIN );
449 _factor = key->getDoubleValue( this, FACTOR_SPIN );
450 _position_deg = key->getDoubleValue( this, POSITION_DEG_SPIN );
451 _last_time_sec = key->getDoubleValue( this, LAST_TIME_SEC_SPIN );
452 dt = sim_time_sec - _last_time_sec;
453 _last_time_sec = sim_time_sec;
454 key->setDoubleValue( _last_time_sec, this, LAST_TIME_SEC_SPIN );
456 velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
457 _position_deg += (dt * velocity_rpms * 360);
458 while (_position_deg < 0)
459 _position_deg += 360.0;
460 while (_position_deg >= 360.0)
461 _position_deg -= 360.0;
462 key->setDoubleValue( _position_deg, this, POSITION_DEG_SPIN );
464 dt = sim_time_sec - _last_time_sec;
465 _last_time_sec = sim_time_sec;
467 velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
468 _position_deg += (dt * velocity_rpms * 360);
469 while (_position_deg < 0)
470 _position_deg += 360.0;
471 while (_position_deg >= 360.0)
472 _position_deg -= 360.0;
475 set_rotation(_matrix, _position_deg, _center, _axis);
476 ((ssgTransform *)_branch)->setTransform(_matrix);
483 ////////////////////////////////////////////////////////////////////////
484 // Implementation of SGTimedAnimation
485 ////////////////////////////////////////////////////////////////////////
487 SGTimedAnimation::SGTimedAnimation (SGPropertyNode_ptr props)
488 : SGAnimation(props, new ssgSelector),
489 _use_personality( props->getBoolValue("use-personality",false) ),
490 _duration_sec(props->getDoubleValue("duration-sec", 1.0)),
491 _last_time_sec( sim_time_sec ),
492 _total_duration_sec( 0 ),
496 vector<SGPropertyNode_ptr> nodes = props->getChildren( "branch-duration-sec" );
497 size_t nb = nodes.size();
498 for ( size_t i = 0; i < nb; i++ ) {
499 size_t ind = nodes[ i ]->getIndex();
500 while ( ind >= _branch_duration_specs.size() ) {
501 _branch_duration_specs.push_back( DurationSpec( _duration_sec ) );
503 SGPropertyNode_ptr rNode = nodes[ i ]->getChild("random");
505 _branch_duration_specs[ ind ] = DurationSpec( nodes[ i ]->getDoubleValue() );
507 _branch_duration_specs[ ind ] = DurationSpec( rNode->getDoubleValue( "min", 0.0 ),
508 rNode->getDoubleValue( "max", 1.0 ) );
513 SGTimedAnimation::~SGTimedAnimation ()
518 SGTimedAnimation::init()
520 if ( !_use_personality ) {
521 for ( int i = 0; i < getBranch()->getNumKids(); i++ ) {
523 if ( i < (int)_branch_duration_specs.size() ) {
524 DurationSpec &sp = _branch_duration_specs[ i ];
525 v = sp._min + sg_random() * ( sp._max - sp._min );
529 _branch_duration_sec.push_back( v );
530 _total_duration_sec += v;
532 // Sanity check : total duration shouldn't equal zero
533 if ( _total_duration_sec < 0.01 ) {
534 _total_duration_sec = 0.01;
537 ((ssgSelector *)getBranch())->selectStep(_step);
541 SGTimedAnimation::update()
543 if ( _use_personality ) {
544 SGPersonalityBranch *key = current_object;
545 if ( !key->getIntValue( this, INIT_TIMED ) ) {
548 for ( size_t i = 0; i < _branch_duration_specs.size(); i++ ) {
549 DurationSpec &sp = _branch_duration_specs[ i ];
550 double v = sp._min + sg_random() * ( sp._max - sp._min );
551 key->setDoubleValue( v, this, BRANCH_DURATION_SEC_TIMED, i );
556 // Sanity check : total duration shouldn't equal zero
557 if ( total < 0.01 ) {
560 offset *= sg_random();
561 key->setDoubleValue( sim_time_sec - offset, this, LAST_TIME_SEC_TIMED );
562 key->setDoubleValue( total, this, TOTAL_DURATION_SEC_TIMED );
563 key->setIntValue( 0, this, STEP_TIMED );
564 key->setIntValue( 1, this, INIT_TIMED );
567 _step = key->getIntValue( this, STEP_TIMED );
568 _last_time_sec = key->getDoubleValue( this, LAST_TIME_SEC_TIMED );
569 _total_duration_sec = key->getDoubleValue( this, TOTAL_DURATION_SEC_TIMED );
570 while ( ( sim_time_sec - _last_time_sec ) >= _total_duration_sec ) {
571 _last_time_sec += _total_duration_sec;
573 double duration = _duration_sec;
574 if ( _step < (int)_branch_duration_specs.size() ) {
575 duration = key->getDoubleValue( this, BRANCH_DURATION_SEC_TIMED, _step );
577 if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
578 _last_time_sec += duration;
580 if ( _step >= getBranch()->getNumKids() )
583 ((ssgSelector *)getBranch())->selectStep( _step );
584 key->setDoubleValue( _last_time_sec, this, LAST_TIME_SEC_TIMED );
585 key->setIntValue( _step, this, STEP_TIMED );
587 while ( ( sim_time_sec - _last_time_sec ) >= _total_duration_sec ) {
588 _last_time_sec += _total_duration_sec;
590 double duration = _duration_sec;
591 if ( _step < (int)_branch_duration_sec.size() ) {
592 duration = _branch_duration_sec[ _step ];
594 if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
595 _last_time_sec += duration;
597 if ( _step >= getBranch()->getNumKids() )
599 ((ssgSelector *)getBranch())->selectStep( _step );
607 ////////////////////////////////////////////////////////////////////////
608 // Implementation of SGRotateAnimation
609 ////////////////////////////////////////////////////////////////////////
611 SGRotateAnimation::SGRotateAnimation( SGPropertyNode *prop_root,
612 SGPropertyNode_ptr props )
613 : SGAnimation(props, new ssgTransform),
614 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
615 _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
616 _factor(props->getDoubleValue("factor", 1.0)),
617 _table(read_interpolation_table(props)),
618 _has_min(props->hasValue("min-deg")),
619 _min_deg(props->getDoubleValue("min-deg")),
620 _has_max(props->hasValue("max-deg")),
621 _max_deg(props->getDoubleValue("max-deg")),
622 _position_deg(props->getDoubleValue("starting-position-deg", 0)),
625 SGPropertyNode_ptr node = props->getChild("condition");
627 _condition = sgReadCondition(prop_root, node);
632 if (props->hasValue("axis/x1-m")) {
633 double x1,y1,z1,x2,y2,z2;
634 x1 = props->getFloatValue("axis/x1-m");
635 y1 = props->getFloatValue("axis/y1-m");
636 z1 = props->getFloatValue("axis/z1-m");
637 x2 = props->getFloatValue("axis/x2-m");
638 y2 = props->getFloatValue("axis/y2-m");
639 z2 = props->getFloatValue("axis/z2-m");
640 _center[0] = (x1+x2)/2;
641 _center[1]= (y1+y2)/2;
642 _center[2] = (z1+z2)/2;
643 float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
644 _axis[0] = (x2-x1)/vector_length;
645 _axis[1] = (y2-y1)/vector_length;
646 _axis[2] = (z2-z1)/vector_length;
648 _axis[0] = props->getFloatValue("axis/x", 0);
649 _axis[1] = props->getFloatValue("axis/y", 0);
650 _axis[2] = props->getFloatValue("axis/z", 0);
652 if (props->hasValue("center/x-m")) {
653 _center[0] = props->getFloatValue("center/x-m", 0);
654 _center[1] = props->getFloatValue("center/y-m", 0);
655 _center[2] = props->getFloatValue("center/z-m", 0);
657 sgNormalizeVec3(_axis);
660 SGRotateAnimation::~SGRotateAnimation ()
666 SGRotateAnimation::update()
668 if (_condition == 0 || _condition->test()) {
670 _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
671 if (_has_min && _position_deg < _min_deg)
672 _position_deg = _min_deg;
673 if (_has_max && _position_deg > _max_deg)
674 _position_deg = _max_deg;
676 _position_deg = _table->interpolate(_prop->getDoubleValue());
678 set_rotation(_matrix, _position_deg, _center, _axis);
679 ((ssgTransform *)_branch)->setTransform(_matrix);
685 ////////////////////////////////////////////////////////////////////////
686 // Implementation of SGBlendAnimation
687 ////////////////////////////////////////////////////////////////////////
689 SGBlendAnimation::SGBlendAnimation( SGPropertyNode *prop_root,
690 SGPropertyNode_ptr props )
691 : SGAnimation(props, new ssgTransform),
692 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
693 _table(read_interpolation_table(props)),
695 _offset(props->getDoubleValue("offset", 0.0)),
696 _factor(props->getDoubleValue("factor", 1.0)),
697 _has_min(props->hasValue("min")),
698 _min(props->getDoubleValue("min", 0.0)),
699 _has_max(props->hasValue("max")),
700 _max(props->getDoubleValue("max", 1.0))
704 SGBlendAnimation::~SGBlendAnimation ()
710 SGBlendAnimation::update()
715 _blend = 1.0 - (_prop->getDoubleValue() * _factor + _offset);
717 if (_has_min && (_blend < _min))
719 if (_has_max && (_blend > _max))
722 _blend = _table->interpolate(_prop->getDoubleValue());
725 if (_blend != _prev_value) {
726 _prev_value = _blend;
727 change_alpha( _branch, _blend );
734 ////////////////////////////////////////////////////////////////////////
735 // Implementation of SGTranslateAnimation
736 ////////////////////////////////////////////////////////////////////////
738 SGTranslateAnimation::SGTranslateAnimation( SGPropertyNode *prop_root,
739 SGPropertyNode_ptr props )
740 : SGAnimation(props, new ssgTransform),
741 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
742 _offset_m(props->getDoubleValue("offset-m", 0.0)),
743 _factor(props->getDoubleValue("factor", 1.0)),
744 _table(read_interpolation_table(props)),
745 _has_min(props->hasValue("min-m")),
746 _min_m(props->getDoubleValue("min-m")),
747 _has_max(props->hasValue("max-m")),
748 _max_m(props->getDoubleValue("max-m")),
749 _position_m(props->getDoubleValue("starting-position-m", 0)),
752 SGPropertyNode_ptr node = props->getChild("condition");
754 _condition = sgReadCondition(prop_root, node);
756 _axis[0] = props->getFloatValue("axis/x", 0);
757 _axis[1] = props->getFloatValue("axis/y", 0);
758 _axis[2] = props->getFloatValue("axis/z", 0);
759 sgNormalizeVec3(_axis);
762 SGTranslateAnimation::~SGTranslateAnimation ()
768 SGTranslateAnimation::update()
770 if (_condition == 0 || _condition->test()) {
772 _position_m = (_prop->getDoubleValue() + _offset_m) * _factor;
773 if (_has_min && _position_m < _min_m)
774 _position_m = _min_m;
775 if (_has_max && _position_m > _max_m)
776 _position_m = _max_m;
778 _position_m = _table->interpolate(_prop->getDoubleValue());
780 set_translation(_matrix, _position_m, _axis);
781 ((ssgTransform *)_branch)->setTransform(_matrix);
788 ////////////////////////////////////////////////////////////////////////
789 // Implementation of SGScaleAnimation
790 ////////////////////////////////////////////////////////////////////////
792 SGScaleAnimation::SGScaleAnimation( SGPropertyNode *prop_root,
793 SGPropertyNode_ptr props )
794 : SGAnimation(props, new ssgTransform),
795 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
796 _x_factor(props->getDoubleValue("x-factor", 1.0)),
797 _y_factor(props->getDoubleValue("y-factor", 1.0)),
798 _z_factor(props->getDoubleValue("z-factor", 1.0)),
799 _x_offset(props->getDoubleValue("x-offset", 1.0)),
800 _y_offset(props->getDoubleValue("y-offset", 1.0)),
801 _z_offset(props->getDoubleValue("z-offset", 1.0)),
802 _table(read_interpolation_table(props)),
803 _has_min_x(props->hasValue("x-min")),
804 _has_min_y(props->hasValue("y-min")),
805 _has_min_z(props->hasValue("z-min")),
806 _min_x(props->getDoubleValue("x-min")),
807 _min_y(props->getDoubleValue("y-min")),
808 _min_z(props->getDoubleValue("z-min")),
809 _has_max_x(props->hasValue("x-max")),
810 _has_max_y(props->hasValue("y-max")),
811 _has_max_z(props->hasValue("z-max")),
812 _max_x(props->getDoubleValue("x-max")),
813 _max_y(props->getDoubleValue("y-max")),
814 _max_z(props->getDoubleValue("z-max"))
818 SGScaleAnimation::~SGScaleAnimation ()
824 SGScaleAnimation::update()
827 _x_scale = _prop->getDoubleValue() * _x_factor + _x_offset;
828 if (_has_min_x && _x_scale < _min_x)
830 if (_has_max_x && _x_scale > _max_x)
833 _x_scale = _table->interpolate(_prop->getDoubleValue());
837 _y_scale = _prop->getDoubleValue() * _y_factor + _y_offset;
838 if (_has_min_y && _y_scale < _min_y)
840 if (_has_max_y && _y_scale > _max_y)
843 _y_scale = _table->interpolate(_prop->getDoubleValue());
847 _z_scale = _prop->getDoubleValue() * _z_factor + _z_offset;
848 if (_has_min_z && _z_scale < _min_z)
850 if (_has_max_z && _z_scale > _max_z)
853 _z_scale = _table->interpolate(_prop->getDoubleValue());
856 set_scale(_matrix, _x_scale, _y_scale, _z_scale );
857 ((ssgTransform *)_branch)->setTransform(_matrix);
862 ////////////////////////////////////////////////////////////////////////
863 // Implementation of SGTexRotateAnimation
864 ////////////////////////////////////////////////////////////////////////
866 SGTexRotateAnimation::SGTexRotateAnimation( SGPropertyNode *prop_root,
867 SGPropertyNode_ptr props )
868 : SGAnimation(props, new ssgTexTrans),
869 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
870 _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
871 _factor(props->getDoubleValue("factor", 1.0)),
872 _table(read_interpolation_table(props)),
873 _has_min(props->hasValue("min-deg")),
874 _min_deg(props->getDoubleValue("min-deg")),
875 _has_max(props->hasValue("max-deg")),
876 _max_deg(props->getDoubleValue("max-deg")),
877 _position_deg(props->getDoubleValue("starting-position-deg", 0))
879 _center[0] = props->getFloatValue("center/x", 0);
880 _center[1] = props->getFloatValue("center/y", 0);
881 _center[2] = props->getFloatValue("center/z", 0);
882 _axis[0] = props->getFloatValue("axis/x", 0);
883 _axis[1] = props->getFloatValue("axis/y", 0);
884 _axis[2] = props->getFloatValue("axis/z", 0);
885 sgNormalizeVec3(_axis);
888 SGTexRotateAnimation::~SGTexRotateAnimation ()
894 SGTexRotateAnimation::update()
897 _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
898 if (_has_min && _position_deg < _min_deg)
899 _position_deg = _min_deg;
900 if (_has_max && _position_deg > _max_deg)
901 _position_deg = _max_deg;
903 _position_deg = _table->interpolate(_prop->getDoubleValue());
905 set_rotation(_matrix, _position_deg, _center, _axis);
906 ((ssgTexTrans *)_branch)->setTransform(_matrix);
911 ////////////////////////////////////////////////////////////////////////
912 // Implementation of SGTexTranslateAnimation
913 ////////////////////////////////////////////////////////////////////////
915 SGTexTranslateAnimation::SGTexTranslateAnimation( SGPropertyNode *prop_root,
916 SGPropertyNode_ptr props )
917 : SGAnimation(props, new ssgTexTrans),
918 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
919 _offset(props->getDoubleValue("offset", 0.0)),
920 _factor(props->getDoubleValue("factor", 1.0)),
921 _step(props->getDoubleValue("step",0.0)),
922 _scroll(props->getDoubleValue("scroll",0.0)),
923 _table(read_interpolation_table(props)),
924 _has_min(props->hasValue("min")),
925 _min(props->getDoubleValue("min")),
926 _has_max(props->hasValue("max")),
927 _max(props->getDoubleValue("max")),
928 _position(props->getDoubleValue("starting-position", 0))
930 _axis[0] = props->getFloatValue("axis/x", 0);
931 _axis[1] = props->getFloatValue("axis/y", 0);
932 _axis[2] = props->getFloatValue("axis/z", 0);
933 sgNormalizeVec3(_axis);
936 SGTexTranslateAnimation::~SGTexTranslateAnimation ()
942 SGTexTranslateAnimation::update()
945 _position = (apply_mods(_prop->getDoubleValue(), _step, _scroll) + _offset) * _factor;
946 if (_has_min && _position < _min)
948 if (_has_max && _position > _max)
951 _position = _table->interpolate(apply_mods(_prop->getDoubleValue(), _step, _scroll));
953 set_translation(_matrix, _position, _axis);
954 ((ssgTexTrans *)_branch)->setTransform(_matrix);
959 ////////////////////////////////////////////////////////////////////////
960 // Implementation of SGTexMultipleAnimation
961 ////////////////////////////////////////////////////////////////////////
963 SGTexMultipleAnimation::SGTexMultipleAnimation( SGPropertyNode *prop_root,
964 SGPropertyNode_ptr props )
965 : SGAnimation(props, new ssgTexTrans),
966 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true))
970 vector<SGPropertyNode_ptr> transform_nodes = props->getChildren("transform");
971 _transform = new TexTransform [transform_nodes.size()];
973 for (i = 0; i < transform_nodes.size(); i++) {
974 SGPropertyNode_ptr transform_props = transform_nodes[i];
976 if (!strcmp("textranslate",transform_props->getStringValue("subtype", 0))) {
978 // transform is a translation
979 _transform[i].subtype = 0;
981 _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
983 _transform[i].offset = transform_props->getDoubleValue("offset", 0.0);
984 _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
985 _transform[i].step = transform_props->getDoubleValue("step",0.0);
986 _transform[i].scroll = transform_props->getDoubleValue("scroll",0.0);
987 _transform[i].table = read_interpolation_table(transform_props);
988 _transform[i].has_min = transform_props->hasValue("min");
989 _transform[i].min = transform_props->getDoubleValue("min");
990 _transform[i].has_max = transform_props->hasValue("max");
991 _transform[i].max = transform_props->getDoubleValue("max");
992 _transform[i].position = transform_props->getDoubleValue("starting-position", 0);
994 _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
995 _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
996 _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
997 sgNormalizeVec3(_transform[i].axis);
999 } else if (!strcmp("texrotate",transform_nodes[i]->getStringValue("subtype", 0))) {
1001 // transform is a rotation
1002 _transform[i].subtype = 1;
1004 _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
1005 _transform[i].offset = transform_props->getDoubleValue("offset-deg", 0.0);
1006 _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
1007 _transform[i].table = read_interpolation_table(transform_props);
1008 _transform[i].has_min = transform_props->hasValue("min-deg");
1009 _transform[i].min = transform_props->getDoubleValue("min-deg");
1010 _transform[i].has_max = transform_props->hasValue("max-deg");
1011 _transform[i].max = transform_props->getDoubleValue("max-deg");
1012 _transform[i].position = transform_props->getDoubleValue("starting-position-deg", 0);
1014 _transform[i].center[0] = transform_props->getFloatValue("center/x", 0);
1015 _transform[i].center[1] = transform_props->getFloatValue("center/y", 0);
1016 _transform[i].center[2] = transform_props->getFloatValue("center/z", 0);
1017 _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
1018 _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
1019 _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
1020 sgNormalizeVec3(_transform[i].axis);
1026 SGTexMultipleAnimation::~SGTexMultipleAnimation ()
1028 delete [] _transform;
1032 SGTexMultipleAnimation::update()
1036 sgMakeIdentMat4(tmatrix);
1037 for (i = 0; i < _num_transforms; i++) {
1039 if(_transform[i].subtype == 0) {
1041 // subtype 0 is translation
1042 if (_transform[i].table == 0) {
1043 _transform[i].position = (apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll) + _transform[i].offset) * _transform[i].factor;
1044 if (_transform[i].has_min && _transform[i].position < _transform[i].min)
1045 _transform[i].position = _transform[i].min;
1046 if (_transform[i].has_max && _transform[i].position > _transform[i].max)
1047 _transform[i].position = _transform[i].max;
1049 _transform[i].position = _transform[i].table->interpolate(apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll));
1051 set_translation(_transform[i].matrix, _transform[i].position, _transform[i].axis);
1052 sgPreMultMat4(tmatrix, _transform[i].matrix);
1054 } else if (_transform[i].subtype == 1) {
1056 // subtype 1 is rotation
1058 if (_transform[i].table == 0) {
1059 _transform[i].position = _transform[i].prop->getDoubleValue() * _transform[i].factor + _transform[i].offset;
1060 if (_transform[i].has_min && _transform[i].position < _transform[i].min)
1061 _transform[i].position = _transform[i].min;
1062 if (_transform[i].has_max && _transform[i].position > _transform[i].max)
1063 _transform[i].position = _transform[i].max;
1065 _transform[i].position = _transform[i].table->interpolate(_transform[i].prop->getDoubleValue());
1067 set_rotation(_transform[i].matrix, _transform[i].position, _transform[i].center, _transform[i].axis);
1068 sgPreMultMat4(tmatrix, _transform[i].matrix);
1071 ((ssgTexTrans *)_branch)->setTransform(tmatrix);
1077 ////////////////////////////////////////////////////////////////////////
1078 // Implementation of SGAlphaTestAnimation
1079 ////////////////////////////////////////////////////////////////////////
1081 SGAlphaTestAnimation::SGAlphaTestAnimation(SGPropertyNode_ptr props)
1082 : SGAnimation(props, new ssgBranch)
1084 _alpha_clamp = props->getFloatValue("alpha-factor", 0.0);
1087 SGAlphaTestAnimation::~SGAlphaTestAnimation ()
1091 void SGAlphaTestAnimation::init()
1093 setAlphaClampToBranch(_branch,_alpha_clamp);
1096 void SGAlphaTestAnimation::setAlphaClampToBranch(ssgBranch *b, float clamp)
1098 int nb = b->getNumKids();
1099 for (int i = 0; i<nb; i++) {
1100 ssgEntity *e = b->getKid(i);
1101 if (e->isAKindOf(ssgTypeLeaf())) {
1102 ssgSimpleState*s = (ssgSimpleState*)((ssgLeaf*)e)->getState();
1103 s->enable( GL_ALPHA_TEST );
1104 s->setAlphaClamp( clamp );
1105 } else if (e->isAKindOf(ssgTypeBranch())) {
1106 setAlphaClampToBranch( (ssgBranch*)e, clamp );
1113 ////////////////////////////////////////////////////////////////////////
1114 // Implementation of SGFlashAnimation
1115 ////////////////////////////////////////////////////////////////////////
1116 SGFlashAnimation::SGFlashAnimation(SGPropertyNode_ptr props)
1117 : SGAnimation( props, new SGCustomTransform )
1119 _axis[0] = props->getFloatValue("axis/x", 0);
1120 _axis[1] = props->getFloatValue("axis/y", 0);
1121 _axis[2] = props->getFloatValue("axis/z", 1);
1123 _center[0] = props->getFloatValue("center/x-m", 0);
1124 _center[1] = props->getFloatValue("center/y-m", 0);
1125 _center[2] = props->getFloatValue("center/z-m", 0);
1127 _offset = props->getFloatValue("offset", 0.0);
1128 _factor = props->getFloatValue("factor", 1.0);
1129 _power = props->getFloatValue("power", 1.0);
1130 _two_sides = props->getBoolValue("two-sides", false);
1132 _min_v = props->getFloatValue("min", 0.0);
1133 _max_v = props->getFloatValue("max", 1.0);
1135 ((SGCustomTransform *)_branch)->setTransCallback( &SGFlashAnimation::flashCallback, this );
1138 SGFlashAnimation::~SGFlashAnimation()
1142 void SGFlashAnimation::flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d )
1144 ((SGFlashAnimation *)d)->flashCallback( r, f, m );
1147 void SGFlashAnimation::flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m )
1149 sgVec3 transformed_axis;
1150 sgXformVec3( transformed_axis, _axis, m );
1151 sgNormalizeVec3( transformed_axis );
1154 sgFullXformPnt3( view, _center, m );
1155 sgNormalizeVec3( view );
1157 float cos_angle = -sgScalarProductVec3( transformed_axis, view );
1158 float scale_factor = 0.f;
1159 if ( _two_sides && cos_angle < 0 )
1160 scale_factor = _factor * (float)pow( -cos_angle, _power ) + _offset;
1161 else if ( cos_angle > 0 )
1162 scale_factor = _factor * (float)pow( cos_angle, _power ) + _offset;
1164 if ( scale_factor < _min_v )
1165 scale_factor = _min_v;
1166 if ( scale_factor > _max_v )
1167 scale_factor = _max_v;
1170 sgMakeIdentMat4( transform );
1171 transform[0][0] = scale_factor;
1172 transform[1][1] = scale_factor;
1173 transform[2][2] = scale_factor;
1174 transform[3][0] = _center[0] * ( 1 - scale_factor );
1175 transform[3][1] = _center[1] * ( 1 - scale_factor );
1176 transform[3][2] = _center[2] * ( 1 - scale_factor );
1179 sgPreMultMat4( r, transform );
1184 ////////////////////////////////////////////////////////////////////////
1185 // Implementation of SGDistScaleAnimation
1186 ////////////////////////////////////////////////////////////////////////
1187 SGDistScaleAnimation::SGDistScaleAnimation(SGPropertyNode_ptr props)
1188 : SGAnimation( props, new SGCustomTransform ),
1189 _factor(props->getFloatValue("factor", 1.0)),
1190 _offset(props->getFloatValue("offset", 0.0)),
1191 _min_v(props->getFloatValue("min", 0.0)),
1192 _max_v(props->getFloatValue("max", 1.0)),
1193 _has_min(props->hasValue("min")),
1194 _has_max(props->hasValue("max")),
1195 _table(read_interpolation_table(props))
1197 _center[0] = props->getFloatValue("center/x-m", 0);
1198 _center[1] = props->getFloatValue("center/y-m", 0);
1199 _center[2] = props->getFloatValue("center/z-m", 0);
1201 ((SGCustomTransform *)_branch)->setTransCallback( &SGDistScaleAnimation::distScaleCallback, this );
1204 SGDistScaleAnimation::~SGDistScaleAnimation()
1208 void SGDistScaleAnimation::distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d )
1210 ((SGDistScaleAnimation *)d)->distScaleCallback( r, f, m );
1213 void SGDistScaleAnimation::distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m )
1216 sgFullXformPnt3( view, _center, m );
1218 float scale_factor = sgLengthVec3( view );
1220 scale_factor = _factor * scale_factor + _offset;
1221 if ( _has_min && scale_factor < _min_v )
1222 scale_factor = _min_v;
1223 if ( _has_max && scale_factor > _max_v )
1224 scale_factor = _max_v;
1226 scale_factor = _table->interpolate( scale_factor );
1230 sgMakeIdentMat4( transform );
1231 transform[0][0] = scale_factor;
1232 transform[1][1] = scale_factor;
1233 transform[2][2] = scale_factor;
1234 transform[3][0] = _center[0] * ( 1 - scale_factor );
1235 transform[3][1] = _center[1] * ( 1 - scale_factor );
1236 transform[3][2] = _center[2] * ( 1 - scale_factor );
1239 sgPreMultMat4( r, transform );
1242 // end of animation.cxx