-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
331 lines (293 loc) · 9.75 KB
/
Model.cpp
File metadata and controls
331 lines (293 loc) · 9.75 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
#include"Model.h"
Model::Model(const char* file)
{
// Make a JSON object
std::string text = get_file_contents(file);
JSON = 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, matricesMeshes[i]);
}
}
void Model::loadMesh(unsigned int indMesh)
{
// Get all accessor indices
unsigned int posAccInd = JSON["meshes"][indMesh]["primitives"][0]["attributes"]["POSITION"];
unsigned int normalAccInd = JSON["meshes"][indMesh]["primitives"][0]["attributes"]["NORMAL"];
unsigned int texAccInd = JSON["meshes"][indMesh]["primitives"][0]["attributes"]["TEXCOORD_0"];
unsigned int indAccInd = JSON["meshes"][indMesh]["primitives"][0]["indices"];
// Use accessor indices to get all vertices components
std::vector<float> posVec = getFloats(JSON["accessors"][posAccInd]);
std::vector<glm::vec3> positions = groupFloatsVec3(posVec);
std::vector<float> normalVec = getFloats(JSON["accessors"][normalAccInd]);
std::vector<glm::vec3> normals = groupFloatsVec3(normalVec);
std::vector<float> texVec = getFloats(JSON["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(JSON["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(unsigned int nextNode, glm::mat4 matrix)
{
// Current node
json node = JSON["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())
{
translationsMeshes.push_back(translation);
rotationsMeshes.push_back(rotation);
scalesMeshes.push_back(scale);
matricesMeshes.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 = JSON["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 = get_file_contents((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 = JSON["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 = JSON["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 < JSON["images"].size(); i++)
{
// uri of current texture
std::string texPath = JSON["images"][i]["uri"];
// Check if the texture has already been loaded
bool skip = false;
for (unsigned int j = 0; j < loadedTexName.size(); j++)
{
if (loadedTexName[j] == texPath)
{
textures.push_back(loadedTex[j]);
skip = true;
break;
}
}
// If the texture has been loaded, skip this
if (!skip)
{
// Load diffuse texture
if (texPath.find("baseColor") != std::string::npos || texPath.find("diffuse") != std::string::npos)
{
Texture diffuse = Texture((fileDirectory + texPath).c_str(), "diffuse", loadedTex.size());
textures.push_back(diffuse);
loadedTex.push_back(diffuse);
loadedTexName.push_back(texPath);
}
// Load specular texture
else if (texPath.find("metallicRoughness") != std::string::npos || texPath.find("specular") != std::string::npos)
{
Texture specular = Texture((fileDirectory + texPath).c_str(), "specular", loadedTex.size());
textures.push_back(specular);
loadedTex.push_back(specular);
loadedTexName.push_back(texPath);
}
}
}
return textures;
}
std::vector<Vertex> Model::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;
}
std::vector<glm::vec2> Model::groupFloatsVec2(std::vector<float> floatVec)
{
std::vector<glm::vec2> vectors;
for (int i = 0; i < floatVec.size(); i)
{
vectors.push_back(glm::vec2(floatVec[i++], floatVec[i++]));
}
return vectors;
}
std::vector<glm::vec3> Model::groupFloatsVec3(std::vector<float> floatVec)
{
std::vector<glm::vec3> vectors;
for (int i = 0; i < floatVec.size(); i)
{
vectors.push_back(glm::vec3(floatVec[i++], floatVec[i++], floatVec[i++]));
}
return vectors;
}
std::vector<glm::vec4> Model::groupFloatsVec4(std::vector<float> floatVec)
{
std::vector<glm::vec4> vectors;
for (int i = 0; i < floatVec.size(); i)
{
vectors.push_back(glm::vec4(floatVec[i++], floatVec[i++], floatVec[i++], floatVec[i++]));
}
return vectors;
}