TDME2  1.9.200
LODObject.cpp
Go to the documentation of this file.
2 
3 #include <memory>
4 #include <string>
5 
6 #include <tdme/tdme.h>
8 #include <tdme/engine/Engine.h>
9 #include <tdme/engine/Object.h>
10 #include <tdme/engine/Partition.h>
11 #include <tdme/engine/Transform.h>
12 
13 using std::make_unique;
14 using std::string;
15 using std::unique_ptr;
16 
23 
24 LODObject::LODObject(
25  const string& id,
26  Model* modelLOD1,
27  LODLevelType levelTypeLOD2,
28  float modelLOD2MinDistance,
29  Model* modelLOD2,
30  LODLevelType levelTypeLOD3,
31  float modelLOD3MinDistance,
32  Model* modelLOD3,
33  float lodNoneMinDistance
34 ):
35  id(id),
36  modelLOD1(modelLOD1),
37  levelTypeLOD2(levelTypeLOD2),
38  modelLOD2MinDistance(modelLOD2MinDistance),
39  modelLOD2(modelLOD2),
40  levelTypeLOD3(levelTypeLOD3),
41  modelLOD3MinDistance(modelLOD3MinDistance),
42  modelLOD3(modelLOD3),
43  lodNoneMinDistance(lodNoneMinDistance)
44 {
45  this->enabled = true;
46  this->pickable = false;
47  this->contributesShadows = false;
48  this->receivesShadows = false;
49  this->effectColorMul.set(1.0f, 1.0f, 1.0f, 1.0f);
50  this->effectColorAdd.set(0.0f, 0.0f, 0.0f, 0.0f);
51  this->effectColorMulLOD2.set(1.0f, 1.0f, 1.0f, 1.0f);
52  this->effectColorAddLOD2.set(0.0f, 0.0f, 0.0f, 0.0f);
53  this->effectColorMulLOD3.set(1.0f, 1.0f, 1.0f, 1.0f);
54  this->effectColorAddLOD3.set(0.0f, 0.0f, 0.0f, 0.0f);
56 
57  if (modelLOD1 != nullptr) {
58  objectLOD1 = make_unique<Object>(id + ".lod1", modelLOD1);
59  objectLOD1->setParentEntity(this);
60  }
61  if (modelLOD2 != nullptr) {
62  objectLOD2 = make_unique<Object>(id + ".lod2", modelLOD2);
63  objectLOD2->setParentEntity(this);
64  }
65  if (modelLOD3 != nullptr) {
66  objectLOD3 = make_unique<Object>(id + ".lod3", modelLOD3);
67  objectLOD3->setParentEntity(this);
68  }
69 
70  if (objectLOD1 != nullptr) objectLOD1->setRenderPass(renderPass);
71  if (objectLOD2 != nullptr) objectLOD2->setRenderPass(renderPass);
72  if (objectLOD3 != nullptr) objectLOD3->setRenderPass(renderPass);
73 
74  if (objectLOD1 != nullptr) objectLOD1->setShader(shaderId);
75  if (objectLOD2 != nullptr) objectLOD2->setShader(shaderId);
76  if (objectLOD3 != nullptr) objectLOD3->setShader(shaderId);
77 
78  levelLOD = 1;
79 }
80 
82 }
83 
85 {
86  if (this->engine != nullptr) this->engine->deregisterEntity(this);
87  this->engine = engine;
88  if (engine != nullptr) engine->registerEntity(this);
89  if (objectLOD1 != nullptr) objectLOD1->setEngine(engine);
90  if (objectLOD2 != nullptr) objectLOD2->setEngine(engine);
91  if (objectLOD3 != nullptr) objectLOD3->setEngine(engine);
92 }
93 
95 {
96 }
97 
98 void LODObject::setTransform(const Transform& transform)
99 {
100  Transform::setTransform(transform);
101  //
102  auto entityTransform = parentTransform * (*this);
103  entityTransformMatrix = entityTransform.getTransformMatrix();
104  // delegate to LOD objects
105  if (objectLOD1 != nullptr) objectLOD1->setTransform(entityTransform);
106  if (objectLOD2 != nullptr) objectLOD2->setTransform(entityTransform);
107  if (objectLOD3 != nullptr) objectLOD3->setTransform(entityTransform);
108  // update entity
109  if (parentEntity == nullptr && frustumCulling == true && engine != nullptr && enabled == true) engine->partition->updateEntity(this);
110  // reset current LOD object
111  objectLOD = nullptr;
112 }
113 
115 {
117  //
118  auto entityTransform = parentTransform * (*this);
119  entityTransformMatrix = entityTransform.getTransformMatrix();
120  // delegate to LOD objects
121  if (objectLOD1 != nullptr) objectLOD1->setTransform(entityTransform);
122  if (objectLOD2 != nullptr) objectLOD2->setTransform(entityTransform);
123  if (objectLOD3 != nullptr) objectLOD3->setTransform(entityTransform);
124  // update entity
125  if (parentEntity == nullptr && frustumCulling == true && engine != nullptr && enabled == true) engine->partition->updateEntity(this);
126  // reset current LOD object
127  objectLOD = nullptr;
128 }
129 
130 void LODObject::setEnabled(bool enabled)
131 {
132  // return if enable state has not changed
133  if (this->enabled == enabled) return;
134  // frustum if root entity
135  if (parentEntity == nullptr) {
136  // frustum culling enabled?
137  if (frustumCulling == true) {
138  // yeo, add or remove from partition
139  if (enabled == true) {
140  if (engine != nullptr) engine->partition->addEntity(this);
141  } else {
142  if (engine != nullptr) engine->partition->removeEntity(this);
143  }
144  }
145  }
146  // reset current LOD object
147  objectLOD = nullptr;
148 }
149 
151  return frustumCulling;
152 }
153 
154 void LODObject::setFrustumCulling(bool frustumCulling) {
155  // check if enabled and engine attached
156  if (enabled == true && engine != nullptr) {
157  // had frustum culling
158  if (this->frustumCulling == true) {
159  // yep, remove if set to false now
160  if (frustumCulling == false) engine->partition->removeEntity(this);
161  } else {
162  // yep, add if set to true now
163  if (frustumCulling == true) engine->partition->addEntity(this);
164  }
165  }
166  this->frustumCulling = frustumCulling;
167  // delegate change to engine
168  if (engine != nullptr) engine->updateEntityRegistration(this);
169 }
170 
172 {
173  // delegate to LOD objects
174  if (objectLOD1 != nullptr) objectLOD1->dispose();
175  if (objectLOD2 != nullptr) objectLOD2->dispose();
176  if (objectLOD3 != nullptr) objectLOD3->dispose();
177 }
178 
180 {
181  // delegate to LOD objects
182  if (objectLOD1 != nullptr) objectLOD1->initialize();
183  if (objectLOD2 != nullptr) objectLOD2->initialize();
184  if (objectLOD3 != nullptr) objectLOD3->initialize();
185 }
void set(float r, float g, float b, float a)
Sets this color by its components.
Definition: Color4.h:66
Engine main class.
Definition: Engine.h:131
unique_ptr< Partition > partition
Definition: Engine.h:289
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
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
Entity * parentEntity
Definition: Entity.h:39
LOD object to be used with engine class.
Definition: LODObject.h:49
void dispose() override
Dispose this entity.
Definition: LODObject.cpp:171
Transform parentTransform
Definition: LODObject.h:89
~LODObject()
Public destructor.
Definition: LODObject.cpp:81
RenderPass renderPass
Definition: LODObject.h:84
unique_ptr< Object > objectLOD3
Definition: LODObject.h:71
void initialize() override
Initiates this entity.
Definition: LODObject.cpp:179
void update() override
Update transform.
Definition: LODObject.cpp:114
Matrix4x4 entityTransformMatrix
Definition: LODObject.h:90
void setTransform(const Transform &transform) override
Set transform.
Definition: LODObject.cpp:98
unique_ptr< Object > objectLOD1
Definition: LODObject.h:69
void setFrustumCulling(bool frustumCulling) override
Set frustum culling.
Definition: LODObject.cpp:154
void setEngine(Engine *engine) override
Set up engine.
Definition: LODObject.cpp:84
void setEnabled(bool enabled) override
Enable/disable rendering.
Definition: LODObject.cpp:130
unique_ptr< Object > objectLOD2
Definition: LODObject.h:70
bool isFrustumCulling() override
Definition: LODObject.cpp:150
void setRenderer(Renderer *renderer) override
Set up renderer.
Definition: LODObject.cpp:94
Object to be used with engine class.
Definition: Object.h:60
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
virtual void setTransform(const Transform &transform)
Set transform.
Definition: Transform.h:177
virtual void update()
Computes transform matrix.
Definition: Transform.cpp:33
Representation of a 3D model.
Definition: Model.h:35
Matrix4x4 & identity()
Creates identity matrix.
Definition: Matrix4x4.h:158
Partition interface.
Definition: Partition.h:18