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