]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/animation.hxx
048d991f513a9ce31b0e341b48d673f59d5c9511
[simgear.git] / simgear / scene / model / animation.hxx
1
2 // animation.hxx - classes to manage model animation.
3 // Written by David Megginson, started 2002.
4 //
5 // This file is in the Public Domain, and comes with no warranty.
6
7 #ifndef _SG_ANIMATION_HXX
8 #define _SG_ANIMATION_HXX 1
9
10 #ifndef __cplusplus
11 # error This library requires C++
12 #endif
13
14 #include <osg/ref_ptr>
15 #include <osg/Group>
16 #include <osg/Node>
17 #include <osg/NodeVisitor>
18 #include <osg/Texture2D>
19 #include <osgDB/ReaderWriter>
20
21 #include <simgear/scene/util/SGNodeMasks.hxx>
22 #include <simgear/props/props.hxx>
23 #include <simgear/props/condition.hxx>
24 #include <simgear/structure/SGExpression.hxx>
25
26 // Has anyone done anything *really* stupid, like making min and max macros?
27 #ifdef min
28 #undef min
29 #endif
30 #ifdef max
31 #undef max
32 #endif
33
34 SGExpressiond*
35 read_value(const SGPropertyNode* configNode, SGPropertyNode* modelRoot,
36            const char* unit, double defMin, double defMax);
37
38 SGVec3d readTranslateAxis(const SGPropertyNode* configNode);
39
40 /**
41  * Base class for animation installers
42  */
43 class SGAnimation : protected osg::NodeVisitor {
44 public:
45   SGAnimation(const SGPropertyNode* configNode, SGPropertyNode* modelRoot);
46   virtual ~SGAnimation();
47
48   static bool animate(osg::Node* node, const SGPropertyNode* configNode,
49                       SGPropertyNode* modelRoot,
50                       const osgDB::Options* options,
51                       const std::string &path, int i);
52
53 protected:
54   void apply(osg::Node* node);
55
56   virtual void install(osg::Node& node);
57   virtual osg::Group* createAnimationGroup(osg::Group& parent);
58
59   virtual void apply(osg::Group& group);
60
61   /**
62    * Read a 3d vector from the configuration property node.
63    *
64    * Reads values from @a name/[xyz]@a prefix and defaults to the according
65    * value of @a def for each value which is not set.
66    *
67    * @param name    Name of the root node containing all coordinates
68    * @param suffix  Suffix appended to each child node (x,y,z)
69    * @param def     Vector containing default values
70    */
71   SGVec3d readVec3( const std::string& name,
72                     const std::string& suffix = "",
73                     const SGVec3d& def = SGVec3d::zeros() ) const;
74   void readRotationCenterAndAxis(SGVec3d& center, SGVec3d& axis) const;
75
76   SGExpressiond* readOffsetValue(const char* tag_name) const;
77
78   void removeMode(osg::Node& node, osg::StateAttribute::GLMode mode);
79   void removeAttribute(osg::Node& node, osg::StateAttribute::Type type);
80   void removeTextureMode(osg::Node& node, unsigned unit,
81                          osg::StateAttribute::GLMode mode);
82   void removeTextureAttribute(osg::Node& node, unsigned unit,
83                               osg::StateAttribute::Type type);
84   void setRenderBinToInherit(osg::Node& node);
85   void cloneDrawables(osg::Node& node);
86
87   std::string getType() const
88   { return std::string(_configNode->getStringValue("type", "")); }
89
90   const SGPropertyNode* getConfig() const
91   { return _configNode; }
92   SGPropertyNode* getModelRoot() const
93   { return _modelRoot; }
94
95   const SGCondition* getCondition() const;
96
97   std::list<std::string> _objectNames;
98 private:
99   void installInGroup(const std::string& name, osg::Group& group,
100                       osg::ref_ptr<osg::Group>& animationGroup);
101
102   class RemoveModeVisitor;
103   class RemoveAttributeVisitor;
104   class RemoveTextureModeVisitor;
105   class RemoveTextureAttributeVisitor;
106   class BinToInheritVisitor;
107   class DrawableCloneVisitor;
108
109   bool _found;
110   std::string _name;
111   SGSharedPtr<SGPropertyNode const> _configNode;
112   SGPropertyNode* _modelRoot;
113   
114   std::list<osg::ref_ptr<osg::Node> > _installedAnimations;
115   bool _enableHOT;
116 };
117
118
119 //////////////////////////////////////////////////////////////////////
120 // Null animation installer
121 //////////////////////////////////////////////////////////////////////
122
123 class SGGroupAnimation : public SGAnimation {
124 public:
125   SGGroupAnimation(const SGPropertyNode*, SGPropertyNode*);
126   virtual osg::Group* createAnimationGroup(osg::Group& parent);
127 };
128
129
130 //////////////////////////////////////////////////////////////////////
131 // Translate animation installer
132 //////////////////////////////////////////////////////////////////////
133
134 class SGTranslateAnimation : public SGAnimation {
135 public:
136   SGTranslateAnimation(const SGPropertyNode* configNode,
137                        SGPropertyNode* modelRoot);
138   virtual osg::Group* createAnimationGroup(osg::Group& parent);
139 private:
140   class UpdateCallback;
141   SGSharedPtr<const SGCondition> _condition;
142   SGSharedPtr<const SGExpressiond> _animationValue;
143   SGVec3d _axis;
144   double _initialValue;
145 };
146
147
148 //////////////////////////////////////////////////////////////////////
149 // Rotate/Spin animation installer
150 //////////////////////////////////////////////////////////////////////
151
152 class SGRotateAnimation : public SGAnimation {
153 public:
154   SGRotateAnimation(const SGPropertyNode* configNode,
155                     SGPropertyNode* modelRoot);
156   virtual osg::Group* createAnimationGroup(osg::Group& parent);
157 private:
158   SGSharedPtr<const SGCondition> _condition;
159   SGSharedPtr<const SGExpressiond> _animationValue;
160   SGVec3d _axis;
161   SGVec3d _center;
162   double _initialValue;
163   bool _isSpin;
164 };
165
166
167 //////////////////////////////////////////////////////////////////////
168 // Scale animation installer
169 //////////////////////////////////////////////////////////////////////
170
171 class SGScaleAnimation : public SGAnimation {
172 public:
173   SGScaleAnimation(const SGPropertyNode* configNode,
174                    SGPropertyNode* modelRoot);
175   virtual osg::Group* createAnimationGroup(osg::Group& parent);
176 private:
177   class UpdateCallback;
178   SGSharedPtr<const SGCondition> _condition;
179   SGSharedPtr<const SGExpressiond> _animationValue[3];
180   SGVec3d _initialValue;
181   SGVec3d _center;
182 };
183
184
185 //////////////////////////////////////////////////////////////////////
186 // dist scale animation installer
187 //////////////////////////////////////////////////////////////////////
188
189 class SGDistScaleAnimation : public SGAnimation {
190 public:
191   SGDistScaleAnimation(const SGPropertyNode* configNode,
192                        SGPropertyNode* modelRoot);
193   virtual osg::Group* createAnimationGroup(osg::Group& parent);
194   class Transform;
195 };
196
197
198 //////////////////////////////////////////////////////////////////////
199 // dist scale animation installer
200 //////////////////////////////////////////////////////////////////////
201
202 class SGFlashAnimation : public SGAnimation {
203 public:
204   SGFlashAnimation(const SGPropertyNode* configNode,
205                    SGPropertyNode* modelRoot);
206   virtual osg::Group* createAnimationGroup(osg::Group& parent);
207 public:
208   class Transform;
209 };
210
211
212 //////////////////////////////////////////////////////////////////////
213 // dist scale animation installer
214 //////////////////////////////////////////////////////////////////////
215
216 class SGBillboardAnimation : public SGAnimation {
217 public:
218   SGBillboardAnimation(const SGPropertyNode* configNode,
219                        SGPropertyNode* modelRoot);
220   virtual osg::Group* createAnimationGroup(osg::Group& parent);
221   class Transform;
222 };
223
224
225 //////////////////////////////////////////////////////////////////////
226 // Range animation installer
227 //////////////////////////////////////////////////////////////////////
228
229 class SGRangeAnimation : public SGAnimation {
230 public:
231   SGRangeAnimation(const SGPropertyNode* configNode,
232                    SGPropertyNode* modelRoot);
233   virtual osg::Group* createAnimationGroup(osg::Group& parent);
234 private:
235   class UpdateCallback;
236   SGSharedPtr<const SGCondition> _condition;
237   SGSharedPtr<const SGExpressiond> _minAnimationValue;
238   SGSharedPtr<const SGExpressiond> _maxAnimationValue;
239   SGVec2d _initialValue;
240 };
241
242
243 //////////////////////////////////////////////////////////////////////
244 // Select animation installer
245 //////////////////////////////////////////////////////////////////////
246
247 class SGSelectAnimation : public SGAnimation {
248 public:
249   SGSelectAnimation(const SGPropertyNode* configNode,
250                     SGPropertyNode* modelRoot);
251   virtual osg::Group* createAnimationGroup(osg::Group& parent);
252 };
253
254
255 //////////////////////////////////////////////////////////////////////
256 // Alpha test animation installer
257 //////////////////////////////////////////////////////////////////////
258
259 class SGAlphaTestAnimation : public SGAnimation {
260 public:
261   SGAlphaTestAnimation(const SGPropertyNode* configNode,
262                        SGPropertyNode* modelRoot);
263   virtual void install(osg::Node& node);
264 };
265
266
267 //////////////////////////////////////////////////////////////////////
268 // Blend animation installer
269 //////////////////////////////////////////////////////////////////////
270
271 class SGBlendAnimation : public SGAnimation {
272 public:
273   SGBlendAnimation(const SGPropertyNode* configNode,
274                    SGPropertyNode* modelRoot);
275   virtual osg::Group* createAnimationGroup(osg::Group& parent);
276   virtual void install(osg::Node& node);
277 private:
278   class BlendVisitor;
279   class UpdateCallback;
280   SGSharedPtr<SGExpressiond> _animationValue;
281 };
282
283
284 //////////////////////////////////////////////////////////////////////
285 // Timed animation installer
286 //////////////////////////////////////////////////////////////////////
287
288 class SGTimedAnimation : public SGAnimation {
289 public:
290   SGTimedAnimation(const SGPropertyNode* configNode,
291                    SGPropertyNode* modelRoot);
292   virtual osg::Group* createAnimationGroup(osg::Group& parent);
293 private:
294   class UpdateCallback;
295 };
296
297
298 //////////////////////////////////////////////////////////////////////
299 // Shadow animation installer
300 //////////////////////////////////////////////////////////////////////
301
302 class SGShadowAnimation : public SGAnimation {
303 public:
304   SGShadowAnimation(const SGPropertyNode* configNode,
305                     SGPropertyNode* modelRoot);
306   virtual osg::Group* createAnimationGroup(osg::Group& parent);
307 private:
308   class UpdateCallback;
309 };
310
311
312 //////////////////////////////////////////////////////////////////////
313 // TextureTransform animation
314 //////////////////////////////////////////////////////////////////////
315
316 class SGTexTransformAnimation : public SGAnimation {
317 public:
318   SGTexTransformAnimation(const SGPropertyNode* configNode,
319                           SGPropertyNode* modelRoot);
320   virtual osg::Group* createAnimationGroup(osg::Group& parent);
321 private:
322   class Transform;
323   class Translation;
324   class Rotation;
325   class UpdateCallback;
326   void appendTexTranslate(const SGPropertyNode* config,
327                           UpdateCallback* updateCallback);
328   void appendTexRotate(const SGPropertyNode* config,
329                        UpdateCallback* updateCallback);
330 };
331
332
333 //////////////////////////////////////////////////////////////////////
334 // Shader animation
335 //////////////////////////////////////////////////////////////////////
336
337 class SGShaderAnimation : public SGAnimation {
338 public:
339   SGShaderAnimation(const SGPropertyNode* configNode,
340                     SGPropertyNode* modelRoot,
341                     const osgDB::Options* options);
342   virtual osg::Group* createAnimationGroup(osg::Group& parent);
343 private:
344   class UpdateCallback;
345   osg::ref_ptr<osg::Texture2D> _effect_texture;
346 };
347
348 //////////////////////////////////////////////////////////////////////
349 // Light animation
350 //////////////////////////////////////////////////////////////////////
351
352 class SGLightAnimation : public SGAnimation {
353 public:
354   SGLightAnimation(const SGPropertyNode* configNode,
355                    SGPropertyNode* modelRoot,
356                    const osgDB::Options* options,
357                    const std::string &path, int i);
358   virtual osg::Group* createAnimationGroup(osg::Group& parent);
359   virtual void install(osg::Node& node);
360 private:
361   std::string _light_type;
362   SGVec3d _position;
363   SGVec3d _direction;
364   SGVec4d _ambient;
365   SGVec4d _diffuse;
366   SGVec4d _specular;
367   SGVec3d _attenuation;
368   double _exponent;
369   double _cutoff;
370   double _near;
371   double _far;
372   std::string _key;
373   class UpdateCallback;
374   friend class UpdateCallback;
375   SGSharedPtr<SGExpressiond> _animationValue;
376   osg::ref_ptr<const osgDB::Options> _options;
377 };
378
379 #endif // _SG_ANIMATION_HXX