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

SLAnimation is the base container for all animation data. More...

#include <SLAnimation.h>

Public Member Functions

 SLAnimation (const SLstring &name, SLfloat duration)
 
 ~SLAnimation ()
 
SLfloat nextKeyframeTime (SLfloat time)
 
SLfloat prevKeyframeTime (SLfloat time)
 
SLbool affectsNode (SLNode *node)
 
void apply (SLfloat time, SLfloat weight=1.0f, SLfloat scale=1.0f)
 
void applyToNode (SLNode *node, SLfloat time, SLfloat weight=1.0f, SLfloat scale=1.0f)
 
void apply (SLAnimSkeleton *skel, SLfloat time, SLfloat weight=1.0f, SLfloat scale=1.0f)
 
void resetNodes ()
 
void drawNodeVisuals (SLSceneView *sv)
 
SLNodeAnimTrackcreateNodeAnimTrack ()
 
SLNodeAnimTrackcreateNodeAnimTrack (SLuint trackID)
 
SLNodeAnimTrackcreateNodeAnimTrackForTranslation (SLNode *target, const SLVec3f &endPos)
 
SLNodeAnimTrackcreateNodeAnimTrackForRotation (SLNode *target, SLfloat angleDeg1, const SLVec3f &axis)
 
SLNodeAnimTrackcreateNodeAnimTrackForRotation2 (SLNode *target, SLfloat angleDeg0, SLfloat angleDeg1, const SLVec3f &axis)
 
SLNodeAnimTrackcreateNodeAnimTrackForRotation3 (SLNode *target, SLfloat angleDeg0, SLfloat angleDeg1, SLfloat angleDeg2, const SLVec3f &axis)
 
SLNodeAnimTrackcreateNodeAnimTrackForRotation4 (SLNode *target, SLfloat angleDeg0, SLfloat angleDeg1, SLfloat angleDeg2, SLfloat angleDeg3, const SLVec3f &axis)
 
SLNodeAnimTrackcreateNodeAnimTrackForRotation360 (SLNode *target, const SLVec3f &axis)
 
SLNodeAnimTrackcreateNodeAnimTrackForScaling (SLNode *target, const SLVec3f &endScale)
 
SLNodeAnimTrackcreateNodeAnimTrackForEllipse (SLNode *target, SLfloat radiusA, SLAxis axisA, SLfloat radiusB, SLAxis axisB)
 
const SLstringname ()
 
SLfloat lengthSec () const
 
void name (const SLstring &name)
 
void lengthSec (SLfloat lengthSec)
 

Protected Attributes

SLstring _name
 name of the animation More...
 
SLfloat _lengthSec
 duration of the animation in seconds More...
 
SLMNodeAnimTrack _nodeAnimTracks
 map of all the node tracks in this animation More...
 

Detailed Description

SLAnimation is the base container for all animation data.

SLAnimation is a container for multiple SLAnimTrack that build an animation. E.g. a walk animation would consist of all the SLAnimTrack that make a SLAnimSkeleton walk. It also knows the length of the animation.

An animation for a SLAnimSkeleton with n joints must consist of 1 to n SLNodeAnimTrack. The SLAnimation class keeps a map with index -> SLNodeAnimTrack pairs, the index for the SLNodeAnimTrack must match the index of a bone in the target SLAnimSkeleton. This method allows us to animate multiple identical, or similar SLSkeletons with the same SLAnimation.

Definition at line 32 of file SLAnimation.h.

Constructor & Destructor Documentation

◆ SLAnimation()

SLAnimation::SLAnimation ( const SLstring name,
SLfloat  duration 
)

Constructor

Definition at line 16 of file SLAnimation.cpp.

17  : _name(name), _lengthSec(duration)
18 {
19 }
SLfloat _lengthSec
duration of the animation in seconds
Definition: SLAnimation.h:80
SLstring _name
name of the animation
Definition: SLAnimation.h:79
const SLstring & name()
Definition: SLAnimation.h:71

◆ ~SLAnimation()

SLAnimation::~SLAnimation ( )

Destructor

Definition at line 23 of file SLAnimation.cpp.

24 {
25  for (auto it : _nodeAnimTracks)
26  delete it.second;
27 }
SLMNodeAnimTrack _nodeAnimTracks
map of all the node tracks in this animation
Definition: SLAnimation.h:81

Member Function Documentation

