-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameObject.cpp
168 lines (138 loc) · 3.99 KB
/
GameObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "GameObject.h"
#include "Components/Component.h"
#include "Components/Transform.h"
#include "imgui/imgui.h"
#include <algorithm>
#include "glew/include/GL/glew.h"
#include "DebugDraw.h"
Component* GameObject::CreateComponent(ComponentType type) {
if (type == ComponentType::Transform) {
components.push_back(new Transform(this));
}
Component *newComponent;
switch (type)
{
case ComponentType::Camera:
//components.push_back(new Camera());
break;
case ComponentType::Material:
newComponent = new Material(this);
components.push_back(newComponent);
break;
case ComponentType::Mesh:
//components.push_back(new Mesh());
break;
default:
return nullptr;
}
if (newComponent) {
newComponent->owner = this;
components.push_back(newComponent);
}
return newComponent;
}
update_status GameObject::Update() {
return UPDATE_CONTINUE;
}
Component* GameObject::FindComponent(ComponentType type) {
for (unsigned int i = 0; i < components.size(); i++) {
if (components[i]->type == type) {
Component* res = components[i];
return res;
}
}
return nullptr;
}
GameObject* GameObject::FindChild(const char* childName)
{
for (unsigned int i = 0; i < children.size(); i++) {
if (strcmp(children[i]->name, childName)) {
GameObject* res = children[i];
return res;
}
}
return nullptr;
}
void GameObject::ShowProperties() {
ImGui::Begin("Properties PopUp");
for (std::vector<Component*>::iterator it = components.begin(); it != components.end(); ++it) {
(*it)->uuid = uuid;
//ImGui::Begin("Component:%s:", (*it)->);
(*it)->DrawView();
//ImGui::End();
}
ImGui::End();
}
void GameObject::DeleteChild(const GameObject* child) {
children.erase(std::remove(children.begin(), children.end(), child), children.end());
}
void GameObject::ComputeAABB()
{
float3 min = float3::zero;
float3 max = float3::zero;
if (myMesh == nullptr)
{
LOG("This gameObject does not have a Mesh thus we compute the AABB from his childs. code 928340");
if (children.size() == 0)
{
LOG("Cannot compute the AABB because gameObject does not have children.code 834839");
return; //leave at this point
}
for (auto child : children)
{
if (child->boundingBox != nullptr)
{
//Min vertex
if (child->boundingBox->minPoint.x < min.x)
min.x = child->boundingBox->minPoint.x;
if (child->boundingBox->minPoint.y < min.y)
min.y = child->boundingBox->minPoint.y;
if (child->boundingBox->minPoint.z < min.z)
min.z = child->boundingBox->minPoint.z;
//Max vertex
if (child->boundingBox->maxPoint.x > max.x)
max.x = child->boundingBox->maxPoint.x;
if (child->boundingBox->maxPoint.y > max.y)
max.y = child->boundingBox->maxPoint.y;
if (child->boundingBox->maxPoint.z > max.z)
max.z = child->boundingBox->maxPoint.z;
}
}
boundingBox = new AABB(min, max);
//Compute globalBoundingBox
float3 globalPos, globalScale;
float3x3 globalRot;
myTransform->worldTransform.Decompose(globalPos, globalRot, globalScale);
globalBoundingBox = new AABB(min + globalPos, max + globalPos);
}
for (auto vertex : myMesh->myMesh->vertices)
{
//Min vertex
if (vertex.Position.x < min.x)
min.x = vertex.Position.x;
if (vertex.Position.y < min.y)
min.y = vertex.Position.y;
if (vertex.Position.z < min.z)
min.z = vertex.Position.z;
//Max vertex
if (vertex.Position.x > max.x)
max.x = vertex.Position.x;
if (vertex.Position.y > max.y)
max.y = vertex.Position.y;
if (vertex.Position.z > max.z)
max.z = vertex.Position.z;
}
boundingBox = new AABB(min, max);
//Compute globalBoundingBox
float3 globalPos, globalScale;
float3x3 globalRot;
myTransform->worldTransform.Decompose(globalPos, globalRot, globalScale);
globalBoundingBox = new AABB(min + globalPos, max + globalPos);
}
void GameObject::DrawAABB()
{
//dd::aabb(globalBoundingBox->minPoint, globalBoundingBox->maxPoint, float3(0, 1, 0));
}
void GameObject::CreateTransform(const float3& pos, const Quat& rot) {
components.push_back(new Transform(this, pos, rot));
}