SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLAnimSkeleton Class Reference

SLAnimSkeleton keeps track of a skeletons joints and animations. More...

#include <SLAnimSkeleton.h>

Public Member Functions

 SLAnimSkeleton ()
 
 ~SLAnimSkeleton ()
 
SLJointcreateJoint (SLuint id)
 
SLJointcreateJoint (const SLstring &name, SLuint id)
 
SLAnimationcreateAnimation (SLAnimManager &aniMan, const SLstring &name, SLfloat duration)
 
void loadAnimation (const SLstring &file)
 
void addAnimation (SLAnimation *anim)
 
void getJointMatrices (SLVMat4f &jointWM)
 
void reset ()
 
SLAnimPlaybackanimPlayback (const SLstring &name)
 
SLMAnimation animations ()
 
SLint numAnimations () const
 
SLJointgetJoint (SLuint id)
 
SLJointgetJoint (const SLstring &name)
 
SLint numJoints () const
 
const SLVJointjoints () const
 
SLJointrootJoint ()
 
SLbool changed () const
 
const SLVec3fminOS ()
 
const SLVec3fmaxOS ()
 
void rootJoint (SLJoint *joint)
 
void changed (SLbool changed)
 
SLbool updateAnimations (SLfloat elapsedTimeSec)
 

Protected Member Functions

void updateMinMax ()
 

Protected Attributes

SLJoint_rootJoint
 pointer to the root joint of skeleton More...
 
SLVJoint _joints
 joint vector for fast access and index to joint mapping More...
 
SLMAnimation _animations
 map of animations for this skeleton More...
 
SLMAnimPlayback _animPlaybacks
 map of animation playbacks for this skeleton More...
 
SLbool _changed
 did this skeleton change this frame (attribute for skeleton instance) More...
 
SLVec3f _minOS
 min point in os for this skeleton (attribute for skeleton instance) More...
 
SLVec3f _maxOS
 max point in os for this skeleton (attribute for skeleton instance) More...
 
SLbool _minMaxOutOfDate
 dirty flag aabb rebuild More...
 

Detailed Description

SLAnimSkeleton keeps track of a skeletons joints and animations.

An SLAnimSkeleton is used to animate a hierarchical object like a human being. An SLAnimSkeleton keeps track of its bones (SLJoints) in a tree structure and points with _root to the root node of the skeleton hierarchy. An SLAnimSkeleton is not actively transforming any SLNode in the scenegraph. It just keeps track of its transformed SLJoint. A mesh that is associated with a skeleton transforms all its vertices every frame by the joint weights. Every vertex of a mesh has weights for four joints by which it can be influenced.

SLAnimations for this skeleton are also kept in this class. The SLAnimations have tracks corresponding to the individual SLJoints in the skeleton.

Note
The current implementation doesn't support multiple instances of the same skeleton animation. It is however not that far away from supporting it. Currently the SLAnimSkeleton class keeps both a SLAnimation map and an SLAnimPlayback map. We can split this into two classes by creating an SLSkeletonInstance class we that keeps the SLAnimPlayback map that references its parent SLAnimSkeleton we would be able to create multiple SLSkeletonInstance instances that use the same SLAnimations but with different states.

This leaves the problem of SLMesh that is not able to be instantiated without copying the data into a completely separate SLMesh. But the solution for SLMesh would take the same approach by creating a mesh instance class that is able to use SLSkeletonInstance.

Note
The current version of the SLAssimpImporter only supports the loading of a single animation. This limitation is mainly because there are very few 3D programs that make use of the possibility to export multiple animations in a single file. This means we would need to extend our importer to be able to load more animations for an already loaded skeleton.

Definition at line 54 of file SLAnimSkeleton.h.

Constructor & Destructor Documentation

◆ SLAnimSkeleton()

SLAnimSkeleton::SLAnimSkeleton ( )

Constructor

Definition at line 17 of file SLAnimSkeleton.cpp.

17  : _rootJoint(nullptr),
18  _minOS(-1, -1, -1),
19  _maxOS(1, 1, 1),
20  _minMaxOutOfDate(true)
21 {
22 }
SLVec3f _minOS
min point in os for this skeleton (attribute for skeleton instance)
SLJoint * _rootJoint
pointer to the root joint of skeleton
SLbool _minMaxOutOfDate
dirty flag aabb rebuild
SLVec3f _maxOS
max point in os for this skeleton (attribute for skeleton instance)

