Bubba-3D  0.9.0
Awesome game engine!
GameObject.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 /*
18 * This file is part of Bubba-3D.
19 *
20 * Bubba-3D is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU Lesser General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
24 *
25 * Bubba-3D is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public License
31 * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
32 */
33 
34 #include <algorithm>
35 #include "linmath/Quaternion.h"
36 #include "Sphere.h"
37 #include "GameObject.h"
38 #include "linmath/float3x3.h"
39 #include "Collider.h"
40 #include "Mesh.h"
41 #include "IComponent.h"
42 #include "IRenderComponent.h"
43 #include "Octree.h"
44 
45 #define NORMAL_TEXTURE_LOCATION 3
46 #define DIFFUSE_TEXTURE_LOCATION 0
47 
48 int GameObject::uniqueId = 0;
49 
50 GameObject::GameObject() {
51  m_modelMatrix = chag::make_identity<chag::float4x4>();
52  id = getUniqueId();
53 }
54 
55 GameObject::GameObject(GameObject* parent) {
56  this->parent = parent;
57  m_modelMatrix = chag::make_identity<chag::float4x4>();
58  id = getUniqueId();
59 }
60 
61 GameObject::GameObject(Mesh *mesh) {
62  m_modelMatrix = chag::make_identity<chag::float4x4>();
63  initGameObject(mesh, mesh);
64 }
65 
66 GameObject::GameObject(Mesh *mesh, Mesh *colliderMesh) {
67  m_modelMatrix = chag::make_identity<chag::float4x4>();
68  initGameObject(mesh, colliderMesh);
69 }
70 
71 GameObject::GameObject(Mesh *mesh, GameObject* parent) {
72  this->parent = parent;
73  m_modelMatrix = chag::make_identity<chag::float4x4>();
74  initGameObject(mesh, mesh);
75 }
76 
77 GameObject::GameObject(Mesh *mesh, Mesh *colliderMesh, GameObject* parent) {
78  this->parent = parent;
79  m_modelMatrix = chag::make_identity<chag::float4x4>();
80  initGameObject(mesh, colliderMesh);
81 }
82 
83 void GameObject::initGameObject(Mesh *mesh, Mesh *colliderMesh) {
84  this->mesh = mesh;
85  this->collisionMesh = colliderMesh;
86  this->m_modelMatrix = chag::make_identity<chag::float4x4>();
87  this->shininess = 0.0f;
88  this->id = getUniqueId();
89  this->sphere = mesh->getSphere();
90  this->octree = createOctree(colliderMesh);
91 }
92 
93 GameObject::~GameObject() {
94  mesh = nullptr;
95 }
96 
97 int GameObject::getUniqueId() {
98  return ++uniqueId;
99 }
100 
101 Octree* GameObject::createOctree(Mesh* mesh) {
102  AABB* aabb = mesh->getAABB();
103  chag::float3 halfVector = (aabb->maxV - aabb->minV) / 2;
104  chag::float3 origin = aabb->maxV - halfVector;
105  Octree* octree = new Octree(origin, halfVector);
106 
107  std::vector<Triangle *> triangles = mesh->getTriangles();
108  octree->insertAll(triangles);
109 
110  return octree;
111 }
112 
113 Sphere GameObject::getTransformedSphere() {
114  float scaling = std::max(scale.x, std::max(scale.y, scale.z));
115  return Sphere(sphere.getPosition()+location, scaling*sphere.getRadius());
116 }
117 
119  for (IComponent *component : components) {
120  component->onDeath();
121  }
122  dirty = true;
123 }
124 
125 bool GameObject::isDirty() {
126  return dirty;
127 }
128 
129 void GameObject::move(chag::float4x4 modelMatrix) {
130  m_modelMatrix = modelMatrix;
131 }
132 
133 void GameObject::update(chag::float4x4 updateMatrix) {
134  m_modelMatrix = m_modelMatrix * updateMatrix;
135 }
136 
138  if (renderComponent != nullptr) {
139  renderComponent->render();
140  }
141  if (children.size() != 0) {
142  for (GameObject *child : children) {
143  child->render();
144  }
145  }
146 }
147 
148 std::vector<Triangle *> GameObject::getTriangles() {
149  return mesh->getTriangles();
150 }
151 
152 chag::float4x4 GameObject::getModelMatrix() {
153  return m_modelMatrix;
154 }
155 
156 
158  renderComponent->renderShadow(shaderProgram);
159  for (GameObject *child : children) {
160  child->renderShadow(shaderProgram);
161  }
162 }
163 
164 void GameObject::addRenderComponent(IRenderComponent* renderer) {
165  this->renderComponent = renderer;
166  components.push_back(renderer);
167  renderer->bind(this);
168 }
169 
170 void GameObject::addComponent(IComponent* newComponent) {
171  components.push_back(newComponent);
172  newComponent->bind(this);
173 }
174 
175 chag::float4x4 GameObject::getFullMatrix() {
176  chag::float4x4 parentMat;
177 
178  chag::float4x4 translation = chag::make_translation(this->getRelativeLocation());
179  chag::float4x4 rotation = chag::makematrix(this->getRelativeRotation());
180  chag::float4x4 scale = chag::make_scale<chag::float4x4>(this->getRelativeScale());
181 
182  parentMat = translation * rotation * scale;
183 
184  if(parent != nullptr) {
185  parentMat = parent->getFullMatrix() * parentMat;
186  }
187 
188  return parentMat;
189 }
190 
191 
192 void GameObject::update(float dt) {
193  for (IComponent *component : components) {
194  component->update(dt);
195  }
196  if (changed) {
197  changed = false;
198 
199  move(getFullMatrix());
200  }
201  for (GameObject *child : children) {
202  child->changed = true;
203  child->update(dt);
204  }
205 }
206 
207 void GameObject::callEvent(EventType type, GameObject* data) {
208  switch (type) {
209  case EventType::BeforeCollision:
210  for (IComponent *component : components) {
211  component->beforeCollision(data);
212  }
213  break;
214  case EventType::DuringCollision:
215  for (IComponent *component : components) {
216  component->duringCollision(data);
217  }
218  break;
219  case EventType::AfterCollision:
220  for (IComponent *component : components) {
221  component->afterCollision(data);
222  }
223  break;
224  }
225  for (GameObject *child : children) {
226  child->callEvent(type, data);
227  }
228 }
229 
230 AABB GameObject::getTransformedAABB() {
231  AABB* meshAabb = this->mesh->getAABB();
232  aabb = multiplyAABBWithModelMatrix(meshAabb, m_modelMatrix);
233 
234  return aabb;
235 }
236 
237 TypeIdentifier GameObject::getIdentifier() {
238  return typeIdentifier;
239 }
240 
241 void GameObject::setIdentifier(TypeIdentifier identifier) {
242  this->typeIdentifier = identifier;
243 }
244 
245 void GameObject::addCollidesWith(TypeIdentifier colliderID) {
246  canCollideWith.push_back(colliderID);
247 }
248 
249 void GameObject::addCollidesWith(std::initializer_list<TypeIdentifier> colliderIDs) {
250  for (TypeIdentifier id : colliderIDs) {
251  addCollidesWith(id);
252  }
253 }
254 bool GameObject::collidesWith(TypeIdentifier id) {
255  for (TypeIdentifier id2 : canCollideWith) {
256  if (id2 == id) {
257  return true;
258  }
259  }
260  return false;
261 }
262 void GameObject::clearCollidesWithList() {
263  canCollideWith = std::vector<TypeIdentifier>();
264 }
265 
266 bool GameObject::isDynamicObject() {
267  return dynamicObject;
268 }
269 
270 void GameObject::setDynamic(bool isDynamic) {
271  dynamicObject = isDynamic;
272 }
273 
275  return octree;
276 }
277 
278 int GameObject::getId() {
279  return id;
280 }
281 
282 chag::float3 GameObject::getAbsoluteScale() {
283  if (parent != nullptr) {
284  return scale * parent->getAbsoluteScale();
285  }
286  return scale;
287 }
288 
289 chag::float3 GameObject::getRelativeScale() {
290  return scale;
291 }
292 
293 chag::Quaternion GameObject::getAbsoluteRotation() {
294  if (parent != nullptr) {
295  return rotation * parent->getAbsoluteRotation();
296  }
297  return rotation;
298 }
299 
300 chag::Quaternion GameObject::getRelativeRotation() {
301  return rotation;
302 }
303 
304 chag::float3 GameObject::getAbsoluteLocation() {
305  chag::float3 pos;
306  pos.x = m_modelMatrix.c4.x;
307  pos.y = m_modelMatrix.c4.y;
308  pos.z = m_modelMatrix.c4.z;
309  return pos;
310 }
311 
312 chag::float3 GameObject::getRelativeLocation() {
313  return location;
314 }
315 
316 void GameObject::updateRotation(chag::Quaternion r) {
317  setRotation(hasRotation ? r * getRelativeRotation() : r);
318 }
319 
320 void GameObject::setScale(chag::float3 s) {
321  scale = s;
322  changed = true;
323 }
324 
325 void GameObject::setLocation(chag::float3 l) {
326  location = l;
327  changed = true;
328 }
329 
330 void GameObject::setRotation(chag::Quaternion r) {
331  rotation = r;
332  hasRotation = changed = true;
333 }
334 
335 void GameObject::addChild(GameObject* child) {
336  children.push_back(child);
337 }
338 
virtual void render()
Definition: GameObject.cpp:137
Sphere getSphere()
Definition: Mesh.cpp:238
virtual void renderShadow(ShaderProgram *shaderProgram)
Definition: GameObject.cpp:157
std::vector< Triangle * > getTriangles()
Definition: Mesh.cpp:325
void update(float dt)
Definition: GameObject.cpp:192
Definition: Mesh.h:45
Class for maintaining OpenGL shader programs.
Definition: ShaderProgram.h:39
Definition: Octree.h:32
Octree * getOctree()
Definition: GameObject.cpp:274
void move(chag::float4x4 model_matrix)
Definition: GameObject.cpp:129
void makeDirty()
Definition: GameObject.cpp:118
A class for containing all information about a object in the game world.
Definition: GameObject.h:67
void callEvent(EventType type, GameObject *data)
Definition: GameObject.cpp:207
Definition: Sphere.h:19
Definition: AABB2.h:23
std::vector< Triangle * > getTriangles()
Definition: GameObject.cpp:148
AABB * getAABB()
Definition: Mesh.cpp:299