TDME2  1.9.200
Object.cpp
Go to the documentation of this file.
1 #include <tdme/engine/Object.h>
2 
3 #include <string>
4 
5 #include <tdme/tdme.h>
8 #include <tdme/engine/Engine.h>
10 #include <tdme/engine/Transform.h>
11 #include <tdme/math/Matrix4x4.h>
12 #include <tdme/math/Quaternion.h>
13 #include <tdme/math/Vector3.h>
15 
16 using std::string;
17 
28 
29 Object::Object(const string& id, Model* model, int instances): ObjectInternal(id, model, instances)
30 {
31  setShader("default");
32 }
33 
34 Object::Object(const string& id, Model* model): ObjectInternal(id, model, 1)
35 {
36  setShader("default");
37 }
38 
40 {
41  if (this->engine != nullptr) this->engine->deregisterEntity(this);
42  this->engine = engine;
43  if (engine != nullptr) engine->registerEntity(this);
44 }
45 
47 {
48 }
49 
50 void Object::setTransform(const Transform& transform)
51 {
52  ObjectInternal::setTransform(transform);
53  //
54  if (parentEntity == nullptr && frustumCulling == true && engine != nullptr && enabled == true) engine->partition->updateEntity(this);
55 }
56 
58 {
59  ObjectInternal::update();
60  //
61  if (parentEntity == nullptr && frustumCulling == true && engine != nullptr && enabled == true) engine->partition->updateEntity(this);
62 }
63 
64 void Object::setEnabled(bool enabled)
65 {
66  // return if enable state has not changed
67  if (this->enabled == enabled) return;
68 
69  // frustum if root entity
70  if (parentEntity == nullptr) {
71  // frustum culling enabled?
72  if (frustumCulling == true) {
73  // yeo, add or remove from partition
74  if (enabled == true) {
75  if (engine != nullptr) engine->partition->addEntity(this);
76  } else {
77  if (engine != nullptr) engine->partition->removeEntity(this);
78  }
79  }
80  }
81  // call parent class::setEnabled()
82  ObjectInternal::setEnabled(enabled);
83 }
84 
86  return frustumCulling;
87 }
88 
89 void Object::setFrustumCulling(bool frustumCulling) {
90  // check if enabled and engine attached
91  if (enabled == true && engine != nullptr) {
92  // had frustum culling
93  if (this->frustumCulling == true) {
94  // yep, remove if set to false now
95  if (frustumCulling == false) engine->partition->removeEntity(this);
96  } else {
97  // yep, add if set to true now
98  if (frustumCulling == true) engine->partition->addEntity(this);
99  }
100  }
101  this->frustumCulling = frustumCulling;
102  // delegate change to engine
103  if (engine != nullptr) engine->updateEntityRegistration(this);
104 }
105 
107 {
108  ObjectInternal::dispose();
109 }
110 
112 {
113  ObjectInternal::initialize();
114 }
115 
116 void Object::setShader(const string& id) {
117  if (model->getShaderModel() == ShaderModel::PBR || model->getShaderModel() == ShaderModel::SPECULARPBR) {
118  shaderId = StringTools::startsWith(id, "pbr-") == true || id.empty() == true?id:"pbr-" + id;
120  } else
121  if (model->getShaderModel() == ShaderModel::SPECULAR) {
122  shaderId = StringTools::startsWith(id, "pbr-") == true?StringTools::substring(id, string("pbr-").size()):id;
124  }
127  Engine::getLightingShader()->hasShader("defer_" + shaderId) == false;
128  // delegate change to engine
129  if (engine != nullptr) engine->updateEntityRegistration(this);
130 }
131 
Engine main class.
Definition: Engine.h:131
unique_ptr< Partition > partition
Definition: Engine.h:289
static LightingShader * getLightingShader()
Definition: Engine.h:642
void updateEntityRegistration(Entity *entity)
Updates registration of engine by performing deregisterEntity() and registerEntity()
Definition: Engine.h:1489
void deregisterEntity(Entity *entity)
Removes a entity from internal lists, those entities can also be sub entities from entity hierarchy o...
Definition: Engine.cpp:382
static uint8_t getUniqueShaderId(const string &shaderId)
Definition: Engine.h:823
void registerEntity(Entity *entity)
Adds a entity to internal lists, those entities can also be sub entities from entity hierarchy or par...
Definition: Engine.cpp:437
void setShader(const string &shaderId)
Set shader.
Entity * parentEntity
Definition: Entity.h:39
Object to be used with engine class.
Definition: Object.h:60
Engine * engine
Definition: Object.h:73
void dispose() override
Dispose this entity.
Definition: Object.cpp:106
void setShader(const string &id)
Set shader.
Definition: Object.cpp:116
void initialize() override
Initiates this entity.
Definition: Object.cpp:111
EntityShaderParameters shaderParameters
Definition: Object.h:85
uint8_t uniqueShaderId
Definition: Object.h:77
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 setTransform(const Transform &transform) override
Set transform.
Definition: Object.cpp:50
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 setEnabled(bool enabled) override
Enable/disable rendering.
Definition: Object.cpp:64
bool isFrustumCulling() override
Definition: Object.cpp:85
void setRenderer(Renderer *renderer) override
Set up renderer.
Definition: Object.cpp:46
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
Representation of a 3D model.
Definition: Model.h:35
ShaderModel * getShaderModel()
Definition: Model.h:164
Interface to lighting shader program.
bool hasShader(const string &shaderId)
Returns if shader with given shader id does exist.
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
String tools class.
Definition: StringTools.h:22
Partition interface.
Definition: Partition.h:18