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>
23 #include <osg/Material>
25 #include <osg/NodeCallback>
26 #include <osg/NodeVisitor>
27 #include <osg/StateSet>
28 #include <osg/Texture2D>
31 #include <simgear/props/props.hxx>
32 #include <simgear/misc/sg_path.hxx>
34 #include <simgear/scene/model/persparam.hxx>
35 #include <simgear/scene/util/SGNodeMasks.hxx>
40 // Don't pull in the headers, since we don't need them here.
44 // Has anyone done anything *really* stupid, like making min and max macros?
54 //////////////////////////////////////////////////////////////////////
56 //////////////////////////////////////////////////////////////////////
59 * Abstract base class for all animations.
61 class SGAnimation : public osg::NodeCallback
64 SGAnimation (SGPropertyNode_ptr props, osg::Group * branch);
66 virtual ~SGAnimation ();
69 * Get the SSG branch holding the animation.
71 virtual osg::Group * getBranch () { return _branch; }
74 * Initialize the animation, after children have been added.
79 * Update the animation.
81 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
84 * Restore the state after the animation.
86 virtual void restore();
88 int get_animation_type(void) { return animation_type; }
101 class SGNullAnimation : public SGAnimation
104 SGNullAnimation (SGPropertyNode_ptr props);
105 virtual ~SGNullAnimation ();
110 * A range, or level-of-detail (LOD) animation.
112 class SGRangeAnimation : public SGAnimation
115 SGRangeAnimation (SGPropertyNode *prop_root,
116 SGPropertyNode_ptr props);
117 virtual ~SGRangeAnimation ();
118 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
120 SGPropertyNode_ptr _min_prop;
121 SGPropertyNode_ptr _max_prop;
126 SGSharedPtr<SGCondition> _condition;
131 * Animation to turn and face the screen.
133 class SGBillboardAnimation : public SGAnimation
136 SGBillboardAnimation (SGPropertyNode_ptr props);
137 virtual ~SGBillboardAnimation ();
142 * Animation to select alternative versions of the same object.
144 class SGSelectAnimation : public SGAnimation
147 SGSelectAnimation( SGPropertyNode *prop_root,
148 SGPropertyNode_ptr props );
149 virtual ~SGSelectAnimation ();
150 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
152 SGSharedPtr<SGCondition> _condition;
157 * Animation to spin an object around a center point.
159 * This animation rotates at a specific velocity.
161 class SGSpinAnimation : public SGAnimation
164 SGSpinAnimation( SGPropertyNode *prop_root,
165 SGPropertyNode_ptr props,
166 double sim_time_sec );
167 virtual ~SGSpinAnimation ();
168 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
170 bool _use_personality;
171 SGPropertyNode_ptr _prop;
172 SGPersonalityParameter<double> _factor;
173 SGPersonalityParameter<double> _position_deg;
174 double _last_time_sec;
177 SGSharedPtr<SGCondition> _condition;
182 * Animation to draw objects for a specific amount of time each.
184 class SGTimedAnimation : public SGAnimation
187 SGTimedAnimation (SGPropertyNode_ptr props);
188 virtual ~SGTimedAnimation ();
190 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
192 bool _use_personality;
193 double _duration_sec;
194 double _last_time_sec;
195 double _total_duration_sec;
197 struct DurationSpec {
198 DurationSpec( double m = 0.0 ) : _min(m), _max(m) {}
199 DurationSpec( double m1, double m2 ) : _min(m1), _max(m2) {}
202 vector<DurationSpec> _branch_duration_specs;
203 vector<double> _branch_duration_sec;
208 * Animation to rotate an object around a center point.
210 * This animation rotates to a specific position.
212 class SGRotateAnimation : public SGAnimation
215 SGRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
216 virtual ~SGRotateAnimation ();
217 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
219 SGPropertyNode_ptr _prop;
222 SGSharedPtr<SGInterpTable> _table;
227 double _position_deg;
230 SGSharedPtr<SGCondition> _condition;
235 * Animation to slide along an axis.
237 class SGTranslateAnimation : public SGAnimation
240 SGTranslateAnimation( SGPropertyNode *prop_root,
241 SGPropertyNode_ptr props );
242 virtual ~SGTranslateAnimation ();
243 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
245 bool _use_personality;
246 SGPropertyNode_ptr _prop;
247 SGPersonalityParameter<double> _offset_m;
248 SGPersonalityParameter<double> _factor;
249 SGSharedPtr<SGInterpTable> _table;
256 SGSharedPtr<SGCondition> _condition;
260 * Animation to blend an object.
262 class SGBlendAnimation : public SGAnimation
265 SGBlendAnimation( SGPropertyNode *prop_root,
266 SGPropertyNode_ptr props );
267 virtual ~SGBlendAnimation ();
268 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
270 bool _use_personality;
271 SGPropertyNode_ptr _prop;
272 SGSharedPtr<SGInterpTable> _table;
274 SGPersonalityParameter<double> _offset;
275 SGPersonalityParameter<double> _factor;
281 * Animation to scale an object.
283 class SGScaleAnimation : public SGAnimation
286 SGScaleAnimation( SGPropertyNode *prop_root,
287 SGPropertyNode_ptr props );
288 virtual ~SGScaleAnimation ();
289 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
291 bool _use_personality;
292 SGPropertyNode_ptr _prop;
293 SGPersonalityParameter<double> _x_factor;
294 SGPersonalityParameter<double> _y_factor;
295 SGPersonalityParameter<double> _z_factor;
296 SGPersonalityParameter<double> _x_offset;
297 SGPersonalityParameter<double> _y_offset;
298 SGPersonalityParameter<double> _z_offset;
299 SGSharedPtr<SGInterpTable> _table;
318 * Animation to rotate texture mappings around a center point.
320 * This animation rotates to a specific position.
322 class SGTexRotateAnimation : public SGAnimation
325 SGTexRotateAnimation( SGPropertyNode *prop_root, SGPropertyNode_ptr props );
326 virtual ~SGTexRotateAnimation ();
327 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
329 SGPropertyNode_ptr _prop;
332 SGSharedPtr<SGInterpTable> _table;
337 double _position_deg;
340 SGSharedPtr<SGCondition> _condition;
341 osg::ref_ptr<osg::TexMat> _texMat;
346 * Animation to slide texture mappings along an axis.
348 class SGTexTranslateAnimation : public SGAnimation
351 SGTexTranslateAnimation( SGPropertyNode *prop_root,
352 SGPropertyNode_ptr props );
353 virtual ~SGTexTranslateAnimation ();
354 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
356 SGPropertyNode_ptr _prop;
361 SGSharedPtr<SGInterpTable> _table;
368 SGSharedPtr<SGCondition> _condition;
369 osg::ref_ptr<osg::TexMat> _texMat;
375 * Classes for handling multiple types of Texture translations on one object
378 class SGTexMultipleAnimation : public SGAnimation
381 SGTexMultipleAnimation( SGPropertyNode *prop_root,
382 SGPropertyNode_ptr props );
383 virtual ~SGTexMultipleAnimation ();
384 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
389 SGPropertyNode_ptr prop;
390 int subtype; // 0=translation, 1=rotation
395 SGSharedPtr<SGInterpTable> table;
404 SGPropertyNode_ptr _prop;
405 TexTransform* _transform;
407 osg::ref_ptr<osg::TexMat> _texMat;
412 * An "animation" to enable the alpha test
414 class SGAlphaTestAnimation : public SGAnimation
417 SGAlphaTestAnimation(SGPropertyNode_ptr props);
418 virtual ~SGAlphaTestAnimation ();
426 * An "animation" to modify material properties
428 class SGMaterialAnimation : public SGAnimation
431 SGMaterialAnimation(SGPropertyNode *prop_root, SGPropertyNode_ptr props,
432 const SGPath &texpath);
433 virtual ~SGMaterialAnimation();
435 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
448 float red, green, blue;
451 SGPropertyNode_ptr red_prop;
452 SGPropertyNode_ptr green_prop;
453 SGPropertyNode_ptr blue_prop;
454 SGPropertyNode_ptr factor_prop;
455 SGPropertyNode_ptr offset_prop;
457 inline bool dirty() {
458 return red >= 0.0 || green >= 0.0 || blue >= 0.0;
461 return red_prop || green_prop || blue_prop
462 || factor_prop || offset_prop;
464 inline bool operator!=(ColorSpec& a) {
465 return red != a.red || green != a.green || blue != a.blue
466 || factor != a.factor || offset != a.offset;
469 v[0] = clamp(red * factor + offset);
470 v[1] = clamp(green * factor + offset);
471 v[2] = clamp(blue * factor + offset);
475 inline float clamp(float val) {
476 return val < 0.0 ? 0.0 : val > 1.0 ? 1.0 : val;
485 SGPropertyNode_ptr value_prop;
486 SGPropertyNode_ptr factor_prop;
487 SGPropertyNode_ptr offset_prop;
488 inline bool dirty() { return value >= 0.0; }
489 inline bool live() { return value_prop || factor_prop || offset_prop; }
490 inline bool operator!=(PropSpec& a) {
491 return value != a.value || factor != a.factor || offset != a.offset;
494 SGSharedPtr<SGCondition> _condition;
495 bool _last_condition;
496 SGPropertyNode_ptr _prop_root;
498 SGPath _texture_base;
503 unsigned _static_update;
511 float _thresh; // alpha_clamp (see man glAlphaFunc)
514 SGPropertyNode_ptr _shi_prop;
515 SGPropertyNode_ptr _thresh_prop;
516 SGPropertyNode_ptr _tex_prop;
517 std::vector<osg::ref_ptr<osg::Material> > _materialList;
518 osg::ref_ptr<osg::AlphaFunc> _alphaFunc;
519 osg::ref_ptr<osg::Texture2D> _texture2D;
521 void cloneMaterials(osg::Group *b);
522 void setMaterialBranch(osg::Group *b);
523 void initColorGroup(SGPropertyNode_ptr, ColorSpec *, int flag);
524 void updateColorGroup(ColorSpec *, int flag);
525 inline float clamp(float val, float min = 0.0, float max = 1.0) {
526 return val < min ? min : val > max ? max : val;
528 const char *path(const char *rel) {
529 return (_tmpstr = _prop_base + rel).c_str();
535 * An "animation" that compute a scale according to
536 * the angle between an axis and the view direction
538 class SGFlashAnimation : public SGAnimation
541 SGFlashAnimation(SGPropertyNode_ptr props);
542 virtual ~SGFlashAnimation ();
547 * An animation that compute a scale according to
548 * the distance from a point and the viewer
550 class SGDistScaleAnimation : public SGAnimation
553 SGDistScaleAnimation(SGPropertyNode_ptr props);
554 virtual ~SGDistScaleAnimation ();
558 * An animation to tell wich objects don't cast shadows.
560 class SGShadowAnimation : public SGAnimation
563 SGShadowAnimation ( SGPropertyNode *prop_root,
564 SGPropertyNode_ptr props );
565 virtual ~SGShadowAnimation ();
566 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
567 bool get_condition_value(void);
569 SGSharedPtr<SGCondition> _condition;
570 bool _condition_value;
574 + * An "animation" that replace fixed opengl pipeline by shaders
576 class SGShaderAnimation : public SGAnimation
579 SGShaderAnimation ( SGPropertyNode *prop_root,
580 SGPropertyNode_ptr props );
581 virtual ~SGShaderAnimation ();
583 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
584 bool get_condition_value(void);
586 SGSharedPtr<SGCondition> _condition;
587 bool _condition_value;
590 osg::Vec4 _param_color;
594 SGPropertyNode_ptr _factor_prop;
597 SGPropertyNode_ptr _speed_prop;
598 osg::ref_ptr<osg::Texture2D> _effectTexture;
599 unsigned char *_textureData;
600 GLint _texWidth, _texHeight;
605 #endif // _SG_ANIMATION_HXX