◆ ~SLAnimSkeleton()

SLAnimSkeleton::~SLAnimSkeleton ( )

Destructor

Definition at line 27 of file SLAnimSkeleton.cpp.

28 {
29  delete _rootJoint;
30  for (auto it : _animations) delete it.second;
31  for (auto it : _animPlaybacks) delete it.second;
32 }
SLMAnimPlayback _animPlaybacks
map of animation playbacks for this skeleton
SLMAnimation _animations
map of animations for this skeleton

Member Function Documentation

◆ addAnimation()

void SLAnimSkeleton::addAnimation ( SLAnimation anim)

◆ animations()

SLMAnimation SLAnimSkeleton::animations ( )
inline

Definition at line 71 of file SLAnimSkeleton.h.

71 { return _animations; }

◆ animPlayback()

SLAnimPlayback * SLAnimSkeleton::animPlayback ( const SLstring name)

Returns an animation state by name.

Definition at line 62 of file SLAnimSkeleton.cpp.

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 }
#define SL_WARN_MSG(message)
Definition: SL.h:241

◆ changed() [1/2]

SLbool SLAnimSkeleton::changed ( ) const
inline

Definition at line 78 of file SLAnimSkeleton.h.

78 { return _changed; }
SLbool _changed
did this skeleton change this frame (attribute for skeleton instance)

◆ changed() [2/2]

void SLAnimSkeleton::changed ( SLbool  changed)
inline

Definition at line 84 of file SLAnimSkeleton.h.

85  {
86  _changed = changed;
87  _minMaxOutOfDate = true;
88  }
SLbool changed() const

◆ createAnimation()

SLAnimation * SLAnimSkeleton::createAnimation ( SLAnimManager aniMan,
const SLstring name,
SLfloat  duration 
)

Create a nw animation owned by this skeleton.

Definition at line 99 of file SLAnimSkeleton.cpp.

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 }
SLVAnimPlayback & animPlaybacks()
Definition: SLAnimManager.h:47
SLVstring & animationNames()
Definition: SLAnimManager.h:46
Manages the playback of an SLAnimation.
SLAnimation is the base container for all animation data.
Definition: SLAnimation.h:33

◆ createJoint() [1/2]

SLJoint * SLAnimSkeleton::createJoint ( const SLstring name,
SLuint  id 
)

Creates a new joint owned by this skeleton.

Definition at line 45 of file SLAnimSkeleton.cpp.

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 }
SLVJoint _joints
joint vector for fast access and index to joint mapping
Specialized SLNode that represents a single joint (or bone) in a skeleton.
Definition: SLJoint.h:27

◆ createJoint() [2/2]

SLJoint * SLAnimSkeleton::createJoint ( SLuint  id)

Creates a new joint owned by this skeleton with a default name.

Definition at line 36 of file SLAnimSkeleton.cpp.

37 {
38  std::ostringstream oss;
39  oss << "Joint " << id;
40  return createJoint(oss.str(), id);
41 }
SLJoint * createJoint(SLuint id)

◆ getJoint() [1/2]

SLJoint * SLAnimSkeleton::getJoint ( const SLstring name)

returns an SLJoint by name.

Definition at line 80 of file SLAnimSkeleton.cpp.

81 {
82  if (!_rootJoint) return nullptr;
83  SLJoint* result = _rootJoint->find<SLJoint>(name);
84  return result;
85 }
T * find(const SLstring &name="", SLbool findRecursive=true)
Definition: SLNode.h:376

◆ getJoint() [2/2]

SLJoint * SLAnimSkeleton::getJoint ( SLuint  id)

Returns an SLJoint by it's internal id.

Definition at line 72 of file SLAnimSkeleton.cpp.

73 {
74  assert(id < _joints.size() && "Index out of bounds");
75  return _joints[id];
76 }

◆ getJointMatrices()

void SLAnimSkeleton::getJointMatrices ( SLVMat4f jointWM)

Fills a SLMat4f array with the final joint matrices for this skeleton.

Definition at line 89 of file SLAnimSkeleton.cpp.

90 {
91  for (SLuint i = 0; i < _joints.size(); i++)
92  {
93  jointWM[i] = _joints[i]->updateAndGetWM() * _joints[i]->offsetMat();
94  }
95 }
unsigned int SLuint
Definition: SL.h:171

◆ joints()

const SLVJoint& SLAnimSkeleton::joints ( ) const
inline

