TDME2  1.9.200
Scene.cpp
Go to the documentation of this file.
2 
3 #include <algorithm>
4 #include <memory>
5 #include <map>
6 #include <string>
7 #include <vector>
8 
9 #include <tdme/tdme.h>
10 #include <tdme/engine/Color4.h>
21 #include <tdme/engine/Engine.h>
23 #include <tdme/engine/Transform.h>
24 #include <tdme/math/Vector3.h>
25 #include <tdme/math/Vector4.h>
26 #include <tdme/utilities/Console.h>
27 
28 using std::make_unique;
29 using std::map;
30 using std::remove;
31 using std::string;
32 using std::unique_ptr;
33 using std::vector;
34 
53 
54 Scene::Scene(const string& name, const string& description): BaseProperties(name, description)
55 {
57  fileName = "untitled.tscene";
58  rotationOrder = RotationOrder::XYZ;
59  library = make_unique<SceneLibrary>(this);
60  entityIdx = 0;
62  for (const auto& parameterName: Engine::getShaderParameterNames("sky")) {
63  auto defaultShaderParameter = Engine::getDefaultShaderParameter("sky", parameterName);
64  if (defaultShaderParameter == nullptr) continue;
65  skyShaderParameters.setShaderParameter(parameterName, defaultShaderParameter->value);
66  }
67 }
68 
70 }
71 
73 {
74  dimension.set(0.0f, 0.0f, 0.0f);
75  auto haveDimension = false;
76  auto levelLeft = 0.0f;
77  auto levelRight = 0.0f;
78  auto levelNear = 0.0f;
79  auto levelFar = 0.0f;
80  auto levelTop = 0.0f;
81  auto levelBottom = 0.0f;
82  Vector3 sideVector(1.0f, 0.0f, 0.0f);
83  Vector3 upVector(0.0f, 1.0f, 0.0f);
84  Vector3 forwardVector(0.0f, 0.0f, 1.0f);
85  Vector3 bbDimension;
86  Vector3 bbMin;
87  Vector3 bbMax;
88  for (const auto& sceneEntity: entities) {
89  BoundingBox cbv;
90  // TODO: Implement me 100%
91  if (sceneEntity->getPrototype()->getType() == Prototype_Type::MODEL) {
92  cbv.fromBoundingVolumeWithTransform(sceneEntity->getPrototype()->getModel()->getBoundingBox(), sceneEntity->getTransform());
93  } else {
94  continue;
95  }
96 
97  //
98  bbDimension.set(
99  cbv.getDimensions().getX(),
100  cbv.getDimensions().getY(),
101  cbv.getDimensions().getZ()
102  );
103  bbDimension.scale(0.5f);
104  bbMin.set(cbv.getCenter());
105  bbMin.sub(bbDimension);
106  bbMax.set(cbv.getCenter());
107  bbMax.add(bbDimension);
108  auto entityLeft = bbMin.getX();
109  auto entityRight = bbMax.getX();
110  auto entityNear = bbMin.getZ();
111  auto entityFar = bbMax.getZ();
112  auto entityBottom = bbMin.getY();
113  auto entityTop = bbMax.getY();
114  if (haveDimension == false) {
115  levelLeft = entityLeft;
116  levelRight = entityRight;
117  levelNear = entityNear;
118  levelFar = entityFar;
119  levelTop = entityTop;
120  levelBottom = entityBottom;
121  haveDimension = true;
122  } else {
123  if (entityLeft < levelLeft) levelLeft = entityLeft;
124  if (entityRight > levelRight) levelRight = entityRight;
125  if (entityNear < levelNear) levelNear = entityNear;
126  if (entityFar > levelFar) levelFar = entityFar;
127  if (entityTop > levelTop) levelTop = entityTop;
128  if (entityBottom < levelBottom) levelBottom = entityBottom;
129  }
130  }
131  for (auto prototype: library->getPrototypes()) {
132  if (prototype->getType() != Prototype_Type::TERRAIN) continue;
133  auto terrain = prototype->getTerrain();
134  auto entityLeft = 0.0f;
135  auto entityRight = terrain->getWidth();
136  auto entityNear = 0.0f;
137  auto entityFar = terrain->getDepth();
138  if (entityLeft < levelLeft) levelLeft = entityLeft;
139  if (entityRight > levelRight) levelRight = entityRight;
140  if (entityNear < levelNear) levelNear = entityNear;
141  if (entityFar > levelFar) levelFar = entityFar;
142  for (auto terrainHeight: terrain->getHeightVector()) {
143  auto entityTop = terrainHeight;
144  auto entityBottom = terrainHeight;
145  if (entityTop > levelTop) levelTop = entityTop;
146  if (entityBottom < levelBottom) levelBottom = entityBottom;
147  }
148  // one terrain only, so break here
149  break;
150  }
151  boundingBox.getMin().set(levelLeft, levelBottom, levelNear);
152  boundingBox.getMax().set(levelRight, levelTop, levelFar);
154  dimension.setX(levelRight - levelLeft);
155  dimension.setZ(levelFar - levelNear);
156  dimension.setY(levelTop - levelBottom);
157 }
158 
160 {
162 }
163 
165 {
166  entitiesById.clear();
167  entities.clear();
168  environmentMappingIds.clear();
169  entityIdx = 0;
170 }
171 
172 void Scene::getEntitiesByPrototypeId(int prototypeId, vector<string>& entitiesByPrototypeId) {
173  for (const auto& entity: entities) {
174  if (entity->getPrototype()->getId() == prototypeId) {
175  entitiesByPrototypeId.push_back(entity->getId());
176  }
177  }
178 }
179 
181 {
182  vector<string> entitiesToRemove;
183  getEntitiesByPrototypeId(prototypeId, entitiesToRemove);
184  for (const auto& entityId: entitiesToRemove) {
185  removeEntity(entityId);
186  }
187 }
188 
189 void Scene::replacePrototypeByIds(int searchPrototypeId, int newEntityId)
190 {
191  auto replaceEntity = getLibrary()->getPrototype(newEntityId);
192  if (replaceEntity == nullptr)
193  return;
194 
195  for (const auto& entity: entities) {
196  if (entity->getPrototype()->getId() == searchPrototypeId) {
197  entity->setPrototype(replaceEntity);
198  }
199  }
200 }
201 
203 {
204  auto _entity = getEntity(entity->getId());
205  if (_entity != nullptr) {
206  removeEntity(entity->getId());
207  Console::println(
208  "Scene::addEntity():: entity with id '" +
209  entity->getId() +
210  "' already exists. Removing it!"
211  );
212  }
213  entitiesById[entity->getId()] = entity;
214  entities.push_back(unique_ptr<SceneEntity>(entity));
215  if (entity->getPrototype()->getType() == Prototype_Type::ENVIRONMENTMAPPING) environmentMappingIds.insert(entity->getId());
216 }
217 
218 bool Scene::removeEntity(const string& id)
219 {
220  auto entityByIdIt = entitiesById.find(id);
221  if (entityByIdIt != entitiesById.end()) {
222  auto entity = entityByIdIt->second;
223  for (auto i = 0; i < entities.size(); i++) {
224  if (entities[i].get() == entity) {
225  entities.erase(entities.begin() + i);
226  break;
227  }
228  }
229  entitiesById.erase(entityByIdIt);
230  if (entity->getPrototype()->getType() == Prototype_Type::ENVIRONMENTMAPPING) environmentMappingIds.erase(entity->getId());
231  return true;
232  } else {
233  return false;
234  }
235 }
236 
237 bool Scene::renameEntity(const string& id, const string& newId) {
238  if (id == newId) return true;
239  if (getEntity(newId) != nullptr) return false;
240  auto entityByIdIt = entitiesById.find(id);
241  if (entityByIdIt != entitiesById.end()) {
242  auto entity = entityByIdIt->second;
243  entitiesById.erase(entityByIdIt);
244  if (entity->getPrototype()->getType() == Prototype_Type::ENVIRONMENTMAPPING) environmentMappingIds.erase(entity->getId());
245  entity->setName(newId);
246  entitiesById[entity->getId()] = entity;
247  if (entity->getPrototype()->getType() == Prototype_Type::ENVIRONMENTMAPPING) environmentMappingIds.insert(entity->getId());
248  return true;
249  } else {
250  return false;
251  }
252 }
253 
254 SceneEntity* Scene::getEntity(const string& id)
255 {
256  auto entityByIdIt = entitiesById.find(id);
257  if (entityByIdIt != entitiesById.end()) {
258  return entityByIdIt->second;
259  }
260  return nullptr;
261 }
262 
265  computeCenter();
266 }
Color 4 definition class.
Definition: Color4.h:18
Engine main class.
Definition: Engine.h:131
static const Shader::ParameterDefaults * getDefaultShaderParameter(const string &shaderId, const string &parameterName)
Returns shader parameter default value for given shader id and parameter name.
Definition: Engine.h:915
static const vector< string > getShaderParameterNames(const string &shaderId)
Returns shader parameter names of shader with given id.
Definition: Engine.h:896
TDME2 engine entity shader parameters.
void setShaderParameter(const string &parameterName, const ShaderParameter &parameterValue)
Set shader parameter for given parameter name.
void setShader(const string &shaderId)
Set shader.
Transform which contain scale, rotations and translation.
Definition: Transform.h:29
Representation of a 3D model.
Definition: Model.h:35
Represents rotation orders of a model.
Definition: RotationOrder.h:23
Axis aligned bounding box used for frustum, this is not directly connectable with physics engine.
Definition: BoundingBox.h:26
const Vector3 & getCenter() const
Definition: BoundingBox.h:121
void fromBoundingVolumeWithTransform(BoundingBox *original, const Transform &transform)
Create bounding volume from given original(of same type) with applied transform.
Definition: BoundingBox.h:150
const Vector3 & getDimensions() const
Definition: BoundingBox.h:128
void update()
Updates this bounding box.
Prototype definition.
Definition: Prototype.h:55
Scene entity definition.
Definition: SceneEntity.h:23
Scene prototype library definition.
Definition: SceneLibrary.h:32
Prototype * getPrototype(int id)
Get a prototype by given id.
Definition: SceneLibrary.h:98
Scene light definition.
Definition: SceneLight.h:20
Scene definition.
Definition: Scene.h:50
map< string, SceneEntity * > entitiesById
Definition: Scene.h:57
bool removeEntity(const string &id)
Removes an entity from scene.
Definition: Scene.cpp:218
void replacePrototypeByIds(int searchPrototypeId, int newPrototypeId)
Replace prototype of given search prototype with new prototype.
Definition: Scene.cpp:189
virtual ~Scene()
Destructor.
Definition: Scene.cpp:69
void removeEntitiesByPrototypeId(int prototypeId)
Remove entities with given prototype id.
Definition: Scene.cpp:180
EntityShaderParameters skyShaderParameters
Definition: Scene.h:64
void getEntitiesByPrototypeId(int prototypeId, vector< string > &entitiesByPrototypeId)
Get entities with given prototype id.
Definition: Scene.cpp:172
RotationOrder * rotationOrder
Definition: Scene.h:54
unique_ptr< SceneLibrary > library
Definition: Scene.h:56
bool renameEntity(const string &id, const string &newId)
Rename an entity from scene.
Definition: Scene.cpp:237
SceneEntity * getEntity(const string &id)
Returns scene entity by id.
Definition: Scene.cpp:254
void addEntity(SceneEntity *entity)
Adds an entity to scene.
Definition: Scene.cpp:202
set< string > environmentMappingIds
Definition: Scene.h:59
string applicationRootPathName
Definition: Scene.h:52
SceneLibrary * getLibrary()
Definition: Scene.h:185
void update()
Update scene dimension, bounding box, center.
Definition: Scene.cpp:263
vector< unique_ptr< SceneEntity > > entities
Definition: Scene.h:58
void clearEntities()
Clears all scene entities.
Definition: Scene.cpp:164
BoundingBox boundingBox
Definition: Scene.h:61
void computeBoundingBox()
Computes scene bounding box.
Definition: Scene.cpp:72
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
float getY() const
Definition: Vector3.h:117
Vector3 & setX(float x)
Sets x component.
Definition: Vector3.h:109
float getX() const
Definition: Vector3.h:100
float getZ() const
Definition: Vector3.h:134
Vector3 & add(float scalar)
Adds a scalar.
Definition: Vector3.h:153
Vector3 & setY(float y)
Sets y component.
Definition: Vector3.h:126
Vector3 clone() const
Clones this vector3.
Definition: Vector3.h:374
Vector3 & sub(float scalar)
Subtracts a scalar.
Definition: Vector3.h:177
Vector3 & scale(float scalar)
Scales by scalar.
Definition: Vector3.h:201
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
Vector3 & setZ(float z)
Sets z component.
Definition: Vector3.h:143
Vector4 class representing vector4 mathematical structure and operations with x, y,...
Definition: Vector4.h:22
Console class.
Definition: Console.h:29