TDME2  1.9.200
LODObjectImposter.cpp
Go to the documentation of this file.
2 
3 #include <memory>
4 #include <string>
5 #include <vector>
6 
7 #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 using std::vector;
17 
23 
24 LODObjectImposter::LODObjectImposter(
25  const string& id,
26  Model* modelLOD1,
27  const vector<Model*>& imposterModelsLOD2,
28  float lod2MinDistance,
29  float lodNoneMinDistance
30 ):
31  id(id),
32  modelLOD1(modelLOD1),
33  lod2MinDistance(lod2MinDistance),
34  lodNoneMinDistance(lodNoneMinDistance)
35 {
36  this->enabled = true;
37  this->pickable = false;
38  this->contributesShadows = false;
39  this->receivesShadows = false;
40  this->effectColorMul.set(1.0f, 1.0f, 1.0f, 1.0f);
41  this->effectColorAdd.set(0.0f, 0.0f, 0.0f, 0.0f);
42  this->effectColorMulLOD2.set(1.0f, 1.0f, 1.0f, 1.0f);
43  this->effectColorAddLOD2.set(0.0f, 0.0f, 0.0f, 0.0f);
45 
46  objectLOD1 = make_unique<Object>(id + ".lod1", modelLOD1);
47  objectLOD1->setParentEntity(this);
48 
49  objectLOD2 = make_unique<ImposterObject>(id + ".lod2", imposterModelsLOD2);
50  objectLOD2->setParentEntity(this);
51 
52  objectLOD1->setRenderPass(renderPass);
53  objectLOD2->setRenderPass(renderPass);
54 
55  objectLOD1->setShader(shaderId);
56  objectLOD2->setShader(shaderId);
57 
58  levelLOD = 1;
59 }
60 
62 }
63 
65 {
66  if (this->engine != nullptr) this->engine->deregisterEntity(this);
67  this->engine = engine;
68  if (engine != nullptr) engine->registerEntity(this);
69  objectLOD1->setEngine(engine);
70  objectLOD2->setEngine(engine);
71 }
72 
74 {
75 }
76 
78 {
79  Transform::setTransform(transform);
80  //
81  auto entityTransform = parentTransform * (*this);
82  entityTransformMatrix = entityTransform.getTransformMatrix();
83  // delegate to LOD objects
84  objectLOD1->setTransform(entityTransform);
85  objectLOD2->setTransform(entityTransform);
86  // update entity
87  if (parentEntity == nullptr && frustumCulling == true && engine != nullptr && enabled == true) engine->partition->updateEntity(this);
88  // reset current LOD object
89  objectLOD = nullptr;
90 }
91 
93 {
95  //
96  auto entityTransform = parentTransform * (*this);
97  entityTransformMatrix = entityTransform.getTransformMatrix();
98  // delegate to LOD objects
99  objectLOD1->setTransform(entityTransform);
100  objectLOD2->setTransform(entityTransform);
101  // update entity
102  if (parentEntity == nullptr && frustumCulling == true && engine != nullptr && enabled == true) engine->partition->updateEntity(this);
103  // reset current LOD object
104  objectLOD = nullptr;
105 }
106 
108 {
109  // return if enable state has not changed
110  if (this->enabled == enabled) return;
111  // frustum if root entity
112  if (parentEntity == nullptr) {
113  // frustum culling enabled?
114  if (frustumCulling == true) {
115  // yeo, add or remove from partition
116  if (enabled == true) {
117  if (engine != nullptr) engine->partition->addEntity(this);
118  } else {
119  if (engine != nullptr) engine->partition->removeEntity(this);
120  }
121  }
122  }
123  // reset current LOD object
124  objectLOD = nullptr;
125 }
126 
128  return frustumCulling;
129 }
130 
131 void LODObjectImposter::setFrustumCulling(bool frustumCulling) {
132  // check if enabled and engine attached
133  if (enabled == true && engine != nullptr) {
134  // had frustum culling
135  if (this->frustumCulling == true) {
136  // yep, remove if set to false now
137  if (frustumCulling == false) engine->partition->removeEntity(this);
138  } else {
139  // yep, add if set to true now
140  if (frustumCulling == true) engine->partition->addEntity(this);
141  }
142  }
143  this->frustumCulling = frustumCulling;
144  // delegate change to engine
145  if (engine != nullptr) engine->updateEntityRegistration(this);
146 }
147 
149 {
150  // delegate to LOD objects
151  objectLOD1->dispose();
152  objectLOD2->dispose();
153 }
154 
156 {
157  // delegate to LOD objects
158  objectLOD1->initialize();
159  objectLOD2->initialize();
160 }
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 + imposter to be used with engine class.
void dispose() override
Dispose this entity.
unique_ptr< ImposterObject > objectLOD2
void initialize() override
Initiates this entity.
void update() override
Update transform.
void setTransform(const Transform &transform) override
Set transform.
~LODObjectImposter()
Public destructor.
void setFrustumCulling(bool frustumCulling) override
Set frustum culling.
void setEngine(Engine *engine) override
Set up engine.
void setEnabled(bool enabled) override
Enable/disable rendering.
void setRenderer(Renderer *renderer) override
Set up renderer.
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