2 // animation.hxx - classes to manage model animation.
3 // Written by David Megginson, started 2002.
5 // This file is in the Public Domain, and comes with no warranty.
7 #ifndef _SG_ANIMATION_HXX
8 #define _SG_ANIMATION_HXX 1
11 # error This library requires C++
20 #include <osg/ref_ptr>
21 #include <osg/AlphaFunc>
22 #include <osg/ColorMatrix>
24 #include <osg/Material>
26 #include <osg/NodeCallback>
27 #include <osg/NodeVisitor>
28 #include <osg/StateSet>
29 #include <osg/Texture2D>
32 #include <simgear/props/props.hxx>
33 #include <simgear/misc/sg_path.hxx>
35 #include <simgear/scene/model/persparam.hxx>
36 #include <simgear/scene/util/SGNodeMasks.hxx>
41 // Don't pull in the headers, since we don't need them here.
44 class SGPersonalityBranch;
46 // Has anyone done anything *really* stupid, like making min and max macros?
56 //////////////////////////////////////////////////////////////////////
58 //////////////////////////////////////////////////////////////////////
61 * Abstract base class for all animations.
63 class SGAnimation : public osg::NodeCallback
66 SGAnimation (SGPropertyNode_ptr props, osg::Group * branch);
68 virtual ~SGAnimation ();
71 * Get the SSG branch holding the animation.
73 virtual osg::Group * getBranch () { return _branch; }
76 * Initialize the animation, after children have been added.
81 * Update the animation.
83 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
86 * Restore the state after the animation.
88 virtual void restore();
90 int get_animation_type(void) { return animation_type; }
103 class SGNullAnimation : public SGAnimation
106 SGNullAnimation (SGPropertyNode_ptr props);
107 virtual ~SGNullAnimation ();
112 * A range, or level-of-detail (LOD) animation.
114 class SGRangeAnimation : public SGAnimation
117 SGRangeAnimation (SGPropertyNode *prop_root,
118 SGPropertyNode_ptr props);
119 virtual ~SGRangeAnimation ();
120 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
122 SGPropertyNode_ptr _min_prop;
123 SGPropertyNode_ptr _max_prop;
128 SGCondition * _condition;
133 * Animation to turn and face the screen.
135 class SGBillboardAnimation : public SGAnimation
138 SGBillboardAnimation (SGPropertyNode_ptr props);
139 virtual ~SGBillboardAnimation ();
144 * Animation to select alternative versions of the same object.
146 class SGSelectAnimation : public SGAnimation
149 SGSelectAnimation( SGPropertyNode *prop_root,
150 SGPropertyNode_ptr props );
151 virtual ~SGSelectAnimation ();
152 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
154 SGCondition * _condition;
159 * Animation to spin an object around a center point.
161 * This animation rotates at a specific velocity.
163 class SGSpinAnimation : public SGAnimation
166 SGSpinAnimation( SGPropertyNode *prop_root,
167 SGPropertyNode_ptr props,
168 double sim_time_sec );
169 virtual ~SGSpinAnimation ();
170 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
172 bool _use_personality;
173 SGPropertyNode_ptr _prop;
174 SGPersonalityParameter<double> _factor;
175 SGPersonalityParameter<double> _position_deg;
176 double _last_time_sec;
179 SGCondition * _condition;
184 * Animation to draw objects for a specific amount of time each.
186 class SGTimedAnimation : public SGAnimation
189 SGTimedAnimation (SGPropertyNode_ptr props);
190 virtual ~SGTimedAnimation ();
192 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
194 bool _use_personality;
195 double _duration_sec;
196 double _last_time_sec;
197 double _total_duration_sec;
199 struct DurationSpec {
200 DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
201 DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
204 vector<DurationSpec> _branch_duration_specs;
205 vector<double> _branch_duration_sec;
210 * Animation to rotate an object around a center point.
212 * This animation rotates to a specific position.
214 class SGRotateAnimation : public SGAnimation
217 SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
218 virtual ~SGRotateAnimation ();
219 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
221 SGPropertyNode_ptr _prop;
224 SGInterpTable * _table;
229 double _position_deg;
232 SGCondition * _condition;
237 * Animation to slide along an axis.
239 class SGTranslateAnimation : public SGAnimation
242 SGTranslateAnimation( SGPropertyNode *prop_root,
243 SGPropertyNode_ptr props );
244 virtual ~SGTranslateAnimation ();
245 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
247 bool _use_personality;
248 SGPropertyNode_ptr _prop;
249 SGPersonalityParameter<double> _offset_m;
250 SGPersonalityParameter<double> _factor;
251 SGInterpTable * _table;
258 SGCondition * _condition;
262 * Animation to blend an object.
264 class SGBlendAnimation : public SGAnimation
267 SGBlendAnimation( SGPropertyNode *prop_root,
268 SGPropertyNode_ptr props );
269 virtual ~SGBlendAnimation ();
270 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
272 bool _use_personality;
273 SGPropertyNode_ptr _prop;
274 SGInterpTable * _table;
276 SGPersonalityParameter<double> _offset;
277 SGPersonalityParameter<double> _factor;
282 osg::ref_ptr<osg::ColorMatrix> _colorMatrix;
286 * Animation to scale an object.
288 class SGScaleAnimation : public SGAnimation
291 SGScaleAnimation( SGPropertyNode *prop_root,
292 SGPropertyNode_ptr props );
293 virtual ~SGScaleAnimation ();
294 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
296 bool _use_personality;
297 SGPropertyNode_ptr _prop;
298 SGPersonalityParameter<double> _x_factor;
299 SGPersonalityParameter<double> _y_factor;
300 SGPersonalityParameter<double> _z_factor;
301 SGPersonalityParameter<double> _x_offset;
302 SGPersonalityParameter<double> _y_offset;
303 SGPersonalityParameter<double> _z_offset;
304 SGInterpTable * _table;
323 * Animation to rotate texture mappings around a center point.
325 * This animation rotates to a specific position.
327 class SGTexRotateAnimation : public SGAnimation
330 SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
331 virtual ~SGTexRotateAnimation ();
332 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
334 SGPropertyNode_ptr _prop;
337 SGInterpTable * _table;
342 double _position_deg;
345 SGCondition * _condition;
346 osg::ref_ptr<osg::TexMat> _texMat;
351 * Animation to slide texture mappings along an axis.
353 class SGTexTranslateAnimation : public SGAnimation
356 SGTexTranslateAnimation( SGPropertyNode *prop_root,
357 SGPropertyNode_ptr props );
358 virtual ~SGTexTranslateAnimation ();
359 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
361 SGPropertyNode_ptr _prop;
366 SGInterpTable * _table;
373 SGCondition * _condition;
374 osg::ref_ptr<osg::TexMat> _texMat;
380 * Classes for handling multiple types of Texture translations on one object
383 class SGTexMultipleAnimation : public SGAnimation
386 SGTexMultipleAnimation( SGPropertyNode *prop_root,
387 SGPropertyNode_ptr props );
388 virtual ~SGTexMultipleAnimation ();
389 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
394 SGPropertyNode_ptr prop;
395 int subtype; // 0=translation, 1=rotation
400 SGInterpTable * table;
409 SGPropertyNode_ptr _prop;
410 TexTransform* _transform;
412 osg::ref_ptr<osg::TexMat> _texMat;
417 * An "animation" to enable the alpha test
419 class SGAlphaTestAnimation : public SGAnimation
422 SGAlphaTestAnimation(SGPropertyNode_ptr props);
423 virtual ~SGAlphaTestAnimation ();
431 * An "animation" to modify material properties
433 class SGMaterialAnimation : public SGAnimation
436 SGMaterialAnimation(SGPropertyNode *prop_root, SGPropertyNode_ptr props,
437 const SGPath &texpath);
438 virtual ~SGMaterialAnimation() {}
440 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
453 float red, green, blue;
456 SGPropertyNode_ptr red_prop;
457 SGPropertyNode_ptr green_prop;
458 SGPropertyNode_ptr blue_prop;
459 SGPropertyNode_ptr factor_prop;
460 SGPropertyNode_ptr offset_prop;
462 inline bool dirty() {
463 return red >= 0.0 || green >= 0.0 || blue >= 0.0;
466 return red_prop || green_prop || blue_prop
467 || factor_prop || offset_prop;
469 inline bool operator!=(ColorSpec& a) {
470 return red != a.red || green != a.green || blue != a.blue
471 || factor != a.factor || offset != a.offset;
474 v[0] = clamp(red * factor + offset);
475 v[1] = clamp(green * factor + offset);
476 v[2] = clamp(blue * factor + offset);
480 inline float clamp(float val) {
481 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0 : val;
490 SGPropertyNode_ptr value_prop;
491 SGPropertyNode_ptr factor_prop;
492 SGPropertyNode_ptr offset_prop;
493 inline bool dirty() { return value >= 0.0; }
494 inline bool live() { return value_prop || factor_prop || offset_prop; }
495 inline bool operator!=(PropSpec& a) {
496 return value != a.value || factor != a.factor || offset != a.offset;
499 SGCondition *_condition;
500 bool _last_condition;
501 SGPropertyNode_ptr _prop_root;
503 SGPath _texture_base;
508 unsigned _static_update;
516 float _thresh; // alpha_clamp (see man glAlphaFunc)
519 SGPropertyNode_ptr _shi_prop;
520 SGPropertyNode_ptr _thresh_prop;
521 SGPropertyNode_ptr _tex_prop;
522 std::vector<osg::ref_ptr<osg::Material> > _materialList;
523 osg::ref_ptr<osg::AlphaFunc> _alphaFunc;
524 osg::ref_ptr<osg::Texture2D> _texture2D;
526 void cloneMaterials(osg::Group *b);
527 void setMaterialBranch(osg::Group *b);
528 void initColorGroup(SGPropertyNode_ptr, ColorSpec *, int flag);
529 void updateColorGroup(ColorSpec *, int flag);
530 inline float clamp(float val, float min = 0.0, float max = 1.0) {
531 return val < min ? min : val > max ? max : val;
533 const char *path(const char *rel) {
534 return (_tmpstr = _prop_base + rel).c_str();
540 * An "animation" that compute a scale according to
541 * the angle between an axis and the view direction
543 class SGFlashAnimation : public SGAnimation
546 SGFlashAnimation(SGPropertyNode_ptr props);
547 virtual ~SGFlashAnimation ();
552 * An animation that compute a scale according to
553 * the distance from a point and the viewer
555 class SGDistScaleAnimation : public SGAnimation
558 SGDistScaleAnimation(SGPropertyNode_ptr props);
559 virtual ~SGDistScaleAnimation ();
563 * An animation to tell wich objects don't cast shadows.
565 class SGShadowAnimation : public SGAnimation
568 SGShadowAnimation ( SGPropertyNode *prop_root,
569 SGPropertyNode_ptr props );
570 virtual ~SGShadowAnimation ();
571 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
572 bool get_condition_value(void);
574 SGCondition * _condition;
575 bool _condition_value;
579 + * An "animation" that replace fixed opengl pipeline by shaders
581 class SGShaderAnimation : public SGAnimation
584 SGShaderAnimation ( SGPropertyNode *prop_root,
585 SGPropertyNode_ptr props );
586 virtual ~SGShaderAnimation ();
588 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
589 bool get_condition_value(void);
591 SGCondition * _condition;
592 bool _condition_value;
595 osg::Vec4 _param_color;
599 SGPropertyNode_ptr _factor_prop;
602 SGPropertyNode_ptr _speed_prop;
603 osg::ref_ptr<osg::Texture2D> _effectTexture;
604 unsigned char *_textureData;
605 GLint _texWidth, _texHeight;
610 #endif // _SG_ANIMATION_HXX