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()
13 #include <osg/AlphaFunc>
14 #include <osg/AutoTransform>
15 #include <osg/Drawable>
17 #include <osg/Geometry>
19 #include <osg/MatrixTransform>
20 #include <osg/StateSet>
23 #include <osg/Texture2D>
24 #include <osgDB/ReadFile>
26 #include <simgear/math/interpolater.hxx>
27 #include <simgear/props/condition.hxx>
28 #include <simgear/props/props.hxx>
29 #include <simgear/math/sg_random.h>
30 #include <simgear/scene/util/SGNodeMasks.hxx>
32 #include "animation.hxx"
36 ////////////////////////////////////////////////////////////////////////
37 // Static utility functions.
38 ////////////////////////////////////////////////////////////////////////
41 * Set up the transform matrix for a spin or rotation.
44 set_rotation (osg::Matrix &matrix, double position_deg,
45 const osg::Vec3 ¢er, const osg::Vec3 &axis)
47 float temp_angle = -position_deg * SG_DEGREES_TO_RADIANS ;
49 float s = (float) sin ( temp_angle ) ;
50 float c = (float) cos ( temp_angle ) ;
51 float t = SG_ONE - c ;
53 // axis was normalized at load time
54 // hint to the compiler to put these into FP registers
59 matrix(0, 0) = t * x * x + c ;
60 matrix(0, 1) = t * y * x - s * z ;
61 matrix(0, 2) = t * z * x + s * y ;
62 matrix(0, 3) = SG_ZERO;
64 matrix(1, 0) = t * x * y + s * z ;
65 matrix(1, 1) = t * y * y + c ;
66 matrix(1, 2) = t * z * y - s * x ;
67 matrix(1, 3) = SG_ZERO;
69 matrix(2, 0) = t * x * z - s * y ;
70 matrix(2, 1) = t * y * z + s * x ;
71 matrix(2, 2) = t * z * z + c ;
72 matrix(2, 3) = SG_ZERO;
74 // hint to the compiler to put these into FP registers
79 matrix(3, 0) = x - x*matrix(0, 0) - y*matrix(1, 0) - z*matrix(2, 0);
80 matrix(3, 1) = y - x*matrix(0, 1) - y*matrix(1, 1) - z*matrix(2, 1);
81 matrix(3, 2) = z - x*matrix(0, 2) - y*matrix(1, 2) - z*matrix(2, 2);
82 matrix(3, 3) = SG_ONE;
86 * Set up the transform matrix for a translation.
89 set_translation (osg::Matrix &matrix, double position_m, const osg::Vec3 &axis)
91 osg::Vec3 xyz = axis * position_m;
92 matrix.makeIdentity();
93 matrix(3, 0) = xyz[0];
94 matrix(3, 1) = xyz[1];
95 matrix(3, 2) = xyz[2];
99 * Set up the transform matrix for a scale operation.
102 set_scale (osg::Matrix &matrix, double x, double y, double z)
104 matrix.makeIdentity();
111 * Modify property value by step and scroll settings in texture translations
114 apply_mods(double property, double step, double scroll)
119 double scrollval = 0.0;
121 // calculate scroll amount (for odometer like movement)
122 double remainder = step - fmod(fabs(property), step);
123 if (remainder < scroll) {
124 scrollval = (scroll - remainder) / scroll * step;
127 // apply stepping of input value
129 modprop = ((floor(property/step) * step) + scrollval);
131 modprop = ((ceil(property/step) * step) + scrollval);
140 * Read an interpolation table from properties.
142 static SGInterpTable *
143 read_interpolation_table (SGPropertyNode_ptr props)
145 SGPropertyNode_ptr table_node = props->getNode("interpolation");
146 if (table_node != 0) {
147 SGInterpTable * table = new SGInterpTable();
148 vector<SGPropertyNode_ptr> entries = table_node->getChildren("entry");
149 for (unsigned int i = 0; i < entries.size(); i++)
150 table->addEntry(entries[i]->getDoubleValue("ind", 0.0),
151 entries[i]->getDoubleValue("dep", 0.0));
160 ////////////////////////////////////////////////////////////////////////
161 // Implementation of SGAnimation
162 ////////////////////////////////////////////////////////////////////////
164 SGAnimation::SGAnimation (SGPropertyNode_ptr props, osg::Group * branch)
168 _branch->setName(props->getStringValue("name", "Animation"));
169 if ( props->getBoolValue( "enable-hot", true ) ) {
170 _branch->setNodeMask(SG_NODEMASK_TERRAIN_BIT|_branch->getNodeMask());
172 _branch->setNodeMask(~SG_NODEMASK_TERRAIN_BIT&_branch->getNodeMask());
176 SGAnimation::~SGAnimation ()
186 SGAnimation::restore()
191 SGAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
197 ////////////////////////////////////////////////////////////////////////
198 // Implementation of SGNullAnimation
199 ////////////////////////////////////////////////////////////////////////
201 SGNullAnimation::SGNullAnimation (SGPropertyNode_ptr props)
202 : SGAnimation(props, new osg::Group)
206 SGNullAnimation::~SGNullAnimation ()
212 ////////////////////////////////////////////////////////////////////////
213 // Implementation of SGRangeAnimation
214 ////////////////////////////////////////////////////////////////////////
216 SGRangeAnimation::SGRangeAnimation (SGPropertyNode *prop_root,
217 SGPropertyNode_ptr props)
218 : SGAnimation(props, new osg::LOD),
219 _min(0.0), _max(0.0), _min_factor(1.0), _max_factor(1.0),
222 SGPropertyNode_ptr node = props->getChild("condition");
224 _condition = sgReadCondition(prop_root, node);
228 node = props->getChild( "min-factor" );
230 _min_factor = props->getFloatValue("min-factor", 1.0);
232 node = props->getChild( "max-factor" );
234 _max_factor = props->getFloatValue("max-factor", 1.0);
236 node = props->getChild( "min-property" );
238 _min_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
239 ranges[0] = _min_prop->getFloatValue() * _min_factor;
241 _min = props->getFloatValue("min-m", 0);
242 ranges[0] = _min * _min_factor;
244 node = props->getChild( "max-property" );
246 _max_prop = (SGPropertyNode *)prop_root->getNode(node->getStringValue(), true);
247 ranges[1] = _max_prop->getFloatValue() * _max_factor;
249 _max = props->getFloatValue("max-m", 0);
250 ranges[1] = _max * _max_factor;
252 static_cast<osg::LOD*>(_branch)->setRange(0, ranges[0], ranges[1]);
255 SGRangeAnimation::~SGRangeAnimation ()
260 SGRangeAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
263 if ( _condition == 0 || _condition->test() ) {
264 if (_min_prop != 0) {
265 ranges[0] = _min_prop->getFloatValue() * _min_factor;
267 ranges[0] = _min * _min_factor;
269 if (_max_prop != 0) {
270 ranges[1] = _max_prop->getFloatValue() * _max_factor;
272 ranges[1] = _max * _max_factor;
276 ranges[1] = 1000000000.f;
278 static_cast<osg::LOD*>(_branch)->setRange(0, ranges[0], ranges[1]);
284 ////////////////////////////////////////////////////////////////////////
285 // Implementation of SGBillboardAnimation
286 ////////////////////////////////////////////////////////////////////////
288 SGBillboardAnimation::SGBillboardAnimation (SGPropertyNode_ptr props)
289 : SGAnimation(props, new osg::AutoTransform)
292 bool spherical = props->getBoolValue("spherical", true);
293 osg::AutoTransform* autoTrans = static_cast<osg::AutoTransform*>(_branch);
295 autoTrans->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN);
297 autoTrans->setAutoRotateMode(osg::AutoTransform::NO_ROTATION);
298 autoTrans->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
300 autoTrans->setAutoScaleToScreen(false);
303 SGBillboardAnimation::~SGBillboardAnimation ()
309 ////////////////////////////////////////////////////////////////////////
310 // Implementation of SGSelectAnimation
311 ////////////////////////////////////////////////////////////////////////
313 SGSelectAnimation::SGSelectAnimation( SGPropertyNode *prop_root,
314 SGPropertyNode_ptr props )
315 : SGAnimation(props, new osg::Switch),
318 SGPropertyNode_ptr node = props->getChild("condition");
320 _condition = sgReadCondition(prop_root, node);
323 SGSelectAnimation::~SGSelectAnimation ()
329 SGSelectAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
331 if (_condition != 0 && _condition->test())
332 static_cast<osg::Switch*>(_branch)->setAllChildrenOn();
334 static_cast<osg::Switch*>(_branch)->setAllChildrenOff();
339 ////////////////////////////////////////////////////////////////////////
340 // Implementation of SGSpinAnimation
341 ////////////////////////////////////////////////////////////////////////
343 SGSpinAnimation::SGSpinAnimation( SGPropertyNode *prop_root,
344 SGPropertyNode_ptr props,
345 double sim_time_sec )
346 : SGAnimation(props, new osg::MatrixTransform),
347 _use_personality( props->getBoolValue("use-personality",false) ),
348 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
349 _factor( props, "factor", 1.0 ),
350 _position_deg( props, "starting-position-deg", 0.0 ),
351 _last_time_sec( sim_time_sec ),
354 SGPropertyNode_ptr node = props->getChild("condition");
356 _condition = sgReadCondition(prop_root, node);
361 if (props->hasValue("axis/x1-m")) {
362 double x1,y1,z1,x2,y2,z2;
363 x1 = props->getFloatValue("axis/x1-m");
364 y1 = props->getFloatValue("axis/y1-m");
365 z1 = props->getFloatValue("axis/z1-m");
366 x2 = props->getFloatValue("axis/x2-m");
367 y2 = props->getFloatValue("axis/y2-m");
368 z2 = props->getFloatValue("axis/z2-m");
369 _center[0] = (x1+x2)/2;
370 _center[1]= (y1+y2)/2;
371 _center[2] = (z1+z2)/2;
372 float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
373 _axis[0] = (x2-x1)/vector_length;
374 _axis[1] = (y2-y1)/vector_length;
375 _axis[2] = (z2-z1)/vector_length;
377 _axis[0] = props->getFloatValue("axis/x", 0);
378 _axis[1] = props->getFloatValue("axis/y", 0);
379 _axis[2] = props->getFloatValue("axis/z", 0);
381 if (props->hasValue("center/x-m")) {
382 _center[0] = props->getFloatValue("center/x-m", 0);
383 _center[1] = props->getFloatValue("center/y-m", 0);
384 _center[2] = props->getFloatValue("center/z-m", 0);
389 if ( _use_personality ) {
391 _position_deg.shuffle();
395 SGSpinAnimation::~SGSpinAnimation ()
400 SGSpinAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
402 double sim_time_sec = nv->getFrameStamp()->getReferenceTime();
403 if ( _condition == 0 || _condition->test() ) {
406 dt = sim_time_sec - _last_time_sec;
407 _last_time_sec = sim_time_sec;
409 velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
410 _position_deg += (dt * velocity_rpms * 360);
411 _position_deg -= 360*floor(_position_deg/360);
414 set_rotation(_matrix, _position_deg, _center, _axis);
415 static_cast<osg::MatrixTransform*>(_branch)->setMatrix(_matrix);
422 ////////////////////////////////////////////////////////////////////////
423 // Implementation of SGTimedAnimation
424 ////////////////////////////////////////////////////////////////////////
426 SGTimedAnimation::SGTimedAnimation (SGPropertyNode_ptr props)
427 : SGAnimation(props, new osg::Switch),
428 _use_personality( props->getBoolValue("use-personality",false) ),
429 _duration_sec(props->getDoubleValue("duration-sec", 1.0)),
431 _total_duration_sec( 0 ),
435 vector<SGPropertyNode_ptr> nodes = props->getChildren( "branch-duration-sec" );
436 size_t nb = nodes.size();
437 for ( size_t i = 0; i < nb; i++ ) {
438 size_t ind = nodes[ i ]->getIndex();
439 while ( ind >= _branch_duration_specs.size() ) {
440 _branch_duration_specs.push_back( DurationSpec( _duration_sec ) );
442 SGPropertyNode_ptr rNode = nodes[ i ]->getChild("random");
444 _branch_duration_specs[ ind ] = DurationSpec( nodes[ i ]->getDoubleValue() );
446 _branch_duration_specs[ ind ] = DurationSpec( rNode->getDoubleValue( "min", 0.0 ),
447 rNode->getDoubleValue( "max", 1.0 ) );
452 SGTimedAnimation::~SGTimedAnimation ()
457 SGTimedAnimation::init()
459 if ( !_use_personality ) {
460 for ( unsigned i = 0; i < getBranch()->getNumChildren(); i++ ) {
462 if ( i < _branch_duration_specs.size() ) {
463 DurationSpec &sp = _branch_duration_specs[ i ];
464 v = sp._min + sg_random() * ( sp._max - sp._min );
468 _branch_duration_sec.push_back( v );
469 _total_duration_sec += v;
472 // Sanity check : total duration shouldn't equal zero
473 if (_duration_sec < 0.01)
474 _duration_sec = 0.01;
475 if ( _total_duration_sec < 0.01 )
476 _total_duration_sec = 0.01;
478 static_cast<osg::Switch*>(getBranch())->setSingleChildOn(_step);
482 SGTimedAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
484 double sim_time_sec = nv->getFrameStamp()->getReferenceTime();
485 _last_time_sec -= _total_duration_sec*floor((sim_time_sec - _last_time_sec)/_total_duration_sec);
486 double duration = _duration_sec;
487 if ( _step < _branch_duration_sec.size() ) {
488 duration = _branch_duration_sec[ _step ];
490 if ( ( sim_time_sec - _last_time_sec ) >= duration ) {
491 _last_time_sec += duration;
493 if ( _step >= getBranch()->getNumChildren() )
495 static_cast<osg::Switch*>(getBranch())->setSingleChildOn(_step);
502 ////////////////////////////////////////////////////////////////////////
503 // Implementation of SGRotateAnimation
504 ////////////////////////////////////////////////////////////////////////
506 SGRotateAnimation::SGRotateAnimation( SGPropertyNode *prop_root,
507 SGPropertyNode_ptr props )
508 : SGAnimation(props, new osg::MatrixTransform),
509 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
510 _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
511 _factor(props->getDoubleValue("factor", 1.0)),
512 _table(read_interpolation_table(props)),
513 _has_min(props->hasValue("min-deg")),
514 _min_deg(props->getDoubleValue("min-deg")),
515 _has_max(props->hasValue("max-deg")),
516 _max_deg(props->getDoubleValue("max-deg")),
517 _position_deg(props->getDoubleValue("starting-position-deg", 0)),
520 SGPropertyNode_ptr node = props->getChild("condition");
522 _condition = sgReadCondition(prop_root, node);
527 if (props->hasValue("axis/x") || props->hasValue("axis/y") || props->hasValue("axis/z")) {
528 _axis[0] = props->getFloatValue("axis/x", 0);
529 _axis[1] = props->getFloatValue("axis/y", 0);
530 _axis[2] = props->getFloatValue("axis/z", 0);
532 double x1,y1,z1,x2,y2,z2;
533 x1 = props->getFloatValue("axis/x1-m", 0);
534 y1 = props->getFloatValue("axis/y1-m", 0);
535 z1 = props->getFloatValue("axis/z1-m", 0);
536 x2 = props->getFloatValue("axis/x2-m", 0);
537 y2 = props->getFloatValue("axis/y2-m", 0);
538 z2 = props->getFloatValue("axis/z2-m", 0);
539 _center[0] = (x1+x2)/2;
540 _center[1]= (y1+y2)/2;
541 _center[2] = (z1+z2)/2;
542 float vector_length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
543 _axis[0] = (x2-x1)/vector_length;
544 _axis[1] = (y2-y1)/vector_length;
545 _axis[2] = (z2-z1)/vector_length;
547 if (props->hasValue("center/x-m") || props->hasValue("center/y-m")
548 || props->hasValue("center/z-m")) {
549 _center[0] = props->getFloatValue("center/x-m", 0);
550 _center[1] = props->getFloatValue("center/y-m", 0);
551 _center[2] = props->getFloatValue("center/z-m", 0);
556 SGRotateAnimation::~SGRotateAnimation ()
562 SGRotateAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
564 if (_condition == 0 || _condition->test()) {
566 _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
567 if (_has_min && _position_deg < _min_deg)
568 _position_deg = _min_deg;
569 if (_has_max && _position_deg > _max_deg)
570 _position_deg = _max_deg;
572 _position_deg = _table->interpolate(_prop->getDoubleValue());
575 set_rotation(_matrix, _position_deg, _center, _axis);
576 static_cast<osg::MatrixTransform*>(_branch)->setMatrix(_matrix);
582 ////////////////////////////////////////////////////////////////////////
583 // Implementation of SGBlendAnimation
584 ////////////////////////////////////////////////////////////////////////
586 class SGBlendAnimationVisitor : public osg::NodeVisitor {
588 SGBlendAnimationVisitor(float blend) :
589 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
592 setVisitorType(osg::NodeVisitor::NODE_VISITOR);
594 virtual void apply(osg::Node& node)
596 updateStateSet(node.getStateSet());
599 virtual void apply(osg::Geode& node)
601 apply((osg::Node&)node);
602 unsigned nDrawables = node.getNumDrawables();
603 for (unsigned i = 0; i < nDrawables; ++i) {
604 osg::Drawable* drawable = node.getDrawable(i);
605 updateStateSet(drawable->getStateSet());
606 osg::Geometry* geometry = drawable->asGeometry();
609 osg::Array* array = geometry->getColorArray();
612 osg::Vec4Array* vec4Array = dynamic_cast<osg::Vec4Array*>(array);
615 geometry->dirtyDisplayList();
617 for (unsigned k = 0; k < vec4Array->size(); ++k) {
618 (*vec4Array)[k][3] = _blend;
622 void updateStateSet(osg::StateSet* stateSet)
626 osg::StateAttribute* stateAttribute = stateSet->getAttribute(osg::StateAttribute::MATERIAL);
629 osg::Material* material = dynamic_cast<osg::Material*>(stateAttribute);
632 material->setAlpha(osg::Material::FRONT_AND_BACK, _blend);
639 SGBlendAnimation::SGBlendAnimation( SGPropertyNode *prop_root,
640 SGPropertyNode_ptr props )
641 : SGAnimation(props, new osg::Group),
642 _use_personality( props->getBoolValue("use-personality",false) ),
643 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
644 _table(read_interpolation_table(props)),
646 _offset(props,"offset",0.0),
647 _factor(props,"factor",1.0),
648 _min(props->getDoubleValue("min", 0.0)),
649 _max(props->getDoubleValue("max", 1.0))
651 if ( _use_personality ) {
657 SGBlendAnimation::~SGBlendAnimation ()
663 SGBlendAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
668 _blend = 1.0 - (_prop->getDoubleValue() * _factor + _offset);
670 _blend = _table->interpolate(_prop->getDoubleValue());
677 if (_blend != _prev_value) {
678 _prev_value = _blend;
679 SGBlendAnimationVisitor visitor(1-_blend);
680 _branch->accept(visitor);
687 ////////////////////////////////////////////////////////////////////////
688 // Implementation of SGTranslateAnimation
689 ////////////////////////////////////////////////////////////////////////
691 SGTranslateAnimation::SGTranslateAnimation( SGPropertyNode *prop_root,
692 SGPropertyNode_ptr props )
693 : SGAnimation(props, new osg::MatrixTransform),
694 _use_personality( props->getBoolValue("use-personality",false) ),
695 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
696 _offset_m( props, "offset-m", 0.0 ),
697 _factor( props, "factor", 1.0 ),
698 _table(read_interpolation_table(props)),
699 _has_min(props->hasValue("min-m")),
700 _min_m(props->getDoubleValue("min-m")),
701 _has_max(props->hasValue("max-m")),
702 _max_m(props->getDoubleValue("max-m")),
703 _position_m(props->getDoubleValue("starting-position-m", 0)),
706 SGPropertyNode_ptr node = props->getChild("condition");
708 _condition = sgReadCondition(prop_root, node);
710 _axis[0] = props->getFloatValue("axis/x", 0);
711 _axis[1] = props->getFloatValue("axis/y", 0);
712 _axis[2] = props->getFloatValue("axis/z", 0);
715 if ( _use_personality ) {
721 SGTranslateAnimation::~SGTranslateAnimation ()
727 SGTranslateAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
729 if (_condition == 0 || _condition->test()) {
732 _position_m = (_prop->getDoubleValue() * _factor) + _offset_m;
733 if (_has_min && _position_m < _min_m)
734 _position_m = _min_m;
735 if (_has_max && _position_m > _max_m)
736 _position_m = _max_m;
738 _position_m = _table->interpolate(_prop->getDoubleValue());
742 set_translation(_matrix, _position_m, _axis);
743 static_cast<osg::MatrixTransform*>(_branch)->setMatrix(_matrix);
750 ////////////////////////////////////////////////////////////////////////
751 // Implementation of SGScaleAnimation
752 ////////////////////////////////////////////////////////////////////////
754 SGScaleAnimation::SGScaleAnimation( SGPropertyNode *prop_root,
755 SGPropertyNode_ptr props )
756 : SGAnimation(props, new osg::MatrixTransform),
757 _use_personality( props->getBoolValue("use-personality",false) ),
758 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
759 _x_factor(props,"x-factor",1.0),
760 _y_factor(props,"y-factor",1.0),
761 _z_factor(props,"z-factor",1.0),
762 _x_offset(props,"x-offset",1.0),
763 _y_offset(props,"y-offset",1.0),
764 _z_offset(props,"z-offset",1.0),
765 _table(read_interpolation_table(props)),
766 _has_min_x(props->hasValue("x-min")),
767 _has_min_y(props->hasValue("y-min")),
768 _has_min_z(props->hasValue("z-min")),
769 _min_x(props->getDoubleValue("x-min")),
770 _min_y(props->getDoubleValue("y-min")),
771 _min_z(props->getDoubleValue("z-min")),
772 _has_max_x(props->hasValue("x-max")),
773 _has_max_y(props->hasValue("y-max")),
774 _has_max_z(props->hasValue("z-max")),
775 _max_x(props->getDoubleValue("x-max")),
776 _max_y(props->getDoubleValue("y-max")),
777 _max_z(props->getDoubleValue("z-max"))
779 if ( _use_personality ) {
789 SGScaleAnimation::~SGScaleAnimation ()
795 SGScaleAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
798 _x_scale = _prop->getDoubleValue() * _x_factor + _x_offset;
799 if (_has_min_x && _x_scale < _min_x)
801 if (_has_max_x && _x_scale > _max_x)
804 _x_scale = _table->interpolate(_prop->getDoubleValue());
808 _y_scale = _prop->getDoubleValue() * _y_factor + _y_offset;
809 if (_has_min_y && _y_scale < _min_y)
811 if (_has_max_y && _y_scale > _max_y)
814 _y_scale = _table->interpolate(_prop->getDoubleValue());
818 _z_scale = _prop->getDoubleValue() * _z_factor + _z_offset;
819 if (_has_min_z && _z_scale < _min_z)
821 if (_has_max_z && _z_scale > _max_z)
824 _z_scale = _table->interpolate(_prop->getDoubleValue());
828 set_scale(_matrix, _x_scale, _y_scale, _z_scale );
829 static_cast<osg::MatrixTransform*>(_branch)->setMatrix(_matrix);
834 ////////////////////////////////////////////////////////////////////////
835 // Implementation of SGTexRotateAnimation
836 ////////////////////////////////////////////////////////////////////////
838 SGTexRotateAnimation::SGTexRotateAnimation( SGPropertyNode *prop_root,
839 SGPropertyNode_ptr props )
840 : SGAnimation(props, new osg::Group),
841 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
842 _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
843 _factor(props->getDoubleValue("factor", 1.0)),
844 _table(read_interpolation_table(props)),
845 _has_min(props->hasValue("min-deg")),
846 _min_deg(props->getDoubleValue("min-deg")),
847 _has_max(props->hasValue("max-deg")),
848 _max_deg(props->getDoubleValue("max-deg")),
849 _position_deg(props->getDoubleValue("starting-position-deg", 0)),
852 SGPropertyNode *node = props->getChild("condition");
854 _condition = sgReadCondition(prop_root, node);
856 _center[0] = props->getFloatValue("center/x", 0);
857 _center[1] = props->getFloatValue("center/y", 0);
858 _center[2] = props->getFloatValue("center/z", 0);
859 _axis[0] = props->getFloatValue("axis/x", 0);
860 _axis[1] = props->getFloatValue("axis/y", 0);
861 _axis[2] = props->getFloatValue("axis/z", 0);
864 osg::StateSet* stateSet = _branch->getOrCreateStateSet();
865 _texMat = new osg::TexMat;
866 stateSet->setTextureAttribute(0, _texMat.get());
869 SGTexRotateAnimation::~SGTexRotateAnimation ()
875 SGTexRotateAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
877 if (!_condition || _condition->test()) {
879 _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
880 if (_has_min && _position_deg < _min_deg)
881 _position_deg = _min_deg;
882 if (_has_max && _position_deg > _max_deg)
883 _position_deg = _max_deg;
885 _position_deg = _table->interpolate(_prop->getDoubleValue());
888 set_rotation(_matrix, _position_deg, _center, _axis);
889 _texMat->setMatrix(_matrix);
895 ////////////////////////////////////////////////////////////////////////
896 // Implementation of SGTexTranslateAnimation
897 ////////////////////////////////////////////////////////////////////////
899 SGTexTranslateAnimation::SGTexTranslateAnimation( SGPropertyNode *prop_root,
900 SGPropertyNode_ptr props )
901 : SGAnimation(props, new osg::Group),
902 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true)),
903 _offset(props->getDoubleValue("offset", 0.0)),
904 _factor(props->getDoubleValue("factor", 1.0)),
905 _step(props->getDoubleValue("step",0.0)),
906 _scroll(props->getDoubleValue("scroll",0.0)),
907 _table(read_interpolation_table(props)),
908 _has_min(props->hasValue("min")),
909 _min(props->getDoubleValue("min")),
910 _has_max(props->hasValue("max")),
911 _max(props->getDoubleValue("max")),
912 _position(props->getDoubleValue("starting-position", 0)),
915 SGPropertyNode *node = props->getChild("condition");
917 _condition = sgReadCondition(prop_root, node);
919 _axis[0] = props->getFloatValue("axis/x", 0);
920 _axis[1] = props->getFloatValue("axis/y", 0);
921 _axis[2] = props->getFloatValue("axis/z", 0);
924 osg::StateSet* stateSet = _branch->getOrCreateStateSet();
925 _texMat = new osg::TexMat;
926 stateSet->setTextureAttribute(0, _texMat.get());
929 SGTexTranslateAnimation::~SGTexTranslateAnimation ()
935 SGTexTranslateAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
937 if (!_condition || _condition->test()) {
939 _position = (apply_mods(_prop->getDoubleValue(), _step, _scroll) + _offset) * _factor;
940 if (_has_min && _position < _min)
942 if (_has_max && _position > _max)
945 _position = _table->interpolate(apply_mods(_prop->getDoubleValue(), _step, _scroll));
948 set_translation(_matrix, _position, _axis);
949 _texMat->setMatrix(_matrix);
955 ////////////////////////////////////////////////////////////////////////
956 // Implementation of SGTexMultipleAnimation
957 ////////////////////////////////////////////////////////////////////////
959 SGTexMultipleAnimation::SGTexMultipleAnimation( SGPropertyNode *prop_root,
960 SGPropertyNode_ptr props )
961 : SGAnimation(props, new osg::Group),
962 _prop((SGPropertyNode *)prop_root->getNode(props->getStringValue("property", "/null"), true))
966 vector<SGPropertyNode_ptr> transform_nodes = props->getChildren("transform");
967 _transform = new TexTransform [transform_nodes.size()];
969 for (i = 0; i < transform_nodes.size(); i++) {
970 SGPropertyNode_ptr transform_props = transform_nodes[i];
972 if (!strcmp("textranslate",transform_props->getStringValue("subtype", 0))) {
974 // transform is a translation
975 _transform[i].subtype = 0;
977 _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
979 _transform[i].offset = transform_props->getDoubleValue("offset", 0.0);
980 _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
981 _transform[i].step = transform_props->getDoubleValue("step",0.0);
982 _transform[i].scroll = transform_props->getDoubleValue("scroll",0.0);
983 _transform[i].table = read_interpolation_table(transform_props);
984 _transform[i].has_min = transform_props->hasValue("min");
985 _transform[i].min = transform_props->getDoubleValue("min");
986 _transform[i].has_max = transform_props->hasValue("max");
987 _transform[i].max = transform_props->getDoubleValue("max");
988 _transform[i].position = transform_props->getDoubleValue("starting-position", 0);
990 _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
991 _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
992 _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
993 _transform[i].axis.normalize();
995 } else if (!strcmp("texrotate",transform_nodes[i]->getStringValue("subtype", 0))) {
997 // transform is a rotation
998 _transform[i].subtype = 1;
1000 _transform[i].prop = (SGPropertyNode *)prop_root->getNode(transform_props->getStringValue("property", "/null"), true);
1001 _transform[i].offset = transform_props->getDoubleValue("offset-deg", 0.0);
1002 _transform[i].factor = transform_props->getDoubleValue("factor", 1.0);
1003 _transform[i].table = read_interpolation_table(transform_props);
1004 _transform[i].has_min = transform_props->hasValue("min-deg");
1005 _transform[i].min = transform_props->getDoubleValue("min-deg");
1006 _transform[i].has_max = transform_props->hasValue("max-deg");
1007 _transform[i].max = transform_props->getDoubleValue("max-deg");
1008 _transform[i].position = transform_props->getDoubleValue("starting-position-deg", 0);
1010 _transform[i].center[0] = transform_props->getFloatValue("center/x", 0);
1011 _transform[i].center[1] = transform_props->getFloatValue("center/y", 0);
1012 _transform[i].center[2] = transform_props->getFloatValue("center/z", 0);
1013 _transform[i].axis[0] = transform_props->getFloatValue("axis/x", 0);
1014 _transform[i].axis[1] = transform_props->getFloatValue("axis/y", 0);
1015 _transform[i].axis[2] = transform_props->getFloatValue("axis/z", 0);
1016 _transform[i].axis.normalize();
1020 osg::StateSet* stateSet = _branch->getOrCreateStateSet();
1021 _texMat = new osg::TexMat;
1022 stateSet->setTextureAttribute(0, _texMat.get());
1025 SGTexMultipleAnimation::~SGTexMultipleAnimation ()
1027 delete [] _transform;
1031 SGTexMultipleAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
1034 osg::Matrix tmatrix;
1035 tmatrix.makeIdentity();
1036 for (i = 0; i < _num_transforms; i++) {
1038 if(_transform[i].subtype == 0) {
1040 // subtype 0 is translation
1041 if (_transform[i].table == 0) {
1042 _transform[i].position = (apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll) + _transform[i].offset) * _transform[i].factor;
1043 if (_transform[i].has_min && _transform[i].position < _transform[i].min)
1044 _transform[i].position = _transform[i].min;
1045 if (_transform[i].has_max && _transform[i].position > _transform[i].max)
1046 _transform[i].position = _transform[i].max;
1048 _transform[i].position = _transform[i].table->interpolate(apply_mods(_transform[i].prop->getDoubleValue(), _transform[i].step,_transform[i].scroll));
1051 set_translation(matrix, _transform[i].position, _transform[i].axis);
1052 tmatrix = matrix*tmatrix;
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());
1069 set_rotation(matrix, _transform[i].position, _transform[i].center, _transform[i].axis);
1070 tmatrix = matrix*tmatrix;
1073 _texMat->setMatrix(tmatrix);
1079 ////////////////////////////////////////////////////////////////////////
1080 // Implementation of SGAlphaTestAnimation
1081 ////////////////////////////////////////////////////////////////////////
1083 SGAlphaTestAnimation::SGAlphaTestAnimation(SGPropertyNode_ptr props)
1084 : SGAnimation(props, new osg::Group)
1086 _alpha_clamp = props->getFloatValue("alpha-factor", 0.0);
1089 SGAlphaTestAnimation::~SGAlphaTestAnimation ()
1093 void SGAlphaTestAnimation::init()
1095 osg::StateSet* stateSet = _branch->getOrCreateStateSet();
1096 osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
1097 alphaFunc->setFunction(osg::AlphaFunc::GREATER);
1098 alphaFunc->setReferenceValue(_alpha_clamp);
1099 stateSet->setAttribute(alphaFunc);
1100 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
1101 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
1102 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
1107 ////////////////////////////////////////////////////////////////////////
1108 // Implementation of SGMaterialAnimation
1109 ////////////////////////////////////////////////////////////////////////
1111 SGMaterialAnimation::SGMaterialAnimation( SGPropertyNode *prop_root,
1112 SGPropertyNode_ptr props, const SGPath &texture_path)
1113 : SGAnimation(props, new osg::Group),
1114 _last_condition(false),
1115 _prop_root(prop_root),
1117 _texture_base(texture_path),
1120 _global(props->getBoolValue("global", false))
1122 SGPropertyNode_ptr n;
1123 n = props->getChild("condition");
1124 _condition = n ? sgReadCondition(_prop_root, n) : 0;
1125 n = props->getChild("property-base");
1127 _prop_base = n->getStringValue();
1128 if (!_prop_base.empty() && _prop_base.end()[-1] != '/')
1132 initColorGroup(props->getChild("diffuse"), &_diff, DIFFUSE);
1133 initColorGroup(props->getChild("ambient"), &_amb, AMBIENT);
1134 initColorGroup(props->getChild("emission"), &_emis, EMISSION);
1135 initColorGroup(props->getChild("specular"), &_spec, SPECULAR);
1137 _shi = props->getFloatValue("shininess", -1.0);
1139 _update |= SHININESS;
1141 SGPropertyNode_ptr group = props->getChild("transparency");
1143 _trans.value = group->getFloatValue("alpha", -1.0);
1144 _trans.factor = group->getFloatValue("factor", 1.0);
1145 _trans.offset = group->getFloatValue("offset", 0.0);
1146 _trans.min = group->getFloatValue("min", 0.0);
1147 if (_trans.min < 0.0)
1149 _trans.max = group->getFloatValue("max", 1.0);
1150 if (_trans.max > 1.0)
1153 _update |= TRANSPARENCY;
1155 n = group->getChild("alpha-prop");
1156 _trans.value_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1157 n = group->getChild("factor-prop");
1158 _trans.factor_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1159 n = group->getChild("offset-prop");
1160 _trans.offset_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1162 _read |= TRANSPARENCY;
1165 _thresh = props->getFloatValue("threshold", -1.0);
1167 _update |= THRESHOLD;
1169 string _texture_str = props->getStringValue("texture", "");
1170 if (!_texture_str.empty()) {
1171 _texture = _texture_base;
1172 _texture.append(_texture_str);
1173 _texture2D = SGLoadTexture2D(_texture);
1177 n = props->getChild("shininess-prop");
1178 _shi_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1179 n = props->getChild("threshold-prop");
1180 _thresh_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1181 n = props->getChild("texture-prop");
1182 _tex_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1184 _static_update = _update;
1186 _alphaFunc = new osg::AlphaFunc(osg::AlphaFunc::GREATER, 0);
1189 void SGMaterialAnimation::initColorGroup(SGPropertyNode_ptr group, ColorSpec *col, int flag)
1194 col->red = group->getFloatValue("red", -1.0);
1195 col->green = group->getFloatValue("green", -1.0);
1196 col->blue = group->getFloatValue("blue", -1.0);
1197 col->factor = group->getFloatValue("factor", 1.0);
1198 col->offset = group->getFloatValue("offset", 0.0);
1203 n = group->getChild("red-prop");
1204 col->red_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1205 n = group->getChild("green-prop");
1206 col->green_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1207 n = group->getChild("blue-prop");
1208 col->blue_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1209 n = group->getChild("factor-prop");
1210 col->factor_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1211 n = group->getChild("offset-prop");
1212 col->offset_prop = n ? _prop_root->getNode(path(n->getStringValue()), true) : 0;
1217 void SGMaterialAnimation::init()
1220 cloneMaterials(_branch);
1223 osg::StateSet* stateSet = _branch->getOrCreateStateSet();
1224 if (_update & THRESHOLD) {
1225 stateSet->setAttribute(_alphaFunc.get(), osg::StateAttribute::OVERRIDE);
1226 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
1228 if ((_update & TEXTURE) && _texture2D.valid()) {
1229 stateSet->setTextureAttribute(0, _texture2D.get(), osg::StateAttribute::OVERRIDE);
1230 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
1235 SGMaterialAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
1238 bool cond = _condition->test();
1239 if (cond && !_last_condition)
1240 _update |= _static_update;
1242 _last_condition = cond;
1249 if (_read & DIFFUSE)
1250 updateColorGroup(&_diff, DIFFUSE);
1251 if (_read & AMBIENT)
1252 updateColorGroup(&_amb, AMBIENT);
1253 if (_read & EMISSION)
1254 updateColorGroup(&_emis, EMISSION);
1255 if (_read & SPECULAR)
1256 updateColorGroup(&_spec, SPECULAR);
1261 _shi = _shi_prop->getFloatValue();
1263 _update |= SHININESS;
1265 if (_read & TRANSPARENCY) {
1266 PropSpec tmp = _trans;
1267 if (_trans.value_prop)
1268 _trans.value = _trans.value_prop->getFloatValue();
1269 if (_trans.factor_prop)
1270 _trans.factor = _trans.factor_prop->getFloatValue();
1271 if (_trans.offset_prop)
1272 _trans.offset = _trans.offset_prop->getFloatValue();
1274 _update |= TRANSPARENCY;
1278 _thresh = _thresh_prop->getFloatValue();
1280 _update |= THRESHOLD;
1283 string t = _tex_prop->getStringValue();
1284 if (!t.empty() && t != _texture_str) {
1286 _texture = _texture_base;
1292 setMaterialBranch(_branch);
1298 void SGMaterialAnimation::updateColorGroup(ColorSpec *col, int flag)
1300 ColorSpec tmp = *col;
1302 col->red = col->red_prop->getFloatValue();
1303 if (col->green_prop)
1304 col->green = col->green_prop->getFloatValue();
1306 col->blue = col->blue_prop->getFloatValue();
1307 if (col->factor_prop)
1308 col->factor = col->factor_prop->getFloatValue();
1309 if (col->offset_prop)
1310 col->offset = col->offset_prop->getFloatValue();
1315 class SGMaterialAnimationCloneVisitor : public osg::NodeVisitor {
1317 SGMaterialAnimationCloneVisitor() :
1318 osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
1320 setVisitorType(osg::NodeVisitor::NODE_VISITOR);
1322 virtual void apply(osg::Node& node)
1325 osg::StateSet* stateSet = node.getStateSet();
1328 if (1 < stateSet->referenceCount()) {
1329 osg::CopyOp copyOp(osg::CopyOp::DEEP_COPY_STATESETS);
1330 osg::Object* object = stateSet->clone(copyOp);
1331 stateSet = static_cast<osg::StateSet*>(object);
1332 node.setStateSet(stateSet);
1334 cloneMaterial(stateSet);
1336 virtual void apply(osg::Geode& node)
1338 apply((osg::Node&)node);
1340 unsigned nDrawables = node.getNumDrawables();
1341 for (unsigned i = 0; i < nDrawables; ++i) {
1342 osg::Drawable* drawable = node.getDrawable(i);
1343 osg::StateSet* stateSet = drawable->getStateSet();
1346 if (1 < stateSet->referenceCount()) {
1347 osg::CopyOp copyOp(osg::CopyOp::DEEP_COPY_STATESETS);
1348 osg::Object* object = stateSet->clone(copyOp);
1349 stateSet = static_cast<osg::StateSet*>(object);
1350 drawable->setStateSet(stateSet);
1352 cloneMaterial(stateSet);
1355 void cloneMaterial(osg::StateSet* stateSet)
1357 osg::StateAttribute* stateAttr;
1358 stateAttr = stateSet->getAttribute(osg::StateAttribute::MATERIAL);
1361 osg::CopyOp copyOp(osg::CopyOp::DEEP_COPY_STATEATTRIBUTES);
1362 osg::Object* object = stateAttr->clone(copyOp);
1363 osg::Material* material = static_cast<osg::Material*>(object);
1364 materialList.push_back(material);
1365 while (stateSet->getAttribute(osg::StateAttribute::MATERIAL)) {
1366 stateSet->removeAttribute(osg::StateAttribute::MATERIAL);
1368 stateSet->setAttribute(material);
1370 std::vector<osg::ref_ptr<osg::Material> > materialList;
1373 void SGMaterialAnimation::cloneMaterials(osg::Group *b)
1375 SGMaterialAnimationCloneVisitor cloneVisitor;
1376 b->accept(cloneVisitor);
1377 _materialList.swap(cloneVisitor.materialList);
1380 void SGMaterialAnimation::setMaterialBranch(osg::Group *b)
1382 std::vector<osg::ref_ptr<osg::Material> >::iterator i;
1383 for (i = _materialList.begin(); i != _materialList.end(); ++i) {
1384 osg::Material* material = i->get();
1385 if (_update & DIFFUSE) {
1386 osg::Vec4 v = _diff.rgba();
1387 float alpha = material->getDiffuse(osg::Material::FRONT_AND_BACK)[3];
1388 material->setColorMode(osg::Material::DIFFUSE);
1389 material->setDiffuse(osg::Material::FRONT_AND_BACK,
1390 osg::Vec4(v[0], v[1], v[2], alpha));
1392 if (_update & AMBIENT) {
1393 material->setColorMode(osg::Material::AMBIENT);
1394 material->setDiffuse(osg::Material::FRONT_AND_BACK, _amb.rgba());
1396 if (_update & EMISSION)
1397 material->setEmission(osg::Material::FRONT_AND_BACK, _emis.rgba());
1398 if (_update & SPECULAR)
1399 material->setSpecular(osg::Material::FRONT_AND_BACK, _spec.rgba());
1400 if (_update & SHININESS)
1401 material->setShininess(osg::Material::FRONT_AND_BACK,
1402 clamp(_shi, 0.0, 128.0));
1403 if (_update & THRESHOLD)
1404 _alphaFunc->setReferenceValue(clamp(_thresh));
1407 if (_update & TRANSPARENCY) {
1408 float trans = _trans.value * _trans.factor + _trans.offset;
1409 trans = trans < _trans.min ? _trans.min : trans > _trans.max ? _trans.max : trans;
1410 SGBlendAnimationVisitor visitor(trans);
1411 _branch->accept(visitor);
1413 if (_update & TEXTURE) {
1415 _texture2D = new osg::Texture2D;
1416 osg::StateSet* stateSet = _branch->getOrCreateStateSet();
1417 stateSet->setTextureAttribute(0, _texture2D.get(), osg::StateAttribute::OVERRIDE);
1418 stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
1420 osg::Image* image = osgDB::readImageFile(_texture.str());
1422 _texture2D->setImage(image);
1423 if (image->isImageTranslucent()) {
1424 osg::StateSet* stateSet = _branch->getOrCreateStateSet();
1425 stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
1426 stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON);
1427 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
1435 ////////////////////////////////////////////////////////////////////////
1436 // Implementation of SGFlashAnimation
1437 ////////////////////////////////////////////////////////////////////////
1438 class SGFlashAnimationTransform : public osg::Transform {
1440 SGFlashAnimationTransform(SGPropertyNode* props)
1442 getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
1444 _axis[0] = props->getFloatValue("axis/x", 0);
1445 _axis[1] = props->getFloatValue("axis/y", 0);
1446 _axis[2] = props->getFloatValue("axis/z", 1);
1449 _center[0] = props->getFloatValue("center/x-m", 0);
1450 _center[1] = props->getFloatValue("center/y-m", 0);
1451 _center[2] = props->getFloatValue("center/z-m", 0);
1453 _offset = props->getFloatValue("offset", 0.0);
1454 _factor = props->getFloatValue("factor", 1.0);
1455 _power = props->getFloatValue("power", 1.0);
1456 _two_sides = props->getBoolValue("two-sides", false);
1458 _min_v = props->getFloatValue("min", 0.0);
1459 _max_v = props->getFloatValue("max", 1.0);
1462 virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,
1463 osg::NodeVisitor* nv) const
1465 double scale_factor = computeScaleFactor(nv);
1466 osg::Matrix transform;
1467 transform(0,0) = scale_factor;
1468 transform(1,1) = scale_factor;
1469 transform(2,2) = scale_factor;
1470 transform(3,0) = _center[0] * ( 1 - scale_factor );
1471 transform(3,1) = _center[1] * ( 1 - scale_factor );
1472 transform(3,2) = _center[2] * ( 1 - scale_factor );
1473 if (_referenceFrame == RELATIVE_RF)
1474 matrix.preMult(transform);
1481 virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,
1482 osg::NodeVisitor* nv) const
1484 double scale_factor = computeScaleFactor(nv);
1485 if (fabs(scale_factor) <= std::numeric_limits<double>::min())
1487 osg::Matrix transform;
1488 double rScaleFactor = 1/scale_factor;
1489 transform(0,0) = rScaleFactor;
1490 transform(1,1) = rScaleFactor;
1491 transform(2,2) = rScaleFactor;
1492 transform(3,0) = rScaleFactor*_center[0] * ( scale_factor - 1 );
1493 transform(3,1) = rScaleFactor*_center[1] * ( scale_factor - 1 );
1494 transform(3,2) = rScaleFactor*_center[2] * ( scale_factor - 1 );
1495 if (_referenceFrame == RELATIVE_RF)
1496 matrix.postMult(transform);
1502 double computeScaleFactor(osg::NodeVisitor* nv) const
1507 osg::Vec3 localEyeToCenter = nv->getEyePoint() - _center;
1508 localEyeToCenter.normalize();
1510 double cos_angle = localEyeToCenter*_axis;
1511 double scale_factor = 0;
1512 if ( _two_sides && cos_angle < 0 )
1513 scale_factor = _factor * pow( -cos_angle, _power ) + _offset;
1514 else if ( cos_angle > 0 )
1515 scale_factor = _factor * pow( cos_angle, _power ) + _offset;
1517 if ( scale_factor < _min_v )
1518 scale_factor = _min_v;
1519 if ( scale_factor > _max_v )
1520 scale_factor = _max_v;
1522 return scale_factor;
1526 osg::Vec3 _axis, _center;
1527 double _power, _factor, _offset, _min_v, _max_v;
1531 SGFlashAnimation::SGFlashAnimation(SGPropertyNode_ptr props)
1532 : SGAnimation( props, new SGFlashAnimationTransform(props) )
1536 SGFlashAnimation::~SGFlashAnimation()
1542 ////////////////////////////////////////////////////////////////////////
1543 // Implementation of SGDistScaleAnimation
1544 ////////////////////////////////////////////////////////////////////////
1545 class SGDistScaleTransform : public osg::Transform {
1547 SGDistScaleTransform(SGPropertyNode* props)
1549 getOrCreateStateSet()->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
1551 _factor = props->getFloatValue("factor", 1.0);
1552 _offset = props->getFloatValue("offset", 0.0);
1553 _min_v = props->getFloatValue("min", 0.0);
1554 _max_v = props->getFloatValue("max", 1.0);
1555 _has_min = props->hasValue("min");
1556 _has_max = props->hasValue("max");
1557 _table = read_interpolation_table(props);
1558 _center[0] = props->getFloatValue("center/x-m", 0);
1559 _center[1] = props->getFloatValue("center/y-m", 0);
1560 _center[2] = props->getFloatValue("center/z-m", 0);
1562 ~SGDistScaleTransform()
1567 virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,
1568 osg::NodeVisitor* nv) const
1570 osg::Matrix transform;
1571 double scale_factor = computeScaleFactor(nv);
1572 transform(0,0) = scale_factor;
1573 transform(1,1) = scale_factor;
1574 transform(2,2) = scale_factor;
1575 transform(3,0) = _center[0] * ( 1 - scale_factor );
1576 transform(3,1) = _center[1] * ( 1 - scale_factor );
1577 transform(3,2) = _center[2] * ( 1 - scale_factor );
1578 if (_referenceFrame == RELATIVE_RF)
1579 matrix.preMult(transform);
1585 virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,
1586 osg::NodeVisitor* nv) const
1588 double scale_factor = computeScaleFactor(nv);
1589 if (fabs(scale_factor) <= std::numeric_limits<double>::min())
1591 osg::Matrix transform;
1592 double rScaleFactor = 1/scale_factor;
1593 transform(0,0) = rScaleFactor;
1594 transform(1,1) = rScaleFactor;
1595 transform(2,2) = rScaleFactor;
1596 transform(3,0) = rScaleFactor*_center[0] * ( scale_factor - 1 );
1597 transform(3,1) = rScaleFactor*_center[1] * ( scale_factor - 1 );
1598 transform(3,2) = rScaleFactor*_center[2] * ( scale_factor - 1 );
1599 if (_referenceFrame == RELATIVE_RF)
1600 matrix.postMult(transform);
1606 double computeScaleFactor(osg::NodeVisitor* nv) const
1611 osg::Vec3 localEyeToCenter = _center - nv->getEyePoint();
1612 double scale_factor = localEyeToCenter.length();
1614 scale_factor = _factor * scale_factor + _offset;
1615 if ( _has_min && scale_factor < _min_v )
1616 scale_factor = _min_v;
1617 if ( _has_max && scale_factor > _max_v )
1618 scale_factor = _max_v;
1620 scale_factor = _table->interpolate( scale_factor );
1623 return scale_factor;
1629 float _factor, _offset, _min_v, _max_v;
1630 bool _has_min, _has_max;
1631 SGInterpTable * _table;
1634 SGDistScaleAnimation::SGDistScaleAnimation(SGPropertyNode_ptr props)
1635 : SGAnimation( props, new SGDistScaleTransform(props) )
1639 SGDistScaleAnimation::~SGDistScaleAnimation()
1643 ////////////////////////////////////////////////////////////////////////
1644 // Implementation of SGShadowAnimation
1645 ////////////////////////////////////////////////////////////////////////
1647 SGShadowAnimation::SGShadowAnimation ( SGPropertyNode *prop_root,
1648 SGPropertyNode_ptr props )
1649 : SGAnimation(props, new osg::Group),
1651 _condition_value(true)
1654 SGPropertyNode_ptr node = props->getChild("condition");
1656 _condition = sgReadCondition(prop_root, node);
1657 _condition_value = false;
1661 SGShadowAnimation::~SGShadowAnimation ()
1667 SGShadowAnimation::operator()(osg::Node* node, osg::NodeVisitor* nv)
1670 _condition_value = _condition->test();
1672 if ( _condition_value ) {
1673 _branch->setNodeMask(SG_NODEMASK_SHADOW_BIT|_branch->getNodeMask());
1675 _branch->setNodeMask(~SG_NODEMASK_SHADOW_BIT&_branch->getNodeMask());
1680 bool SGShadowAnimation::get_condition_value(void) {
1681 return _condition_value;
1684 // end of animation.cxx