SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAnimManager.cpp
Go to the documentation of this file.
1 /**
2  * \file SLAnimManager.cpp
3  * \date Autumn 2014
4  * \remarks Please use clangformat to format the code. See more code style on
5  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
6  * \authors Marc Wacker, Marcus Hudritsch
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 #include <SLScene.h>
11 
12 //-----------------------------------------------------------------------------
13 //! destructor
15 {
16  clear();
17 }
18 
19 //-----------------------------------------------------------------------------
20 //! Clears and deletes all node animations and skeletons
22 {
23  for (const auto& it : _animationNamesMap)
24  delete it.second;
25  _animationNamesMap.clear();
26 
27  for (const auto& it : _animPlaybackNamesMap)
28  delete it.second;
29  _animPlaybackNamesMap.clear();
30 
31  for (auto* skeleton : _skeletons)
32  delete skeleton;
33  _skeletons.clear();
34 
35  _animationNames.clear();
36  _animPlaybacks.clear();
37 }
38 //-----------------------------------------------------------------------------
39 //! Add a skeleton to the skeleton vector
41 {
42  _skeletons.push_back(skel);
43 }
44 //-----------------------------------------------------------------------------
45 /*! Creates a new node animation
46  @param duration length of the animation
47 */
49 {
50  SLuint index = (SLuint)_animationNamesMap.size();
51  std::ostringstream oss;
52 
53  do
54  {
55  oss.clear();
56  oss << "Node_" << index;
57  index++;
58  } while (_animationNamesMap.find(oss.str()) != _animationNamesMap.end());
59 
60  return createNodeAnimation(oss.str(), duration);
61 }
62 //-----------------------------------------------------------------------------
63 /*! Creates new SLAnimation istance for node animations. It will already create
64  and set parameters for the respective SLAnimPlayback.
65 */
67  SLfloat duration,
68  SLbool enabled,
69  SLEasingCurve easing,
70  SLAnimLooping looping)
71 {
72  SLAnimation* anim = createNodeAnimation(name, duration);
73  SLAnimPlayback* playback = animPlaybackByName(name);
74  playback->enabled(enabled);
75  playback->easing(easing);
76  playback->loop(looping);
77  return anim;
78 }
79 //-----------------------------------------------------------------------------
80 /*! Creates a new node animation
81  @param name the animation name
82  @param duration length of the animation
83 */
85  SLfloat duration)
86 {
87  assert(_animationNamesMap.find(name) == _animationNamesMap.end() &&
88  "node animation with same name already exists!");
89 
90  SLAnimation* anim = new SLAnimation(name, duration);
91  _animationNamesMap[name] = anim;
92 
93  SLAnimPlayback* playback = new SLAnimPlayback(anim);
94  _animPlaybackNamesMap[name] = playback;
95 
96  // Add node animation to the combined vector
97  _animationNames.push_back(name);
98  _animPlaybacks.push_back(playback);
99 
100  return anim;
101 }
102 //-----------------------------------------------------------------------------
103 //! Returns the playback of a node animation or skeleton by name if it exists.
105 {
106  for(auto playback : _animPlaybacks)
107  {
108  if (playback->parentAnimation()->name() == name)
109  return playback;
110  }
111 
112 
113  SL_WARN_MSG("*** SLAnimManager::animPlaybackByName: animation not found ***");
114  return nullptr;
115 }
116 //-----------------------------------------------------------------------------
117 //! Advances the time of all enabled animation plays.
119 {
120  // reset the dirty flag on all skeletons
121  for (auto* skeleton : _skeletons)
122  skeleton->changed(false);
123 
124  SLbool updated = false;
125 
126  // advance time for node animations and apply them
127  // @todo currently we can't blend between normal node animations because we
128  // reset them per animation playback. so the last playback that affects a
129  // node will have its animation applied.
130  // We need to save the playback differently if we want to blend them.
131 
132  for (const auto& it : _animPlaybackNamesMap)
133  {
134  SLAnimPlayback* playback = it.second;
135  if (playback->enabled())
136  {
137  playback->parentAnimation()->resetNodes();
138  playback->advanceTime(elapsedTimeSec);
139  playback->parentAnimation()->apply(playback->localTime(),
140  playback->weight());
141  updated = true;
142  }
143  }
144 
145  // update the skeletons separately
146  for (auto* skeleton : _skeletons)
147  updated |= skeleton->updateAnimations(elapsedTimeSec);
148 
149  return updated;
150 }
151 //-----------------------------------------------------------------------------
152 //! Draws the animation visualizations.
154 {
155  for (const auto& it : _animPlaybackNamesMap)
156  {
157  SLAnimPlayback* playback = it.second;
158  playback->parentAnimation()->drawNodeVisuals(sv);
159  }
160 
161  // skeletons are drawn from within SLSceneView per node
162 }
163 //-----------------------------------------------------------------------------
float SLfloat
Definition: SL.h:173
unsigned int SLuint
Definition: SL.h:171
#define SL_WARN_MSG(message)
Definition: SL.h:241
bool SLbool
Definition: SL.h:175
string SLstring
Definition: SL.h:158
SLEasingCurve
Enumeration for animation easing curves.
Definition: SLEnums.h:180
SLAnimLooping
Enumeration for animation modes.
Definition: SLEnums.h:167
SLVSkeleton _skeletons
all skeleton instances
Definition: SLAnimManager.h:57
SLAnimPlayback * animPlaybackByName(const SLstring &name)
Returns the playback of a node animation or skeleton by name if it exists.
~SLAnimManager()
destructor
SLMAnimation _animationNamesMap
map name to animation
Definition: SLAnimManager.h:58
SLAnimation * createNodeAnimation(SLfloat duration)
void addSkeleton(SLAnimSkeleton *skel)
Add a skeleton to the skeleton vector.
void clear()
Clears and deletes all node animations and skeletons.
SLVstring _animationNames
vector with all animation names
Definition: SLAnimManager.h:60
SLMAnimPlayback _animPlaybackNamesMap
map name to animation playbacks
Definition: SLAnimManager.h:59
SLVAnimPlayback _animPlaybacks
vector with all animation playbacks
Definition: SLAnimManager.h:61
void drawVisuals(SLSceneView *sv)
Draws the animation visualizations.
SLbool update(SLfloat elapsedTimeSec)
Advances the time of all enabled animation plays.
Manages the playback of an SLAnimation.
SLbool enabled() const
SLfloat localTime() const
SLAnimLooping loop() const
SLEasingCurve easing() const
void advanceTime(SLfloat delta)
SLfloat weight() const
SLAnimation * parentAnimation()
SLAnimSkeleton keeps track of a skeletons joints and animations.
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
void drawNodeVisuals(SLSceneView *sv)
void resetNodes()
void apply(SLfloat time, SLfloat weight=1.0f, SLfloat scale=1.0f)
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69