vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Animator.h
Go to the documentation of this file.
1 #ifndef ANIMATOR_H
2 #define ANIMATOR_H
3 
4 #include <vrecko/Ability.h>
5 #include <vrecko/IOPins.h>
6 #include <vrecko/Timer.h>
7 #include <helpers/DynamicArray.h>
8 
9 using namespace vrecko;
10 
11 namespace APObjectUtils
12 {
13 
14 
15 #ifndef ANIMATOR_IMP_EXP
16  #ifdef AP_OBJECTUTILS_EXPORTS
17  #define ANIMATOR_IMP_EXP __declspec(dllexport)
18  #else
19  #define ANIMATOR_IMP_EXP __declspec(dllimport)
20  #endif
21 #endif
22 
23 
25  {
26  public:
27 
29  public:
31 
32  osg::Vec3 translation;
33  osg::Quat rotation;
34  osg::Vec3 scale;
35 
36  float fAlpha;
37 
39  };
40 
41  static Animator* getAnimatorOnObject(vrecko::EnvironmentObject *object);
42  // Returns an animator if there is one.
43 
44  static Animator* addAnimatorToObject(vrecko::EnvironmentObject *object, bool bReuseOldIfPossible = true);
45  // Creates an Animator ability and assignes it to the object.
46  // If the object already had an ability of this type, it will be returned
47  // and no new ability will be created.
48 
49 
50  // Which properties are being animated?
52  AnimSRT = 1, // Scaling + Rotation + Translation
53  AnimAlpha = 2, // Transparency
54  AnimNodeMask = 4 // Node mask
55  // (The mask is interpolated: The masks of the nearest animation points are ORed)
56  };
57  enum LoopType {
58  NoLoop, // No looping
59  Simple, // Simple looping
60  BiDi // Bi-directional looping (ping-pong)
61  };
63  Linear, // linear interpolation
64  Sine // sine interpolation with soft start and finish
65  };
66 
67  Animator();
68  virtual ~Animator();
69 
70  virtual void preInitialize();
71  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParametersNode);
72  virtual void update();
73 // virtual void processEvent(const std::string &strInputName, void *pValue);
74  virtual void clean();
75 
76  bool removeAnimation();
77 
78  bool isAnimating() { return bAnimating; };
79 
80  bool stopAnimation();
81  bool startAnimation();
82 
83  bool animateToPoint(int pointIndex);
84 
85  /* Stops the animation and sets to position to the specified one.
86  Allowed values are from 0.0f to "animationPointsNumber - 1" where 0.0f = start,
87  "animationPointsNumber - 1" = end */
88  bool resetToPos(float fPos);
89 
90 
91  bool setAnimationPointsNumber(unsigned int number);
92  inline unsigned int getAnimationPointsNumber() { return points.dwNum; }
93 
94  bool setEnabledAnimations(unsigned long dwAnimations);
95  // the [dwAnimations] is a combination of Animator::AnimationTypes
96  inline unsigned long getEnabledAnimations() { return enabledAnimations; }
97  // the [dwAnimations] is a combination of Animator::AnimationTypes
98 
99  bool setTranslation(int pointIndex, const osg::Vec3 &trans);
100  osg::Vec3 &getTranslation(int pointIndex);
101 
102  bool setRotation(int pointIndex, const osg::Quat &rot);
103  inline osg::Quat &getRotation(int pointIndex);
104 
105  bool setScale(int pointIndex, const osg::Vec3 &scale);
106  osg::Vec3 &getScale(int pointIndex);
107 
108  bool setAlpha(int pointIndex, float alpha); // 0.0 to 1.0
109  float getAlpha(int pointIndex);
110 
111  bool setNodeMask(int pointIndex, osg::Node::NodeMask nodeMask);
112  osg::Node::NodeMask getNodeMask(int pointIndex);
113 
114  bool setTimePerLoop(float timeInS) { timePerLoop = timeInS; return true; };
115  inline float getTimePerLoop() { return timePerLoop; }
116 
117  bool setLoopType(LoopType newType) { loopType = newType; return true; };
118  inline LoopType getLoopType() { return loopType; };
119 
120  bool setInterpolationType(InterpolationType newType) { interpolationType = newType; return true; };
121  inline InterpolationType getInterpolationType() { return interpolationType; };
122 
123  bool setTransformationFromMatrix(int startPointIndex, int endPointIndex, const osg::Matrix &matrix);
124 
125  /* Returns the tranformation matrix belonging to the specified _actual_ position.
126  Allowed values are from 0.0f to "animationPointsNumber - 1" where 0.0f = start,
127  "animationPointsNumber - 1" = end */
128  bool getAnimMatrix(float fActPos, osg::Matrix &outMatrix);
129 
130  protected:
132 
133  unsigned int enabledAnimations;
136 
138 
139  osg::ref_ptr<osg::StateSet> stateSet;
140 
141 /* osg::Vec3 translationFrom, translationTo;
142  osg::Vec3 rotationAxisStart, rotationAxisDirFrom, rotationAxisDirTo;
143  float rotationAngleFrom, rotationAngleTo;
144  osg::Matrix cachedRotationTransformation1, cachedRotationTransformation2;
145  // Cached transformations to transform TO and FROM rotation axis start.
146 
147  osg::Vec3 scaleFrom, scaleTo;
148 // osg::Quat scaleOrientation;
149 */
150 
151  float timePerLoop; // time in seconds
152 
154  float fActTime; // MAIN CONTROL of the animation (0.0 to "animationPointsNumber - 1" for one loop)
155  int iActDir; // current direction of the animation (1 or -1)
156 
157  osg::Timer_t lastTimerTick;
158 
159  float convertActTimeToActPos();
160  // Returns a number (0.0 <= number <= (animationPointsNumber - 1))
161  // Includes any ease in or out, dumping or whatever will be available.
162  float convertActPosToNormPos(float fActPos, int *outPoint1, int *outPoint2);
163  // Returns normalized position (0.0 <= "normPos" <= 1.0) along with
164  // the boundary points (1 and 2)
165 
166  bool updateActTime();
167  bool updateAnimation(float fActPos);
168 
169  bool calcAnimMatrix(float fNormPos, int iPoint1, int iPoint2, osg::Matrix &outMatrix);
170  // 0.0f <= [fNormPos] <= 1.0
171 
172  DECLARE_INPUT(Trigger, MessageVoid);
173  DECLARE_INPUT(AnimateToPoint, MessageInt);
174 
175  DECLARE_OUTPUT(AnimationEnded, MessageBool);
176  };
177 
178 }
179 
180 
181 #endif