]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/animation.hxx
Extend locked-track animation to support a slave element.
[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 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   void removeMode(osg::Node& node, osg::StateAttribute::GLMode mode);
77   void removeAttribute(osg::Node& node, osg::StateAttribute::Type type);
78   void removeTextureMode(osg::Node& node, unsigned unit,
79                          osg::StateAttribute::GLMode mode);
80   void removeTextureAttribute(osg::Node& node, unsigned unit,
81                               osg::StateAttribute::Type type);
82   void setRenderBinToInherit(osg::Node& node);
83   void cloneDrawables(osg::Node& node);
84
85   std::string getType() const
86   { return std::string(_configNode->getStringValue("type", "")); }
87
88   const SGPropertyNode* getConfig() const
89   { return _configNode; }
90   SGPropertyNode* getModelRoot() const
91   { return _modelRoot; }
92
93   const SGCondition* getCondition() const;
94
95   std::list<std::string> _objectNames;
96 private:
97   void installInGroup(const std::string& name, osg::Group& group,
98                       osg::ref_ptr<osg::Group>& animationGroup);
99
100   class RemoveModeVisitor;
101   class RemoveAttributeVisitor;
102   class RemoveTextureModeVisitor;
103   class RemoveTextureAttributeVisitor;
104   class BinToInheritVisitor;
105   class DrawableCloneVisitor;
106
107   bool _found;
108   std::string _name;
109   SGSharedPtr<SGPropertyNode const> _configNode;
110   SGPropertyNode* _modelRoot;
111   
112   std::list<osg::ref_ptr<osg::Node> > _installedAnimations;
113   bool _enableHOT;
114 };
115
116
117 //////////////////////////////////////////////////////////////////////
118 // Null animation installer
119 //////////////////////////////////////////////////////////////////////
120
121 class SGGroupAnimation : public SGAnimation {
122 public:
123   SGGroupAnimation(const SGPropertyNode*, SGPropertyNode*);
124   virtual osg::Group* createAnimationGroup(osg::Group& parent);
125 };
126
127
128 //////////////////////////////////////////////////////////////////////
129 // Translate animation installer
130 //////////////////////////////////////////////////////////////////////
131
132 class SGTranslateAnimation : public SGAnimation {
133 public:
134   SGTranslateAnimation(const SGPropertyNode* configNode,
135                        SGPropertyNode* modelRoot);
136   virtual osg::Group* createAnimationGroup(osg::Group& parent);
137 private:
138   class UpdateCallback;
139   SGSharedPtr<const SGCondition> _condition;
140   SGSharedPtr<const SGExpressiond> _animationValue;
141   SGVec3d _axis;
142   double _initialValue;
143 };
144
145
146 //////////////////////////////////////////////////////////////////////
147 // Rotate/Spin animation installer
148 //////////////////////////////////////////////////////////////////////
149
150 class SGRotateAnimation : public SGAnimation {
151 public:
152   SGRotateAnimation(const SGPropertyNode* configNode,
153                     SGPropertyNode* modelRoot);
154   virtual osg::Group* createAnimationGroup(osg::Group& parent);
155 private:
156   SGSharedPtr<const SGCondition> _condition;
157   SGSharedPtr<const SGExpressiond> _animationValue;
158   SGVec3d _axis;
159   SGVec3d _center;
160   double _initialValue;
161   bool _isSpin;
162 };
163
164
165 //////////////////////////////////////////////////////////////////////
166 // Scale animation installer
167 //////////////////////////////////////////////////////////////////////
168
169 class SGScaleAnimation : public SGAnimation {
170 public:
171   SGScaleAnimation(const SGPropertyNode* configNode,
172                    SGPropertyNode* modelRoot);
173   virtual osg::Group* createAnimationGroup(osg::Group& parent);
174 private:
175   class UpdateCallback;
176   SGSharedPtr<const SGCondition> _condition;
177   SGSharedPtr<const SGExpressiond> _animationValue[3];
178   SGVec3d _initialValue;
179   SGVec3d _center;
180 };
181
182
183 //////////////////////////////////////////////////////////////////////
184 // dist scale animation installer
185 //////////////////////////////////////////////////////////////////////
186
187 class SGDistScaleAnimation : public SGAnimation {
188 public:
189   SGDistScaleAnimation(const SGPropertyNode* configNode,
190                        SGPropertyNode* modelRoot);
191   virtual osg::Group* createAnimationGroup(osg::Group& parent);
192   class Transform;
193 };
194
195
196 //////////////////////////////////////////////////////////////////////
197 // dist scale animation installer
198 //////////////////////////////////////////////////////////////////////
199
200 class SGFlashAnimation : public SGAnimation {
201 public:
202   SGFlashAnimation(const SGPropertyNode* configNode,
203                    SGPropertyNode* modelRoot);
204   virtual osg::Group* createAnimationGroup(osg::Group& parent);
205 public:
206   class Transform;
207 };
208
209
210 //////////////////////////////////////////////////////////////////////
211 // dist scale animation installer
212 //////////////////////////////////////////////////////////////////////
213
214 class SGBillboardAnimation : public SGAnimation {
215 public:
216   SGBillboardAnimation(const SGPropertyNode* configNode,
217                        SGPropertyNode* modelRoot);
218   virtual osg::Group* createAnimationGroup(osg::Group& parent);
219   class Transform;
220 };
221
222
223 //////////////////////////////////////////////////////////////////////
224 // Range animation installer
225 //////////////////////////////////////////////////////////////////////
226
227 class SGRangeAnimation : public SGAnimation {
228 public:
229   SGRangeAnimation(const SGPropertyNode* configNode,
230                    SGPropertyNode* modelRoot);
231   virtual osg::Group* createAnimationGroup(osg::Group& parent);
232 private:
233   class UpdateCallback;
234   SGSharedPtr<const SGCondition> _condition;
235   SGSharedPtr<const SGExpressiond> _minAnimationValue;
236   SGSharedPtr<const SGExpressiond> _maxAnimationValue;
237   SGVec2d _initialValue;
238 };
239
240
241 //////////////////////////////////////////////////////////////////////
242 // Select animation installer
243 //////////////////////////////////////////////////////////////////////
244
245 class SGSelectAnimation : public SGAnimation {
246 public:
247   SGSelectAnimation(const SGPropertyNode* configNode,
248                     SGPropertyNode* modelRoot);
249   virtual osg::Group* createAnimationGroup(osg::Group& parent);
250 };
251
252
253 //////////////////////////////////////////////////////////////////////
254 // Alpha test animation installer
255 //////////////////////////////////////////////////////////////////////
256
257 class SGAlphaTestAnimation : public SGAnimation {
258 public:
259   SGAlphaTestAnimation(const SGPropertyNode* configNode,
260                        SGPropertyNode* modelRoot);
261   virtual void install(osg::Node& node);
262 };
263
264
265 //////////////////////////////////////////////////////////////////////
266 // Blend animation installer
267 //////////////////////////////////////////////////////////////////////
268
269 class SGBlendAnimation : public SGAnimation {
270 public:
271   SGBlendAnimation(const SGPropertyNode* configNode,
272                    SGPropertyNode* modelRoot);
273   virtual osg::Group* createAnimationGroup(osg::Group& parent);
274   virtual void install(osg::Node& node);
275 private:
276   class BlendVisitor;
277   class UpdateCallback;
278   SGSharedPtr<SGExpressiond> _animationValue;
279 };
280
281
282 //////////////////////////////////////////////////////////////////////
283 // Timed animation installer
284 //////////////////////////////////////////////////////////////////////
285
286 class SGTimedAnimation : public SGAnimation {
287 public:
288   SGTimedAnimation(const SGPropertyNode* configNode,
289                    SGPropertyNode* modelRoot);
290   virtual osg::Group* createAnimationGroup(osg::Group& parent);
291 private:
292   class UpdateCallback;
293 };
294
295
296 //////////////////////////////////////////////////////////////////////
297 // Shadow animation installer
298 //////////////////////////////////////////////////////////////////////
299
300 class SGShadowAnimation : public SGAnimation {
301 public:
302   SGShadowAnimation(const SGPropertyNode* configNode,
303                     SGPropertyNode* modelRoot);
304   virtual osg::Group* createAnimationGroup(osg::Group& parent);
305 private:
306   class UpdateCallback;
307 };
308
309
310 //////////////////////////////////////////////////////////////////////
311 // TextureTransform animation
312 //////////////////////////////////////////////////////////////////////
313
314 class SGTexTransformAnimation : public SGAnimation {
315 public:
316   SGTexTransformAnimation(const SGPropertyNode* configNode,
317                           SGPropertyNode* modelRoot);
318   virtual osg::Group* createAnimationGroup(osg::Group& parent);
319 private:
320   class Transform;
321   class Translation;
322   class Rotation;
323   class UpdateCallback;
324   void appendTexTranslate(const SGPropertyNode* config,
325                           UpdateCallback* updateCallback);
326   void appendTexRotate(const SGPropertyNode* config,
327                        UpdateCallback* updateCallback);
328 };
329
330
331 //////////////////////////////////////////////////////////////////////
332 // Shader animation
333 //////////////////////////////////////////////////////////////////////
334
335 class SGShaderAnimation : public SGAnimation {
336 public:
337   SGShaderAnimation(const SGPropertyNode* configNode,
338                     SGPropertyNode* modelRoot,
339                     const osgDB::Options* options);
340   virtual osg::Group* createAnimationGroup(osg::Group& parent);
341 private:
342   class UpdateCallback;
343   osg::ref_ptr<osg::Texture2D> _effect_texture;
344 };
345
346 //////////////////////////////////////////////////////////////////////
347 // Light animation
348 //////////////////////////////////////////////////////////////////////
349
350 class SGLightAnimation : public SGAnimation {
351 public:
352   SGLightAnimation(const SGPropertyNode* configNode,
353                    SGPropertyNode* modelRoot,
354                    const osgDB::Options* options,
355                    const string &path, int i);
356   virtual osg::Group* createAnimationGroup(osg::Group& parent);
357   virtual void install(osg::Node& node);
358 private:
359   string _light_type;
360   SGVec3d _position;
361   SGVec3d _direction;
362   SGVec4d _ambient;
363   SGVec4d _diffuse;
364   SGVec4d _specular;
365   SGVec3d _attenuation;
366   double _exponent;
367   double _cutoff;
368   double _near;
369   double _far;
370   string _key;
371   class UpdateCallback;
372   friend class UpdateCallback;
373   SGSharedPtr<SGExpressiond> _animationValue;
374   osg::ref_ptr<const osgDB::Options> _options;
375 };
376
377 #endif // _SG_ANIMATION_HXX