-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilereader.cpp
More file actions
430 lines (369 loc) · 21.8 KB
/
filereader.cpp
File metadata and controls
430 lines (369 loc) · 21.8 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
#include "filereader.h"
static int koe = 0;
FileReader::FileReader() {
this->mpLoader = std::make_unique<tinygltf::TinyGLTF>();
this->mpModel = nullptr;
}
/// <summary>
/// LoadFile function opens a glTF file and reads its content into the tinygltf::Model object.
/// </summary>
/// <param name="name">Filepath to the file to open.</param>
/// <returns></returns>
bool FileReader::loadFile(std::string name) {
std::string err;
std::string warn;
tinygltf::Model temp;
// bool ret = this->mpLoader->LoadBinaryFromFile(&temp, &err, &warn, name); // This is for .glb files. Does not work!
bool ret = this->mpLoader->LoadASCIIFromFile(&temp, &err, &warn, name); // This is for .gltf files.
if (ret && err.empty() && warn.empty()) {
this->mpModel = std::make_unique<tinygltf::Model>(temp);
return true;
} else {
if (!warn.empty()) {
qWarning(warn.c_str());
}
if (!err.empty()) {
qFatal(err.c_str());
}
return false;
}
}
tinygltf::Model* FileReader::getModel() {
if (this->mpModel == nullptr) return nullptr;
return this->mpModel.get();
}
/// <summary>
/// Traverse function reads the vertices contents of a single node and recursively reads all child nodes.
/// </summary>
/// <param name="nodeIndex">The index of the node we want to investigate.</param>
/// <param name="vertices">Vector where to store Vertex data.</param>
/// <param name="transformation">Transformation matrix.</param>
/// <param name="model">Optional Model object consisting one glTF file.</param>
void FileReader::traverse(int& nodeIndex, std::vector<Vertex>& vertices,
tinygltf::Model* model, glm::mat4x4 transformation) {
if (model == nullptr) {
model = this->getModel();
}
if (model == nullptr) {
qFatal("Tinygltf model is null!");
}
// Each node in glTF does or doesn't have a set of meshes. However, tinygltf can have only
// single mesh in a node. Let's investigate it.
int meshIndex = -1;
if (model->nodes.size() > nodeIndex) {
meshIndex = model->nodes[nodeIndex].mesh;
// If meshIndex is invalid, skip it.
if (meshIndex < 0) goto down;
// If this node has transformation matrix, we apply it now.
std::vector<double> vec = model->nodes[nodeIndex].matrix;
if (vec.size() == 16) {
glm::mat4x4 mat = { vec[0], vec[1], vec[2], vec[3], vec[4], vec[5], vec[6], vec[7],
vec[8], vec[9], vec[10], vec[11], vec[12], vec[13], vec[14], vec[15] };
transformation = transformation * mat;
}
// Let's enum every primitive in a mesh.
for (int j = 0; j < model->meshes[meshIndex].primitives.size(); j++) {
// Each primitive has it's part of the vertex data, ie. indexes of accessors where
// vertex position, normal and texture coordinate data as well other attributes can be
// acqured. CreateVertexBuffer creates a vertex buffer, so we are only interested to get
// vertex positions. They can be served as indexed or non indexed data.
int indexAccessor = -1;
int positionAccessor = -1;
int colorAccessor = -1;
try {
for (std::pair<const std::string, int> attribute : model->meshes[meshIndex].primitives[j].attributes) {
// Vertex position data accessor.
if (attribute.first == "POSITION") {
positionAccessor = attribute.second;
}
// Vertex first color data accessor.
if (attribute.first == "COLOR_0") {
colorAccessor = attribute.second;
}
}
// Possible vertex attributes indexing accessor.
indexAccessor = model->meshes[meshIndex].primitives[j].indices;
}
catch (...) {
// Three dots means we catch any exception and continue.
continue;
}
getVertexData(vertices, *model, positionAccessor, indexAccessor, colorAccessor, transformation);
}
}
down:
// Meshes are done for this node. Now check are there any child nodes.
std::vector<int> children = model->nodes[nodeIndex].children;
for (int j = 0; j < children.size(); j++) {
traverse(children[j], vertices, model, transformation);
}
}
/// <summary>
/// GetVertexData function reads vertexes from tinygltf model into a std::vector.
/// </summary>
/// <param name="vertices">A vector where vertices are added into.</param>
/// <param name="model">The source of all glTF data.</param>
/// <param name="posAcc">A glTF accessor index where to find vertex positions.</param>
/// <param name="indAcc">A glTF accessor index where to find vertex indices. If not available, ingnore.</param>
void FileReader::getVertexData(std::vector<Vertex>& vertices,
tinygltf::Model& model, int& posAcc, int indAcc, int colorAcc, glm::mat4x4 transformation) {
// Always need position accessor.
if (posAcc < 0) return;
// Index accessor is not needed necessarily.
if (indAcc < 0) {
// We want only positions. Ensure they are float 3D vectors.
if (model.accessors[posAcc].componentType == TINYGLTF_COMPONENT_TYPE_FLOAT &&
model.accessors[posAcc].type == TINYGLTF_TYPE_VEC3) {
// Get the index and offset of the bufferView to get the buffer.
int bufferView = model.accessors[posAcc].bufferView;
int byteOffsetBufferView = model.accessors[posAcc].byteOffset;
// Get the index, offset of the stride, lengt and stride of the buffer
// to get the vectors.
int bufferIndex = model.bufferViews[bufferView].buffer;
int byteOffsetStride = model.bufferViews[bufferView].byteOffset;
int byteLength = model.bufferViews[bufferView].byteLength;
int byteStride = std::max(model.bufferViews[bufferView].byteStride, size_t(12));
// Store each position into the vertices vector. glTF buffers have little endian
// byte order.
try {
std::vector<unsigned char> buffer = model.buffers[bufferIndex].data;
for (int i = 0; i < (model.accessors[posAcc].count); i++) {
// Define three position coordinates (X, Y, Z).
uint8_t c1 = buffer[3 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
uint8_t c2 = buffer[2 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
uint8_t c3 = buffer[1 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
uint8_t c4 = buffer[0 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
uint32_t four = ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4 << 0));
float pos1;
std::memcpy(&pos1, &four, sizeof(float));
c1 = buffer[7 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
c2 = buffer[6 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
c3 = buffer[5 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
c4 = buffer[4 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
four = ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4 << 0));
float pos2;
std::memcpy(&pos2, &four, sizeof(float));
c1 = buffer[11 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
c2 = buffer[10 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
c3 = buffer[9 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
c4 = buffer[8 + i * byteStride + byteOffsetBufferView + byteOffsetStride];
four = ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4 << 0));
float pos3;
std::memcpy(&pos3, &four, sizeof(float));
// Need to transform vertex position into its absolute position in space.
// This makes the Model matrix computation in common ModelViewProjection
// matrix computation chain.
glm::vec4 position(pos1, pos2, pos3, 1.0);
position = transformation * position;
// Need to update minmax.
this->setMinMax(position);
// If any color not returned, we shouldn't accept vertex. But glTF file can contain vertex data
// without vertex color. Because we don't have textures or materials implemented yet,
// we paint colorless vertex as green, so we can see it.
auto temp = this->getColor(model, colorAcc, i);
if (!temp.has_value()) {
temp = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f);
}
vertices.push_back({ glm::vec3(position), temp.value()});
}
}
catch (...) {
qWarning("GetVertexData function had exception. Vertex data is invalid.");
}
}
}
else {
// We have indexers too. Ensure they are unsigned byte, short or int scalars and positions
// are float 3D vectors.
int indexComponentType = -1;
switch (model.accessors[indAcc].componentType) {
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE: indexComponentType = 1;
break;
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT: indexComponentType = 2;
break;
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT: indexComponentType = 4;
break;
}
if (indexComponentType != -1 &&
model.accessors[indAcc].type == TINYGLTF_TYPE_SCALAR &&
model.accessors[posAcc].componentType == TINYGLTF_COMPONENT_TYPE_FLOAT &&
model.accessors[posAcc].type == TINYGLTF_TYPE_VEC3) {
// Get the indexes and offsets of the bufferViews to get the data of
// positions and indexes.
int bufferViewPos = model.accessors[posAcc].bufferView;
int byteOffsetBufferViewPos = model.accessors[posAcc].byteOffset;
int bufferViewInd = model.accessors[indAcc].bufferView;
int byteOffsetBufferViewInd = model.accessors[indAcc].byteOffset;
// Get the index, offset of the stride, lengt and stride of the buffer
// to get the vectors and their indexes.
int bufferIndexPos = model.bufferViews[bufferViewPos].buffer;
int byteOffsetStridePos = model.bufferViews[bufferViewPos].byteOffset;
int byteLengthPos = model.bufferViews[bufferViewPos].byteLength;
int byteStridePos = std::max(model.bufferViews[bufferViewPos].byteStride, size_t(12));
int bufferIndexInd = model.bufferViews[bufferViewInd].buffer;
int byteOffsetStrideInd = model.bufferViews[bufferViewInd].byteOffset;
int byteLengthInd = model.bufferViews[bufferViewInd].byteLength;
// Store each position into the vertices vector. glTF buffers have little endian
// byte order. Now each scalar index represent the location of one vertex position
// whose vector can be found in that index.
try {
std::vector<unsigned char> bufferInd = model.buffers[bufferIndexInd].data;
std::vector<unsigned char> bufferPos = model.buffers[bufferIndexPos].data;
for (int i = 0; i < (model.accessors[posAcc].count); i++) {
// Define the index.
uint32_t index = 0;
switch (indexComponentType) {
case 4: index = (uint32_t) (
((bufferInd[3 + i * 4 + byteOffsetBufferViewInd + byteOffsetStrideInd] << 24) |
(bufferInd[2 + i * 4 + byteOffsetBufferViewInd + byteOffsetStrideInd] << 16) |
(bufferInd[1 + i * 4 + byteOffsetBufferViewInd + byteOffsetStrideInd] << 8) |
bufferInd[0 + i * 4 + byteOffsetBufferViewInd + byteOffsetStrideInd]));
break;
case 2: index = (uint32_t) (
((bufferInd[1 + i * 2 + byteOffsetBufferViewInd + byteOffsetStrideInd] << 8) |
bufferInd[0 + i * 2 + byteOffsetBufferViewInd + byteOffsetStrideInd]));
break;
case 1: index = (uint32_t) bufferInd[0 + i + byteOffsetBufferViewInd + byteOffsetStrideInd];
break;
}
// Define three position coordinates (X, Y, Z).
uint8_t c1 = bufferPos[3 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
uint8_t c2 = bufferPos[2 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
uint8_t c3 = bufferPos[1 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
uint8_t c4 = bufferPos[0 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
uint32_t four = ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4 << 0));
float pos1;
std::memcpy(&pos1, &four, sizeof(float));
c1 = bufferPos[7 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
c2 = bufferPos[6 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
c3 = bufferPos[5 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
c4 = bufferPos[4 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
four = ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4 << 0));
float pos2;
std::memcpy(&pos2, &four, sizeof(float));
c1 = bufferPos[11 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
c2 = bufferPos[10 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
c3 = bufferPos[9 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
c4 = bufferPos[8 + index * byteStridePos + byteOffsetBufferViewPos + byteOffsetStridePos];
four = ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4 << 0));
float pos3;
std::memcpy(&pos3, &four, sizeof(float));
// Need to transform vertex position into its absolute position in space.
// This makes the Model matrix computation in common ModelViewProjection
// matrix computation chain.
glm::vec4 position(pos1, pos2, pos3, 1.0);
position = transformation * position;
// Need to update minmax.
this->setMinMax(position);
// If any color not returned, we shouldn't accept vertex. But glTF file can contain vertex data
// without vertex color. Because we don't have textures or materials implemented yet,
// we paint colorless vertex as green, so we can see it.
auto temp = this->getColor(model, colorAcc, index);
if (!temp.has_value()) {
temp = glm::vec4(0.0f, 1.0f, 0.0f, 1.0f);
}
vertices.push_back({ glm::vec3(position), glm::vec3(temp.value()) });
}
}
catch (...) {
qWarning("GetVertexData function had exception. Vertex data is invalid.");
}
}
}
}
/// <summary>
/// GetColor function reads the color of a single vertex.
/// </summary>
/// <param name="model">The source of all glTF data.</param>
/// <param name="colorAcc">The index of color accessor.</param>
/// <param name="index">The index of color.</param>
/// <returns>Vec4 color vector.</returns>
std::optional<glm::vec4> FileReader::getColor(tinygltf::Model& model, const int& colorAcc, const unsigned int& index) {
// Ensure we have correct color data.
int colorComponentType = -1;
if (colorAcc == -1) return std::optional<glm::vec4>(std::nullopt);
switch (model.accessors[colorAcc].componentType) {
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE: colorComponentType = 1;
break;
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT: colorComponentType = 2;
break;
case TINYGLTF_COMPONENT_TYPE_FLOAT: colorComponentType = 4;
break;
}
if (colorComponentType != -1 && model.accessors[colorAcc].type == TINYGLTF_TYPE_VEC4) {
// Get the index and offset of the bufferView to get the buffer.
int bufferView = model.accessors[colorAcc].bufferView;
int byteOffsetBufferView = model.accessors[colorAcc].byteOffset;
// Get the index, lengt and stride of the buffer to get the color vector.
int bufferIndex = model.bufferViews[bufferView].buffer;
int byteOffsetStride = model.bufferViews[bufferView].byteOffset;
int byteLength = model.bufferViews[bufferView].byteLength;
// Parse float values for vec4.
std::vector<unsigned char> buffer = model.buffers[bufferIndex].data;
switch (colorComponentType) {
case 1: {
float c1 = (float) buffer[3 + index + byteOffsetBufferView + byteOffsetStride];
float c2 = (float) buffer[2 + index + byteOffsetBufferView + byteOffsetStride];
float c3 = (float) buffer[1 + index + byteOffsetBufferView + byteOffsetStride];
float c4 = (float) buffer[0 + index + byteOffsetBufferView + byteOffsetStride];
return glm::vec4(c1 / 255.0f, c2 / 255.0f, c3 / 255.0f, c4 / 255.0f);
}
case 2: {
// Get the step stride of the color data.
int byteStride = std::max(model.bufferViews[bufferView].byteStride, size_t(2));
float c1 = (float) ((buffer[1 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 8) |
(buffer[0 + index * byteStride + byteOffsetBufferView + byteOffsetStride]));
float c2 = (float) ((buffer[3 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 8) |
(buffer[2 + index * byteStride + byteOffsetBufferView + byteOffsetStride]));
float c3 = (float) ((buffer[5 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 8) |
(buffer[4 + index * byteStride + byteOffsetBufferView + byteOffsetStride]));
float c4 = (float) ((buffer[7 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 8) |
(buffer[6 + index * byteStride + byteOffsetBufferView + byteOffsetStride]));
return glm::vec4(c1 / 65025.0f, c2 / 65025.0f, c3 / 65025.0f, c4 / 65025.0f);
}
case 4: {
// Get the step stride of the color data.
int byteStride = std::max(model.bufferViews[bufferView].byteStride, size_t(4));
uint32_t c1 = (uint32_t) (
(buffer[3 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 24) |
(buffer[2 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 16) |
(buffer[1 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 8) |
(buffer[0 + index * byteStride + byteOffsetBufferView + byteOffsetStride]));
uint32_t c2 = (uint32_t) (
(buffer[7 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 24) |
(buffer[6 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 16) |
(buffer[5 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 8) |
(buffer[4 + index * byteStride + byteOffsetBufferView + byteOffsetStride]));
uint32_t c3 = (uint32_t) (
(buffer[11 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 24) |
(buffer[10 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 16) |
(buffer[9 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 8) |
(buffer[8 + index * byteStride + byteOffsetBufferView + byteOffsetStride]));
uint32_t c4 = (uint32_t) (
(buffer[15 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 24) |
(buffer[14 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 16) |
(buffer[13 + index * byteStride + byteOffsetBufferView + byteOffsetStride] << 8) |
(buffer[12 + index * byteStride + byteOffsetBufferView + byteOffsetStride]));
float cc1, cc2, cc3, cc4;
std::memcpy(&cc1, &c1, sizeof(float));
std::memcpy(&cc2, &c2, sizeof(float));
std::memcpy(&cc3, &c3, sizeof(float));
std::memcpy(&cc4, &c4, sizeof(float));
return glm::vec4(cc1, cc2, cc3, cc4);
}
}
}
return std::optional<glm::vec4>(std::nullopt);
}
/// <summary>
/// SetMinMax function is used to find out the absolute bounding box consisting the whole scene.
/// </summary>
/// <param name="vec">Vertex position vector.</param>
void FileReader::setMinMax(glm::vec4& vec) {
mMax.x = std::max(mMax.x, vec.x);
mMax.y = std::max(mMax.y, vec.y);
mMax.z = std::max(mMax.z, vec.z);
mMin.x = std::min(mMin.x, vec.x);
mMin.y = std::min(mMin.y, vec.y);
mMin.z = std::min(mMin.z, vec.z);
}