◆ affectsNode()

SLbool SLAnimation::affectsNode ( SLNode node)

Returns true if node is the animationTarget of any of the SLNodeAnimationTracks in this animation.

Definition at line 83 of file SLAnimation.cpp.

84 {
85  for (auto it : _nodeAnimTracks)
86  if (it.second->animatedNode() == node)
87  return true;
88 
89  return false;
90 }

◆ apply() [1/2]

void SLAnimation::apply ( SLAnimSkeleton skel,
SLfloat  time,
SLfloat  weight = 1.0f,
SLfloat  scale = 1.0f 
)

Applies all the tracks to their respective joints in the passed in skeleton.

Definition at line 140 of file SLAnimation.cpp.

144 {
145  for (auto it : _nodeAnimTracks)
146  {
147  SLJoint* joint = skel->getJoint(it.first);
148  it.second->applyToNode(joint, time, weight, scale);
149  }
150 }
SLJoint * getJoint(SLuint id)
Specialized SLNode that represents a single joint (or bone) in a skeleton.
Definition: SLJoint.h:27

◆ apply() [2/2]

void SLAnimation::apply ( SLfloat  time,
SLfloat  weight = 1.0f,
SLfloat  scale = 1.0f 
)

Applies all animation tracks for the passed in timestamp, weight and scale.

Definition at line 121 of file SLAnimation.cpp.

122 {
123  for (auto it : _nodeAnimTracks)
124  it.second->apply(time, weight, scale);
125 }

◆ applyToNode()

void SLAnimation::applyToNode ( SLNode node,
SLfloat  time,
SLfloat  weight = 1.0f,
SLfloat  scale = 1.0f 
)

Applies all node tracks of this animation on a single node

Definition at line 129 of file SLAnimation.cpp.

133 {
134  for (auto it : _nodeAnimTracks)
135  it.second->applyToNode(node, time, weight, scale);
136 }

◆ createNodeAnimTrack() [1/2]

SLNodeAnimTrack * SLAnimation::createNodeAnimTrack ( )

Creates a new SLNodeAnimationTrack with the next free handle.

Definition at line 94 of file SLAnimation.cpp.

95 {
96  SLuint freeIndex = 0;
97 
98  auto it = _nodeAnimTracks.begin();
99  for (; it != _nodeAnimTracks.end() && freeIndex == it->first; ++it, ++freeIndex)
100  {
101  }
102 
103  return createNodeAnimTrack(freeIndex);
104 }
unsigned int SLuint
Definition: SL.h:171
SLNodeAnimTrack * createNodeAnimTrack()
Definition: SLAnimation.cpp:94

◆ createNodeAnimTrack() [2/2]

SLNodeAnimTrack * SLAnimation::createNodeAnimTrack ( SLuint  trackID)

Creates a new SLNodeAnimationTrack with the passed in track id.

Definition at line 108 of file SLAnimation.cpp.

109 {
110  // track with same handle already exists
111  if (_nodeAnimTracks.find(trackID) != _nodeAnimTracks.end())
112  return nullptr;
113 
114  _nodeAnimTracks[trackID] = new SLNodeAnimTrack(this);
115 
116  return _nodeAnimTracks[trackID];
117 }
Specialized animation track for node animations.
Definition: SLAnimTrack.h:66

◆ createNodeAnimTrackForEllipse()

SLNodeAnimTrack * SLAnimation::createNodeAnimTrackForEllipse ( SLNode target,
SLfloat  radiusA,
SLAxis  axisA,
SLfloat  radiusB,
SLAxis  axisB 
)

Specialized SLNodeAnimationTrack creator for an elliptic node animation

Definition at line 309 of file SLAnimation.cpp.

