OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
animation.hpp
Go to the documentation of this file.
1 #ifndef GAME_RENDER_ANIMATION_H
2 #define GAME_RENDER_ANIMATION_H
3 
4 #include "../mwworld/ptr.hpp"
5 
7 
8 namespace ESM
9 {
10  struct Light;
11  struct MagicEffect;
12 }
13 
14 namespace Resource
15 {
16  class ResourceSystem;
17 }
18 
19 namespace NifOsg
20 {
21  class KeyframeHolder;
22  class KeyframeController;
23 }
24 
25 namespace SceneUtil
26 {
27  class LightSource;
28  class LightListCallback;
29  class Skeleton;
30 }
31 
32 namespace MWRender
33 {
34 
35 class ResetAccumRootCallback;
36 class RotateController;
37 class GlowUpdater;
38 
40 {
41 private:
42  float mTime;
43 public:
44  virtual float getValue(osg::NodeVisitor* nv);
45 
46  void addTime(float duration);
47  void resetTime(float time);
48  float getTime() const;
49 
51 };
52 
55 {
56 public:
57  PartHolder(osg::ref_ptr<osg::Node> node);
58 
59  ~PartHolder();
60 
62  void unlink();
63 
64  osg::ref_ptr<osg::Node> getNode()
65  {
66  return mNode;
67  }
68 
69 private:
70  osg::ref_ptr<osg::Node> mNode;
71 
72  void operator= (const PartHolder&);
73  PartHolder(const PartHolder&);
74 };
75 typedef boost::shared_ptr<PartHolder> PartHolderPtr;
76 
77 class Animation
78 {
79 public:
80  enum BoneGroup {
85  };
86 
87  enum BlendMask {
92 
94 
96  };
97  /* This is the number of *discrete* blend masks. */
98  static const size_t sNumBlendMasks = 4;
99 
102  {
104  AnimPriority(int priority)
105  {
106  for (unsigned int i=0; i<sNumBlendMasks; ++i)
107  mPriority[i] = priority;
108  }
109 
110  bool operator == (const AnimPriority& other) const
111  {
112  for (unsigned int i=0; i<sNumBlendMasks; ++i)
113  if (other.mPriority[i] != mPriority[i])
114  return false;
115  return true;
116  }
117 
119  {
120  return mPriority[n];
121  }
122 
123  const int& operator[] (BoneGroup n) const
124  {
125  return mPriority[n];
126  }
127 
128  bool contains(int priority) const
129  {
130  for (unsigned int i=0; i<sNumBlendMasks; ++i)
131  if (priority == mPriority[i])
132  return true;
133  return false;
134  }
135 
137  };
138 
140  {
141  public:
142  virtual void handleTextKey(const std::string &groupname, const std::multimap<float, std::string>::const_iterator &key,
143  const std::multimap<float, std::string>& map) = 0;
144  };
145 
146  void setTextKeyListener(TextKeyListener* listener);
147 
148 protected:
150  {
151  private:
152  boost::shared_ptr<float> mTimePtr;
153 
154  public:
155 
156  void setTimePtr(boost::shared_ptr<float> time)
157  { mTimePtr = time; }
158  boost::shared_ptr<float> getTimePtr() const
159  { return mTimePtr; }
160 
161  virtual float getValue(osg::NodeVisitor* nv);
162  };
163 
165  {
166  public:
167  virtual float getValue(osg::NodeVisitor *nv)
168  {
169  return 0.f;
170  }
171  };
172 
173  struct AnimSource;
174 
175  struct AnimState {
176  boost::shared_ptr<AnimSource> mSource;
177  float mStartTime;
180  float mStopTime;
181 
182  typedef boost::shared_ptr<float> TimePtr;
184  float mSpeedMult;
185 
186  bool mPlaying;
188  size_t mLoopCount;
189 
193 
195  mTime(new float), mSpeedMult(1.0f), mPlaying(false), mLoopingEnabled(true),
196  mLoopCount(0), mPriority(0), mBlendMask(0), mAutoDisable(true)
197  {
198  }
199  ~AnimState();
200 
201  float getTime() const
202  {
203  return *mTime;
204  }
205  void setTime(float time)
206  {
207  *mTime = time;
208  }
209 
210  bool shouldLoop() const
211  {
212  return getTime() >= mLoopStopTime && mLoopingEnabled && mLoopCount > 0;
213  }
214  };
215  typedef std::map<std::string,AnimState> AnimStateMap;
217 
218  typedef std::vector<boost::shared_ptr<AnimSource> > AnimSourceList;
220 
221  osg::ref_ptr<osg::Group> mInsert;
222 
223  osg::ref_ptr<osg::Group> mObjectRoot;
225 
226  // The node expected to accumulate movement during movement animations.
227  osg::ref_ptr<osg::Node> mAccumRoot;
228 
229  // The controller animating that node.
230  osg::ref_ptr<NifOsg::KeyframeController> mAccumCtrl;
231 
232  // Used to reset the position of the accumulation root every frame - the movement should be applied to the physics system
233  osg::ref_ptr<ResetAccumRootCallback> mResetAccumRootCallback;
234 
235  // Keep track of controllers that we added to our scene graph.
236  // We may need to rebuild these controllers when the active animation groups / sources change.
237  typedef std::multimap<osg::ref_ptr<osg::Node>, osg::ref_ptr<osg::NodeCallback> > ControllerMap;
239 
240  boost::shared_ptr<AnimationTime> mAnimationTimePtr[sNumBlendMasks];
241 
242  // Stored in all lowercase for a case-insensitive lookup
243  typedef std::map<std::string, osg::ref_ptr<osg::MatrixTransform> > NodeMap;
244  mutable NodeMap mNodeMap;
245  mutable bool mNodeMapCreated;
246 
248 
250 
251  osg::Vec3f mAccumulate;
252 
254  {
255  std::string mModelName; // Just here so we don't add the same effect twice
257  boost::shared_ptr<EffectAnimationTime> mAnimTime;
260  bool mLoop;
261  std::string mBoneName;
262  };
263 
264  std::vector<EffectParams> mEffects;
265 
267 
268  osg::ref_ptr<RotateController> mHeadController;
271 
272  osg::ref_ptr<SceneUtil::LightSource> mGlowLight;
273  osg::ref_ptr<GlowUpdater> mGlowUpdater;
274 
275  float mAlpha;
276 
277  osg::ref_ptr<SceneUtil::LightListCallback> mLightListCallback;
278 
279  const NodeMap& getNodeMap() const;
280 
281  /* Sets the appropriate animations on the bone groups based on priority.
282  */
283  void resetActiveGroups();
284 
285  size_t detectBlendMask(const osg::Node* node) const;
286 
287  /* Updates the position of the accum root node for the given time, and
288  * returns the wanted movement vector from the previous time. */
289  void updatePosition(float oldtime, float newtime, osg::Vec3f& position);
290 
291  /* Resets the animation to the time of the specified start marker, without
292  * moving anything, and set the end time to the specified stop marker. If
293  * the marker is not found, or if the markers are the same, it returns
294  * false.
295  */
296  bool reset(AnimState &state, const std::multimap<float, std::string> &keys,
297  const std::string &groupname, const std::string &start, const std::string &stop,
298  float startpoint, bool loopfallback);
299 
300  void handleTextKey(AnimState &state, const std::string &groupname, const std::multimap<float, std::string>::const_iterator &key,
301  const std::multimap<float, std::string>& map);
302 
311  void setObjectRoot(const std::string &model, bool forceskeleton, bool baseonly, bool isCreature);
312 
317  void addAnimSource(const std::string &model);
318 
320  void addExtraLight(osg::ref_ptr<osg::Group> parent, const ESM::Light *light);
321 
322  void clearAnimSources();
323 
328  virtual void addControllers();
329 
330  osg::Vec4f getEnchantmentColor(const MWWorld::ConstPtr& item) const;
331 
332  void addGlow(osg::ref_ptr<osg::Node> node, osg::Vec4f glowColor, float glowDuration = -1);
333 
335  virtual void setRenderBin();
336 
337 public:
338 
339  Animation(const MWWorld::Ptr &ptr, osg::ref_ptr<osg::Group> parentNode, Resource::ResourceSystem* resourceSystem);
340  virtual ~Animation();
341 
342  MWWorld::ConstPtr getPtr() const;
343 
346  void setActive(bool active);
347 
348  osg::Group* getOrCreateObjectRoot();
349 
350  osg::Group* getObjectRoot();
351 
362  void addEffect (const std::string& model, int effectId, bool loop = false, const std::string& bonename = "", std::string texture = "");
363  void removeEffect (int effectId);
364  void getLoopingEffects (std::vector<int>& out) const;
365 
366  // Add a spell casting glow to an object. From measuring video taken from the original engine,
367  // the glow seems to be about 1.5 seconds except for telekinesis, which is 1 second.
368  void addSpellCastGlow(const ESM::MagicEffect *effect, float glowDuration = 1.5);
369 
370  virtual void updatePtr(const MWWorld::Ptr &ptr);
371 
372  bool hasAnimation(const std::string &anim) const;
373 
374  // Specifies the axis' to accumulate on. Non-accumulated axis will just
375  // move visually, but not affect the actual movement. Each x/y/z value
376  // should be on the scale of 0 to 1.
377  void setAccumulation(const osg::Vec3f& accum);
378 
399  void play(const std::string &groupname, const AnimPriority& priority, int blendMask, bool autodisable,
400  float speedmult, const std::string &start, const std::string &stop,
401  float startpoint, size_t loops, bool loopfallback=false);
402 
405  void adjustSpeedMult (const std::string& groupname, float speedmult);
406 
408  bool isPlaying(const std::string &groupname) const;
409 
411  bool upperBodyReady() const;
412 
419  bool getInfo(const std::string &groupname, float *complete=NULL, float *speedmult=NULL) const;
420 
422  float getStartTime(const std::string &groupname) const;
423 
425  float getTextKeyTime(const std::string &textKey) const;
426 
428  float getCurrentTime(const std::string& groupname) const;
429 
430  size_t getCurrentLoopCount(const std::string& groupname) const;
431 
435  void disable(const std::string &groupname);
436 
438  float getVelocity(const std::string &groupname) const;
439 
440  virtual osg::Vec3f runAnimation(float duration);
441 
442  void setLoopingEnabled(const std::string &groupname, bool enabled);
443 
445  void updateEffects(float duration);
446 
449  const osg::Node* getNode(const std::string& name) const;
450 
451  virtual void showWeapons(bool showWeapon) {}
452  virtual void showCarriedLeft(bool show) {}
453  virtual void setWeaponGroup(const std::string& group) {}
454  virtual void setVampire(bool vampire) {}
456  void setAlpha(float alpha);
457  virtual void setPitchFactor(float factor) {}
458  virtual void attachArrow() {}
459  virtual void releaseArrow(float attackStrength) {}
460  virtual void enableHeadAnimation(bool enable) {}
461  // TODO: move outside of this class
464  virtual void setLightEffect(float effect);
465 
466  virtual void setHeadPitch(float pitchRadians);
467  virtual void setHeadYaw(float yawRadians);
468  virtual float getHeadPitch() const;
469  virtual float getHeadYaw() const;
470  virtual void setAccurateAiming(bool enabled) {}
471 
472 private:
473  Animation(const Animation&);
474  void operator=(Animation&);
475 };
476 
477 class ObjectAnimation : public Animation {
478 public:
479  ObjectAnimation(const MWWorld::Ptr& ptr, const std::string &model, Resource::ResourceSystem* resourceSystem, bool animated, bool allowLight);
480 };
481 
482 }
483 #endif
int mPriority[sNumBlendMasks]
Definition: animation.hpp:136
void addAnimSource(const std::string &model)
Definition: animation.cpp:499
void adjustSpeedMult(const std::string &groupname, float speedmult)
Definition: animation.cpp:878
AnimSourceList mAnimSources
Definition: animation.hpp:219
Animation(const MWWorld::Ptr &ptr, osg::ref_ptr< osg::Group > parentNode, Resource::ResourceSystem *resourceSystem)
Definition: animation.cpp:418
float mStartTime
Definition: animation.hpp:177
osg::ref_ptr< osg::Group > mObjectRoot
Definition: animation.hpp:223
AnimState()
Definition: animation.hpp:194
boost::shared_ptr< EffectAnimationTime > mAnimTime
Definition: animation.hpp:257
osg::Group * getObjectRoot()
Definition: animation.cpp:1162
Wrapper class that constructs and provides access to the most commonly used resource subsystems...
Definition: resourcesystem.hpp:24
boost::shared_ptr< float > getTimePtr() const
Definition: animation.hpp:158
bool getInfo(const std::string &groupname, float *complete=NULL, float *speedmult=NULL) const
Definition: animation.cpp:893
void setTimePtr(boost::shared_ptr< float > time)
Definition: animation.hpp:156
virtual void showWeapons(bool showWeapon)
Definition: animation.hpp:451
int mEffectId
Definition: animation.hpp:259
Handles the bone matrices for any number of child RigGeometries.
Definition: skeleton.hpp:36
void setAlpha(float alpha)
A value < 1 makes the animation translucent, 1.f = fully opaque.
Definition: animation.cpp:1427
Definition: animation.hpp:77
Detaches the node from its parent when the object goes out of scope.
Definition: animation.hpp:54
osg::ref_ptr< osg::Node > mNode
Definition: animation.hpp:70
static const size_t sNumBlendMasks
Definition: animation.hpp:98
bool isPlaying(const std::string &groupname) const
Definition: animation.cpp:885
Definition: animation.hpp:95
virtual float getHeadYaw() const
Definition: animation.cpp:1561
void addExtraLight(osg::ref_ptr< osg::Group > parent, const ESM::Light *light)
Definition: animation.cpp:1286
virtual float getValue(osg::NodeVisitor *nv)
Definition: animation.cpp:1575
boost::shared_ptr< AnimationTime > mAnimationTimePtr[sNumBlendMasks]
Definition: animation.hpp:240
PartHolderPtr mObjects
Definition: animation.hpp:256
virtual void setWeaponGroup(const std::string &group)
Definition: animation.hpp:453
int & operator[](BoneGroup n)
Definition: animation.hpp:118
float getTextKeyTime(const std::string &textKey) const
Get the absolute position in the animation track of the text key.
Definition: animation.cpp:597
virtual void setHeadPitch(float pitchRadians)
Definition: animation.cpp:1546
float mTime
Definition: animation.hpp:42
bool contains(int priority) const
Definition: animation.hpp:128
void addTime(float duration)
Definition: animation.cpp:1580
virtual void setHeadYaw(float yawRadians)
Definition: animation.cpp:1551
void disable(const std::string &groupname)
Definition: animation.cpp:933
Definition: animation.hpp:175
void setTextKeyListener(TextKeyListener *listener)
Definition: animation.cpp:799
void addGlow(osg::ref_ptr< osg::Node > node, osg::Vec4f glowColor, float glowDuration=-1)
Definition: animation.cpp:1218
Definition: animation.hpp:164
void clearAnimSources()
Definition: animation.cpp:559
osg::ref_ptr< SceneUtil::LightListCallback > mLightListCallback
Definition: animation.hpp:277
Definition: animation.hpp:84
Definition: animation.hpp:82
osg::ref_ptr< NifOsg::KeyframeController > mAccumCtrl
Definition: animation.hpp:230
bool mLoop
Definition: animation.hpp:260
virtual float getValue(osg::NodeVisitor *nv)
Definition: animation.hpp:167
int mBlendMask
Definition: animation.hpp:191
void setActive(bool active)
Definition: animation.cpp:449
void addSpellCastGlow(const ESM::MagicEffect *effect, float glowDuration=1.5)
Definition: animation.cpp:1196
void resetActiveGroups()
Definition: animation.cpp:815
Definition: animation.hpp:90
Definition: nifloader.hpp:40
float mLoopStartTime
Definition: animation.hpp:178
NodeMap mNodeMap
Definition: animation.hpp:244
void setTime(float time)
Definition: animation.hpp:205
virtual void handleTextKey(const std::string &groupname, const std::multimap< float, std::string >::const_iterator &key, const std::multimap< float, std::string > &map)=0
float mMaxControllerLength
Definition: animation.hpp:258
bool mNodeMapCreated
Definition: animation.hpp:245
std::multimap< osg::ref_ptr< osg::Node >, osg::ref_ptr< osg::NodeCallback > > ControllerMap
Definition: animation.hpp:237
void handleTextKey(AnimState &state, const std::string &groupname, const std::multimap< float, std::string >::const_iterator &key, const std::multimap< float, std::string > &map)
Definition: animation.cpp:613
osg::ref_ptr< osg::Node > mAccumRoot
Definition: animation.hpp:227
osg::Vec3f mAccumulate
Definition: animation.hpp:251
MWWorld::Ptr mPtr
Definition: animation.hpp:247
float mSpeedMult
Definition: animation.hpp:184
EffectAnimationTime()
Definition: animation.hpp:50
virtual void updatePtr(const MWWorld::Ptr &ptr)
Definition: animation.cpp:455
float mAlpha
Definition: animation.hpp:275
virtual void attachArrow()
Definition: animation.hpp:458
void removeEffect(int effectId)
Definition: animation.cpp:1358
AnimStateMap mStates
Definition: animation.hpp:216
void updateEffects(float duration)
This is typically called as part of runAnimation, but may be called manually if needed.
Definition: animation.cpp:1379
PartHolder(osg::ref_ptr< osg::Node > node)
Definition: animation.cpp:1627
const NodeMap & getNodeMap() const
Definition: animation.cpp:804
osg::ref_ptr< RotateController > mHeadController
Definition: animation.hpp:268
float mHeadPitchRadians
Definition: animation.hpp:270
virtual void setAccurateAiming(bool enabled)
Definition: animation.hpp:470
float mLoopStopTime
Definition: animation.hpp:179
TextKeyListener * mTextKeyListener
Definition: animation.hpp:266
void setObjectRoot(const std::string &model, bool forceskeleton, bool baseonly, bool isCreature)
Definition: animation.cpp:1095
bool reset(AnimState &state, const std::multimap< float, std::string > &keys, const std::string &groupname, const std::string &start, const std::string &stop, float startpoint, bool loopfallback)
Definition: animation.cpp:724
Definition: controller.hpp:195
osg::ref_ptr< osg::Group > mInsert
Definition: animation.hpp:221
bool mAutoDisable
Definition: animation.hpp:192
virtual float getHeadPitch() const
Definition: animation.cpp:1556
osg::Vec4f getEnchantmentColor(const MWWorld::ConstPtr &item) const
Definition: animation.cpp:1262
size_t detectBlendMask(const osg::Node *node) const
Definition: animation.cpp:468
bool operator==(const AnimPriority &other) const
Definition: animation.hpp:110
Definition: animation.hpp:477
osg::ref_ptr< GlowUpdater > mGlowUpdater
Definition: animation.hpp:273
~AnimState()
Definition: animation.cpp:1620
virtual void enableHeadAnimation(bool enable)
Definition: animation.hpp:460
BlendMask
Definition: animation.hpp:87
virtual void setRenderBin()
Set the render bin for this animation's object root. May be customized by subclasses.
Definition: animation.cpp:1461
bool upperBodyReady() const
Returns true if no important animations are currently playing on the upper body.
Definition: animation.cpp:1404
bool mLoopingEnabled
Definition: animation.hpp:187
virtual void showCarriedLeft(bool show)
Definition: animation.hpp:452
Pointer to a const LiveCellRef.
Definition: ptr.hpp:90
Definition: loadmgef.hpp:13
Definition: animation.hpp:88
void operator=(Animation &)
size_t mLoopCount
Definition: animation.hpp:188
void addEffect(const std::string &model, int effectId, bool loop=false, const std::string &bonename="", std::string texture="")
Add an effect mesh attached to a bone or the insert scene node.
Definition: animation.cpp:1302
~PartHolder()
Definition: animation.cpp:1632
float getTime() const
Definition: animation.hpp:201
boost::shared_ptr< AnimSource > mSource
Definition: animation.hpp:176
Definition: animation.hpp:89
AnimPriority mPriority
Definition: animation.hpp:190
float mHeadYawRadians
Definition: animation.hpp:269
virtual void setVampire(bool vampire)
Definition: animation.hpp:454
size_t getCurrentLoopCount(const std::string &groupname) const
Definition: animation.cpp:924
std::string mBoneName
Definition: animation.hpp:261
virtual osg::Vec3f runAnimation(float duration)
Definition: animation.cpp:1003
osg::ref_ptr< osg::Node > getNode()
Definition: animation.hpp:64
BoneGroup
Definition: animation.hpp:80
void setLoopingEnabled(const std::string &groupname, bool enabled)
Definition: animation.cpp:1088
bool hasAnimation(const std::string &anim) const
Definition: animation.cpp:571
float mStopTime
Definition: animation.hpp:180
osg::ref_ptr< SceneUtil::LightSource > mGlowLight
Definition: animation.hpp:272
std::vector< EffectParams > mEffects
Definition: animation.hpp:264
osg::ref_ptr< ResetAccumRootCallback > mResetAccumRootCallback
Definition: animation.hpp:233
void play(const std::string &groupname, const AnimPriority &priority, int blendMask, bool autodisable, float speedmult, const std::string &start, const std::string &stop, float startpoint, size_t loops, bool loopfallback=false)
Definition: animation.cpp:643
virtual void addControllers()
Definition: animation.cpp:1513
Definition: animation.hpp:91
ObjectAnimation(const MWWorld::Ptr &ptr, const std::string &model, Resource::ResourceSystem *resourceSystem, bool animated, bool allowLight)
Definition: animation.cpp:1597
Definition: animation.hpp:93
boost::shared_ptr< float > TimePtr
Definition: animation.hpp:182
void unlink()
Unreferences mNode without detaching it from the graph. Only use if you know what you are doing...
Definition: animation.cpp:1645
SceneUtil::Skeleton * mSkeleton
Definition: animation.hpp:224
std::string mModelName
Definition: animation.hpp:255
std::vector< boost::shared_ptr< AnimSource > > AnimSourceList
Definition: animation.hpp:218
virtual float getValue(osg::NodeVisitor *nv)
Definition: animation.cpp:1568
Definition: animation.hpp:139
virtual void releaseArrow(float attackStrength)
Definition: animation.hpp:459
Definition: animation.hpp:253
bool shouldLoop() const
Definition: animation.hpp:210
osg::Group * getOrCreateObjectRoot()
Definition: animation.cpp:1167
void operator=(const PartHolder &)
Definition: animation.hpp:39
Definition: animation.hpp:81
Pointer to a LiveCellRef.
Definition: ptr.hpp:19
float getCurrentTime(const std::string &groupname) const
Get the current absolute position in the animation track for the animation that is currently playing ...
Definition: animation.cpp:915
Holds an animation priority value for each BoneGroup.
Definition: animation.hpp:101
void resetTime(float time)
Definition: animation.cpp:1585
Definition: controller.hpp:11
std::map< std::string, AnimState > AnimStateMap
Definition: animation.hpp:215
const osg::Node * getNode(const std::string &name) const
Definition: animation.cpp:1417
void enable(CodeContainer &code, Literals &literals, const std::string &id)
Definition: generator.cpp:870
std::map< std::string, osg::ref_ptr< osg::MatrixTransform > > NodeMap
Definition: animation.hpp:243
boost::shared_ptr< float > mTimePtr
Definition: animation.hpp:152
float getTime() const
Definition: animation.cpp:1590
Definition: loadligh.hpp:17
float getVelocity(const std::string &groupname) const
Definition: animation.cpp:941
boost::shared_ptr< PartHolder > PartHolderPtr
Definition: animation.hpp:75
bool mPlaying
Definition: animation.hpp:186
virtual void setLightEffect(float effect)
Definition: animation.cpp:1473
Resource::ResourceSystem * mResourceSystem
Definition: animation.hpp:249
MWWorld::ConstPtr getPtr() const
Definition: animation.cpp:444
Definition: animation.hpp:83
Definition: animation.hpp:149
ControllerMap mActiveControllers
Definition: animation.hpp:238
float getStartTime(const std::string &groupname) const
Get the absolute position in the animation track of the first text key with the given group...
Definition: animation.cpp:584
TimePtr mTime
Definition: animation.hpp:183
void getLoopingEffects(std::vector< int > &out) const
Definition: animation.cpp:1370
AnimPriority(int priority)
Convenience constructor, initialises all priorities to the same value.
Definition: animation.hpp:104
virtual ~Animation()
Definition: animation.cpp:436
const char * name
Definition: crashcatcher.cpp:59
void setAccumulation(const osg::Vec3f &accum)
Definition: animation.cpp:460
virtual void setPitchFactor(float factor)
Definition: animation.hpp:457
void updatePosition(float oldtime, float newtime, osg::Vec3f &position)
Definition: animation.cpp:996