-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
583 lines (506 loc) · 17.7 KB
/
Model.cpp
File metadata and controls
583 lines (506 loc) · 17.7 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include "Model.h"
template<typename V>
std::vector<V> groupFloats(const std::vector<GLfloat>& floats, const size_t& components) {
if (floats.size() % components != 0) {
throw std::invalid_argument{ "Invalid input size" };
}
std::vector<V> result;
result.reserve(floats.size() / components);
for (size_t i = 0; i < floats.size(); i += components) {
V vec;
for (size_t j = 0; j < components; ++j) {
vec[j] = floats[i + j];
}
result.push_back(vec);
}
return result;
}
#if 0
std::vector<Vertex> assembleVertices(std::vector<glm::vec3> positions, std::vector<glm::vec3> normals, std::vector<glm::vec2> texUVs)
{
std::vector<Vertex> vertices;
for (size_t i = 0; i < positions.size(); i++)
{
vertices.push_back(Vertex{ positions[i], normals[i], glm::vec3{ 1.0f, 1.0f, 1.0f }, texUVs[i] });
}
return vertices;
}
Model::Model(const char* file) : file{ file }, jsonObject(json::parse(read_file(file))), data{ getData() }
{
traverseNode(0);
}
void Model::draw(Shader& shader, Camera& camera)
{
for (size_t i = 0; i < meshes.size(); i++)
{
meshes[i].draw(shader, camera, meshMatrices[i]);
}
}
void Model::loadMesh(size_t indMesh)
{
size_t posAccInd = jsonObject["meshes"][indMesh]["primitives"][0]["attributes"]["POSITION"];
size_t normalAccInd = jsonObject["meshes"][indMesh]["primitives"][0]["attributes"]["NORMAL"];
size_t texAccInd = jsonObject["meshes"][indMesh]["primitives"][0]["attributes"]["TEXCOORD_0"];
size_t indAccInd = jsonObject["meshes"][indMesh]["primitives"][0]["indices"];
std::vector<GLfloat> posVec = getFloats(jsonObject["accessors"][posAccInd]);
std::vector<glm::vec3> positions = groupFloatsVec3(posVec);
std::vector<GLfloat> normalVec = getFloats(jsonObject["accessors"][normalAccInd]);
std::vector<glm::vec3> normals = groupFloatsVec3(normalVec);
std::vector<GLfloat> texVec = getFloats(jsonObject["accessors"][texAccInd]);
std::vector<glm::vec2> texUVs = groupFloatsVec2(texVec);
std::vector<Vertex> vertices = assembleVertices(positions, normals, texUVs);
std::vector<GLuint> indices = getIndices(jsonObject["accessors"][indAccInd]);
std::vector<Texture> textures = getTextures();
meshes.push_back(Mesh{ vertices, indices, textures });
}
std::vector<stbi_uc> Model::getData()
{
std::string uri = jsonObject["buffers"][0]["uri"];
std::string fileStr(file);
std::string fileDirectory = fileStr.substr(0, fileStr.find_last_of('/') + 1);
std::string bytesText = read_file(fileDirectory + uri);
std::vector<stbi_uc> data(bytesText.begin(), bytesText.end());
return data;
}
void Model::traverseNode(size_t nextNode, glm::mat4 matrix)
{
json node = jsonObject["nodes"][nextNode];
glm::vec3 translation(0.0f, 0.0f, 0.0f);
if (node.find("translation") != node.end())
{
GLfloat transValues[3];
for (size_t i = 0; i < node["translation"].size(); i++)
{
transValues[i] = node["translation"][i];
}
translation = glm::make_vec3(transValues);
}
glm::quat rotation(1.0f, 0.0f, 0.0f, 0.0f);
if (node.find("rotation") != node.end())
{
GLfloat rotValues[] =
{
node["rotation"][3],
node["rotation"][0],
node["rotation"][1],
node["rotation"][2]
};
rotation = glm::make_quat(rotValues);
}
glm::vec3 scale(1.0f, 1.0f, 1.0f);
if (node.find("scale") != node.end())
{
GLfloat scaleValues[3];
for (size_t i = 0; i < node["scale"].size(); i++)
{
scaleValues[i] = node["scale"][i];
}
scale = glm::make_vec3(scaleValues);
}
glm::mat4 matNode(1.0f);
if (node.find("matrix") != node.end())
{
GLfloat matValues[16];
for (size_t i = 0; i < node["matrix"].size(); i++)
{
matValues[i] = node["matrix"][i];
}
matNode = glm::make_mat4(matValues);
}
glm::mat4 trans = glm::translate(glm::mat4(1.0f), translation);
glm::mat4 rot = glm::mat4_cast(rotation);
glm::mat4 sca = glm::scale(glm::mat4(1.0f), scale);
glm::mat4 matNextNode = matrix * matNode * trans * rot * sca;
if (node.find("mesh") != node.end())
{
meshTranslations.push_back(translation);
meshRotations.push_back(rotation);
meshScales.push_back(scale);
meshMatrices.push_back(matNextNode);
loadMesh(node["mesh"]);
}
if (node.find("children") != node.end())
{
for (size_t i = 0; i < node["children"].size(); i++)
{
traverseNode(node["children"][i], matNextNode);
}
}
}
std::vector<GLfloat> Model::getFloats(json accessor)
{
std::vector<GLfloat> floatVec;
size_t bufViewInd = accessor.value("bufferView", 1);
size_t count = accessor["count"];
size_t accByteOff = accessor.value("byteOffset", 0);
std::string type = accessor["type"];
json bufferView = jsonObject["bufferViews"][bufViewInd];
size_t byteOffset = bufferView["byteOffset"];
size_t numPerVert = type == "SCALAR" ? 1 : type == "VEC2" ? 2 : type == "VEC3" ? 3 : type == "VEC4" ? 4 : throw std::invalid_argument{ "Invalid type. (Not SCALAR, VEC2, VEC3 or VEC4)" };
size_t beginningOfData = byteOffset + accByteOff;
size_t lengthOfData = count * 4 * numPerVert;
for (size_t i = beginningOfData; i < beginningOfData + lengthOfData;)
{
stbi_uc bytes[] = { data[i++], data[i++], data[i++], data[i++] };
float value;
std::memcpy(&value, bytes, sizeof(GLfloat));
floatVec.push_back(value);
}
return floatVec;
}
std::vector<GLuint> Model::getIndices(json accessor)
{
std::vector<GLuint> indices;
size_t bufViewInd = accessor.value("bufferView", 0);
size_t count = accessor["count"];
size_t accByteOff = accessor.value("byteOffset", 0);
GLuint componentType = accessor["componentType"];
json bufferView = jsonObject["bufferViews"][bufViewInd];
size_t byteOffset = bufferView["byteOffset"];
size_t beginningOfData = byteOffset + accByteOff;
switch (componentType)
{
case 5125: // unsigned int
for (size_t i = beginningOfData; i < byteOffset + accByteOff + count * 4;)
{
stbi_uc bytes[] = { data[i++], data[i++], data[i++], data[i++] };
GLuint value;
std::memcpy(&value, bytes, sizeof(GLuint));
indices.push_back(value);
}
break;
case 5123: // unsigned short
for (size_t i = beginningOfData; i < byteOffset + accByteOff + count * 2;)
{
stbi_uc bytes[] = { data[i++], data[i++] };
GLushort value;
std::memcpy(&value, bytes, sizeof(GLushort));
indices.push_back(static_cast<GLuint>(value));
}
break;
case 5122: // short
for (size_t i = beginningOfData; i < byteOffset + accByteOff + count * 2;)
{
stbi_uc bytes[] = { data[i++], data[i++] };
GLshort value;
std::memcpy(&value, bytes, sizeof(GLshort));
indices.push_back(static_cast<GLuint>(value));
}
break;
default:
throw std::runtime_error{ "Unrecognized componentType: " + componentType };
// don't forget to put a break here in the future!
}
return indices;
}
std::vector<Texture> Model::getTextures()
{
std::vector<Texture> textures;
std::string fileStr{ file };
std::string fileDirectory = fileStr.substr(0, fileStr.find_last_of('/') + 1);
bool skip;
for (size_t i = 0; i < jsonObject["images"].size(); i++)
{
std::string texPath = jsonObject["images"][i]["uri"];
skip = false;
for (size_t j = 0; j < loadedTexNames.size(); j++)
{
if (loadedTexNames[j] == texPath)
{
textures.push_back(loadedTextures[j]);
skip = true;
break;
}
}
if (skip)
{
continue;
}
if (texPath.find("baseColor") != std::string::npos)
{
Texture diffuse{ (fileDirectory + texPath).c_str(), "diffuse", static_cast<GLuint>(loadedTextures.size()), false };
textures.push_back(diffuse);
loadedTextures.push_back(diffuse);
}
else if (texPath.find("metallicRoughness") != std::string::npos)
{
Texture specular{ (fileDirectory + texPath).c_str(), "specular", static_cast<GLuint>(loadedTextures.size()), false };
textures.push_back(specular);
loadedTextures.push_back(specular);
}
else
{
throw std::runtime_error{ "Unrecongnized texture path name" };
}
loadedTexNames.push_back(texPath);
}
return textures;
}
#else
#include "Model.h"
Model::Model(const char* file)
{
// Make a jsonObject object
std::string text = read_file(file);
jsonObject = json::parse(text);
// Get the binary data
Model::file = file;
data = getData();
// Traverse all nodes
traverseNode(0);
}
void Model::draw(Shader& shader, Camera& camera)
{
// Go over all meshes and draw each one
for (unsigned int i = 0; i < meshes.size(); i++)
{
meshes[i].Mesh::draw(shader, camera, meshMatrices[i]);
}
}
void Model::loadMesh(size_t indMesh)
{
// Get all accessor indices
unsigned int posAccInd = jsonObject["meshes"][indMesh]["primitives"][0]["attributes"]["POSITION"];
unsigned int normalAccInd = jsonObject["meshes"][indMesh]["primitives"][0]["attributes"]["NORMAL"];
unsigned int texAccInd = jsonObject["meshes"][indMesh]["primitives"][0]["attributes"]["TEXCOORD_0"];
unsigned int indAccInd = jsonObject["meshes"][indMesh]["primitives"][0]["indices"];
// Use accessor indices to get all vertices components
std::vector<float> posVec = getFloats(jsonObject["accessors"][posAccInd]);
std::vector<glm::vec3> positions = groupFloatsVec3(posVec);
std::vector<float> normalVec = getFloats(jsonObject["accessors"][normalAccInd]);
std::vector<glm::vec3> normals = groupFloatsVec3(normalVec);
std::vector<float> texVec = getFloats(jsonObject["accessors"][texAccInd]);
std::vector<glm::vec2> texUVs = groupFloatsVec2(texVec);
// Combine all the vertex components and also get the indices and textures
std::vector<Vertex> vertices = assembleVertices(positions, normals, texUVs);
std::vector<GLuint> indices = getIndices(jsonObject["accessors"][indAccInd]);
std::vector<Texture> textures = getTextures();
// Combine the vertices, indices, and textures into a mesh
meshes.push_back(Mesh(vertices, indices, textures));
}
void Model::traverseNode(size_t nextNode, glm::mat4 matrix)
{
// Current node
json node = jsonObject["nodes"][nextNode];
// Get translation if it exists
glm::vec3 translation = glm::vec3(0.0f, 0.0f, 0.0f);
if (node.find("translation") != node.end())
{
float transValues[3];
for (unsigned int i = 0; i < node["translation"].size(); i++)
transValues[i] = (node["translation"][i]);
translation = glm::make_vec3(transValues);
}
// Get quaternion if it exists
glm::quat rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f);
if (node.find("rotation") != node.end())
{
float rotValues[4] =
{
node["rotation"][3],
node["rotation"][0],
node["rotation"][1],
node["rotation"][2]
};
rotation = glm::make_quat(rotValues);
}
// Get scale if it exists
glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f);
if (node.find("scale") != node.end())
{
float scaleValues[3];
for (unsigned int i = 0; i < node["scale"].size(); i++)
scaleValues[i] = (node["scale"][i]);
scale = glm::make_vec3(scaleValues);
}
// Get matrix if it exists
glm::mat4 matNode = glm::mat4(1.0f);
if (node.find("matrix") != node.end())
{
float matValues[16];
for (unsigned int i = 0; i < node["matrix"].size(); i++)
matValues[i] = (node["matrix"][i]);
matNode = glm::make_mat4(matValues);
}
// Initialize matrices
glm::mat4 trans = glm::mat4(1.0f);
glm::mat4 rot = glm::mat4(1.0f);
glm::mat4 sca = glm::mat4(1.0f);
// Use translation, rotation, and scale to change the initialized matrices
trans = glm::translate(trans, translation);
rot = glm::mat4_cast(rotation);
sca = glm::scale(sca, scale);
// Multiply all matrices together
glm::mat4 matNextNode = matrix * matNode * trans * rot * sca;
// Check if the node contains a mesh and if it does load it
if (node.find("mesh") != node.end())
{
meshTranslations.push_back(translation);
meshRotations.push_back(rotation);
meshScales.push_back(scale);
meshMatrices.push_back(matNextNode);
loadMesh(node["mesh"]);
}
// Check if the node has children, and if it does, apply this function to them with the matNextNode
if (node.find("children") != node.end())
{
for (unsigned int i = 0; i < node["children"].size(); i++)
traverseNode(node["children"][i], matNextNode);
}
}
std::vector<unsigned char> Model::getData()
{
// Create a place to store the raw text, and get the uri of the .bin file
std::string bytesText;
std::string uri = jsonObject["buffers"][0]["uri"];
// Store raw text data into bytesText
std::string fileStr = std::string(file);
std::string fileDirectory = fileStr.substr(0, fileStr.find_last_of('/') + 1);
bytesText = read_file((fileDirectory + uri).c_str());
// Transform the raw text data into bytes and put them in a vector
std::vector<unsigned char> data(bytesText.begin(), bytesText.end());
return data;
}
std::vector<float> Model::getFloats(json accessor)
{
std::vector<float> floatVec;
// Get properties from the accessor
unsigned int buffViewInd = accessor.value("bufferView", 1);
unsigned int count = accessor["count"];
unsigned int accByteOffset = accessor.value("byteOffset", 0);
std::string type = accessor["type"];
// Get properties from the bufferView
json bufferView = jsonObject["bufferViews"][buffViewInd];
unsigned int byteOffset = bufferView["byteOffset"];
// Interpret the type and store it into numPerVert
unsigned int numPerVert;
if (type == "SCALAR") numPerVert = 1;
else if (type == "VEC2") numPerVert = 2;
else if (type == "VEC3") numPerVert = 3;
else if (type == "VEC4") numPerVert = 4;
else throw std::invalid_argument("Type is invalid (not SCALAR, VEC2, VEC3, or VEC4)");
// Go over all the bytes in the data at the correct place using the properties from above
unsigned int beginningOfData = byteOffset + accByteOffset;
unsigned int lengthOfData = count * 4 * numPerVert;
for (unsigned int i = beginningOfData; i < beginningOfData + lengthOfData; i)
{
unsigned char bytes[] = { data[i++], data[i++], data[i++], data[i++] };
float value;
std::memcpy(&value, bytes, sizeof(float));
floatVec.push_back(value);
}
return floatVec;
}
std::vector<GLuint> Model::getIndices(json accessor)
{
std::vector<GLuint> indices;
// Get properties from the accessor
unsigned int buffViewInd = accessor.value("bufferView", 0);
unsigned int count = accessor["count"];
unsigned int accByteOffset = accessor.value("byteOffset", 0);
unsigned int componentType = accessor["componentType"];
// Get properties from the bufferView
json bufferView = jsonObject["bufferViews"][buffViewInd];
unsigned int byteOffset = bufferView["byteOffset"];
// Get indices with regards to their type: unsigned int, unsigned short, or short
unsigned int beginningOfData = byteOffset + accByteOffset;
if (componentType == 5125)
{
for (unsigned int i = beginningOfData; i < byteOffset + accByteOffset + count * 4; i)
{
unsigned char bytes[] = { data[i++], data[i++], data[i++], data[i++] };
unsigned int value;
std::memcpy(&value, bytes, sizeof(unsigned int));
indices.push_back((GLuint)value);
}
}
else if (componentType == 5123)
{
for (unsigned int i = beginningOfData; i < byteOffset + accByteOffset + count * 2; i)
{
unsigned char bytes[] = { data[i++], data[i++] };
unsigned short value;
std::memcpy(&value, bytes, sizeof(unsigned short));
indices.push_back((GLuint)value);
}
}
else if (componentType == 5122)
{
for (unsigned int i = beginningOfData; i < byteOffset + accByteOffset + count * 2; i)
{
unsigned char bytes[] = { data[i++], data[i++] };
short value;
std::memcpy(&value, bytes, sizeof(short));
indices.push_back((GLuint)value);
}
}
return indices;
}
std::vector<Texture> Model::getTextures()
{
std::vector<Texture> textures;
std::string fileStr = std::string(file);
std::string fileDirectory = fileStr.substr(0, fileStr.find_last_of('/') + 1);
// Go over all images
for (unsigned int i = 0; i < jsonObject["images"].size(); i++)
{
// uri of current texture
std::string texPath = jsonObject["images"][i]["uri"];
// Check if the texture has already been loaded
bool skip = false;
for (unsigned int j = 0; j < loadedTexNames.size(); j++)
{
if (loadedTexNames[j] == texPath)
{
textures.push_back(loadedTextures[j]);
skip = true;
break;
}
}
// If the texture has been loaded, skip this
if (!skip)
{
// Load diffuse texture
if (texPath.find("baseColor") != std::string::npos)
{
Texture diffuse = Texture((fileDirectory + texPath).c_str(), "diffuse", loadedTextures.size());
textures.push_back(diffuse);
loadedTextures.push_back(diffuse);
loadedTexNames.push_back(texPath);
}
// Load specular texture
else if (texPath.find("metallicRoughness") != std::string::npos)
{
Texture specular = Texture((fileDirectory + texPath).c_str(), "specular", loadedTextures.size());
textures.push_back(specular);
loadedTextures.push_back(specular);
loadedTexNames.push_back(texPath);
}
}
}
return textures;
}
std::vector<Vertex> assembleVertices
(
std::vector<glm::vec3> positions,
std::vector<glm::vec3> normals,
std::vector<glm::vec2> texUVs
)
{
std::vector<Vertex> vertices;
for (int i = 0; i < positions.size(); i++)
{
vertices.push_back
(
Vertex
{
positions[i],
normals[i],
glm::vec3(1.0f, 1.0f, 1.0f),
texUVs[i]
}
);
}
return vertices;
}
#endif