-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathComponentMesh.cpp
More file actions
289 lines (241 loc) · 6.64 KB
/
ComponentMesh.cpp
File metadata and controls
289 lines (241 loc) · 6.64 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "ComponentMesh.h"
#include "Application.h"
#include "ModuleEditor.h"
#include "MeshImporter.h"
#include "imgui\imgui.h"
#include "GameObject.h"
#include "ComponentTransform.h"
#include "ComponentMaterial.h"
#include "ComponentCamera.h"
#include "ResourceFileMesh.h"
#include "ComponentBone.h"
#include "ResourceFileBone.h"
#include "glut/glut.h"
#include "ModuleRenderer3D.h"
#include "ModuleResourceManager.h"
#include "Brofiler/include/Brofiler.h"
ComponentMesh::ComponentMesh(ComponentType type, GameObject* game_object) : Component(type, game_object)
{
aabb.SetNegativeInfinity();
bounding_box.SetNegativeInfinity();
}
ComponentMesh::~ComponentMesh()
{
if (rc_mesh)
{
rc_mesh->Unload();
rc_mesh = nullptr;
}
mesh = nullptr;
App->renderer3D->RemoveBuffer(weight_id);
App->renderer3D->RemoveBuffer(bone_id);
}
void ComponentMesh::Update()
{
BROFILER_CATEGORY("ComponentMesh::Update", Profiler::Color::AliceBlue)
//Component must be active to update
if (!IsActive())
return;
if (mesh)
{
game_object->mesh_to_draw = mesh;
App->renderer3D->AddToDraw(GetGameObject());
}
if (App->renderer3D->renderAABBs)
{
App->renderer3D->DrawAABB(bounding_box.minPoint, bounding_box.maxPoint, float4(1, 1, 0, 1));
}
}
void ComponentMesh::OnInspector(bool debug)
{
string str = (string("Geometry Mesh") + string("##") + std::to_string(uuid));
if (ImGui::CollapsingHeader(str.c_str(), ImGuiTreeNodeFlags_DefaultOpen))
{
if (ImGui::IsItemClicked(1))
{
ImGui::OpenPopup("delete##mesh");
}
if (ImGui::BeginPopup("delete##mesh"))
{
if (ImGui::MenuItem("Delete"))
{
Remove();
}
ImGui::EndPopup();
}
//Active
bool is_active = IsActive();
if (ImGui::Checkbox("###activeMesh", &is_active))
{
SetActive(is_active);
}
if (mesh)
{
ImGui::Text("Number of vertices %d", mesh->num_vertices);
ImGui::Text("Number of indices %d", mesh->num_indices);
if (mesh->uvs != nullptr)
ImGui::Text("Has UVs: yes");
else
ImGui::Text("Has UVs: no");
if (mesh->normals != nullptr)
ImGui::Text("Has Normals: yes");
else
ImGui::Text("Has Normals: no");
if (mesh->colors != nullptr)
ImGui::Text("Has Colors: yes");
else
ImGui::Text("Has Colors: no");
ImGui::Text("Vertices id: %i", mesh->id_vertices);
ImGui::Text("Indices id: %i", mesh->id_indices);
ImGui::Text("UVs id: %i", mesh->id_uvs);
}
else
{
ImGui::TextColored(ImVec4(1, 0, 0, 1), "WARNING");
ImGui::SameLine(); ImGui::Text("No mesh was loaded.");
}
}
}
void ComponentMesh::OnTransformModified()
{
RecalculateBoundingBox();
}
bool ComponentMesh::SetMesh(Mesh *mesh)
{
bool ret = false;
if (mesh)
{
this->mesh = mesh;
aabb.Enclose((float3*)mesh->vertices, mesh->num_vertices);
RecalculateBoundingBox();
game_object->mesh_to_draw = this->mesh;
ret = true;
}
return ret;
}
void ComponentMesh::SetResourceMesh(ResourceFileMesh* resource)
{
rc_mesh = resource;
mesh = rc_mesh->GetMesh();
}
void ComponentMesh::RecalculateBoundingBox()
{
math::OBB ob = aabb;
ob.Transform(game_object->GetGlobalMatrix());
bounding_box = ob.MinimalEnclosingAABB();
game_object->bounding_box = &bounding_box;
}
void ComponentMesh::Save(Data & file)const
{
Data data;
data.AppendInt("type", type);
data.AppendUInt("UUID", uuid);
data.AppendBool("active", active);
if (mesh)
data.AppendString("path", mesh->file_path.data());
else
data.AppendString("path", "");
file.AppendArrayValue(data);
}
void ComponentMesh::Load(Data & conf)
{
uuid = conf.GetUInt("UUID");
active = conf.GetBool("active");
const char* path = conf.GetString("path");
rc_mesh = (ResourceFileMesh*)App->resource_manager->LoadResource(path, ResourceFileType::RES_MESH);
if (rc_mesh)
{
Mesh* mesh = rc_mesh->GetMesh();
if (mesh)
{
mesh->file_path = path;
SetMesh(mesh);
OnTransformModified();
}
}
else
{
LOG("[ERROR] Mesh path (%s) for %s cannot be loaded",path, game_object->name.data());
App->editor->DisplayWarning(WarningType::W_ERROR, "Mesh path (%s) for %s cannot be loaded", path, game_object->name.data());
}
}
void ComponentMesh::Remove()
{
game_object->RemoveComponent(this);
ComponentMaterial* material = (ComponentMaterial*)game_object->GetComponent(C_MATERIAL);
if (material != nullptr)
material->Remove();
}
bool ComponentMesh::HasBones()
{
return bones_reference.size() > 0;
}
void ComponentMesh::AddBone(ComponentBone* bone)
{
for (uint i = 0; i < bones_reference.size(); i++)
if (bones_reference[i].bone == bone)
return;
bones_reference.push_back(Bone_Reference(bone, bone->GetResource()->offset));
if (bones_vertex.empty())
{
bones_vertex = std::vector<Bone_Vertex>(mesh->num_vertices);
}
ResourceFileBone* rBone = bone->GetResource();
for (uint i = 0; i < rBone->numWeights; i++)
{
uint data_b_index = bones_reference.size() - 1;
float data_b_float = rBone->weights[i];
bones_vertex[rBone->weightsIndex[i]].AddBone(data_b_index, data_b_float);
}
}
void ComponentMesh::DeformAnimMesh()
{
BROFILER_CATEGORY("ComponentMesh::DeformAnimMesh", Profiler::Color::Maroon)
bones_trans.clear();
for (uint i = 0; i < bones_reference.size(); i++)
{
float4x4 matrix = bones_reference[i].bone->GetSystemTransform();
matrix = game_object->transform->GetLocalTransformMatrix().Inverted() * matrix;
float4x4 bone_trn_mat = matrix * bones_reference[i].offset;
bones_trans.push_back(bone_trn_mat.Transposed());
}
}
void ComponentMesh::InitAnimBuffers()
{
if (mesh != nullptr)
{
int size = mesh->num_vertices * 4;
float* weights = new float[size];
int* bones_ids = new int[size];
for (size_t i = 0; i < bones_vertex.size(); ++i)
{
int ver_id = i * 4;
if (bones_vertex[i].weights.size() != bones_vertex[i].bone_index.size())
{
LOG("[WARNING] %s has different number of weights and index in the animation", game_object->name); //Just in case
App->editor->DisplayWarning(WarningType::W_WARNING, "%s has different number of weights and index in the animation", game_object->name);
return;
}
//Reset all to zero
for (int w = 0; w < 4; ++w)
{
weights[ver_id + w] = 0;
bones_ids[ver_id + w] = 0;
}
for (size_t w = 0; w < bones_vertex[i].weights.size(); ++w)
{
weights[ver_id + w] = bones_vertex[i].weights[w];
bones_ids[ver_id + w] = bones_vertex[i].bone_index[w];
}
}
MeshImporter::LoadAnimBuffers(weights, size, weight_id, bones_ids, size, bone_id);
delete[] weights;
delete[] bones_ids;
animated = true;
}
else
{
// LOG("[WARNING] Trying to init animation buffers from '%s' without a mesh", game_object->name);
// App->editor->DisplayWarning(WarningType::W_WARNING, "Trying to init animation buffers from '%s' without a mesh", game_object->name);
}
}