314 {
315  assert(axisA != axisB && radiusA > 0 && radiusB > 0);
317  target->setInitialState();
318  track->animatedNode(target);
319 
320  /* The ellipse is defined by 5 keyframes: A,B,C,D and again A
321 
322  c2----B----c1
323  c3 c0
324  | |
325  | | |
326  C --0-- A
327  | | |
328  | |
329  c4 c7
330  c5----D----c6
331  */
332 
333  SLVec3f A(0, 0, 0);
334  A.comp[axisA] = radiusA;
335  SLVec3f B(0, 0, 0);
336  B.comp[axisB] = radiusB;
337  SLVec3f C(0, 0, 0);
338  C.comp[axisA] = -radiusA;
339  SLVec3f D(0, 0, 0);
340  D.comp[axisB] = -radiusB;
341 
342  // Control points with the magic factor kappa for control points
343  SLfloat k = 0.5522847498f;
344 
345  SLVVec3f controls;
346  controls.resize(8);
347  for (SLuint i = 0; i < controls.size(); ++i)
348  controls[i].set(0, 0, 0);
349  controls[0].comp[axisA] = radiusA;
350  controls[0].comp[axisB] = k * radiusB;
351  controls[1].comp[axisB] = radiusB;
352  controls[1].comp[axisA] = k * radiusA;
353  controls[2].comp[axisB] = radiusB;
354  controls[2].comp[axisA] = k * -radiusA;
355  controls[3].comp[axisA] = -radiusA;
356  controls[3].comp[axisB] = k * radiusB;
357  controls[4].comp[axisA] = -radiusA;
358  controls[4].comp[axisB] = k * -radiusB;
359  controls[5].comp[axisB] = -radiusB;
360  controls[5].comp[axisA] = k * -radiusA;
361  controls[6].comp[axisB] = -radiusB;
362  controls[6].comp[axisA] = k * radiusA;
363  controls[7].comp[axisA] = radiusA;
364  controls[7].comp[axisB] = k * -radiusB;
365 
366  // Add keyframes
367  SLfloat t4 = lengthSec() / 4.0f;
368  track->createNodeKeyframe(0.0f * t4)->translation(A);
369  track->createNodeKeyframe(1.0f * t4)->translation(B);
370  track->createNodeKeyframe(2.0f * t4)->translation(C);
371  track->createNodeKeyframe(3.0f * t4)->translation(D);
372  track->createNodeKeyframe(4.0f * t4)->translation(A);
373 
374  // Build curve data w. cumulated times
375  SLVVec4f points;
376  points.resize((SLulong)track->numKeyframes());
377  for (SLuint i = 0; i < (SLuint)track->numKeyframes(); ++i)
378  {
380  points[i].set(kf->translation().x,
381  kf->translation().y,
382  kf->translation().z,
383  kf->time());
384  }
385 
386  // create curve and delete temp arrays again
387  track->interpolationCurve(new SLCurveBezier(points, controls));
389 
390  return track;
391 }
float SLfloat
Definition: SL.h:173
unsigned long SLulong
Definition: SL.h:165
int SLint
Definition: SL.h:170
@ AI_bezier
Definition: SLEnums.h:162
vector< SLVec3f > SLVVec3f
Definition: SLVec3.h:325
vector< SLVec4f > SLVVec4f
Definition: SLVec4.h:239
void time(SLfloat t)
SLAnimKeyframe * keyframe(SLint index)
Definition: SLAnimTrack.cpp:48
SLint numKeyframes() const
Definition: SLAnimTrack.h:47
SLfloat lengthSec() const
Definition: SLAnimation.h:72
The SLCurveBezier class implements a Bezier curve interpolation.
Definition: SLCurveBezier.h:25
void animatedNode(SLNode *target)
Definition: SLAnimTrack.h:73
void interpolationCurve(SLCurve *curve)
SLTransformKeyframe * createNodeKeyframe(SLfloat time)
void translationInterpolation(SLAnimInterpolation interp)
Definition: SLAnimTrack.h:82
void setInitialState()
Definition: SLNode.cpp:1084
SLTransformKeyframe is a specialized SLKeyframe for node transformations.
void translation(const SLVec3f &t)
void set(std::string path, const std::vector< char > &data)
Definition: SLIOMemory.cpp:29

◆ createNodeAnimTrackForRotation()

SLNodeAnimTrack * SLAnimation::createNodeAnimTrackForRotation ( SLNode target,
SLfloat  angleDeg,
const SLVec3f axis 
)

Specialized SLNodeAnimationTrack creator for a two keyframe rotation animation from 0° to angleDeg.

Definition at line 184 of file SLAnimation.cpp.

187 {
189  target->setInitialState();
190  track->animatedNode(target);
191  track->createNodeKeyframe(0.0f);
192  track->createNodeKeyframe(lengthSec())->rotation(SLQuat4f(angleDeg, axis));
193  return track;
194 }
SLQuat4< SLfloat > SLQuat4f
Definition: SLQuat4.h:846
void rotation(const SLQuat4f &r)

◆ createNodeAnimTrackForRotation2()

SLNodeAnimTrack * SLAnimation::createNodeAnimTrackForRotation2 ( SLNode target,
SLfloat  angleDeg0,
SLfloat  angleDeg1,
const SLVec3f axis 
)

Specialized SLNodeAnimationTrack creator for 2 keyframes at angleDeg0 and angleDeg1.

Definition at line 199 of file SLAnimation.cpp.

203 {
205  target->setInitialState();
206  track->animatedNode(target);
207 
208  SLTransformKeyframe* frame0 = track->createNodeKeyframe(0.0f);
209  frame0->rotation(SLQuat4f(angleDeg0, axis));
210 
212  frame1->rotation(SLQuat4f(angleDeg1, axis));
213 
214  return track;
215 }

◆ createNodeAnimTrackForRotation3()

SLNodeAnimTrack * SLAnimation::createNodeAnimTrackForRotation3 ( SLNode target,
SLfloat  angleDeg0,
SLfloat  angleDeg1,
SLfloat  angleDeg2,
const SLVec3f axis 
)

Specialized SLNodeAnimationTrack creator for 3 keyframes at angleDeg0, angleDeg1 and angleDeg2.

Definition at line 220 of file SLAnimation.cpp.

225 {
227  target->setInitialState();
228  track->animatedNode(target);
229 
230  SLTransformKeyframe* frame0 = track->createNodeKeyframe(0.0f);
231  frame0->rotation(SLQuat4f(angleDeg0, axis));
232 
233  SLTransformKeyframe* frame1 = track->createNodeKeyframe(lengthSec() * 0.5f);
234  frame1->rotation(SLQuat4f(angleDeg1, axis));
235 
237  frame2->rotation(SLQuat4f(angleDeg2, axis));
238 
239  return track;
240 }

◆ createNodeAnimTrackForRotation360()

SLNodeAnimTrack * SLAnimation::createNodeAnimTrackForRotation360 ( SLNode target,
const SLVec3f axis 
)

Specialized SLNodeAnimationTrack creator for a 360 deg. node rotation track with 3 keyframes from 0° to 180° to 360°.

Definition at line 274 of file SLAnimation.cpp.

276 {
278  target->setInitialState();
279  track->animatedNode(target);
280 
281  SLTransformKeyframe* frame0 = track->createNodeKeyframe(0.0f);
282  frame0->rotation(SLQuat4f(0.0f, axis));
283 
284  SLTransformKeyframe* frame1 = track->createNodeKeyframe(lengthSec() * 0.5f);
285  frame1->rotation(SLQuat4f(180.0f, axis));
286 
288  frame2->rotation(SLQuat4f(360.0f, axis));
289 
290  return track;
291 }

◆ createNodeAnimTrackForRotation4()

SLNodeAnimTrack * SLAnimation::createNodeAnimTrackForRotation4 ( SLNode target,
SLfloat  angleDeg0,
SLfloat  angleDeg1,
SLfloat  angleDeg2,
SLfloat  angleDeg3,
const SLVec3f axis 
)

Specialized SLNodeAnimationTrack creator for 4 keyframes at angleDeg0, angleDeg1, angleDeg2 and angleDeg3.

Definition at line 245 of file SLAnimation.cpp.

251 {
253  target->setInitialState();
254  track->animatedNode(target);
255 
256  SLTransformKeyframe* frame0 = track->createNodeKeyframe(0.0f);
257  frame0->rotation(SLQuat4f(angleDeg0, axis));
258 
259  SLTransformKeyframe* frame1 = track->createNodeKeyframe(lengthSec() * 0.3333f);
260  frame1->rotation(SLQuat4f(angleDeg1, axis));
261 
262  SLTransformKeyframe* frame2 = track->createNodeKeyframe(lengthSec() * 0.6666f);
263  frame2->rotation(SLQuat4f(angleDeg2, axis));
264 
266  frame3->rotation(SLQuat4f(angleDeg3, axis));
267 
268  return track;
269 }

◆ createNodeAnimTrackForScaling()

SLNodeAnimTrack * SLAnimation::createNodeAnimTrackForScaling ( SLNode target,
const SLVec3f endScale 
)

Specialized SLNodeAnimationTrack creator for a two keyframe scaling animation

Definition at line 295 of file SLAnimation.cpp.

