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 <simgear_config.h>
10 #include <string.h> // for strcmp()
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>
22 #include "animation.hxx"
23 #include "custtrans.hxx"
24 #include "personality.hxx"
27 ////////////////////////////////////////////////////////////////////////
28 // Static utility functions.
29 ////////////////////////////////////////////////////////////////////////
32 * Set up the transform matrix for a spin or rotation.
35 set_rotation (sgMat4 &matrix, double position_deg,
36 sgVec3 ¢er, sgVec3 &axis)
38 float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
40 float s = (float) sin ( temp_angle ) ;
41 float c = (float) cos ( temp_angle ) ;
42 float t = SG_ONE - c ;
44 // axis was normalized at load time
45 // hint to the compiler to put these into FP registers
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;
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;
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;
65 // hint to the compiler to put these into FP registers
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;
77 * Set up the transform matrix for a translation.
80 set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
83 sgScaleVec3(xyz, axis, position_m);
84 sgMakeTransMat4(matrix, xyz);
88 * Set up the transform matrix for a scale operation.
91 set_scale (sgMat4 &matrix, double x, double y, double z)
93 sgMakeIdentMat4( matrix );
100 * Recursively process all kids to change the alpha values
103 change_alpha( ssgBase *_branch, float _blend )
107 for (i = 0; i < ((ssgBranch *)_branch)->getNumKids(); i++)
108 change_alpha( ((ssgBranch *)_branch)->getKid(i), _blend );
110 if ( !_branch->isAKindOf(ssgTypeLeaf())
111 && !_branch->isAKindOf(ssgTypeVtxTable())
112 && !_branch->isAKindOf(ssgTypeVTable()) )
115 int num_colors = ((ssgLeaf *)_branch)->getNumColours();
116 // unsigned int select_ = (_blend == 1.0) ? false : true;
118 for (i = 0; i < num_colors; i++)
120 // ((ssgSelector *)_branch)->select( select_ );
121 float *color = ((ssgLeaf *)_branch)->getColour(i);
127 * Modify property value by step and scroll settings in texture translations
130 apply_mods(double property, double step, double scroll)
135 double scrollval = 0.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;
143 // apply stepping of input value
145 modprop = ((floor(property/step) * step) + scrollval);
147 modprop = ((ceil(property/step) * step) + scrollval);
156 * Read an interpolation table from properties.
158 static SGInterpTable *
159 read_interpolation_table (SGPropertyNode_ptr props)
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));
176 ////////////////////////////////////////////////////////////////////////
177 // Implementation of SGAnimation
178 ////////////////////////////////////////////////////////////////////////
180 // Initialize the static data member
181 double SGAnimation::sim_time_sec = 0.0;
182 SGPersonalityBranch *SGAnimation::current_object = 0;
184 SGAnimation::SGAnimation (SGPropertyNode_ptr props, ssgBranch * branch)
188 _branch->setName(props->getStringValue("name", 0));
189 if ( props->getBoolValue( "enable-hot", true ) ) {
190 _branch->setTraversalMaskBits( SSGTRAV_HOT );
192 _branch->clrTraversalMaskBits( SSGTRAV_HOT );
196 SGAnimation::~SGAnimation ()
206 SGAnimation::update()
212 SGAnimation::restore()
218 ////////////////////////////////////////////////////////////////////////
219 // Implementation of SGNullAnimation
220 ////////////////////////////////////////////////////////////////////////
222 SGNullAnimation::SGNullAnimation (SGPropertyNode_ptr props)
223 : SGAnimation(props, new ssgBranch)
227 SGNullAnimation::~SGNullAnimation ()
233 ////////////////////////////////////////////////////////////////////////
234 // Implementation of SGRangeAnimation
235 ////////////////////////////////////////////////////////////////////////
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),
243 SGPropertyNode_ptr node = props->getChild("condition");
245 _condition = sgReadCondition(prop_root, node);
249 node = props->getChild( "min-factor" );
251 _min_factor = props->getFloatValue("min-factor", 1.0);
253 node = props->getChild( "max-factor" );
255 _max_factor = props->getFloatValue("max-factor", 1.0);
257 node = props->getChild( "min-property" );
259 _min_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
260 ranges[0] = _min_prop->getFloatValue() * _min_factor;
262 _min = props->getFloatValue("min-m", 0);
263 ranges[0] = _min * _min_factor;
265 node = props->getChild( "max-property" );
267 _max_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
268 ranges[1] = _max_prop->getFloatValue() * _max_factor;
270 _max = props->getFloatValue("max-m", 0);
271 ranges[1] = _max * _max_factor;
273 ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
276 SGRangeAnimation::~SGRangeAnimation ()
281 SGRangeAnimation::update()
284 if ( _condition == 0 || _condition->test() ) {
285 if (_min_prop != 0) {
286 ranges[0] = _min_prop->getFloatValue() * _min_factor;
288 ranges[0] = _min * _min_factor;
290 if (_max_prop != 0) {
291 ranges[1] = _max_prop->getFloatValue() * _max_factor;
293 ranges[1] = _max * _max_factor;
297 ranges[1] = 1000000000.f;
299 ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
305 ////////////////////////////////////////////////////////////////////////
306 // Implementation of SGBillboardAnimation
307 ////////////////////////////////////////////////////////////////////////
309 SGBillboardAnimation::SGBillboardAnimation (SGPropertyNode_ptr props)
310 : SGAnimation(props, new ssgCutout(props->getBoolValue("spherical", true)))
314 SGBillboardAnimation::~SGBillboardAnimation ()
320 ////////////////////////////////////////////////////////////////////////
321 // Implementation of SGSelectAnimation
322 ////////////////////////////////////////////////////////////////////////
324 SGSelectAnimation::SGSelectAnimation( SGPropertyNode *prop_root,
325 SGPropertyNode_ptr props )
326 : SGAnimation(props, new ssgSelector),
329 SGPropertyNode_ptr node = props->getChild("condition");
331 _condition = sgReadCondition(prop_root, node);
334 SGSelectAnimation::~SGSelectAnimation ()
340 SGSelectAnimation::update()
342 if (_condition != 0 && _condition->test())
343 ((ssgSelector *)_branch)->select(0xffff);
345 ((ssgSelector *)_branch)->select(0x0000);
351 ////////////////////////////////////////////////////////////////////////
352 // Implementation of SGSpinAnimation
353 ////////////////////////////////////////////////////////////////////////
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 ),
364 SGPropertyNode_ptr node = props->getChild("condition");
366 _condition = sgReadCondition(prop_root, node);
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;
387 _axis[0] = props->getFloatValue("axis/x", 0);
388 _axis[1] = props->getFloatValue("axis/y", 0);
389 _axis[2] = props->getFloatValue("axis/z", 0);
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);
396 sgNormalizeVec3(_axis);
398 //_factor(props->getDoubleValue("factor", 1.0)),
402 SGPropertyNode_ptr factor_n = props->getNode( "factor" );
403 if ( factor_n != 0 ) {
404 SGPropertyNode_ptr rand_n = factor_n->getNode( "random" );
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 );
410 _factor = _factor_min = _factor_max = props->getDoubleValue("factor", 1.0);
413 //_position_deg(props->getDoubleValue("starting-position-deg", 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" );
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 );
425 _position_deg = _position_deg_min = _position_deg_max =
426 props->getDoubleValue("starting-position-deg", 1.0);
431 SGSpinAnimation::~SGSpinAnimation ()
436 SGSpinAnimation::update()
438 if ( _condition == 0 || _condition->test() ) {
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 );
447 key->setDoubleValue( sim_time_sec, this, LAST_TIME_SEC_SPIN );
448 key->setIntValue( 1, this, INIT_SPIN );
450 v = _position_deg_min + sg_random() * ( _position_deg_max - _position_deg_min );
451 key->setDoubleValue( v, this, POSITION_DEG_SPIN );
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 );
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 );
469 dt = sim_time_sec - _last_time_sec;
470 _last_time_sec = sim_time_sec;
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;
480 set_rotation(_matrix, _position_deg, _center, _axis);
481 ((ssgTransform *)_branch)->setTransform(_matrix);
488 ////////////////////////////////////////////////////////////////////////
489 // Implementation of SGTimedAnimation
490 ////////////////////////////////////////////////////////////////////////
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 ),
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 ) );
508 SGPropertyNode_ptr rNode = nodes[ i ]->getChild("random");
510 _branch_duration_specs[ ind ] = DurationSpec( nodes[ i ]->getDoubleValue() );
512 _branch_duration_specs[ ind ] = DurationSpec( rNode->getDoubleValue( "min", 0.0 ),
513 rNode->getDoubleValue( "max", 1.0 ) );
518 SGTimedAnimation::~SGTimedAnimation ()
523 SGTimedAnimation::init()
525 if ( !_use_personality ) {
526 for ( int i = 0; i < getBranch()->getNumKids(); i++ ) {
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 );
534 _branch_duration_sec.push_back( v );
535 _total_duration_sec += v;
537 // Sanity check : total duration shouldn't equal zero
538 if ( _total_duration_sec < 0.01 ) {
539 _total_duration_sec = 0.01;
542 ((ssgSelector *)getBranch())->selectStep(_step);
546 SGTimedAnimation::update()
548 if ( _use_personality ) {
549 SGPersonalityBranch *key = current_object;
550 if ( !key->getIntValue( this, INIT_TIMED ) ) {
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 );
561 // Sanity check : total duration shouldn't equal zero
562 if ( total < 0.01 ) {
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 );
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;
578 double duration = _duration_sec;
579 if ( _step < (int)_branch_duration_specs.size() ) {
580 duration = key->getDoubleValue( this, BRANCH_DURATION_SEC_TIMED, _step );
582 if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
583 _last_time_sec += duration;
585 if ( _step >= getBranch()->getNumKids() )
588 ((ssgSelector *)getBranch())->selectStep( _step );
589 key->setDoubleValue( _last_time_sec, this, LAST_TIME_SEC_TIMED );
590 key->setIntValue( _step, this, STEP_TIMED );
592 while ( ( sim_time_sec - _last_time_sec ) >= _total_duration_sec ) {
593 _last_time_sec += _total_duration_sec;
595 double duration = _duration_sec;
596 if ( _step < (int)_branch_duration_sec.size() ) {
597 duration = _branch_duration_sec[ _step ];
599 if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
600 _last_time_sec += duration;
602 if ( _step >= getBranch()->getNumKids() )
604 ((ssgSelector *)getBranch())->selectStep( _step );
612 ////////////////////////////////////////////////////////////////////////
613 // Implementation of SGRotateAnimation
614 ////////////////////////////////////////////////////////////////////////
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)),
630 SGPropertyNode_ptr node = props->getChild("condition");
632 _condition = sgReadCondition(prop_root, node);
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);
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;
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);
663 sgNormalizeVec3(_axis);
666 SGRotateAnimation::~SGRotateAnimation ()
672 SGRotateAnimation::update()
674 if (_condition == 0 || _condition->test()) {
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;
682 _position_deg = _table->interpolate(_prop->getDoubleValue());
684 set_rotation(_matrix, _position_deg, _center, _axis);
685 ((ssgTransform *)_branch)->setTransform(_matrix);
691 ////////////////////////////////////////////////////////////////////////
692 // Implementation of SGBlendAnimation
693 ////////////////////////////////////////////////////////////////////////
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)),
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))
710 SGBlendAnimation::~SGBlendAnimation ()
716 SGBlendAnimation::update()
721 _blend = 1.0 - (_prop->getDoubleValue() * _factor + _offset);
723 if (_has_min && (_blend < _min))
725 if (_has_max && (_blend > _max))
728 _blend = _table->interpolate(_prop->getDoubleValue());
731 if (_blend != _prev_value) {
732 _prev_value = _blend;
733 change_alpha( _branch, _blend );
740 ////////////////////////////////////////////////////////////////////////
741 // Implementation of SGTranslateAnimation
742 ////////////////////////////////////////////////////////////////////////
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)),
758 SGPropertyNode_ptr node = props->getChild("condition");
760 _condition = sgReadCondition(prop_root, node);
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);
768 SGTranslateAnimation::~SGTranslateAnimation ()
774 SGTranslateAnimation::update()
776 if (_condition == 0 || _condition->test()) {
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;
784 _position_m = _table->interpolate(_prop->getDoubleValue());
786 set_translation(_matrix, _position_m, _axis);
787 ((ssgTransform *)_branch)->setTransform(_matrix);
794 ////////////////////////////////////////////////////////////////////////
795 // Implementation of SGScaleAnimation
796 ////////////////////////////////////////////////////////////////////////
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"))
824 SGScaleAnimation::~SGScaleAnimation ()
830 SGScaleAnimation::update()
833 _x_scale = _prop->getDoubleValue() * _x_factor + _x_offset;
834 if (_has_min_x && _x_scale < _min_x)
836 if (_has_max_x && _x_scale > _max_x)
839 _x_scale = _table->interpolate(_prop->getDoubleValue());
843 _y_scale = _prop->getDoubleValue() * _y_factor + _y_offset;
844 if (_has_min_y && _y_scale < _min_y)
846 if (_has_max_y && _y_scale > _max_y)
849 _y_scale = _table->interpolate(_prop->getDoubleValue());
853 _z_scale = _prop->getDoubleValue() * _z_factor + _z_offset;
854 if (_has_min_z && _z_scale < _min_z)
856 if (_has_max_z && _z_scale > _max_z)
859 _z_scale = _table->interpolate(_prop->getDoubleValue());
862 set_scale(_matrix, _x_scale, _y_scale, _z_scale );
863 ((ssgTransform *)_branch)->setTransform(_matrix);
868 ////////////////////////////////////////////////////////////////////////
869 // Implementation of SGTexRotateAnimation
870 ////////////////////////////////////////////////////////////////////////
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))
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);
894 SGTexRotateAnimation::~SGTexRotateAnimation ()
900 SGTexRotateAnimation::update()
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;
909 _position_deg = _table->interpolate(_prop->getDoubleValue());
911 set_rotation(_matrix, _position_deg, _center, _axis);
912 ((ssgTexTrans *)_branch)->setTransform(_matrix);
917 ////////////////////////////////////////////////////////////////////////
918 // Implementation of SGTexTranslateAnimation
919 ////////////////////////////////////////////////////////////////////////
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))
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);
942 SGTexTranslateAnimation::~SGTexTranslateAnimation ()
948 SGTexTranslateAnimation::update()
951 _position = (apply_mods(_prop->getDoubleValue(), _step, _scroll) + _offset) * _factor;
952 if (_has_min && _position < _min)
954 if (_has_max && _position > _max)
957 _position = _table->interpolate(apply_mods(_prop->getDoubleValue(), _step, _scroll));
959 set_translation(_matrix, _position, _axis);
960 ((ssgTexTrans *)_branch)->setTransform(_matrix);
965 ////////////////////////////////////////////////////////////////////////
966 // Implementation of SGTexMultipleAnimation
967 ////////////////////////////////////////////////////////////////////////
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))
976 vector<SGPropertyNode_ptr> transform_nodes = props->getChildren("transform");
977 _transform = new TexTransform [transform_nodes.size()];
979 for (i = 0; i < transform_nodes.size(); i++) {
980 SGPropertyNode_ptr transform_props = transform_nodes[i];
982 if (!strcmp("textranslate",transform_props->getStringValue("subtype", 0))) {
984 // transform is a translation
985 _transform[i].subtype = 0;
987 _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
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);
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);
1005 } else if (!strcmp("texrotate",transform_nodes[i]->getStringValue("subtype", 0))) {
1007 // transform is a rotation
1008 _transform[i].subtype = 1;
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);
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);
1032 SGTexMultipleAnimation::~SGTexMultipleAnimation ()
1034 delete [] _transform;
1038 SGTexMultipleAnimation::update()
1042 sgMakeIdentMat4(tmatrix);
1043 for (i = 0; i < _num_transforms; i++) {
1045 if(_transform[i].subtype == 0) {
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;
1055 _transform[i].position = _transform[i].table->interpolate(apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll));
1057 set_translation(_transform[i].matrix, _transform[i].position, _transform[i].axis);
1058 sgPreMultMat4(tmatrix, _transform[i].matrix);
1060 } else if (_transform[i].subtype == 1) {
1062 // subtype 1 is rotation
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;
1071 _transform[i].position = _transform[i].table->interpolate(_transform[i].prop->getDoubleValue());
1073 set_rotation(_transform[i].matrix, _transform[i].position, _transform[i].center, _transform[i].axis);
1074 sgPreMultMat4(tmatrix, _transform[i].matrix);
1077 ((ssgTexTrans *)_branch)->setTransform(tmatrix);
1083 ////////////////////////////////////////////////////////////////////////
1084 // Implementation of SGAlphaTestAnimation
1085 ////////////////////////////////////////////////////////////////////////
1087 SGAlphaTestAnimation::SGAlphaTestAnimation(SGPropertyNode_ptr props)
1088 : SGAnimation(props, new ssgBranch)
1090 _alpha_clamp = props->getFloatValue("alpha-factor", 0.0);
1093 SGAlphaTestAnimation::~SGAlphaTestAnimation ()
1097 void SGAlphaTestAnimation::init()
1099 setAlphaClampToBranch(_branch,_alpha_clamp);
1102 void SGAlphaTestAnimation::setAlphaClampToBranch(ssgBranch *b, float clamp)
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 );
1119 ////////////////////////////////////////////////////////////////////////
1120 // Implementation of SGMaterialAnimation
1121 ////////////////////////////////////////////////////////////////////////
1123 SGMaterialAnimation::SGMaterialAnimation( SGPropertyNode *prop_root,
1124 SGPropertyNode_ptr props, const SGPath &texture_path)
1125 : SGAnimation(props, new ssgBranch),
1126 _last_condition(false),
1127 _prop_root(prop_root),
1129 _texture_base(texture_path),
1130 _cached_material(0),
1131 _cloned_material(0),
1134 _global(props->getBoolValue("global", false))
1136 SGPropertyNode_ptr n;
1137 n = props->getChild("condition");
1138 _condition = n ? sgReadCondition(_prop_root, n) : 0;
1139 n = props->getChild("property-base");
1141 _prop_base = n->getStringValue();
1142 if (!_prop_base.empty() && _prop_base.end()[-1] != '/')
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);
1151 _shi = props->getFloatValue("shininess", -1.0);
1153 _update |= SHININESS;
1155 SGPropertyNode_ptr group = props->getChild("transparency");
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)
1163 _trans.max = group->getFloatValue("max", 1.0);
1164 if (_trans.max > 1.0)
1167 _update |= TRANSPARENCY;
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;
1176 _read |= TRANSPARENCY;
1179 _thresh = props->getFloatValue("threshold", -1.0);
1181 _update |= THRESHOLD;
1183 string _texture_str = props->getStringValue("texture", "");
1184 if (!_texture_str.empty()) {
1185 _texture = _texture_base;
1186 _texture.append(_texture_str);
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;
1197 _static_update = _update;
1200 void SGMaterialAnimation::initColorGroup(SGPropertyNode_ptr group, ColorSpec *col, int flag)
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);
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;
1228 void SGMaterialAnimation::init()
1231 cloneMaterials(_branch);
1234 int SGMaterialAnimation::update()
1237 bool cond = _condition->test();
1238 if (cond && !_last_condition)
1239 _update |= _static_update;
1241 _last_condition = cond;
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);
1258 _shi = _shi_prop->getFloatValue();
1260 _update |= SHININESS;
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();
1271 _update |= TRANSPARENCY;
1275 _thresh = _thresh_prop->getFloatValue();
1277 _update |= THRESHOLD;
1280 string t = _tex_prop->getStringValue();
1281 if (!t.empty() && t != _texture_str) {
1283 _texture = _texture_base;
1289 setMaterialBranch(_branch);
1295 void SGMaterialAnimation::updateColorGroup(ColorSpec *col, int flag)
1297 ColorSpec tmp = *col;
1299 col->red = col->red_prop->getFloatValue();
1300 if (col->green_prop)
1301 col->green = col->green_prop->getFloatValue();
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();
1312 void SGMaterialAnimation::cloneMaterials(ssgBranch *b)
1314 for (int i = 0; i < b->getNumKids(); i++)
1315 cloneMaterials((ssgBranch *)b->getKid(i));
1317 if (!b->isAKindOf(ssgTypeLeaf()) || !((ssgLeaf *)b)->hasState())
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);
1325 ((ssgLeaf *)b)->setState(_cloned_material);
1328 void SGMaterialAnimation::setMaterialBranch(ssgBranch *b)
1330 for (int i = 0; i < b->getNumKids(); i++)
1331 setMaterialBranch((ssgBranch *)b->getKid(i));
1333 if (!b->isAKindOf(ssgTypeLeaf()) || !((ssgLeaf *)b)->hasState())
1336 ssgSimpleState *s = (ssgSimpleState *)((ssgLeaf *)b)->getState();
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);
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);
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);
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);
1379 s->disable(GL_BLEND);
1380 s->disable(GL_ALPHA_TEST);
1389 ////////////////////////////////////////////////////////////////////////
1390 // Implementation of SGFlashAnimation
1391 ////////////////////////////////////////////////////////////////////////
1392 SGFlashAnimation::SGFlashAnimation(SGPropertyNode_ptr props)
1393 : SGAnimation( props, new SGCustomTransform )
1395 _axis[0] = props->getFloatValue("axis/x", 0);
1396 _axis[1] = props->getFloatValue("axis/y", 0);
1397 _axis[2] = props->getFloatValue("axis/z", 1);
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);
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);
1408 _min_v = props->getFloatValue("min", 0.0);
1409 _max_v = props->getFloatValue("max", 1.0);
1411 ((SGCustomTransform *)_branch)->setTransCallback( &SGFlashAnimation::flashCallback, this );
1414 SGFlashAnimation::~SGFlashAnimation()
1418 void SGFlashAnimation::flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d )
1420 ((SGFlashAnimation *)d)->flashCallback( r, f, m );
1423 void SGFlashAnimation::flashCallback( sgMat4 r, sgFrustum *f, sgMat4 m )
1425 sgVec3 transformed_axis;
1426 sgXformVec3( transformed_axis, _axis, m );
1427 sgNormalizeVec3( transformed_axis );
1430 sgFullXformPnt3( view, _center, m );
1431 sgNormalizeVec3( view );
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;
1440 if ( scale_factor < _min_v )
1441 scale_factor = _min_v;
1442 if ( scale_factor > _max_v )
1443 scale_factor = _max_v;
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 );
1455 sgPreMultMat4( r, transform );
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))
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);
1477 ((SGCustomTransform *)_branch)->setTransCallback( &SGDistScaleAnimation::distScaleCallback, this );
1480 SGDistScaleAnimation::~SGDistScaleAnimation()
1484 void SGDistScaleAnimation::distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m, void *d )
1486 ((SGDistScaleAnimation *)d)->distScaleCallback( r, f, m );
1489 void SGDistScaleAnimation::distScaleCallback( sgMat4 r, sgFrustum *f, sgMat4 m )
1492 sgFullXformPnt3( view, _center, m );
1494 float scale_factor = sgLengthVec3( view );
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;
1502 scale_factor = _table->interpolate( scale_factor );
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 );
1515 sgPreMultMat4( r, transform );
1518 ////////////////////////////////////////////////////////////////////////
1519 // Implementation of SGShadowAnimation
1520 ////////////////////////////////////////////////////////////////////////
1522 SGShadowAnimation::SGShadowAnimation ( SGPropertyNode *prop_root,
1523 SGPropertyNode_ptr props )
1524 : SGAnimation(props, new ssgBranch),
1526 _condition_value(true)
1529 SGPropertyNode_ptr node = props->getChild("condition");
1531 _condition = sgReadCondition(prop_root, node);
1532 _condition_value = false;
1536 SGShadowAnimation::~SGShadowAnimation ()
1542 SGShadowAnimation::update()
1545 _condition_value = _condition->test();
1549 bool SGShadowAnimation::get_condition_value(void) {
1550 return _condition_value;
1553 // end of animation.cxx