TDME2  1.9.200
ObjectAnimation.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <vector>
6 
7 #include <tdme/tdme.h>
8 #include <tdme/engine/fwd-tdme.h>
13 #include <tdme/engine/Engine.h>
14 #include <tdme/math/fwd-tdme.h>
15 
16 using std::string;
17 using std::unordered_map;
18 using std::vector;
19 
28 
29 /**
30  * Object animation class
31  * @author Andreas Drewke
32  */
34 {
39 private:
40  /**
41  * Determine skinned node count
42  * @param nodes nodes
43  */
44  int32_t determineSkinnedNodeCount(const unordered_map<string, Node*>& nodes);
45 
46  /**
47  * Determine skinned node count
48  * @param nodes nodes
49  * @param count current count
50  */
51  int32_t determineSkinnedNodeCount(const unordered_map<string, Node*>& nodes, int32_t count);
52 
53  /**
54  * Determine skinned nodes
55  * @param nodes nodes
56  * @param skinningNodes skinning nodes
57  * @param idx idx
58  */
59  int32_t determineSkinnedNodes(const unordered_map<string, Node*>& nodes, vector<Node*>& skinningNodes, int32_t idx);
60 
61 protected:
62  struct FlattenedNode {
64  const string& nodeId,
67  const Animation* nodeAnimation,
71  ):
72  nodeId(nodeId),
79  {}
80  const string nodeId;
87  };
90  const Joint* joint,
93  ):
94  joint(joint),
97  {}
98  const Joint* joint;
101  };
102 
105  unordered_map<string, Matrix4x4*> overriddenTransformMatrices;
106  vector<unordered_map<string, Matrix4x4*>> transformMatrices;
109  vector<unordered_map<string, Matrix4x4*>> skinningNodesMatrices;
110  vector<Node*> skinningNodes;
111  vector<vector<NodeSkinningJoint>> skinningNodesNodeSkinningJoints;
112  vector<AnimationState> baseAnimations;
114  unordered_map<string, AnimationState*> overlayAnimationsById;
115  unordered_map<string, AnimationState*> overlayAnimationsByJointId;
116  vector<vector<FlattenedNode>> nodeLists;
117 
118  // forbid class copy
120 
121  /**
122  * Public constructor
123  * @param model model
124  * @param animationProcessingTarget animation processing target
125  */
127 
128  /**
129  * Destructor
130  */
131  virtual ~ObjectAnimation();
132 
133  /**
134  * Creates all nodes transform matrices
135  * @param matrices matrices
136  * @param nodeList flattened node list
137  * @param nodes nodes
138  * @param parentTransformMatrix parent transform matrix
139  * @param animationState animation state
140  */
141  void createNodesTransformMatrices(unordered_map<string, Matrix4x4*>& matrices, vector<FlattenedNode>& nodeList, const unordered_map<string, Node*>& nodes, Matrix4x4* parentTransformMatrix = nullptr, AnimationState* animationState = nullptr);
142 
143  /**
144  * Update node list
145  * @param nodeList flattened node list
146  * @param nodeIdx node index
147  * @param nodes nodes
148  * @param animationState animation state
149  */
150  void updateNodeList(vector<FlattenedNode>& nodeList, int& nodeIdx, const unordered_map<string, Node*>& nodes, AnimationState* animationState = nullptr);
151 
152  /**
153  * Update node list
154  * @param nodeList flattened node list
155  */
156  inline void updateNodeList(vector<FlattenedNode>& nodeList) {
157  auto nodeIdx = 0;
158  updateNodeList(nodeList, nodeIdx, model->getSubNodes());
159  }
160 
161  /**
162  * Update node lists
163  */
164  inline void updateNodeLists() {
165  for (auto& nodeList: nodeLists) updateNodeList(nodeList);
166  }
167 
168  /**
169  * Comutes all nodes transform matrices
170  * @param nodeList flattened node list
171  * @param parentTransformMatrix parent transform matrix
172  * @param animationState animation state
173  */
174  void computeNodesTransformMatrices(vector<FlattenedNode>& nodeList, const Matrix4x4 parentTransformMatrix, AnimationState* animationState);
175 
176  /**
177  * Compute animation for given animation state into nodes transform matrices given by flattened node list
178  * @param nodeList flattened node list
179  * @param instanceTransformMatrix object transform matrix
180  * @param baseAnimation base animation
181  * @param contextIdx context index
182  * @param lastFrameAtTime time of last animation computation
183  * @param currentFrameAtTime time of current animation computation
184  */
185  void computeAnimation(vector<FlattenedNode>& nodeList, const Matrix4x4& instanceTransformMatrix, AnimationState& baseAnimation, int contextIdx, int64_t lastFrameAtTime, int64_t currentFrameAtTime);
186 
187  /**
188  * Update skinning transform matrices
189  */
190  void updateSkinningJoints();
191 
192  /**
193  * Get skinning nodes transform matrices
194  * @param node node
195  * @return matrices
196  */
197  unordered_map<string, Matrix4x4*>* getSkinningNodesTransformMatrices(Node* node);
198 
199 public:
200 
201  /**
202  * Sets up a base animation to play
203  * @param id id
204  * @param speed speed whereas 1.0 is default speed
205  */
206  void setAnimation(const string& id, float speed = 1.0f);
207 
208  /**
209  * Set up animation speed
210  * @param speed speed whereas 1.0 is default speed
211  */
212  void setAnimationSpeed(float speed);
213 
214  /**
215  * Overlays a animation above the base animation
216  * @param id id
217  */
218  void addOverlayAnimation(const string& id);
219 
220  /**
221  * Removes a overlay animation
222  * @param id id
223  */
224  void removeOverlayAnimation(const string& id);
225 
226  /**
227  * Removes all finished overlay animations
228  */
230 
231  /**
232  * Removes all overlay animations
233  */
235 
236  /**
237  * @return active animation setup id
238  */
239  const string getAnimation();
240 
241  /**
242  * Returns current base animation time
243  * @return 0.0 <= time <= 1.0
244  */
245  float getAnimationTime();
246 
247  /**
248  * Returns if there is currently running a overlay animation with given id
249  * @param id id
250  * @return animation is running
251  */
252  bool hasOverlayAnimation(const string& id);
253 
254  /**
255  * Returns current overlay animation time
256  * @param id id
257  * @return 0.0 <= time <= 1.0
258  */
259  float getOverlayAnimationTime(const string& id);
260 
261  /**
262  * Returns transform matrix for given node
263  * @param id node id
264  * @return transform matrix or identity matrix if not found
265  */
266  const Matrix4x4 getNodeTransformMatrix(const string& id);
267 
268  /**
269  * Set transform matrix for given node
270  * @param id node id
271  * @param matrix transform matrix
272  */
273  void setNodeTransformMatrix(const string& id, const Matrix4x4& matrix);
274 
275  /**
276  * Unset transform matrix for given node
277  * @param id node id
278  */
279  void unsetNodeTransformMatrix(const string& id);
280 
281  /**
282  * Computes animations
283  * @param contextIdx context index
284  * @param objectTransformMatrix object transform matrix
285  * @param lastFrameAtTime time of last animation computation
286  * @param currentFrameAtTime time of current animation computation
287  */
288  void computeAnimation(int contextIdx, const Matrix4x4& objectTransformMatrix, int64_t lastFrameAtTime, int64_t currentFrameAtTime);
289 
290 };
Engine main class.
Definition: Engine.h:131
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
Joint / Bone.
Definition: Joint.h:19
Representation of a 3D model.
Definition: Model.h:35
unordered_map< string, Node * > & getSubNodes()
Returns object's sub nodes.
Definition: Model.h:220
Model node.
Definition: Node.h:32
void setAnimation(const string &id, float speed=1.0f)
Sets up a base animation to play.
vector< unordered_map< string, Matrix4x4 * > > transformMatrices
void computeNodesTransformMatrices(vector< FlattenedNode > &nodeList, const Matrix4x4 parentTransformMatrix, AnimationState *animationState)
Comutes all nodes transform matrices.
const Matrix4x4 getNodeTransformMatrix(const string &id)
Returns transform matrix for given node.
void setNodeTransformMatrix(const string &id, const Matrix4x4 &matrix)
Set transform matrix for given node.
void computeAnimation(vector< FlattenedNode > &nodeList, const Matrix4x4 &instanceTransformMatrix, AnimationState &baseAnimation, int contextIdx, int64_t lastFrameAtTime, int64_t currentFrameAtTime)
Compute animation for given animation state into nodes transform matrices given by flattened node lis...
void addOverlayAnimation(const string &id)
Overlays a animation above the base animation.
unordered_map< string, AnimationState * > overlayAnimationsById
Engine::AnimationProcessingTarget animationProcessingTarget
float getOverlayAnimationTime(const string &id)
Returns current overlay animation time.
void createNodesTransformMatrices(unordered_map< string, Matrix4x4 * > &matrices, vector< FlattenedNode > &nodeList, const unordered_map< string, Node * > &nodes, Matrix4x4 *parentTransformMatrix=nullptr, AnimationState *animationState=nullptr)
Creates all nodes transform matrices.
void removeOverlayAnimation(const string &id)
Removes a overlay animation.
bool hasOverlayAnimation(const string &id)
Returns if there is currently running a overlay animation with given id.
unordered_map< string, Matrix4x4 * > overriddenTransformMatrices
void updateSkinningJoints()
Update skinning transform matrices.
vector< vector< NodeSkinningJoint > > skinningNodesNodeSkinningJoints
void updateNodeList(vector< FlattenedNode > &nodeList)
Update node list.
unordered_map< string, AnimationState * > overlayAnimationsByJointId
unordered_map< string, Matrix4x4 * > * getSkinningNodesTransformMatrices(Node *node)
Get skinning nodes transform matrices.
ObjectAnimation(Model *model, Engine::AnimationProcessingTarget animationProcessingTarget)
Public constructor.
int32_t determineSkinnedNodeCount(const unordered_map< string, Node * > &nodes)
Determine skinned node count.
void setAnimationSpeed(float speed)
Set up animation speed.
vector< unordered_map< string, Matrix4x4 * > > skinningNodesMatrices
void removeOverlayAnimations()
Removes all overlay animations.
void updateNodeList(vector< FlattenedNode > &nodeList, int &nodeIdx, const unordered_map< string, Node * > &nodes, AnimationState *animationState=nullptr)
Update node list.
void unsetNodeTransformMatrix(const string &id)
Unset transform matrix for given node.
float getAnimationTime()
Returns current base animation time.
void removeFinishedOverlayAnimations()
Removes all finished overlay animations.
int32_t determineSkinnedNodes(const unordered_map< string, Node * > &nodes, vector< Node * > &skinningNodes, int32_t idx)
Determine skinned nodes.
Object node specifically for rendering.
Definition: ObjectNode.h:41
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
FlattenedNode(const string &nodeId, const Matrix4x4 *nodeTransformMatrix, Matrix4x4 *nodeOverriddenTransformMatrix, const Animation *nodeAnimation, AnimationState *nodeAnimationState, const Matrix4x4 *parentTransformMatrix, Matrix4x4 *transformMatrix)
NodeSkinningJoint(const Joint *joint, const Matrix4x4 *nodeTransformMatrix, Matrix4x4 *skinningNodeTransformMatrix)
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6