Definition at line 76 of file SLAnimSkeleton.h.

76 { return _joints; }

◆ loadAnimation()

void SLAnimSkeleton::loadAnimation ( const SLstring file)

◆ maxOS()

const SLVec3f & SLAnimSkeleton::maxOS ( )

getter for current the current max object space vertex.

Definition at line 173 of file SLAnimSkeleton.cpp.

174 {
175  if (_minMaxOutOfDate)
176  updateMinMax();
177 
178  return _maxOS;
179 }

◆ minOS()

const SLVec3f & SLAnimSkeleton::minOS ( )

getter for current the current min object space vertex.

Definition at line 163 of file SLAnimSkeleton.cpp.

164 {
165  if (_minMaxOutOfDate)
166  updateMinMax();
167 
168  return _minOS;
169 }

◆ numAnimations()

SLint SLAnimSkeleton::numAnimations ( ) const
inline

Definition at line 72 of file SLAnimSkeleton.h.

72 { return (SLint)_animations.size(); }
int SLint
Definition: SL.h:170

◆ numJoints()

SLint SLAnimSkeleton::numJoints ( ) const
inline

Definition at line 75 of file SLAnimSkeleton.h.

75 { return (SLint)_joints.size(); }

◆ reset()

void SLAnimSkeleton::reset ( )

Resets all joints.

Definition at line 119 of file SLAnimSkeleton.cpp.

120 {
121  // update all joints
122  for (auto j : _joints)
123  j->resetToInitialState();
124 }

◆ rootJoint() [1/2]

SLJoint* SLAnimSkeleton::rootJoint ( )
inline

Definition at line 77 of file SLAnimSkeleton.h.

77 { return _rootJoint; }

◆ rootJoint() [2/2]

void SLAnimSkeleton::rootJoint ( SLJoint joint)
inline

Definition at line 83 of file SLAnimSkeleton.h.

83 { _rootJoint = joint; }

◆ updateAnimations()

SLbool SLAnimSkeleton::updateAnimations ( SLfloat  elapsedTimeSec)

Updates the skeleton based on its active animation states

Definition at line 128 of file SLAnimSkeleton.cpp.

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 }
bool SLbool
Definition: SL.h:175
SLbool changed() const
SLbool enabled() const
SLfloat localTime() const
void advanceTime(SLfloat delta)
SLfloat weight() const
SLAnimation * parentAnimation()
void apply(SLfloat time, SLfloat weight=1.0f, SLfloat scale=1.0f)

◆ updateMinMax()

void SLAnimSkeleton::updateMinMax ( )
protected

Calculate the current min and max values in local space based on joint radii.

Definition at line 184 of file SLAnimSkeleton.cpp.

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 }
float SLfloat
Definition: SL.h:173
SLVec3< SLfloat > SLVec3f
Definition: SLVec3.h:318
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43

Member Data Documentation

◆ _animations

SLMAnimation SLAnimSkeleton::_animations
protected

map of animations for this skeleton

Definition at line 97 of file SLAnimSkeleton.h.

◆ _animPlaybacks

SLMAnimPlayback SLAnimSkeleton::_animPlaybacks
protected

map of animation playbacks for this skeleton

Definition at line 98 of file SLAnimSkeleton.h.

◆ _changed

SLbool SLAnimSkeleton::_changed
protected

did this skeleton change this frame (attribute for skeleton instance)

Definition at line 99 of file SLAnimSkeleton.h.

◆ _joints

SLVJoint SLAnimSkeleton::_joints
protected

joint vector for fast access and index to joint mapping

Definition at line 96 of file SLAnimSkeleton.h.

◆ _maxOS

SLVec3f SLAnimSkeleton::_maxOS
protected

max point in os for this skeleton (attribute for skeleton instance)

Definition at line 101 of file SLAnimSkeleton.h.

◆ _minMaxOutOfDate

SLbool SLAnimSkeleton::_minMaxOutOfDate
protected

dirty flag aabb rebuild

Definition at line 102 of file SLAnimSkeleton.h.

◆ _minOS

SLVec3f SLAnimSkeleton::_minOS
protected

min point in os for this skeleton (attribute for skeleton instance)

Definition at line 100 of file SLAnimSkeleton.h.

◆ _rootJoint

SLJoint* SLAnimSkeleton::_rootJoint
protected

pointer to the root joint of skeleton

Definition at line 95 of file SLAnimSkeleton.h.


The documentation for this class was generated from the following files: