TDME2  1.9.200
Model.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 #include <unordered_map>
6 #include <vector>
7 
8 #include <tdme/tdme.h>
12 #include <tdme/math/fwd-tdme.h>
13 #include <tdme/math/Matrix4x4.h>
15 
16 using std::string;
17 using std::unique_ptr;
18 using std::unordered_map;
19 using std::vector;
20 
21 using std::string;
29 
30 /**
31  * Representation of a 3D model
32  * @author andreas.drewke
33  */
35 {
36  friend class Node;
37 
38 public:
40 
42  STATIC_DLL_IMPEXT static constexpr float FPS_DEFAULT { 30.0f };
43  STATIC_DLL_IMPEXT static uint32_t uidCounter;
44 
45 private:
47  uint32_t uniqueId;
48  string id;
49  string name;
53  unordered_map<string, Material*> materials;
54  unordered_map<string, Node*> nodes;
55  unordered_map<string, Node*> subNodes;
56  bool skinning;
57  float fps;
58  unordered_map<string, AnimationSetup*> animationSetups;
60  unique_ptr<BoundingBox> boundingBox;
62 
65 
66  /**
67  * Delete sub nodes
68  * @param subNodes sub nodes
69  */
70  void deleteSubNodes(const unordered_map<string, Node*>& subNodes);
71 
72  /**
73  * Set up if model has skinning
74  * @param hasSkinning has skinning
75  */
76  inline void setHasSkinning(bool hasSkinning) {
78  }
79 
80  /**
81  * Computes a transform matrix at a given frame for a given node id recursivly
82  * @param nodes nodes
83  * @param parentTransformMatrix parent transform matrix
84  * @param frame frame
85  * @param nodeId node id
86  * @param transformMatrix transform matrix
87  * @return target node transform
88  */
89  bool computeTransformMatrix(const unordered_map<string, Node*>& nodes, const Matrix4x4& parentTransformMatrix, int32_t frame, const string& nodeId, Matrix4x4& transformMatrix);
90 
91 public:
92  // forbid class copy
94 
95  /**
96  * Public constructor
97  * @param id id
98  * @param name name
99  * @param upVector up vector
100  * @param rotationOrder rotation order
101  * @param boundingBox bounding box
102  * @param authoringTool authoring tool
103  */
105 
106  /**
107  * Deconstructor
108  */
109  ~Model();
110 
111  /**
112  * @return authoring tool
113  */
115  return authoringTool;
116  }
117 
118  /**
119  * @return unique model id
120  */
121  inline uint32_t getUniqueId() {
122  return uniqueId;
123  }
124 
125  /**
126  * @return model id
127  */
128  inline const string& getId() {
129  return id;
130  }
131 
132  /**
133  * @return model name
134  */
135  inline const string& getName() {
136  return name;
137  }
138 
139  /**
140  * @return up vector
141  */
142  inline UpVector* getUpVector() {
143  return upVector;
144  }
145 
146  /**
147  * Set up vector
148  * @param upVector up vector
149  */
150  inline void setUpVector(UpVector* upVector) {
151  this->upVector = upVector;
152  }
153 
154  /**
155  * @return rotation order
156  */
158  return rotationOrder;
159  }
160 
161  /**
162  * @return preferred shader model
163  */
165  return shaderModel;
166  }
167 
168  /**
169  * Set preferred shader model
170  * @param shaderModel preferred shader model
171  */
173  this->shaderModel = shaderModel;
174  }
175 
176  /**
177  * @return material ids
178  */
179  const vector<string> getMaterialIds();
180 
181  /**
182  * Returns all object materials
183  * @return materials
184  */
185  inline unordered_map<string, Material*>& getMaterials() {
186  return materials;
187  }
188 
189  /**
190  * Returns all object's node ids
191  * @return all node ids
192  */
193  const vector<string> getNodeIds();
194 
195  /**
196  * Returns all object's nodes
197  * @return all nodes
198  */
199  inline unordered_map<string, Node*>& getNodes() {
200  return nodes;
201  }
202 
203  /**
204  * Returns a node by given name or null
205  * @param id id
206  * @return
207  */
208  inline Node* getNodeById(const string& id) {
209  auto nodeIt = nodes.find(id);
210  if (nodeIt != nodes.end()) {
211  return nodeIt->second;
212  }
213  return nullptr;
214  }
215 
216  /**
217  * Returns object's sub nodes
218  * @return sub nodes
219  */
220  inline unordered_map<string, Node*>& getSubNodes() {
221  return subNodes;
222  }
223 
224  /**
225  * Returns a sub node by given name or null
226  * @param id id
227  * @return
228  */
229  inline Node* getSubNodeById(const string& id) {
230  auto nodeIt = subNodes.find(id);
231  if (nodeIt != subNodes.end()) {
232  return nodeIt->second;
233  }
234  return nullptr;
235  }
236 
237  /**
238  * @return has skinning
239  */
240  inline bool hasSkinning() {
241  return skinning;
242  }
243 
244  /**
245  * @return frames per seconds
246  */
247  inline float getFPS() {
248  return fps;
249  }
250 
251  /**
252  * Set model animation frames per seconds
253  * @param fps fps
254  */
255  inline void setFPS(float fps) {
256  this->fps = fps;
257  }
258 
259  /**
260  * @return animation setups
261  */
262  inline const unordered_map<string, AnimationSetup*>& getAnimationSetups() {
263  return animationSetups;
264  }
265 
266  /**
267  * @return animation setup ids
268  */
269  const vector<string> getAnimationSetupIds();
270 
271  /**
272  * @return animation setup for given id or nullptr
273  */
274  inline AnimationSetup* getAnimationSetup(const string& id) {
275  auto animationSetupIt = animationSetups.find(id);
276  if (animationSetupIt != animationSetups.end()) {
277  return animationSetupIt->second;
278  }
279  return nullptr;
280  }
281 
282  /**
283  * Adds an base animation setup
284  * @param id id
285  * @param startFrame start frame
286  * @param endFrame end frame
287  * @param loop loop
288  * @param speed speed whereas 1.0 is default speed
289  * @return animation setup
290  */
291  AnimationSetup* addAnimationSetup(const string& id, int32_t startFrame, int32_t endFrame, bool loop, float speed = 1.0f);
292 
293  /**
294  * Adds an overlay animation setup
295  * @param id id
296  * @param overlayFromNodeId overlay from node id
297  * @param startFrame start frame
298  * @param endFrame end frame
299  * @param loop loop
300  * @param speed speed whereas 1.0 is default speed
301  * @return animation setup
302  */
303  AnimationSetup* addOverlayAnimationSetup(const string& id, const string& overlayFromNodeId, int32_t startFrame, int32_t endFrame, bool loop, float speed = 1.0f);
304 
305  /**
306  * Rename animation set up
307  * @param id id
308  * @param newId new id
309  * @return success
310  */
311  bool renameAnimationSetup(const string& id, const string& newId);
312 
313  /**
314  * Remove animation setup
315  * @param id id
316  * @return success
317  */
318  bool removeAnimationSetup(const string& id);
319 
320  /**
321  * Clear animation setups
322  */
323  void clearAnimationSetups();
324 
325  /**
326  * @return if model has animations
327  */
328  inline bool hasAnimations() {
329  // no registered animation
330  if (animationSetups.size() == 0) return false;
331  // default animation with frames > 0
332  return
335  }
336 
337  /**
338  * @return import transform matrix like converting Z-UP to Y-UP
339  */
341  return importTransformMatrix;
342  }
343 
344  /**
345  * Set import transform matrix
346  * @param importTransformMatrix import transform matrix like converting Z-UP to Y-UP
347  */
349  this->importTransformMatrix = importTransformMatrix;
350  }
351 
352  /**
353  * @return bounding box
354  */
356 
357  /**
358  * Invalidates bounding box
359  */
360  void invalidateBoundingBox();
361 
362  /**
363  * Computes a transform matrix at a given frame for a given node id recursivly
364  * @param nodeId node id
365  * @param parentTransformMatrix parent transform matrix
366  * @param transformMatrix target node transform matrix
367  * @param frame frame or -1 for not using animation data
368  */
369  inline bool computeTransformMatrix(const string& nodeId, const Matrix4x4& parentTransformMatrix, Matrix4x4& transformMatrix, int32_t frame = -1) {
370  return computeTransformMatrix(subNodes, parentTransformMatrix, frame, nodeId, transformMatrix);
371  }
372 
373  /**
374  * Computes a transform matrix at a given frame for a given node id recursivly
375  * @param nodeId node id
376  * @param transformMatrix target node transform matrix
377  * @param frame frame
378  */
379  inline bool computeTransformMatrix(const string& nodeId, Matrix4x4& transformMatrix, int32_t frame = 0) {
380  return computeTransformMatrix(subNodes, importTransformMatrix, frame, nodeId, transformMatrix);
381  }
382 
383  /**
384  * @return if vertices have been updated
385  */
386  inline bool hasBoundingBoxUpdate() {
387  auto updated = boundingBoxUpdated;
388  boundingBoxUpdated = false;
389  return updated;
390  }
391 
392  /**
393  * @return if specular textures will be embedded in model files
394  */
395  inline bool hasEmbeddedSpecularTextures() const {
396  return embedSpecularTextures;
397  }
398 
399  /**
400  * Set if to embed specular textures
401  * @param embedTextures embed specular textures
402  */
403  inline void setEmbedSpecularTextures(bool embedTextures) {
404  this->embedSpecularTextures = embedTextures;
405  }
406 
407  /**
408  * @return if PBR textures will be embedded in model files
409  */
410  inline bool hasEmbeddedPBRTextures() const {
411  return embedPBRTextures;
412  }
413 
414  /**
415  * Set if to embed PBR textures
416  * @param embedTextures embed PBR textures
417  */
418  inline void setEmbedPBRTextures(bool embedTextures) {
419  this->embedPBRTextures = embedTextures;
420  }
421 
422 };
Representation of a 3D model.
Definition: Model.h:35
const vector< string > getMaterialIds()
Definition: Model.cpp:80
unordered_map< string, Node * > & getSubNodes()
Returns object's sub nodes.
Definition: Model.h:220
unordered_map< string, Material * > & getMaterials()
Returns all object materials.
Definition: Model.h:185
void setImportTransformMatrix(const Matrix4x4 &importTransformMatrix)
Set import transform matrix.
Definition: Model.h:348
bool computeTransformMatrix(const string &nodeId, Matrix4x4 &transformMatrix, int32_t frame=0)
Computes a transform matrix at a given frame for a given node id recursivly.
Definition: Model.h:379
~Model()
Deconstructor.
Definition: Model.cpp:67
void clearAnimationSetups()
Clear animation setups.
Definition: Model.cpp:143
RotationOrder * getRotationOrder()
Definition: Model.h:157
AuthoringTool authoringTool
Definition: Model.h:46
bool hasEmbeddedPBRTextures() const
Definition: Model.h:410
AnimationSetup * getAnimationSetup(const string &id)
Definition: Model.h:274
unordered_map< string, Node * > nodes
Definition: Model.h:54
void setShaderModel(ShaderModel *shaderModel)
Set preferred shader model.
Definition: Model.h:172
void invalidateBoundingBox()
Invalidates bounding box.
Definition: Model.cpp:192
AuthoringTool getAuthoringTool()
Definition: Model.h:114
bool removeAnimationSetup(const string &id)
Remove animation setup.
Definition: Model.cpp:135
unordered_map< string, Node * > subNodes
Definition: Model.h:55
RotationOrder * rotationOrder
Definition: Model.h:51
static STATIC_DLL_IMPEXT string ANIMATIONSETUP_DEFAULT
Definition: Model.h:41
const Matrix4x4 & getImportTransformMatrix()
Definition: Model.h:340
static constexpr STATIC_DLL_IMPEXT float FPS_DEFAULT
Definition: Model.h:42
void setUpVector(UpVector *upVector)
Set up vector.
Definition: Model.h:150
unordered_map< string, Node * > & getNodes()
Returns all object's nodes.
Definition: Model.h:199
void setHasSkinning(bool hasSkinning)
Set up if model has skinning.
Definition: Model.h:76
ShaderModel * getShaderModel()
Definition: Model.h:164
void setEmbedPBRTextures(bool embedTextures)
Set if to embed PBR textures.
Definition: Model.h:418
void setEmbedSpecularTextures(bool embedTextures)
Set if to embed specular textures.
Definition: Model.h:403
Model(const string &id, const string &name, UpVector *upVector, RotationOrder *rotationOrder, BoundingBox *boundingBox, AuthoringTool authoringTool=AUTHORINGTOOL_UNKNOWN)
Public constructor.
Definition: Model.cpp:49
Node * getSubNodeById(const string &id)
Returns a sub node by given name or null.
Definition: Model.h:229
static STATIC_DLL_IMPEXT uint32_t uidCounter
Definition: Model.h:43
const vector< string > getNodeIds()
Returns all object's node ids.
Definition: Model.cpp:87
const string & getName()
Definition: Model.h:135
AnimationSetup * addAnimationSetup(const string &id, int32_t startFrame, int32_t endFrame, bool loop, float speed=1.0f)
Adds an base animation setup.
Definition: Model.cpp:101
AnimationSetup * addOverlayAnimationSetup(const string &id, const string &overlayFromNodeId, int32_t startFrame, int32_t endFrame, bool loop, float speed=1.0f)
Adds an overlay animation setup.
Definition: Model.cpp:113
UpVector * upVector
Definition: Model.h:50
bool hasEmbeddedSpecularTextures() const
Definition: Model.h:395
ShaderModel * shaderModel
Definition: Model.h:52
unordered_map< string, Material * > materials
Definition: Model.h:53
unique_ptr< BoundingBox > boundingBox
Definition: Model.h:60
void deleteSubNodes(const unordered_map< string, Node * > &subNodes)
Delete sub nodes.
Definition: Model.cpp:73
bool computeTransformMatrix(const unordered_map< string, Node * > &nodes, const Matrix4x4 &parentTransformMatrix, int32_t frame, const string &nodeId, Matrix4x4 &transformMatrix)
Computes a transform matrix at a given frame for a given node id recursivly.
Definition: Model.cpp:159
const string & getId()
Definition: Model.h:128
UpVector * getUpVector()
Definition: Model.h:142
const vector< string > getAnimationSetupIds()
Definition: Model.cpp:94
const unordered_map< string, AnimationSetup * > & getAnimationSetups()
Definition: Model.h:262
bool renameAnimationSetup(const string &id, const string &newId)
Rename animation set up.
Definition: Model.cpp:125
BoundingBox * getBoundingBox()
Definition: Model.cpp:150
uint32_t getUniqueId()
Definition: Model.h:121
bool computeTransformMatrix(const string &nodeId, const Matrix4x4 &parentTransformMatrix, Matrix4x4 &transformMatrix, int32_t frame=-1)
Computes a transform matrix at a given frame for a given node id recursivly.
Definition: Model.h:369
Matrix4x4 importTransformMatrix
Definition: Model.h:59
unordered_map< string, AnimationSetup * > animationSetups
Definition: Model.h:58
void setFPS(float fps)
Set model animation frames per seconds.
Definition: Model.h:255
Node * getNodeById(const string &id)
Returns a node by given name or null.
Definition: Model.h:208
Model node.
Definition: Node.h:32
Represents rotation orders of a model.
Definition: RotationOrder.h:23
Model up vector.
Definition: UpVector.h:20
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
#define STATIC_DLL_IMPEXT
Definition: tdme.h:15
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6