TDME2  1.9.200
Object.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <string>
5 
6 #include <tdme/tdme.h>
7 #include <tdme/engine/fwd-tdme.h>
9 #include <tdme/engine/Color4.h>
19 #include <tdme/engine/Camera.h>
20 #include <tdme/engine/Engine.h>
21 #include <tdme/engine/Entity.h>
23 #include <tdme/engine/Timing.h>
24 #include <tdme/engine/Transform.h>
25 #include <tdme/math/Matrix4x4.h>
26 #include <tdme/math/Quaternion.h>
27 #include <tdme/math/Vector3.h>
28 
29 using std::map;
30 using std::string;
31 using std::to_string;
32 
52 
53 /**
54  * Object to be used with engine class
55  * @author Andreas Drewke
56  */
58  : public ObjectInternal
59  , public Entity
60 {
61 private:
62 
63  friend class Engine;
64  friend class ImposterObject;
65  friend class LODObject;
66  friend class LODObjectImposter;
67  friend class ObjectRenderGroup;
68  friend class ObjectParticleSystem;
72 
73  Engine* engine { nullptr };
74  bool frustumCulling { true };
76  string shaderId;
77  uint8_t uniqueShaderId { 0 };
82  bool disableDepthTest { false };
83  int64_t frameTransformLast { -1LL };
84  int64_t timeTransformLast { -1LL };
86  bool requiresPreRender { false };
87  bool requiresForwardShading { false };
89 
90  static constexpr int UNIQUEMODELID_NONE { -1 };
91 
93 
94  /**
95  * Set unique model id
96  * @param uniqueModelId unique model id
97  */
98  inline void setUniqueModelId(int uniqueModelId) {
99  this->uniqueModelId = uniqueModelId;
100  }
101 
102  /**
103  * @return unique model id
104  */
105  inline int getUniqueModelId() {
106  return uniqueModelId;
107  }
108 
109  /**
110  * @return if this object instance requries animation computation each frame
111  */
113  return model->hasSkinning() == true || model->hasAnimations() == true;
114  }
115 
116  /**
117  * Compute animations
118  * @param contextIdx context index
119  */
120  inline void computeAnimations(int contextIdx) {
121  auto timing = engine->getTiming();
122  auto currentFrameAtTime = timing->getCurrentFrameAtTime();
123  auto currentFrame = timing->getFrame();
124  auto distanceFromCamera = (engine->getCamera()->getLookFrom() - getWorldBoundingBox()->computeClosestPointInBoundingBox(engine->getCamera()->getLookFrom())).computeLengthSquared();
125  if (animationComputingLODEnabled == true) {
126  if (distanceFromCamera > Math::square(Engine::getAnimationComputationReduction2Distance())) {
127  if (frameTransformLast != -1LL && currentFrame - frameTransformLast < 4) return;
128  } else
129  if (distanceFromCamera > Math::square(Math::square(Engine::getAnimationComputationReduction1Distance()))) {
130  if (frameTransformLast != -1LL && currentFrame - frameTransformLast < 2) return;
131  }
132  }
133  computeAnimation(contextIdx, timeTransformLast, currentFrameAtTime);
134  frameTransformLast = timing->getFrame();
135  timeTransformLast = currentFrameAtTime;
136  }
137 
138  /**
139  * Compute animations
140  * @param contextIdx context index
141  * @param lastFrameAtTime time of last animation computation
142  * @param currentFrameAtTime time of current animation computation
143  */
144  inline void computeAnimation(int contextIdx, int64_t lastFrameAtTime, int64_t currentFrameAtTime) override {
145  ObjectInternal::computeAnimation(contextIdx, lastFrameAtTime, currentFrameAtTime);
146  }
147 
148  /**
149  * @return if this object instance requires a preRender() call each frame
150  */
151  inline bool isRequiringPreRender() {
152  return
153  requiresPreRender == true ||
155  }
156 
157  /**
158  * Pre render step like uploading VBOs and such
159  * @param contextIdx context index
160  */
161  inline void preRender(int contextIdx) {
162  if (model->hasBoundingBoxUpdate() == true) updateBoundingBox();
163  for (auto objectNode: objectNodes) {
164  if (objectNode->renderer->needsPreRender() == true) {
165  objectNode->renderer->preRender(contextIdx);
166  }
167  }
168  }
169 
170  /**
171  * @return if this object needs forward shading
172  */
174  return requiresForwardShading == true || reflectionEnvironmentMappingId.empty() == false;
175  }
176 
177  // overridden methods
178  inline void setParentTransform(const Transform& parentTransform) override {
179  //
180  ObjectInternal::setParentTransform(parentTransform);
181  //
183  }
184 
185 public:
186  // forbid class copy
188 
189  /**
190  * Public constructor
191  * @param id id
192  * @param model model
193  * @param instances instances to compute and render by duplication, which only is intended to be used with skinned meshes
194  */
195  Object(const string& id, Model* model, int instances);
196 
197  /**
198  * Public constructor
199  * @param id id
200  * @param model model
201  */
202  Object(const string& id, Model* model);
203 
204  /**
205  * Set up if this object instance needs a preRender() call each frame
206  * @param requiresPreRender requires pre render
207  */
209  this->requiresPreRender = requiresPreRender;
210  }
211 
212  // overridden methods
213  inline EntityType getEntityType() override {
214  return ENTITYTYPE_OBJECT;
215  }
216 
217  void setEngine(Engine* engine) override;
218  void setRenderer(Renderer* renderer) override;
219  void initialize() override;
220  void dispose() override;
221 
222  inline bool isEnabled() override {
223  return ObjectInternal::isEnabled();
224  }
225 
226  void setEnabled(bool enabled) override;
227  bool isFrustumCulling() override;
228  void setFrustumCulling(bool frustumCulling) override;
229  void update() override;
230 
231  inline BoundingBox* getBoundingBox() override {
232  return ObjectInternal::getBoundingBox();
233  }
234 
235  inline BoundingBox* getWorldBoundingBox() override {
236  return ObjectInternal::getWorldBoundingBox();
237  }
238 
239  inline const Color4& getEffectColorAdd() const override {
240  return ObjectInternal::getEffectColorAdd();
241  }
242 
243  inline void setEffectColorAdd(const Color4& effectColorAdd) override {
244  return ObjectInternal::setEffectColorAdd(effectColorAdd);
245  }
246 
247  inline const Color4& getEffectColorMul() const override {
248  return ObjectInternal::getEffectColorMul();
249  }
250 
251  inline void setEffectColorMul(const Color4& effectColorMul) override {
252  return ObjectInternal::setEffectColorMul(effectColorMul);
253  }
254 
255  inline const string& getId() override {
256  return ObjectInternal::getId();
257  }
258 
259  inline bool isContributesShadows() override {
260  return ObjectInternal::isContributesShadows();
261  }
262 
263  inline void setContributesShadows(bool contributesShadows) override {
264  ObjectInternal::setContributesShadows(contributesShadows);
265  }
266 
267  inline bool isReceivesShadows() override {
268  return ObjectInternal::isReceivesShadows();
269  }
270 
271  inline void setReceivesShadows(bool receivesShadows) override {
272  ObjectInternal::setReceivesShadows(receivesShadows);
273  }
274 
275  inline bool isPickable() override {
276  return ObjectInternal::isPickable();
277  }
278 
279  inline void setPickable(bool pickable) override {
280  ObjectInternal::setPickable(pickable);
281  }
282 
283  inline const Vector3& getTranslation() const override {
284  return instanceTransform[currentInstance].getTranslation();
285  }
286 
287  inline void setTranslation(const Vector3& translation) override {
288  instanceTransform[currentInstance].setTranslation(translation);
289  }
290 
291  inline const Vector3& getScale() const override {
292  return instanceTransform[currentInstance].getScale();
293  }
294 
295  inline void setScale(const Vector3& scale) override {
296  instanceTransform[currentInstance].setScale(scale);
297  }
298 
299  inline const int getRotationCount() const override {
300  return instanceTransform[currentInstance].getRotationCount();
301  }
302 
303  inline Rotation& getRotation(const int idx) override {
304  return instanceTransform[currentInstance].getRotation(idx);
305  }
306 
307  inline void addRotation(const Vector3& axis, const float angle) override {
308  instanceTransform[currentInstance].addRotation(axis, angle);
309  }
310 
311  inline void removeRotation(const int idx) override {
312  instanceTransform[currentInstance].removeRotation(idx);
313  }
314 
315  inline const Vector3& getRotationAxis(const int idx) const override {
316  return instanceTransform[currentInstance].getRotationAxis(idx);
317  }
318 
319  inline void setRotationAxis(const int idx, const Vector3& axis) override {
320  instanceTransform[currentInstance].setRotationAxis(idx, axis);
321  }
322 
323  inline const float getRotationAngle(const int idx) const override {
324  return instanceTransform[currentInstance].getRotationAngle(idx);
325  }
326 
327  inline void setRotationAngle(const int idx, const float angle) override {
328  instanceTransform[currentInstance].setRotationAngle(idx, angle);
329  }
330 
331  inline const Quaternion& getRotationsQuaternion() const override {
332  return instanceTransform[currentInstance].getRotationsQuaternion();
333  }
334 
335  inline const Matrix4x4& getTransformMatrix() const override {
336  return ObjectBase::getTransformMatrix();
337  }
338 
339  inline const Transform& getParentTransform() const override {
340  return parentTransform;
341  }
342 
343  inline const Transform& getTransform() const override {
345  }
346 
347  void setTransform(const Transform& transform) override;
348 
349  inline RenderPass getRenderPass() const override {
350  return renderPass;
351  }
352 
353  inline void setRenderPass(RenderPass renderPass) override {
354  this->renderPass = renderPass;
355  }
356 
357  /**
358  * @return shader id
359  */
360  inline const string& getShader() {
361  return shaderId;
362  }
363 
364  /**
365  * Set shader
366  * @param id shader id
367  */
368  void setShader(const string& id);
369 
370  /**
371  * @return unique shader id
372  */
373  inline uint8_t getUniqueShaderId() {
374  return uniqueShaderId;
375  }
376 
377  /**
378  * @return reflection environment mapping id
379  */
380  inline const string& getReflectionEnvironmentMappingId() {
382  }
383 
384  /**
385  * @return reflection environment mapping id
386  */
388  this->reflectionEnvironmentMappingId = reflectionEnvironmentMappingId;
389  }
390 
391  /**
392  * @return if object has a reflection environment mapping position
393  */
396  }
397 
398  /**
399  * @return reflection environment mapping position
400  */
403  }
404 
405  /**
406  * Set reflection environment mapping position
407  * @param reflectionEnvironmentMappingPosition reflection environment mapping position
408  */
411  this->reflectionEnvironmentMappingPosition = reflectionEnvironmentMappingPosition;
412  }
413 
414  /**
415  * Unset reflection environment mapping position
416  */
419  this->reflectionEnvironmentMappingPosition.set(0.0f, 0.0f, 0.0f);
420  }
421 
422  /**
423  * @return if to exclude from a certain effect pass
424  */
426  return excludeFromEffectPass;
427  }
428 
429  /**
430  * Set exclude from effect pass
431  * @param effectPass effect pass
432  */
433  inline void setExcludeEffectPass(Engine::EffectPass effectPass) {
434  this->excludeFromEffectPass = effectPass;
435  }
436 
437  /**
438  * @return if depth test is disabled
439  */
440  inline bool isDisableDepthTest() const {
441  return disableDepthTest;
442  }
443 
444  /**
445  * Set disable depth test
446  * @param disableDepthTest disable depth test
447  */
449  this->disableDepthTest = disableDepthTest;
450  }
451 
452  /**
453  * Returns shader parameter for given parameter name, if the value does not exist, the default will be returned
454  * @param shaderId shader id
455  * @param parameterName parameter name
456  * @return shader parameter
457  */
458  inline const ShaderParameter getShaderParameter(const string& parameterName) {
459  return shaderParameters.getShaderParameter(parameterName);
460  }
461 
462  /**
463  * Set shader parameter for given parameter name
464  * @param shaderId shader id
465  * @param parameterName parameter name
466  * @param paraemterValue parameter value
467  */
468  inline void setShaderParameter(const string& parameterName, const ShaderParameter& parameterValue) {
469  shaderParameters.setShaderParameter(parameterName, parameterValue);
470  }
471 
472  /**
473  * @return if transform computing LOD is enabled
474  */
475  inline bool isAnimationComputationLODEnabled() const {
477  }
478 
479  /**
480  * Set transform computing LOD enabled
481  * @param animationComputationLODEnabled animation computation LOD enabled
482  */
483  inline void setAnimationComputationLODEnabled(bool animationComputationLODEnabled) {
484  this->animationComputingLODEnabled = animationComputationLODEnabled;
485  }
486 
487 };
const Vector3 & getLookFrom() const
Definition: Camera.h:219
Color 4 definition class.
Definition: Color4.h:18
Engine main class.
Definition: Engine.h:131
static float getAnimationComputationReduction2Distance()
Definition: Engine.h:808
Camera * getCamera()
Definition: Engine.h:1071
static STATIC_DLL_IMPEXT AnimationProcessingTarget animationProcessingTarget
Definition: Engine.h:212
static float getAnimationComputationReduction1Distance()
Definition: Engine.h:793
Timing * getTiming()
Definition: Engine.h:1064
TDME2 engine entity shader parameters.
void setShaderParameter(const string &parameterName, const ShaderParameter &parameterValue)
Set shader parameter for given parameter name.
const ShaderParameter getShaderParameter(const string &parameterName) const
Returns shader parameter for given parameter name, if the value does not exist, the default will be r...
Engine entity.
Definition: Entity.h:30
Imposter object to be used with engine class.
LOD object + imposter to be used with engine class.
LOD object to be used with engine class.
Definition: LODObject.h:49
Object particle system entity to be used with engine class.
Object render group for static objects that might be animated by shaders.
Object to be used with engine class.
Definition: Object.h:60
bool animationComputingLODEnabled
Definition: Object.h:88
BoundingBox * getBoundingBox() override
Definition: Object.h:231
Engine::EffectPass excludeFromEffectPass
Definition: Object.h:81
const Vector3 & getTranslation() const override
Definition: Object.h:283
const string & getReflectionEnvironmentMappingId()
Definition: Object.h:380
Engine * engine
Definition: Object.h:73
const Transform & getTransform() const override
Definition: Object.h:343
RenderPass getRenderPass() const override
Definition: Object.h:349
void dispose() override
Dispose this entity.
Definition: Object.cpp:106
void setShader(const string &id)
Set shader.
Definition: Object.cpp:116
void setTranslation(const Vector3 &translation) override
Set translation.
Definition: Object.h:287
bool hasReflectionEnvironmentMappingPosition()
Definition: Object.h:394
void removeRotation(const int idx) override
Remove rotation.
Definition: Object.h:311
Rotation & getRotation(const int idx) override
Get rotation at given index.
Definition: Object.h:303
RenderPass renderPass
Definition: Object.h:75
const int getRotationCount() const override
Definition: Object.h:299
const Vector3 & getRotationAxis(const int idx) const override
Definition: Object.h:315
void initialize() override
Initiates this entity.
Definition: Object.cpp:111
bool isReceivesShadows() override
Definition: Object.h:267
EntityShaderParameters shaderParameters
Definition: Object.h:85
uint8_t uniqueShaderId
Definition: Object.h:77
Engine::EffectPass getExcludeFromEffectPass() const
Definition: Object.h:425
void setRenderPass(RenderPass renderPass) override
Set render pass.
Definition: Object.h:353
void setReceivesShadows(bool receivesShadows) override
Enable/disable receives shadows.
Definition: Object.h:271
const string & getShader()
Definition: Object.h:360
void addRotation(const Vector3 &axis, const float angle) override
Add rotation.
Definition: Object.h:307
void update() override
Update transform.
Definition: Object.cpp:57
Object(const string &id, Model *model, int instances)
Public constructor.
Definition: Object.cpp:29
bool requiresForwardShading
Definition: Object.h:87
void unsetReflectionEnvironmentMappingPosition()
Unset reflection environment mapping position.
Definition: Object.h:417
void setPickable(bool pickable) override
Set this entity pickable.
Definition: Object.h:279
bool reflectionEnvironmentMappingPositionSet
Definition: Object.h:79
friend class SkinnedObjectRenderGroup
Definition: Object.h:69
void setEffectColorMul(const Color4 &effectColorMul) override
Set effect color that will be multiplied with fragment color.
Definition: Object.h:251
static constexpr int UNIQUEMODELID_NONE
Definition: Object.h:90
const ShaderParameter getShaderParameter(const string &parameterName)
Returns shader parameter for given parameter name, if the value does not exist, the default will be r...
Definition: Object.h:458
bool isPickable() override
Definition: Object.h:275
void setUniqueModelId(int uniqueModelId)
Set unique model id.
Definition: Object.h:98
int getUniqueModelId()
Definition: Object.h:105
void setShaderParameter(const string &parameterName, const ShaderParameter &parameterValue)
Set shader parameter for given parameter name.
Definition: Object.h:468
bool requiresPreRender
Definition: Object.h:86
void setTransform(const Transform &transform) override
Set transform.
Definition: Object.cpp:50
bool isAnimationComputationLODEnabled() const
Definition: Object.h:475
uint8_t getUniqueShaderId()
Definition: Object.h:373
void setRequiresPreRender(bool requiresPreRender)
Set up if this object instance needs a preRender() call each frame.
Definition: Object.h:208
void setReflectionEnvironmentMappingId(const string &reflectionEnvironmentMappingId)
Definition: Object.h:387
const Quaternion & getRotationsQuaternion() const override
Definition: Object.h:331
void setScale(const Vector3 &scale) override
Set scale.
Definition: Object.h:295
bool isRequiringAnimationComputation()
Definition: Object.h:112
bool isDisableDepthTest() const
Definition: Object.h:440
bool isContributesShadows() override
Definition: Object.h:259
string reflectionEnvironmentMappingId
Definition: Object.h:78
Vector3 reflectionEnvironmentMappingPosition
Definition: Object.h:80
const Vector3 & getScale() const override
Definition: Object.h:291
const float getRotationAngle(const int idx) const override
Definition: Object.h:323
void setReflectionEnvironmentMappingPosition(const Vector3 &reflectionEnvironmentMappingPosition)
Set reflection environment mapping position.
Definition: Object.h:409
void computeAnimation(int contextIdx, int64_t lastFrameAtTime, int64_t currentFrameAtTime) override
Compute animations.
Definition: Object.h:144
BoundingBox * getWorldBoundingBox() override
Definition: Object.h:235
const Color4 & getEffectColorMul() const override
The effect color will be multiplied with fragment color.
Definition: Object.h:247
void setFrustumCulling(bool frustumCulling) override
Set frustum culling.
Definition: Object.cpp:89
void setEngine(Engine *engine) override
Set up engine.
Definition: Object.cpp:39
void setExcludeEffectPass(Engine::EffectPass effectPass)
Set exclude from effect pass.
Definition: Object.h:433
void setAnimationComputationLODEnabled(bool animationComputationLODEnabled)
Set transform computing LOD enabled.
Definition: Object.h:483
void setEnabled(bool enabled) override
Enable/disable rendering.
Definition: Object.cpp:64
bool isRequiringForwardShading()
Definition: Object.h:173
const Transform & getParentTransform() const override
Definition: Object.h:339
const Matrix4x4 & getTransformMatrix() const override
Definition: Object.h:335
const string & getId() override
Definition: Object.h:255
EntityType getEntityType() override
Definition: Object.h:213
void setContributesShadows(bool contributesShadows) override
Enable/disable contributes shadows.
Definition: Object.h:263
bool isRequiringPreRender()
Definition: Object.h:151
void setRotationAxis(const int idx, const Vector3 &axis) override
Set rotation axis.
Definition: Object.h:319
void preRender(int contextIdx)
Pre render step like uploading VBOs and such.
Definition: Object.h:161
bool isFrustumCulling() override
Definition: Object.cpp:85
bool isEnabled() override
Definition: Object.h:222
void computeAnimations(int contextIdx)
Compute animations.
Definition: Object.h:120
int64_t frameTransformLast
Definition: Object.h:83
bool disableDepthTest
Definition: Object.h:82
int64_t timeTransformLast
Definition: Object.h:84
const Vector3 & getReflectionEnvironmentMappingPosition()
Definition: Object.h:401
void setEffectColorAdd(const Color4 &effectColorAdd) override
Set effect color that will be added to fragment color.
Definition: Object.h:243
const Color4 & getEffectColorAdd() const override
The effect color will be added to fragment color.
Definition: Object.h:239
void setParentTransform(const Transform &parentTransform) override
Set parent transform.
Definition: Object.h:178
void setRenderer(Renderer *renderer) override
Set up renderer.
Definition: Object.cpp:46
void setRotationAngle(const int idx, const float angle) override
Definition: Object.h:327
void setDisableDepthTest(bool disableDepthTest)
Set disable depth test.
Definition: Object.h:448
Rotation representation.
Definition: Rotation.h:18
Shader parameter model class.
Timing class.
Definition: Timing.h:16
int64_t getCurrentFrameAtTime()
Definition: Timing.h:77
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
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
Vector3 computeClosestPointInBoundingBox(const Vector3 &point)
Compute closest point in bounding box of given point.
Definition: BoundingBox.h:78
Object node specifically for rendering.
Definition: ObjectNode.h:41
Matrix4x4 class representing matrix4x4 mathematical structure and operations for 3d space.
Definition: Matrix4x4.h:23
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
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6