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"
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));
187 SGAnimation::~SGAnimation ()
197 SGAnimation::update()
203 SGAnimation::restore()
209 ////////////////////////////////////////////////////////////////////////
210 // Implementation of SGNullAnimation
211 ////////////////////////////////////////////////////////////////////////
213 SGNullAnimation::SGNullAnimation (SGPropertyNode_ptr props)
214 : SGAnimation(props, new ssgBranch)
218 SGNullAnimation::~SGNullAnimation ()
224 ////////////////////////////////////////////////////////////////////////
225 // Implementation of SGRangeAnimation
226 ////////////////////////////////////////////////////////////////////////
228 SGRangeAnimation::SGRangeAnimation (SGPropertyNode *prop_root,
229 SGPropertyNode_ptr props)
230 : SGAnimation(props, new ssgRangeSelector),
231 _min(0.0), _max(0.0), _min_factor(1.0), _max_factor(1.0),
234 SGPropertyNode_ptr node = props->getChild("condition");
236 _condition = sgReadCondition(prop_root, node);
240 node = props->getChild( "min-factor" );
242 _min_factor = props->getFloatValue("min-factor", 1.0);
244 node = props->getChild( "max-factor" );
246 _max_factor = props->getFloatValue("max-factor", 1.0);
248 node = props->getChild( "min-property" );
250 _min_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
251 ranges[0] = _min_prop->getFloatValue() * _min_factor;
253 _min = props->getFloatValue("min-m", 0);
254 ranges[0] = _min * _min_factor;
256 node = props->getChild( "max-property" );
258 _max_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
259 ranges[1] = _max_prop->getFloatValue() * _max_factor;
261 _max = props->getFloatValue("max-m", 0);
262 ranges[1] = _max * _max_factor;
264 ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
267 SGRangeAnimation::~SGRangeAnimation ()
272 SGRangeAnimation::update()
275 if ( _condition == 0 || _condition->test() ) {
276 if (_min_prop != 0) {
277 ranges[0] = _min_prop->getFloatValue() * _min_factor;
279 ranges[0] = _min * _min_factor;
281 if (_max_prop != 0) {
282 ranges[1] = _max_prop->getFloatValue() * _max_factor;
284 ranges[1] = _max * _max_factor;
288 ranges[1] = 1000000000.f;
290 ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
296 ////////////////////////////////////////////////////////////////////////
297 // Implementation of SGBillboardAnimation
298 ////////////////////////////////////////////////////////////////////////
300 SGBillboardAnimation::SGBillboardAnimation (SGPropertyNode_ptr props)
301 : SGAnimation(props, new ssgCutout(props->getBoolValue("spherical", true)))
305 SGBillboardAnimation::~SGBillboardAnimation ()
311 ////////////////////////////////////////////////////////////////////////
312 // Implementation of SGSelectAnimation
313 ////////////////////////////////////////////////////////////////////////
315 SGSelectAnimation::SGSelectAnimation( SGPropertyNode *prop_root,
316 SGPropertyNode_ptr props )
317 : SGAnimation(props, new ssgSelector),
320 SGPropertyNode_ptr node = props->getChild("condition");
322 _condition = sgReadCondition(prop_root, node);
325 SGSelectAnimation::~SGSelectAnimation ()
331 SGSelectAnimation::update()
333 if (_condition != 0 && _condition->test())
334 ((ssgSelector *)_branch)->select(0xffff);
336 ((ssgSelector *)_branch)->select(0x0000);
342 ////////////////////////////////////////////////////////////////////////
343 // Implementation of SGSpinAnimation
344 ////////////////////////////////////////////////////////////////////////
346 SGSpinAnimation::SGSpinAnimation( SGPropertyNode *prop_root,
347 SGPropertyNode_ptr props,
348 double sim_time_sec )
349 : SGAnimation(props, new ssgTransform),
350 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
351 _factor(props->getDoubleValue("factor", 1.0)),
352 _position_deg(props->getDoubleValue("starting-position-deg", 0)),
353 _last_time_sec( sim_time_sec ),
356 SGPropertyNode_ptr node = props->getChild("condition");
358 _condition = sgReadCondition(prop_root, node);
363 if (props->hasValue("axis/x1-m")) {
364 double x1,y1,z1,x2,y2,z2;
365 x1 = props->getFloatValue("axis/x1-m");
366 y1 = props->getFloatValue("axis/y1-m");
367 z1 = props->getFloatValue("axis/z1-m");
368 x2 = props->getFloatValue("axis/x2-m");
369 y2 = props->getFloatValue("axis/y2-m");
370 z2 = props->getFloatValue("axis/z2-m");
371 _center[0] = (x1+x2)/2;
372 _center[1]= (y1+y2)/2;
373 _center[2] = (z1+z2)/2;
374 float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
375 _axis[0] = (x2-x1)/vector_length;
376 _axis[1] = (y2-y1)/vector_length;
377 _axis[2] = (z2-z1)/vector_length;
379 _axis[0] = props->getFloatValue("axis/x", 0);
380 _axis[1] = props->getFloatValue("axis/y", 0);
381 _axis[2] = props->getFloatValue("axis/z", 0);
383 if (props->hasValue("center/x-m")) {
384 _center[0] = props->getFloatValue("center/x-m", 0);
385 _center[1] = props->getFloatValue("center/y-m", 0);
386 _center[2] = props->getFloatValue("center/z-m", 0);
388 sgNormalizeVec3(_axis);
391 SGSpinAnimation::~SGSpinAnimation ()
396 SGSpinAnimation::update()
398 if ( _condition == 0 || _condition->test() ) {
399 double dt = sim_time_sec - _last_time_sec;
400 _last_time_sec = sim_time_sec;
402 float velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
403 _position_deg += (dt * velocity_rpms * 360);
404 while (_position_deg < 0)
405 _position_deg += 360.0;
406 while (_position_deg >= 360.0)
407 _position_deg -= 360.0;
408 set_rotation(_matrix, _position_deg, _center, _axis);
409 ((ssgTransform *)_branch)->setTransform(_matrix);
416 ////////////////////////////////////////////////////////////////////////
417 // Implementation of SGTimedAnimation
418 ////////////////////////////////////////////////////////////////////////
420 SGTimedAnimation::SGTimedAnimation (SGPropertyNode_ptr props)
421 : SGAnimation(props, new ssgSelector),
422 _use_personality( props->getBoolValue("use-personality",false) ),
423 _duration_sec(props->getDoubleValue("duration-sec", 1.0)),
424 _last_time_sec( sim_time_sec ),
425 _total_duration_sec( 0 ),
429 vector<SGPropertyNode_ptr> nodes = props->getChildren( "branch-duration-sec" );
430 size_t nb = nodes.size();
431 for ( size_t i = 0; i < nb; i++ ) {
432 size_t ind = nodes[ i ]->getIndex();
433 while ( ind >= _branch_duration_specs.size() ) {
434 _branch_duration_specs.push_back( DurationSpec( _duration_sec ) );
436 SGPropertyNode_ptr rNode = nodes[ i ]->getChild("random");
438 _branch_duration_specs[ ind ] = DurationSpec( nodes[ i ]->getDoubleValue() );
440 _branch_duration_specs[ ind ] = DurationSpec( rNode->getDoubleValue( "min", 0.0 ),
441 rNode->getDoubleValue( "max", 1.0 ) );
444 if ( !_use_personality ) {
445 for ( size_t i = 0; i < _branch_duration_specs.size(); i++ ) {
446 DurationSpec &sp = _branch_duration_specs[ i ];
447 double v = sp._min + sg_random() * ( sp._max - sp._min );
448 _branch_duration_sec.push_back( v );
449 _total_duration_sec += v;
452 ((ssgSelector *)getBranch())->selectStep(_step);
455 SGTimedAnimation::~SGTimedAnimation ()
460 SGTimedAnimation::update()
462 if ( _use_personality ) {
463 SGPersonalityBranch *key = current_object;
464 if ( !key->getIntValue( this, INIT ) ) {
466 for ( size_t i = 0; i < _branch_duration_specs.size(); i++ ) {
467 DurationSpec &sp = _branch_duration_specs[ i ];
468 double v = sp._min + sg_random() * ( sp._max - sp._min );
469 key->setDoubleValue( v, this, BRANCH_DURATION_SEC, i );
472 key->setDoubleValue( sim_time_sec, this, LAST_TIME_SEC );
473 key->setDoubleValue( total, this, TOTAL_DURATION_SEC );
474 key->setIntValue( 0, this, STEP );
475 key->setIntValue( 1, this, INIT );
478 _step = key->getIntValue( this, STEP );
479 _last_time_sec = key->getDoubleValue( this, LAST_TIME_SEC );
480 _total_duration_sec = key->getDoubleValue( this, TOTAL_DURATION_SEC );
481 while ( ( sim_time_sec - _last_time_sec ) >= _total_duration_sec ) {
482 _last_time_sec += _total_duration_sec;
484 double duration = _duration_sec;
485 if ( _step < (int)_branch_duration_specs.size() ) {
486 duration = key->getDoubleValue( this, BRANCH_DURATION_SEC, _step );
488 if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
489 _last_time_sec += duration;
491 if ( _step >= getBranch()->getNumKids() )
494 ((ssgSelector *)getBranch())->selectStep( _step );
495 key->setDoubleValue( _last_time_sec, this, LAST_TIME_SEC );
496 key->setIntValue( _step, this, STEP );
498 while ( ( sim_time_sec - _last_time_sec ) >= _total_duration_sec ) {
499 _last_time_sec += _total_duration_sec;
501 double duration = _duration_sec;
502 if ( _step < (int)_branch_duration_sec.size() ) {
503 duration = _branch_duration_sec[ _step ];
505 if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
506 _last_time_sec += duration;
508 if ( _step >= getBranch()->getNumKids() )
510 ((ssgSelector *)getBranch())->selectStep( _step );
518 ////////////////////////////////////////////////////////////////////////
519 // Implementation of SGRotateAnimation
520 ////////////////////////////////////////////////////////////////////////
522 SGRotateAnimation::SGRotateAnimation( SGPropertyNode *prop_root,
523 SGPropertyNode_ptr props )
524 : SGAnimation(props, new ssgTransform),
525 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
526 _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
527 _factor(props->getDoubleValue("factor", 1.0)),
528 _table(read_interpolation_table(props)),
529 _has_min(props->hasValue("min-deg")),
530 _min_deg(props->getDoubleValue("min-deg")),
531 _has_max(props->hasValue("max-deg")),
532 _max_deg(props->getDoubleValue("max-deg")),
533 _position_deg(props->getDoubleValue("starting-position-deg", 0)),
536 SGPropertyNode_ptr node = props->getChild("condition");
538 _condition = sgReadCondition(prop_root, node);
543 if (props->hasValue("axis/x1-m")) {
544 double x1,y1,z1,x2,y2,z2;
545 x1 = props->getFloatValue("axis/x1-m");
546 y1 = props->getFloatValue("axis/y1-m");
547 z1 = props->getFloatValue("axis/z1-m");
548 x2 = props->getFloatValue("axis/x2-m");
549 y2 = props->getFloatValue("axis/y2-m");
550 z2 = props->getFloatValue("axis/z2-m");
551 _center[0] = (x1+x2)/2;
552 _center[1]= (y1+y2)/2;
553 _center[2] = (z1+z2)/2;
554 float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
555 _axis[0] = (x2-x1)/vector_length;
556 _axis[1] = (y2-y1)/vector_length;
557 _axis[2] = (z2-z1)/vector_length;
559 _axis[0] = props->getFloatValue("axis/x", 0);
560 _axis[1] = props->getFloatValue("axis/y", 0);
561 _axis[2] = props->getFloatValue("axis/z", 0);
563 if (props->hasValue("center/x-m")) {
564 _center[0] = props->getFloatValue("center/x-m", 0);
565 _center[1] = props->getFloatValue("center/y-m", 0);
566 _center[2] = props->getFloatValue("center/z-m", 0);
568 sgNormalizeVec3(_axis);
571 SGRotateAnimation::~SGRotateAnimation ()
577 SGRotateAnimation::update()
579 if (_condition == 0 || _condition->test()) {
581 _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
582 if (_has_min && _position_deg < _min_deg)
583 _position_deg = _min_deg;
584 if (_has_max && _position_deg > _max_deg)
585 _position_deg = _max_deg;
587 _position_deg = _table->interpolate(_prop->getDoubleValue());
589 set_rotation(_matrix, _position_deg, _center, _axis);
590 ((ssgTransform *)_branch)->setTransform(_matrix);
596 ////////////////////////////////////////////////////////////////////////
597 // Implementation of SGBlendAnimation
598 ////////////////////////////////////////////////////////////////////////
600 SGBlendAnimation::SGBlendAnimation( SGPropertyNode *prop_root,
601 SGPropertyNode_ptr props )
602 : SGAnimation(props, new ssgTransform),
603 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
604 _table(read_interpolation_table(props)),
606 _offset(props->getDoubleValue("offset", 0.0)),
607 _factor(props->getDoubleValue("factor", 1.0)),
608 _has_min(props->hasValue("min")),
609 _min(props->getDoubleValue("min", 0.0)),
610 _has_max(props->hasValue("max")),
611 _max(props->getDoubleValue("max", 1.0))
615 SGBlendAnimation::~SGBlendAnimation ()
621 SGBlendAnimation::update()
626 _blend = 1.0 - (_prop->getDoubleValue() * _factor + _offset);
628 if (_has_min && (_blend < _min))
630 if (_has_max && (_blend > _max))
633 _blend = _table->interpolate(_prop->getDoubleValue());
636 if (_blend != _prev_value) {
637 _prev_value = _blend;
638 change_alpha( _branch, _blend );
645 ////////////////////////////////////////////////////////////////////////
646 // Implementation of SGTranslateAnimation
647 ////////////////////////////////////////////////////////////////////////
649 SGTranslateAnimation::SGTranslateAnimation( SGPropertyNode *prop_root,
650 SGPropertyNode_ptr props )
651 : SGAnimation(props, new ssgTransform),
652 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
653 _offset_m(props->getDoubleValue("offset-m", 0.0)),
654 _factor(props->getDoubleValue("factor", 1.0)),
655 _table(read_interpolation_table(props)),
656 _has_min(props->hasValue("min-m")),
657 _min_m(props->getDoubleValue("min-m")),
658 _has_max(props->hasValue("max-m")),
659 _max_m(props->getDoubleValue("max-m")),
660 _position_m(props->getDoubleValue("starting-position-m", 0)),
663 SGPropertyNode_ptr node = props->getChild("condition");
665 _condition = sgReadCondition(prop_root, node);
667 _axis[0] = props->getFloatValue("axis/x", 0);
668 _axis[1] = props->getFloatValue("axis/y", 0);
669 _axis[2] = props->getFloatValue("axis/z", 0);
670 sgNormalizeVec3(_axis);
673 SGTranslateAnimation::~SGTranslateAnimation ()
679 SGTranslateAnimation::update()
681 if (_condition == 0 || _condition->test()) {
683 _position_m = (_prop->getDoubleValue() + _offset_m) * _factor;
684 if (_has_min && _position_m < _min_m)
685 _position_m = _min_m;
686 if (_has_max && _position_m > _max_m)
687 _position_m = _max_m;
689 _position_m = _table->interpolate(_prop->getDoubleValue());
691 set_translation(_matrix, _position_m, _axis);
692 ((ssgTransform *)_branch)->setTransform(_matrix);
699 ////////////////////////////////////////////////////////////////////////
700 // Implementation of SGScaleAnimation
701 ////////////////////////////////////////////////////////////////////////
703 SGScaleAnimation::SGScaleAnimation( SGPropertyNode *prop_root,
704 SGPropertyNode_ptr props )
705 : SGAnimation(props, new ssgTransform),
706 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
707 _x_factor(props->getDoubleValue("x-factor", 1.0)),
708 _y_factor(props->getDoubleValue("y-factor", 1.0)),
709 _z_factor(props->getDoubleValue("z-factor", 1.0)),
710 _x_offset(props->getDoubleValue("x-offset", 1.0)),
711 _y_offset(props->getDoubleValue("y-offset", 1.0)),
712 _z_offset(props->getDoubleValue("z-offset", 1.0)),
713 _table(read_interpolation_table(props)),
714 _has_min_x(props->hasValue("x-min")),
715 _has_min_y(props->hasValue("y-min")),
716 _has_min_z(props->hasValue("z-min")),
717 _min_x(props->getDoubleValue("x-min")),
718 _min_y(props->getDoubleValue("y-min")),
719 _min_z(props->getDoubleValue("z-min")),
720 _has_max_x(props->hasValue("x-max")),
721 _has_max_y(props->hasValue("y-max")),
722 _has_max_z(props->hasValue("z-max")),
723 _max_x(props->getDoubleValue("x-max")),
724 _max_y(props->getDoubleValue("y-max")),
725 _max_z(props->getDoubleValue("z-max"))
729 SGScaleAnimation::~SGScaleAnimation ()
735 SGScaleAnimation::update()
738 _x_scale = _prop->getDoubleValue() * _x_factor + _x_offset;
739 if (_has_min_x && _x_scale < _min_x)
741 if (_has_max_x && _x_scale > _max_x)
744 _x_scale = _table->interpolate(_prop->getDoubleValue());
748 _y_scale = _prop->getDoubleValue() * _y_factor + _y_offset;
749 if (_has_min_y && _y_scale < _min_y)
751 if (_has_max_y && _y_scale > _max_y)
754 _y_scale = _table->interpolate(_prop->getDoubleValue());
758 _z_scale = _prop->getDoubleValue() * _z_factor + _z_offset;
759 if (_has_min_z && _z_scale < _min_z)
761 if (_has_max_z && _z_scale > _max_z)
764 _z_scale = _table->interpolate(_prop->getDoubleValue());
767 set_scale(_matrix, _x_scale, _y_scale, _z_scale );
768 ((ssgTransform *)_branch)->setTransform(_matrix);
773 ////////////////////////////////////////////////////////////////////////
774 // Implementation of SGTexRotateAnimation
775 ////////////////////////////////////////////////////////////////////////
777 SGTexRotateAnimation::SGTexRotateAnimation( SGPropertyNode *prop_root,
778 SGPropertyNode_ptr props )
779 : SGAnimation(props, new ssgTexTrans),
780 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
781 _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
782 _factor(props->getDoubleValue("factor", 1.0)),
783 _table(read_interpolation_table(props)),
784 _has_min(props->hasValue("min-deg")),
785 _min_deg(props->getDoubleValue("min-deg")),
786 _has_max(props->hasValue("max-deg")),
787 _max_deg(props->getDoubleValue("max-deg")),
788 _position_deg(props->getDoubleValue("starting-position-deg", 0))
790 _center[0] = props->getFloatValue("center/x", 0);
791 _center[1] = props->getFloatValue("center/y", 0);
792 _center[2] = props->getFloatValue("center/z", 0);
793 _axis[0] = props->getFloatValue("axis/x", 0);
794 _axis[1] = props->getFloatValue("axis/y", 0);
795 _axis[2] = props->getFloatValue("axis/z", 0);
796 sgNormalizeVec3(_axis);
799 SGTexRotateAnimation::~SGTexRotateAnimation ()
805 SGTexRotateAnimation::update()
808 _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
809 if (_has_min && _position_deg < _min_deg)
810 _position_deg = _min_deg;
811 if (_has_max && _position_deg > _max_deg)
812 _position_deg = _max_deg;
814 _position_deg = _table->interpolate(_prop->getDoubleValue());
816 set_rotation(_matrix, _position_deg, _center, _axis);
817 ((ssgTexTrans *)_branch)->setTransform(_matrix);
822 ////////////////////////////////////////////////////////////////////////
823 // Implementation of SGTexTranslateAnimation
824 ////////////////////////////////////////////////////////////////////////
826 SGTexTranslateAnimation::SGTexTranslateAnimation( SGPropertyNode *prop_root,
827 SGPropertyNode_ptr props )
828 : SGAnimation(props, new ssgTexTrans),
829 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
830 _offset(props->getDoubleValue("offset", 0.0)),
831 _factor(props->getDoubleValue("factor", 1.0)),
832 _step(props->getDoubleValue("step",0.0)),
833 _scroll(props->getDoubleValue("scroll",0.0)),
834 _table(read_interpolation_table(props)),
835 _has_min(props->hasValue("min")),
836 _min(props->getDoubleValue("min")),
837 _has_max(props->hasValue("max")),
838 _max(props->getDoubleValue("max")),
839 _position(props->getDoubleValue("starting-position", 0))
841 _axis[0] = props->getFloatValue("axis/x", 0);
842 _axis[1] = props->getFloatValue("axis/y", 0);
843 _axis[2] = props->getFloatValue("axis/z", 0);
844 sgNormalizeVec3(_axis);
847 SGTexTranslateAnimation::~SGTexTranslateAnimation ()
853 SGTexTranslateAnimation::update()
856 _position = (apply_mods(_prop->getDoubleValue(), _step, _scroll) + _offset) * _factor;
857 if (_has_min && _position < _min)
859 if (_has_max && _position > _max)
862 _position = _table->interpolate(apply_mods(_prop->getDoubleValue(), _step, _scroll));
864 set_translation(_matrix, _position, _axis);
865 ((ssgTexTrans *)_branch)->setTransform(_matrix);
870 ////////////////////////////////////////////////////////////////////////
871 // Implementation of SGTexMultipleAnimation
872 ////////////////////////////////////////////////////////////////////////
874 SGTexMultipleAnimation::SGTexMultipleAnimation( SGPropertyNode *prop_root,
875 SGPropertyNode_ptr props )
876 : SGAnimation(props, new ssgTexTrans),
877 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true))
881 vector<SGPropertyNode_ptr> transform_nodes = props->getChildren("transform");
882 _transform = new TexTransform [transform_nodes.size()];
884 for (i = 0; i < transform_nodes.size(); i++) {
885 SGPropertyNode_ptr transform_props = transform_nodes[i];
887 if (!strcmp("textranslate",transform_props->getStringValue("subtype", 0))) {
889 // transform is a translation
890 _transform[i].subtype = 0;
892 _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
894 _transform[i].offset = transform_props->getDoubleValue("offset", 0.0);
895 _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
896 _transform[i].step = transform_props->getDoubleValue("step",0.0);
897 _transform[i].scroll = transform_props->getDoubleValue("scroll",0.0);
898 _transform[i].table = read_interpolation_table(transform_props);
899 _transform[i].has_min = transform_props->hasValue("min");
900 _transform[i].min = transform_props->getDoubleValue("min");
901 _transform[i].has_max = transform_props->hasValue("max");
902 _transform[i].max = transform_props->getDoubleValue("max");
903 _transform[i].position = transform_props->getDoubleValue("starting-position", 0);
905 _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
906 _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
907 _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
908 sgNormalizeVec3(_transform[i].axis);
910 } else if (!strcmp("texrotate",transform_nodes[i]->getStringValue("subtype", 0))) {
912 // transform is a rotation
913 _transform[i].subtype = 1;
915 _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
916 _transform[i].offset = transform_props->getDoubleValue("offset-deg", 0.0);
917 _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
918 _transform[i].table = read_interpolation_table(transform_props);
919 _transform[i].has_min = transform_props->hasValue("min-deg");
920 _transform[i].min = transform_props->getDoubleValue("min-deg");
921 _transform[i].has_max = transform_props->hasValue("max-deg");
922 _transform[i].max = transform_props->getDoubleValue("max-deg");
923 _transform[i].position = transform_props->getDoubleValue("starting-position-deg", 0);
925 _transform[i].center[0] = transform_props->getFloatValue("center/x", 0);
926 _transform[i].center[1] = transform_props->getFloatValue("center/y", 0);
927 _transform[i].center[2] = transform_props->getFloatValue("center/z", 0);
928 _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
929 _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
930 _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
931 sgNormalizeVec3(_transform[i].axis);
937 SGTexMultipleAnimation::~SGTexMultipleAnimation ()
944 SGTexMultipleAnimation::update()
948 sgMakeIdentMat4(tmatrix);
949 for (i = 0; i < _num_transforms; i++) {
951 if(_transform[i].subtype == 0) {
953 // subtype 0 is translation
954 if (_transform[i].table == 0) {
955 _transform[i].position = (apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll) + _transform[i].offset) * _transform[i].factor;
956 if (_transform[i].has_min && _transform[i].position < _transform[i].min)
957 _transform[i].position = _transform[i].min;
958 if (_transform[i].has_max && _transform[i].position > _transform[i].max)
959 _transform[i].position = _transform[i].max;
961 _transform[i].position = _transform[i].table->interpolate(apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll));
963 set_translation(_transform[i].matrix, _transform[i].position, _transform[i].axis);
964 sgPreMultMat4(tmatrix, _transform[i].matrix);
966 } else if (_transform[i].subtype == 1) {
968 // subtype 1 is rotation
970 if (_transform[i].table == 0) {
971 _transform[i].position = _transform[i].prop->getDoubleValue() * _transform[i].factor + _transform[i].offset;
972 if (_transform[i].has_min && _transform[i].position < _transform[i].min)
973 _transform[i].position = _transform[i].min;
974 if (_transform[i].has_max && _transform[i].position > _transform[i].max)
975 _transform[i].position = _transform[i].max;
977 _transform[i].position = _transform[i].table->interpolate(_transform[i].prop->getDoubleValue());
979 set_rotation(_transform[i].matrix, _transform[i].position, _transform[i].center, _transform[i].axis);
980 sgPreMultMat4(tmatrix, _transform[i].matrix);
983 ((ssgTexTrans *)_branch)->setTransform(tmatrix);
989 ////////////////////////////////////////////////////////////////////////
990 // Implementation of SGAlphaTestAnimation
991 ////////////////////////////////////////////////////////////////////////
993 SGAlphaTestAnimation::SGAlphaTestAnimation(SGPropertyNode_ptr props)
994 : SGAnimation(props, new ssgBranch)
996 _alpha_clamp = props->getFloatValue("alpha-factor", 0.0);
999 SGAlphaTestAnimation::~SGAlphaTestAnimation ()
1003 void SGAlphaTestAnimation::init()
1005 setAlphaClampToBranch(_branch,_alpha_clamp);
1008 void SGAlphaTestAnimation::setAlphaClampToBranch(ssgBranch *b, float clamp)
1010 int nb = b->getNumKids();
1011 for (int i = 0; i<nb; i++) {
1012 ssgEntity *e = b->getKid(i);
1013 if (e->isAKindOf(ssgTypeLeaf())) {
1014 ssgSimpleState*s = (ssgSimpleState*)((ssgLeaf*)e)->getState();
1015 s->enable( GL_ALPHA_TEST );
1016 s->setAlphaClamp( clamp );
1017 } else if (e->isAKindOf(ssgTypeBranch())) {
1018 setAlphaClampToBranch( (ssgBranch*)e, clamp );
1025 ////////////////////////////////////////////////////////////////////////
1026 // Implementation of SGFlashAnimation
1027 ////////////////////////////////////////////////////////////////////////
1028 SGFlashAnimation::SGFlashAnimation(SGPropertyNode_ptr props)
1029 : SGAnimation( props, new SGFlash )
1032 axis[0] = props->getFloatValue("axis/x", 0);
1033 axis[1] = props->getFloatValue("axis/y", 0);
1034 axis[2] = props->getFloatValue("axis/z", 1);
1035 ((SGFlash *)_branch)->setAxis( axis );
1038 center[0] = props->getFloatValue("center/x-m", 0);
1039 center[1] = props->getFloatValue("center/y-m", 0);
1040 center[2] = props->getFloatValue("center/z-m", 0);
1041 ((SGFlash *)_branch)->setCenter( center );
1043 float offset = props->getFloatValue("offset", 0.0);
1044 float factor = props->getFloatValue("factor", 1.0);
1045 float power = props->getFloatValue("power", 1.0);
1046 bool two_sides = props->getBoolValue("two-sides", false);
1047 ((SGFlash *)_branch)->setParameters( power, factor, offset, two_sides );
1049 float v_min = props->getFloatValue("min", 0.0);
1050 float v_max = props->getFloatValue("max", 1.0);
1051 ((SGFlash *)_branch)->setClampValues( v_min, v_max );
1054 SGFlashAnimation::~SGFlashAnimation()
1058 // end of animation.cxx