-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.cpp
More file actions
411 lines (351 loc) · 15.2 KB
/
Mesh.cpp
File metadata and controls
411 lines (351 loc) · 15.2 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "Mesh.h"
#define TINYOBJLOADER_IMPLEMENTATION
#define TINYGLTF_IMPLEMENTATION
#define STBI_MSC_SECURE_CRT
#define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <tiny_obj_loader.h>
#include <tiny_gltf.h>
float ucharToFloat(UCHAR a, UCHAR b, UCHAR c, UCHAR d) {
float f;
UCHAR c_arr[] = { a, b, c, d };
memcpy(&f, &c_arr, sizeof(f));
return f;
}
unsigned short int ucharToUint(UCHAR a, UCHAR b) {
unsigned short int f;
UCHAR c_arr[] = { a, b };
memcpy(&f, &c_arr, sizeof(f));
return f;
}
UINT maxOfThree(UINT a, UINT b, UINT c) {
UINT max = a;
if (max < b)
max = b;
if (max < c)
max = c;
return max;
}
Mesh::Mesh(const wchar_t* full_path) : Resource(full_path)
{
tinyobj::attrib_t attribs;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::wstring input_file_temp(full_path);
std::string input_file(input_file_temp.begin(), input_file_temp.end()), warn, err;
std::filesystem::path filesystem_path = (std::filesystem::path)full_path;
std::vector<VertexMesh> list_vertices;
std::vector<unsigned int> list_indices;
bool res = FALSE;
if (filesystem_path.extension() == ".gltf" || filesystem_path.extension() == ".glb") {
tinygltf::TinyGLTF loader;
tinygltf::Model model;
if (filesystem_path.extension() == ".gltf") {
// Read the 3D model from a '.gltf' file
res = loader.LoadASCIIFromFile(&model, &err, &warn, input_file);
}
else {
// Read the 3D model from a '.glb' file
res = loader.LoadBinaryFromFile(&model, &err, &warn, input_file);
}
if (!res) throw std::exception("Could not read Mesh from gltf file.");
std::vector<Vec3> posVec, normalVec;
std::vector<Vec2> texVec;
size_t index_global_offset = 0, mat_id = 0;
// Parse meshes
for (size_t i = 0; i < model.meshes.size(); i++) {
for (size_t j = 0; j < model.meshes[i].primitives.size(); j++) {
tinygltf::Primitive primitive = model.meshes[i].primitives[j];
for (auto& attrib : primitive.attributes) {
tinygltf::Accessor acc = model.accessors[attrib.second];
tinygltf::BufferView bfv = model.bufferViews[acc.bufferView];
tinygltf::Buffer buffer = model.buffers[bfv.buffer];
UINT jmp = 1;
if (acc.type == 2)
jmp = 8;
if (acc.type == 3)
jmp = 12;
for (size_t k = bfv.byteOffset; k < bfv.byteOffset + bfv.byteLength; k+=jmp) {
if (attrib.first == "TEXCOORD_0") {
texVec.push_back(
Vec2(
ucharToFloat(
buffer.data[k],
buffer.data[k + 1],
buffer.data[k + 2],
buffer.data[k + 3]
),
ucharToFloat(
buffer.data[k + 4],
buffer.data[k + 5],
buffer.data[k + 6],
buffer.data[k + 7]
)
)
);
}
if (attrib.first == "POSITION") {
posVec.push_back(
Vec3(
-ucharToFloat(
buffer.data[k],
buffer.data[k + 1],
buffer.data[k + 2],
buffer.data[k + 3]
),
ucharToFloat(
buffer.data[k + 4],
buffer.data[k + 5],
buffer.data[k + 6],
buffer.data[k + 7]
),
ucharToFloat(
buffer.data[k + 8],
buffer.data[k + 9],
buffer.data[k + 10],
buffer.data[k + 11]
)
)
);
}
if (attrib.first == "NORMAL") {
normalVec.push_back(
Vec3(
ucharToFloat(
buffer.data[k],
buffer.data[k + 1],
buffer.data[k + 2],
buffer.data[k + 3]
),
ucharToFloat(
buffer.data[k + 4],
buffer.data[k + 5],
buffer.data[k + 6],
buffer.data[k + 7]
),
ucharToFloat(
buffer.data[k + 8],
buffer.data[k + 9],
buffer.data[k + 10],
buffer.data[k + 11]
)
)
);
}
}
}
tinygltf::BufferView bvi = model.bufferViews[model.accessors[primitive.indices].bufferView];
for (size_t k = bvi.byteOffset; k < bvi.byteLength + bvi.byteOffset; k += 2) {
list_indices.push_back(
ucharToUint(
model.buffers[bvi.buffer].data[k],
model.buffers[bvi.buffer].data[k + 1]
)
);
}
MaterialSlot mat_slot;
mat_slot.StartIndex = index_global_offset;
mat_slot.MaterialId = mat_id;
mat_slot.NumIndices = list_indices.size();
mMaterialSlots.push_back(mat_slot);
index_global_offset += list_indices.size();
}
mat_id++;
}
// Save in the list_vertices
size_t max_v = maxOfThree(posVec.size(), normalVec.size(), texVec.size());
for (size_t i = 0; i < max_v; i++) {
Vec3 pos;
Vec3 normal;
Vec2 tex;
if (i < posVec.size()) {
pos = posVec[i];
}
if (i < normalVec.size()) {
normal = normalVec[i];
}
if (i < texVec.size()) {
tex = texVec[i];
}
VertexMesh v_mesh(pos, tex, normal, Vec3(), Vec3());
list_vertices.push_back(v_mesh);
}
}
else if (filesystem_path.extension() == ".obj") {
std::string mtldir = input_file.substr(0, input_file.find_last_of("\\/"));
// Read the 3D Model from an '.obj' file
res = tinyobj::LoadObj(
&attribs,
&shapes,
&materials,
&warn,
&err,
input_file.c_str(),
mtldir.c_str()
);
if (!res) throw std::exception("Could not read Mesh from .obj file.");
size_t size_vertex_index_lists = 0;
for (auto& shape : shapes) {
size_vertex_index_lists += shape.mesh.indices.size();
}
list_vertices.reserve(size_vertex_index_lists);
list_indices.reserve(size_vertex_index_lists);
mMaterialSlots.resize(materials.size());
size_t index_global_offset = 0;
// Store the data from the '.obj' file
for (size_t m = 0; m < materials.size(); m++) {
mMaterialSlots[m].StartIndex = index_global_offset;
mMaterialSlots[m].MaterialId = m;
for (auto& shape : shapes) {
size_t index_offset = 0;
for (size_t f = 0; f < shape.mesh.num_face_vertices.size(); f++) {
unsigned char num_face_verts = shape.mesh.num_face_vertices[f];
if (shape.mesh.material_ids[f] != m) {
index_offset += num_face_verts;
continue;
}
Vec3 vertices_face[3];
Vec2 texcoords_face[3];
for (unsigned char v = 0; v < num_face_verts; v++) {
tinyobj::index_t index = shape.mesh.indices[index_offset + v];
tinyobj::real_t vx = attribs.vertices[(size_t)(index.vertex_index) * 3 + 0];
tinyobj::real_t vy = attribs.vertices[(size_t)(index.vertex_index) * 3 + 1];
tinyobj::real_t vz = attribs.vertices[(size_t)(index.vertex_index) * 3 + 2];
tinyobj::real_t tx = 0, ty = 0;
if (!attribs.texcoords.empty()) {
tx = attribs.texcoords[(size_t)(index.texcoord_index) * 2 + 0];
ty = attribs.texcoords[(size_t)(index.texcoord_index) * 2 + 1];
}
vertices_face[v] = Vec3(vx, vy, vz);
texcoords_face[v] = Vec2(tx, ty);
}
Vec3 tangent, binormal;
this->computeTangents(
vertices_face[0], vertices_face[1], vertices_face[2],
texcoords_face[0], texcoords_face[1], texcoords_face[2],
tangent, binormal
);
for (unsigned char v = 0; v < num_face_verts; v++) {
tinyobj::index_t index = shape.mesh.indices[index_offset + v];
tinyobj::real_t vx = attribs.vertices[(size_t)(index.vertex_index) * 3 + 0];
tinyobj::real_t vy = attribs.vertices[(size_t)(index.vertex_index) * 3 + 1];
tinyobj::real_t vz = attribs.vertices[(size_t)(index.vertex_index) * 3 + 2];
tinyobj::real_t tx = 0, ty = 0;
if (!attribs.texcoords.empty()) {
tx = attribs.texcoords[(size_t)(index.texcoord_index) * 2 + 0];
ty = attribs.texcoords[(size_t)(index.texcoord_index) * 2 + 1];
}
tinyobj::real_t nx = 0, ny = 0, nz = 0;
if (!attribs.normals.empty()) {
nx = attribs.normals[(size_t)(index.normal_index) * 3 + 0];
ny = attribs.normals[(size_t)(index.normal_index) * 3 + 1];
nz = attribs.normals[(size_t)(index.normal_index) * 3 + 2];
}
Vec3 v_tangent, v_binormal;
v_binormal = Vec3::cross(Vec3(nx, ny, nz), tangent);
v_tangent = Vec3::cross(v_binormal, Vec3(nx, ny, nz));
VertexMesh vertex(
Vec3(vx, vy, vz), // position
Vec2(tx, ty), // texcoord
Vec3(nx, ny, nz), // normal
v_tangent,
v_binormal
);
list_vertices.push_back(vertex);
list_indices.push_back((unsigned int)index_global_offset + v);
}
index_offset += num_face_verts;
index_global_offset += num_face_verts;
}
}
mMaterialSlots[m].NumIndices = index_global_offset - mMaterialSlots[m].StartIndex;
}
}
#if _DEBUG
if (!err.empty()) {
std::cout << "MESH ERR: " << err << std::endl;
throw std::exception("Could not read Mesh from file.");
}
if (!warn.empty()) std::cout << "MESH WARN:" << warn << std::endl;
#endif
void* shader_byte_code = nullptr;
size_t size_shader = 0;
// Retrieve the shader byte code of the 'VertexMeshLayoutShader.hlsl' compiled in GraphicsEngine constructor
GraphicsEngine::get()->getVertexMeshLayoutShaderByteCodeAndSize(
&shader_byte_code, &size_shader
);
// Create vertex and index buffer for the created mesh
mVertexBuffer = GraphicsEngine::get()->getRenderSystem()->createVertexBuffer(
&list_vertices[0],
sizeof(VertexMesh),
(UINT)list_vertices.size(),
shader_byte_code,
(UINT)size_shader
);
mIndexBuffer = GraphicsEngine::get()->getRenderSystem()->createIndexBuffer(
&list_indices[0],
(UINT)list_indices.size()
);
mRadius = this->boundingSphereRadius(list_vertices);
}
Mesh::Mesh(
VertexMesh* vertex_list_data,
unsigned int vertex_list_size,
unsigned int* index_list_data,
unsigned int index_list_size,
MaterialSlot* material_slot_list,
unsigned int material_slot_list_size
) : Resource(L"")
{
void* shader_byte_code = nullptr;
size_t size_shader = 0;
GraphicsEngine::get()->getVertexMeshLayoutShaderByteCodeAndSize(&shader_byte_code, &size_shader);
mVertexBuffer = GraphicsEngine::get()->getRenderSystem()->createVertexBuffer(
vertex_list_data,
sizeof(VertexMesh),
(UINT)vertex_list_size,
shader_byte_code,
(UINT)size_shader
);
mIndexBuffer = GraphicsEngine::get()->getRenderSystem()->createIndexBuffer(
index_list_data,
(UINT)index_list_size
);
mMaterialSlots.resize(material_slot_list_size);
for (unsigned int i = 0; i < material_slot_list_size; i++) {
mMaterialSlots[i] = material_slot_list[i];
}
}
Mesh::~Mesh() = default;
const VertexBufferPtr& Mesh::getVertexBuffer()
{
return mVertexBuffer;
}
const IndexBufferPtr& Mesh::getIndexBuffer()
{
return mIndexBuffer;
}
const MaterialSlot& Mesh::getMaterialSlot(unsigned int slot)
{
return mMaterialSlots[slot];
}
size_t Mesh::getNumMaterialSlots()
{
return mMaterialSlots.size();
}
void Mesh::computeTangents(const Vec3& v0, const Vec3& v1, const Vec3& v2, const Vec2& t0, const Vec2& t1, const Vec2& t2, Vec3& tangent, Vec3& binormal)
{
Vec3 deltaPos1 = v1 - v0;
Vec3 deltaPos2 = v2 - v0;
Vec2 deltaUV1 = t1 - t0;
Vec2 deltaUV2 = t2 - t0;
// float r = 1.0f / (deltaUV1.m_x * deltaUV2.m_y - deltaUV1.m_y * deltaUV2.m_x);
tangent = (deltaPos1 * deltaUV2.m_y - deltaPos2 * deltaUV1.m_y);
tangent = Vec3::normalize(tangent);
binormal = (deltaPos2 * deltaUV1.m_x - deltaPos1 * deltaUV2.m_x);
binormal = Vec3::normalize(binormal);
}
float Mesh::boundingSphereRadius(const std::vector<VertexMesh>& list_vertices)
{
return 0.0f;
}