SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAnimSkeleton.cpp
Go to the documentation of this file.
1 /**
2  * \file SLAnimSkeleton.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 #include <SLSceneView.h>
12 #include <SLAnimSkeleton.h>
13 
14 //-----------------------------------------------------------------------------
15 /*! Constructor
16  */
17 SLAnimSkeleton::SLAnimSkeleton() : _rootJoint(nullptr),
18  _minOS(-1, -1, -1),
19  _maxOS(1, 1, 1),
20  _minMaxOutOfDate(true)
21 {
22 }
23 
24 //-----------------------------------------------------------------------------
25 /*! Destructor
26  */
28 {
29  delete _rootJoint;
30  for (auto it : _animations) delete it.second;
31  for (auto it : _animPlaybacks) delete it.second;
32 }
33 //-----------------------------------------------------------------------------
34 /*! Creates a new joint owned by this skeleton with a default name.
35  */
37 {
38  std::ostringstream oss;
39  oss << "Joint " << id;
40  return createJoint(oss.str(), id);
41 }
42 //-----------------------------------------------------------------------------
43 /*! Creates a new joint owned by this skeleton.
44  */
46 {
47  SLJoint* result = new SLJoint(name, id, this);
48 
49  assert((id >= _joints.size() ||
50  (id < _joints.size() && _joints[id] == nullptr)) &&
51  "Trying to create a joint with an already existing id.");
52 
53  if (_joints.size() <= id)
54  _joints.resize(id + 1);
55 
56  _joints[id] = result;
57  return result;
58 }
59 //-----------------------------------------------------------------------------
60 /*! Returns an animation state by name.
61  */
63 {
64  if (_animPlaybacks.find(name) != _animPlaybacks.end())
65  return _animPlaybacks[name];
66  SL_WARN_MSG("*** Playback found in SLAnimSkeleton::getNodeAnimPlayack ***");
67  return nullptr;
68 }
69 //-----------------------------------------------------------------------------
70 /*! Returns an SLJoint by it's internal id.
71  */
73 {
74  assert(id < _joints.size() && "Index out of bounds");
75  return _joints[id];
76 }
77 //-----------------------------------------------------------------------------
78 /*! returns an SLJoint by name.
79  */
81 {
82  if (!_rootJoint) return nullptr;
83  SLJoint* result = _rootJoint->find<SLJoint>(name);
84  return result;
85 }
86 //-----------------------------------------------------------------------------
87 /*! Fills a SLMat4f array with the final joint matrices for this skeleton.
88  */
90 {
91  for (SLuint i = 0; i < _joints.size(); i++)
92  {
93  jointWM[i] = _joints[i]->updateAndGetWM() * _joints[i]->offsetMat();
94  }
95 }
96 //-----------------------------------------------------------------------------
97 /*! Create a nw animation owned by this skeleton.
98  */
100 {
101  assert(_animations.find(name) == _animations.end() &&
102  "animation with same name already exists!");
103 
104  SLAnimation* anim = new SLAnimation(name, duration);
105  _animations[name] = anim;
106 
107  SLAnimPlayback* play = new SLAnimPlayback(anim);
108  _animPlaybacks[name] = play;
109 
110  // Add node animation to the combined vector
111  aniMan.animationNames().push_back(name);
112  aniMan.animPlaybacks().push_back(play);
113 
114  return anim;
115 }
116 //-----------------------------------------------------------------------------
117 /*! Resets all joints.
118  */
120 {
121  // update all joints
122  for (auto j : _joints)
123  j->resetToInitialState();
124 }
125 //-----------------------------------------------------------------------------
126 /*! Updates the skeleton based on its active animation states
127  */
129 {
130  SLbool animated = false;
131 
132  for (auto it : _animPlaybacks)
133  {
134  SLAnimPlayback* pb = it.second;
135  if (pb->enabled())
136  {
137  pb->advanceTime(elapsedTimeSec);
138  animated |= pb->changed();
139  }
140  }
141 
142  // return if nothing changed
143  if (!animated)
144  return false;
145 
146  // reset the skeleton and apply all enabled animations
147  reset();
148 
149  for (auto it : _animPlaybacks)
150  {
151  SLAnimPlayback* pb = it.second;
152  if (pb->enabled())
153  {
154  pb->parentAnimation()->apply(this, pb->localTime(), pb->weight());
155  pb->changed(false); // remove changed dirty flag from the pb again
156  }
157  }
158  return true;
159 }
160 //-----------------------------------------------------------------------------
161 /*! getter for current the current min object space vertex.
162  */
164 {
165  if (_minMaxOutOfDate)
166  updateMinMax();
167 
168  return _minOS;
169 }
170 //-----------------------------------------------------------------------------
171 /*! getter for current the current max object space vertex.
172  */
174 {
175  if (_minMaxOutOfDate)
176  updateMinMax();
177 
178  return _maxOS;
179 }
180 //-----------------------------------------------------------------------------
181 /*! Calculate the current min and max values in local space based on joint
182 radii.
183 */
185 {
186  // recalculate the new min and max os based on bone radius
187  SLbool firstSet = false;
188  for (auto joint : _joints)
189  {
190  SLfloat r = joint->radius();
191 
192  // ignore joints with a zero radius
193  if (r == 0.0f)
194  continue;
195 
196  SLVec3f jointPos = joint->updateAndGetWM().translation();
197  SLVec3f curMin = jointPos - SLVec3f(r, r, r);
198  SLVec3f curMax = jointPos + SLVec3f(r, r, r);
199 
200  if (!firstSet)
201  {
202  _minOS = curMin;
203  _maxOS = curMax;
204  firstSet = true;
205  }
206  else
207  {
208  _minOS.x = std::min(_minOS.x, curMin.x);
209  _minOS.y = std::min(_minOS.y, curMin.y);
210  _minOS.z = std::min(_minOS.z, curMin.z);
211 
212  _maxOS.x = std::max(_maxOS.x, curMax.x);
213  _maxOS.y = std::max(_maxOS.y, curMax.y);
214  _maxOS.z = std::max(_maxOS.z, curMax.z);
215  }
216  }
217  _minMaxOutOfDate = false;
218 }
219 //-----------------------------------------------------------------------------
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
vector< SLMat4f > SLVMat4f
Definition: SLMat4.h:1585
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
SLAnimManager is the central class for all animation handling.
Definition: SLAnimManager.h:27
SLVAnimPlayback & animPlaybacks()
Definition: SLAnimManager.h:47
SLVstring & animationNames()
Definition: SLAnimManager.h:46
Manages the playback of an SLAnimation.
SLbool changed() const
SLbool enabled() const
SLfloat localTime() const
void advanceTime(SLfloat delta)
SLfloat weight() const
SLAnimation * parentAnimation()
const SLVec3f & maxOS()
SLMAnimPlayback _animPlaybacks
map of animation playbacks for this skeleton
void getJointMatrices(SLVMat4f &jointWM)
SLVec3f _minOS
min point in os for this skeleton (attribute for skeleton instance)
SLAnimPlayback * animPlayback(const SLstring &name)
SLJoint * _rootJoint
pointer to the root joint of skeleton
SLbool updateAnimations(SLfloat elapsedTimeSec)
SLbool _minMaxOutOfDate
dirty flag aabb rebuild
SLMAnimation _animations
map of animations for this skeleton
SLVec3f _maxOS
max point in os for this skeleton (attribute for skeleton instance)
SLJoint * createJoint(SLuint id)
SLAnimation * createAnimation(SLAnimManager &aniMan, const SLstring &name, SLfloat duration)
SLJoint * getJoint(SLuint id)
SLVJoint _joints
joint vector for fast access and index to joint mapping
const SLVec3f & minOS()
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33
void apply(SLfloat time, SLfloat weight=1.0f, SLfloat scale=1.0f)
Specialized SLNode that represents a single joint (or bone) in a skeleton.
Definition: SLJoint.h:27
T * find(const SLstring &name="", SLbool findRecursive=true)
Definition: SLNode.h:376
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43