297 {
299  target->setInitialState();
300  track->animatedNode(target);
301  track->createNodeKeyframe(0.0f);
302  track->createNodeKeyframe(lengthSec())->scale(endScale);
303  return track;
304 }
void scale(const SLVec3f &s)

◆ createNodeAnimTrackForTranslation()

SLNodeAnimTrack * SLAnimation::createNodeAnimTrackForTranslation ( SLNode target,
const SLVec3f endPos 
)

Specialized SLNodeAnimationTrack creator for a two keyframe translation animation

Definition at line 170 of file SLAnimation.cpp.

172 {
174  target->setInitialState();
175  track->animatedNode(target);
176  track->createNodeKeyframe(0.0f);
177  track->createNodeKeyframe(lengthSec())->translation(endPos);
178  return track;
179 }

◆ drawNodeVisuals()

void SLAnimation::drawNodeVisuals ( SLSceneView sv)

Draws the visualizations of all node tracks

Definition at line 154 of file SLAnimation.cpp.

155 {
156  for (auto it : _nodeAnimTracks)
157  it.second->drawVisuals(sv);
158 }
SceneView class represents a dynamic real time 3D view onto the scene.
Definition: SLSceneView.h:69

◆ lengthSec() [1/2]

SLfloat SLAnimation::lengthSec ( ) const
inline

Definition at line 72 of file SLAnimation.h.

72 { return _lengthSec; }

◆ lengthSec() [2/2]

void SLAnimation::lengthSec ( SLfloat  lengthSec)

Setter for the animation length

Definition at line 31 of file SLAnimation.cpp.

32 {
34 }

◆ name() [1/2]

const SLstring& SLAnimation::name ( )
inline

Definition at line 71 of file SLAnimation.h.

71 { return _name; }

◆ name() [2/2]

void SLAnimation::name ( const SLstring name)
inline

Definition at line 75 of file SLAnimation.h.

75 { _name = name; }

◆ nextKeyframeTime()

SLfloat SLAnimation::nextKeyframeTime ( SLfloat  time)

Returns the timestamp for the next keyframe in all of the tracks.

Definition at line 38 of file SLAnimation.cpp.

39 {
40  // find the closest keyframe time to the right
41  SLfloat result = _lengthSec;
42  SLAnimKeyframe* kf1;
43  SLAnimKeyframe* kf2;
44 
45  for (auto it : _nodeAnimTracks)
46  {
47  it.second->getKeyframesAtTime(time, &kf1, &kf2);
48  if (kf2->time() < result && kf2->time() >= time)
49  result = kf2->time();
50  }
51 
52  return result;
53 }
Base class for all animation keyframes.

◆ prevKeyframeTime()

SLfloat SLAnimation::prevKeyframeTime ( SLfloat  time)

Returns the timestamp for the previous keyframe in all of the tracks.

Definition at line 57 of file SLAnimation.cpp.

58 {
59  // find the closest keyframe time to the right
60  SLfloat result = 0.0;
61  SLAnimKeyframe* kf1;
62  SLAnimKeyframe* kf2;
63 
64  // shift the time a little bit to the left or else the getKeyframesAtTime function
65  // would return the same keyframe over and over again
66  time -= 0.01f;
67  if (time <= 0.0f)
68  return 0.0f;
69 
70  for (auto it : _nodeAnimTracks)
71  {
72  it.second->getKeyframesAtTime(time, &kf1, &kf2);
73  if (kf1->time() > result && kf1->time() <= time)
74  result = kf1->time();
75  }
76 
77  return result;
78 }

◆ resetNodes()

void SLAnimation::resetNodes ( )

Resets all default animation targets to their initial state.

Definition at line 162 of file SLAnimation.cpp.

163 {
164  for (auto it : _nodeAnimTracks)
165  it.second->animatedNode()->resetToInitialState();
166 }

Member Data Documentation

◆ _lengthSec

SLfloat SLAnimation::_lengthSec
protected

duration of the animation in seconds

Definition at line 80 of file SLAnimation.h.

◆ _name

SLstring SLAnimation::_name
protected

name of the animation

Definition at line 79 of file SLAnimation.h.

◆ _nodeAnimTracks

SLMNodeAnimTrack SLAnimation::_nodeAnimTracks
protected

map of all the node tracks in this animation

Definition at line 81 of file SLAnimation.h.


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