35 #include "linmath/Quaternion.h" 37 #include "GameObject.h" 38 #include "linmath/float3x3.h" 41 #include "IComponent.h" 42 #include "IRenderComponent.h" 45 #define NORMAL_TEXTURE_LOCATION 3 46 #define DIFFUSE_TEXTURE_LOCATION 0 48 int GameObject::uniqueId = 0;
50 GameObject::GameObject() {
51 m_modelMatrix = chag::make_identity<chag::float4x4>();
56 this->parent = parent;
57 m_modelMatrix = chag::make_identity<chag::float4x4>();
61 GameObject::GameObject(
Mesh *mesh) {
62 m_modelMatrix = chag::make_identity<chag::float4x4>();
63 initGameObject(mesh, mesh);
66 GameObject::GameObject(
Mesh *mesh,
Mesh *colliderMesh) {
67 m_modelMatrix = chag::make_identity<chag::float4x4>();
68 initGameObject(mesh, colliderMesh);
72 this->parent = parent;
73 m_modelMatrix = chag::make_identity<chag::float4x4>();
74 initGameObject(mesh, mesh);
78 this->parent = parent;
79 m_modelMatrix = chag::make_identity<chag::float4x4>();
80 initGameObject(mesh, colliderMesh);
83 void GameObject::initGameObject(
Mesh *mesh,
Mesh *colliderMesh) {
85 this->collisionMesh = colliderMesh;
86 this->m_modelMatrix = chag::make_identity<chag::float4x4>();
87 this->shininess = 0.0f;
88 this->
id = getUniqueId();
90 this->octree = createOctree(colliderMesh);
93 GameObject::~GameObject() {
97 int GameObject::getUniqueId() {
101 Octree* GameObject::createOctree(
Mesh* mesh) {
103 chag::float3 halfVector = (aabb->maxV - aabb->minV) / 2;
104 chag::float3 origin = aabb->maxV - halfVector;
107 std::vector<Triangle *> triangles = mesh->
getTriangles();
108 octree->insertAll(triangles);
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());
120 component->onDeath();
125 bool GameObject::isDirty() {
130 m_modelMatrix = modelMatrix;
134 m_modelMatrix = m_modelMatrix * updateMatrix;
138 if (renderComponent !=
nullptr) {
139 renderComponent->render();
141 if (children.size() != 0) {
152 chag::float4x4 GameObject::getModelMatrix() {
153 return m_modelMatrix;
158 renderComponent->renderShadow(shaderProgram);
160 child->renderShadow(shaderProgram);
165 this->renderComponent = renderer;
166 components.push_back(renderer);
167 renderer->bind(
this);
170 void GameObject::addComponent(
IComponent* newComponent) {
171 components.push_back(newComponent);
172 newComponent->bind(
this);
175 chag::float4x4 GameObject::getFullMatrix() {
176 chag::float4x4 parentMat;
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());
182 parentMat = translation * rotation * scale;
184 if(parent !=
nullptr) {
185 parentMat = parent->getFullMatrix() * parentMat;
194 component->update(dt);
199 move(getFullMatrix());
202 child->changed =
true;
209 case EventType::BeforeCollision:
211 component->beforeCollision(data);
214 case EventType::DuringCollision:
216 component->duringCollision(data);
219 case EventType::AfterCollision:
221 component->afterCollision(data);
226 child->callEvent(type, data);
230 AABB GameObject::getTransformedAABB() {
232 aabb = multiplyAABBWithModelMatrix(meshAabb, m_modelMatrix);
237 TypeIdentifier GameObject::getIdentifier() {
238 return typeIdentifier;
241 void GameObject::setIdentifier(TypeIdentifier identifier) {
242 this->typeIdentifier = identifier;
245 void GameObject::addCollidesWith(TypeIdentifier colliderID) {
246 canCollideWith.push_back(colliderID);
249 void GameObject::addCollidesWith(std::initializer_list<TypeIdentifier> colliderIDs) {
250 for (TypeIdentifier
id : colliderIDs) {
254 bool GameObject::collidesWith(TypeIdentifier
id) {
255 for (TypeIdentifier id2 : canCollideWith) {
262 void GameObject::clearCollidesWithList() {
263 canCollideWith = std::vector<TypeIdentifier>();
266 bool GameObject::isDynamicObject() {
267 return dynamicObject;
270 void GameObject::setDynamic(
bool isDynamic) {
271 dynamicObject = isDynamic;
278 int GameObject::getId() {
282 chag::float3 GameObject::getAbsoluteScale() {
283 if (parent !=
nullptr) {
284 return scale * parent->getAbsoluteScale();
289 chag::float3 GameObject::getRelativeScale() {
293 chag::Quaternion GameObject::getAbsoluteRotation() {
294 if (parent !=
nullptr) {
295 return rotation * parent->getAbsoluteRotation();
300 chag::Quaternion GameObject::getRelativeRotation() {
304 chag::float3 GameObject::getAbsoluteLocation() {
306 pos.x = m_modelMatrix.c4.x;
307 pos.y = m_modelMatrix.c4.y;
308 pos.z = m_modelMatrix.c4.z;
312 chag::float3 GameObject::getRelativeLocation() {
316 void GameObject::updateRotation(chag::Quaternion r) {
317 setRotation(hasRotation ? r * getRelativeRotation() : r);
320 void GameObject::setScale(chag::float3 s) {
325 void GameObject::setLocation(chag::float3 l) {
330 void GameObject::setRotation(chag::Quaternion r) {
332 hasRotation = changed =
true;
335 void GameObject::addChild(
GameObject* child) {
336 children.push_back(child);
virtual void renderShadow(ShaderProgram *shaderProgram)
std::vector< Triangle * > getTriangles()
Class for maintaining OpenGL shader programs.
void move(chag::float4x4 model_matrix)
A class for containing all information about a object in the game world.
void callEvent(EventType type, GameObject *data)
std::vector< Triangle * > getTriangles()