TDME2  1.9.200
EntityHierarchy.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <string>
5 #include <unordered_map>
6 #include <vector>
7 
8 #include <tdme/tdme.h>
9 #include <tdme/engine/fwd-tdme.h>
11 #include <tdme/engine/Color4.h>
16 #include <tdme/engine/Camera.h>
17 #include <tdme/engine/Entity.h>
18 #include <tdme/engine/Object.h>
19 #include <tdme/engine/Rotation.h>
20 #include <tdme/engine/Transform.h>
21 #include <tdme/math/Matrix4x4.h>
22 #include <tdme/math/Vector3.h>
23 
24 using std::remove;
25 using std::string;
26 using std::to_string;
27 using std::unordered_map;
28 using std::vector;
29 
42 
43 /**
44  * Entity hierarchy to be used with engine class
45  * @author Andreas Drewke
46  */
48  public Transform,
49  public Entity
50 {
51 private:
54  string id;
56  Entity* entity { nullptr };
57  unordered_map<string, EntityHierarchyLevel*> children;
58  };
59  Engine* engine { nullptr };
60  Renderer* renderer { nullptr };
61  bool frustumCulling { true };
62  bool initialized { false };
63 
64  string id;
65  bool enabled;
66  bool pickable;
73  vector<Entity*> entities;
74  EntityHierarchyLevel entityRoot { string(), nullptr, nullptr };
75 
77 
81 
82  // overridden methods
83  inline void setParentTransform(const Transform& parentTransform) override {
84  //
85  this->parentTransform = parentTransform;
86  auto entityTransform = parentTransform * (*this);
87  entityTransformMatrix = entityTransform.getTransformMatrix();
89  //
91  }
92 
93  /**
94  * Get entity hierarchy level by given entity id
95  * @param id id
96  */
97  inline EntityHierarchyLevel* getEntityHierarchyLevel(const string& id) {
98  if (id.empty()) return &entityRoot;
100  }
101 
102  /**
103  * Retrieve entity hierarchy level by given entity id or nullptr if not found
104  * @param entityHierarchyLevel entity hierarchy level
105  * @param id entity id
106  * @return entity hierarchy level by given entity id or nullptr if not found
107  */
108  inline EntityHierarchyLevel* getEntityHierarchyLevel(EntityHierarchyLevel* entityHierarchyLevel, const string& id) {
109  if (id == entityHierarchyLevel->id) return entityHierarchyLevel;
110  for (const auto& [childEntityId, childEntity]: entityHierarchyLevel->children) {
111  auto childEntityHierarchyLevel = getEntityHierarchyLevel(childEntity, id);
112  if (childEntityHierarchyLevel != nullptr) return childEntityHierarchyLevel;
113  }
114  return nullptr;
115  }
116 
117  /**
118  * Update hierarchy from given entity hierarchy level ongoing
119  * @param parentTransform parent transform
120  * @param entityHierarchyLevel entity hierarchy level
121  * @param depth depth
122  * @param firstEntity first entity
123  */
124  void updateHierarchy(const Transform& parentTransform, EntityHierarchyLevel* entityHierarchyLevel, int depth, bool& firstEntity);
125 
126  /**
127  * Delete hierarchy from given entity hierarchy level ongoing
128  * @param entityHierarchyLevel entity hierarchy level
129  * @param depth depth
130  */
131  void deleteHierarchy(EntityHierarchyLevel* entityHierarchyLevel, int depth);
132 
133 public:
134  // forbid class copy
136 
137  /**
138  * Public constructor
139  * @param id id
140  */
141  EntityHierarchy(const string& id);
142 
143  /**
144  * Destructor
145  */
146  virtual ~EntityHierarchy();
147 
148  // overridden method
149  inline EntityType getEntityType() override {
151  }
152 
153  /**
154  * @return entity from hierarchy by given unique id
155  */
156  inline Entity* getEntity(const string& id) {
157  auto entityHierarchyLevel = getEntityHierarchyLevel(id);
158  if (entityHierarchyLevel == nullptr || entityHierarchyLevel->parent == nullptr) return nullptr;
159  return entityHierarchyLevel->entity;
160  }
161 
162  /**
163  * Adds a entity to the hierarchy
164  * @param entity entity to add
165  * @param parentId parent entity id to add entity to
166  */
167  void addEntity(Entity* entity, const string& parentId = string());
168 
169  /**
170  * Removes a entity from hierarchy by given unique entity id
171  */
172  void removeEntity(const string& id);
173 
174  /**
175  * Query sub entities of parent entity
176  * @param parentId parent entity id
177  * @return entities
178  */
179  const vector<Entity*> query(const string& parentId = string());
180 
181  /**
182  * @return entities
183  */
184  inline const vector<Entity*>& getEntities() {
185  return entities;
186  }
187 
188  /**
189  * Returns first found entity with given entity type
190  * @param entityType entity type
191  * @return entity
192  */
193  inline Entity* getEntityByType(EntityType entityType) {
194  for (auto entity: entities) {
195  if (entity->getEntityType() == entityType) {
196  return entity;
197  }
198  }
199  return nullptr;
200  }
201 
202  /**
203  * Return entities with given entity type
204  * @param entityType entity type
205  * @return entities by type
206  */
207  inline vector<Entity*> getEntitiesByType(EntityType entityType) {
208  vector<Entity*> entitiesByType;
209  for (auto entity: entities) {
210  if (entity->getEntityType() == entityType) {
211  entitiesByType.push_back(entity);
212  }
213  }
214  return entitiesByType;
215  }
216 
217  // overridden methods
218  void setEngine(Engine* engine) override;
219  void setRenderer(Renderer* renderer) override;
220  void initialize() override;
221  void dispose() override;
222 
223  inline bool isEnabled() override {
224  return enabled;
225  }
226 
227  void setEnabled(bool enabled) override;
228  bool isFrustumCulling() override;
229  void setFrustumCulling(bool frustumCulling) override;
230  void update() override;
231 
232  inline BoundingBox* getBoundingBox() override {
233  return &boundingBox;
234  }
235 
236  inline BoundingBox* getWorldBoundingBox() override {
237  return &worldBoundingBox;
238  }
239 
240  inline const Color4& getEffectColorMul() const override {
241  return effectColorMul;
242  }
243 
244  inline void setEffectColorMul(const Color4& effectColorMul) override {
245  this->effectColorMul = effectColorMul;
246  for (auto entity: entities) entity->setEffectColorMul(effectColorMul);
247  }
248 
249  inline const Color4& getEffectColorAdd() const override {
250  return effectColorAdd;
251  }
252 
253  inline void setEffectColorAdd(const Color4& effectColorAdd) override {
254  this->effectColorAdd = effectColorAdd;
255  for (auto entity: entities) entity->setEffectColorAdd(effectColorAdd);
256  }
257 
258  inline const string& getId() override {
259  return id;
260  }
261 
262  inline bool isContributesShadows() override {
263  return contributesShadows;
264  }
265 
266  inline void setContributesShadows(bool contributesShadows) override {
267  this->contributesShadows = contributesShadows;
268  for (auto entity: entities) entity->setContributesShadows(contributesShadows);
269  }
270 
271  inline bool isReceivesShadows() override {
272  return receivesShadows;
273  }
274 
275  inline void setReceivesShadows(bool receivesShadows) override {
276  this->receivesShadows = receivesShadows;
277  for (auto entity: entities) entity->setReceivesShadows(receivesShadows);
278  }
279 
280  inline bool isPickable() override {
281  return pickable;
282  }
283 
284  inline void setPickable(bool pickable) override {
285  this->pickable = pickable;
286  for (auto entity: entities) entity->setPickable(pickable);
287  }
288 
289  inline const Vector3& getTranslation() const override {
290  return Transform::getTranslation();
291  }
292 
293  inline void setTranslation(const Vector3& translation) override {
295  }
296 
297  inline const Vector3& getScale() const override {
298  return Transform::getScale();
299  }
300 
301  inline void setScale(const Vector3& scale) override {
303  }
304 
305  inline const int getRotationCount() const override {
307  }
308 
309  inline Rotation& getRotation(const int idx) override {
310  return Transform::getRotation(idx);
311  }
312 
313  inline void addRotation(const Vector3& axis, const float angle) override {
314  Transform::addRotation(axis, angle);
315  }
316 
317  inline void removeRotation(const int idx) override {
319  }
320 
321  inline const Vector3& getRotationAxis(const int idx) const override {
322  return Transform::getRotationAxis(idx);
323  }
324 
325  inline void setRotationAxis(const int idx, const Vector3& axis) override {
326  Transform::setRotationAxis(idx, axis);
327  }
328 
329  inline const float getRotationAngle(const int idx) const override {
330  return Transform::getRotationAngle(idx);
331  }
332 
333  inline void setRotationAngle(const int idx, const float angle) override {
334  Transform::setRotationAngle(idx, angle);
335  }
336 
337  inline const Quaternion& getRotationsQuaternion() const override {
339  }
340 
341  inline const Matrix4x4& getTransformMatrix() const override {
342  return entityTransformMatrix;
343  }
344 
345  inline const Transform& getParentTransform() const override {
346  return parentTransform;
347  }
348 
349  inline const Transform& getTransform() const override {
350  return *this;
351  }
352 
353  void setTransform(const Transform& transform) override;
354 
355  inline RenderPass getRenderPass() const override {
356  return renderPass;
357  }
358 
359  inline void setRenderPass(RenderPass renderPass) override {
360  this->renderPass = renderPass;
361  }
362 
363 };
Color 4 definition class.
Definition: Color4.h:18
Engine main class.
Definition: Engine.h:131
Entity hierarchy to be used with engine class.
BoundingBox * getBoundingBox() override
const Vector3 & getTranslation() const override
void updateHierarchy(const Transform &parentTransform, EntityHierarchyLevel *entityHierarchyLevel, int depth, bool &firstEntity)
Update hierarchy from given entity hierarchy level ongoing.
const Transform & getTransform() const override
RenderPass getRenderPass() const override
void dispose() override
Dispose this entity.
void setTranslation(const Vector3 &translation) override
Set translation.
void removeRotation(const int idx) override
Remove rotation.
Rotation & getRotation(const int idx) override
Get rotation at given index.
const int getRotationCount() const override
Entity * getEntityByType(EntityType entityType)
Returns first found entity with given entity type.
const Vector3 & getRotationAxis(const int idx) const override
void initialize() override
Initiates this entity.
void setRenderPass(RenderPass renderPass) override
Set render pass.
void setReceivesShadows(bool receivesShadows) override
Enable/disable receives shadows.
void addRotation(const Vector3 &axis, const float angle) override
Add rotation.
void update() override
Update transform.
void setPickable(bool pickable) override
Set this entity pickable.
void setEffectColorMul(const Color4 &effectColorMul) override
Set effect color that will be multiplied with fragment color.
vector< Entity * > getEntitiesByType(EntityType entityType)
Return entities with given entity type.
void addEntity(Entity *entity, const string &parentId=string())
Adds a entity to the hierarchy.
void setTransform(const Transform &transform) override
Set transform.
void deleteHierarchy(EntityHierarchyLevel *entityHierarchyLevel, int depth)
Delete hierarchy from given entity hierarchy level ongoing.
EntityHierarchyLevel entityRoot
const vector< Entity * > & getEntities()
EntityHierarchyLevel * getEntityHierarchyLevel(const string &id)
Get entity hierarchy level by given entity id.
const Quaternion & getRotationsQuaternion() const override
void setScale(const Vector3 &scale) override
Set scale.
bool isContributesShadows() override
const Vector3 & getScale() const override
const vector< Entity * > query(const string &parentId=string())
Query sub entities of parent entity.
const float getRotationAngle(const int idx) const override
BoundingBox * getWorldBoundingBox() override
void removeEntity(const string &id)
Removes a entity from hierarchy by given unique entity id.
const Color4 & getEffectColorMul() const override
The effect color will be multiplied with fragment color.
void setFrustumCulling(bool frustumCulling) override
Set frustum culling.
void setEngine(Engine *engine) override
Set up engine.
Entity * getEntity(const string &id)
void setEnabled(bool enabled) override
Enable/disable rendering.
EntityHierarchyLevel * getEntityHierarchyLevel(EntityHierarchyLevel *entityHierarchyLevel, const string &id)
Retrieve entity hierarchy level by given entity id or nullptr if not found.
const Transform & getParentTransform() const override
const Matrix4x4 & getTransformMatrix() const override
const string & getId() override
EntityType getEntityType() override
void setContributesShadows(bool contributesShadows) override
Enable/disable contributes shadows.
void setRotationAxis(const int idx, const Vector3 &axis) override
Set rotation axis.
virtual ~EntityHierarchy()
Destructor.
void setEffectColorAdd(const Color4 &effectColorAdd) override
Set effect color that will be added to fragment color.
const Color4 & getEffectColorAdd() const override
The effect color will be added to fragment color.
void setParentTransform(const Transform &parentTransform) override
Set parent transform.
void setRenderer(Renderer *renderer) override
Set up renderer.
void setRotationAngle(const int idx, const float angle) override
Engine entity.
Definition: Entity.h:30
virtual void setEffectColorMul(const Color4 &effectColorMul)=0
Set effect color that will be multiplied with fragment color.
virtual EntityType getEntityType()=0
virtual void setContributesShadows(bool contributesShadows)=0
Enable/disable contributes shadows.
friend class EntityHierarchy
Definition: Entity.h:32
@ ENTITYTYPE_ENTITYHIERARCHY
Definition: Entity.h:89
virtual void setPickable(bool pickable)=0
Set this entity pickable.
virtual void setReceivesShadows(bool receivesShadows)=0
Enable/disable receives shadows.
virtual void setEffectColorAdd(const Color4 &effectColorAdd)=0
Set effect color that will be added to fragment color.
Object to be used with engine class.
Definition: Object.h:60
Rotation representation.
Definition: Rotation.h:18
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
void setRotationAngle(const int idx, const float angle)
Definition: Transform.h:155
void setRotationAxis(const int idx, const Vector3 &axis)
Set rotation axis.
Definition: Transform.h:138
void setTranslation(const Vector3 &translation)
Set translation.
Definition: Transform.h:64
void removeRotation(const int idx)
Remove rotation.
Definition: Transform.h:121
const Vector3 & getScale() const
Definition: Transform.h:71
Rotation & getRotation(const int idx)
Get rotation at given index.
Definition: Transform.h:95
void setScale(const Vector3 &scale)
Set scale.
Definition: Transform.h:79
const int getRotationCount() const
Definition: Transform.h:86
const Vector3 & getRotationAxis(const int idx) const
Definition: Transform.h:129
const Quaternion & getRotationsQuaternion() const
Definition: Transform.h:162
const Vector3 & getTranslation() const
Definition: Transform.h:56
void addRotation(const Vector3 &axis, const float angle)
Add rotation.
Definition: Transform.h:113
const float getRotationAngle(const int idx) const
Definition: Transform.h:146
Representation of a 3D model.
Definition: Model.h:35
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
void fromBoundingVolumeWithTransformMatrix(BoundingBox *original, const Matrix4x4 &transformMatrix)
Create bounding volume from given original(of same type) with applied transform matrix.
Definition: BoundingBox.cpp:79
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
Matrix4x4 clone() const
Clones this matrix.
Definition: Matrix4x4.h:619
Matrix4x4 & invert()
Inverts this matrix.
Definition: Matrix4x4.h:479
Quaternion class representing quaternion mathematical structure and operations with x,...
Definition: Quaternion.h:24
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
unordered_map< string, EntityHierarchyLevel * > children
EntityHierarchyLevel(const string &id, EntityHierarchyLevel *parent, Entity *entity)
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6