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