-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathAssImpModelLoader.cpp
More file actions
362 lines (321 loc) · 12.3 KB
/
AssImpModelLoader.cpp
File metadata and controls
362 lines (321 loc) · 12.3 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
#include "AssImpModelLoader.h"
using namespace std;
bool AssImpModelProgressHandler::Update(float percentage)
{
emit fileReadProcessed(percentage);
return true;
}
/* Functions */
// Constructor, expects a filepath to a 3D model.
AssImpModelLoader::AssImpModelLoader(QOpenGLShaderProgram* prog) : QObject(), _prog(prog)
{
initializeOpenGLFunctions();
_loadingCancelled = false;
_progHandler = new AssImpModelProgressHandler();
_importer.SetProgressHandler(_progHandler);
connect(_progHandler, SIGNAL(fileReadProcessed(float)), this, SLOT(processFileReadProgress(float)));
}
AssImpModelLoader::~AssImpModelLoader()
{
disconnect(_progHandler, SIGNAL(fileReadProcessed(float)), this, SLOT(processFileReadProgress(float)));
//delete _progHandler; // causes crash
_progHandler = nullptr;
}
void AssImpModelLoader::processFileReadProgress(float percentage)
{
emit fileReadProcessed(percentage);
}
void AssImpModelLoader::cancelLoading()
{
_loadingCancelled = true;
}
vector<AssImpMesh*> AssImpModelLoader::getMeshes() const
{
return _meshes;
}
/* Functions */
// Loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector.
void AssImpModelLoader::loadModel(string path)
{
_loadingCancelled = false;
_path = std::string(path);
_meshes.clear();
_loadedTextures.clear();
// Read file via ASSIMP
_importer.SetPropertyFloat("PP_GSN_MAX_SMOOTHING_ANGLE", 15);
const aiScene* scene = _importer.ReadFile(path, aiProcess_CalcTangentSpace |
aiProcess_GenSmoothNormals |
aiProcess_JoinIdenticalVertices |
aiProcess_Triangulate |
aiProcess_GenUVCoords |
aiProcess_SortByPType);
// Check for errors
if (!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) // if is Not Zero
{
_errorMessage = _importer.GetErrorString();
cout << "ERROR::ASSIMP:: " << _importer.GetErrorString() << endl;
return;
}
// Retrieve the directory path of the filepath
this->directory = path.substr(0, path.find_last_of('/'));
// Process ASSIMP's root node recursively
this->processNode(0, scene->mRootNode, scene);
}
// Processes a node in a recursive fashion. Processes each individual mesh located at the node and repeats this process on its children nodes (if any).
void AssImpModelLoader::processNode(int nodeNum, aiNode* node, const aiScene* scene)
{
if (_loadingCancelled)
{
emit loadingCancelled();
return;
}
// Process each mesh located at the current node
for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
// The node object only contains indices to index the actual objects in the scene.
// The scene contains all the data, node is just to keep stuff organized (like relations between nodes).
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
this->_meshes.push_back(this->processMesh(mesh, scene));
}
// After we've processed all of the meshes (if any) we then recursively process each of the children nodes
for (unsigned int i = 0; i < node->mNumChildren; i++)
{
if (_loadingCancelled)
{
emit loadingCancelled();
return;
}
this->processNode(++nodeNum, node->mChildren[i], scene);
emit nodeProcessed(nodeNum, node->mNumChildren);
}
}
AssImpMesh* AssImpModelLoader::processMesh(aiMesh* mesh, const aiScene* scene)
{
// Data to fill
vector<Vertex> vertices;
vector<unsigned int> indices;
vector<Texture> textures;
// Walk through each of the mesh's vertices
int step = 0;
unsigned int nbVertices = mesh->mNumVertices;
for (unsigned int i = 0; i < nbVertices; i++)
{
step++;
Vertex vertex;
glm::vec3 vector; // We declare a placeholder vector since assimp uses its own vector class that doesn't directly convert to glm's vec3 class so we transfer the data to this placeholder glm::vec3 first.
// Positions
vector.x = mesh->mVertices[i].x;
vector.y = mesh->mVertices[i].y;
vector.z = mesh->mVertices[i].z;
vertex.Position = vector;
// Normals
vector.x = mesh->mNormals[i].x;
vector.y = mesh->mNormals[i].y;
vector.z = mesh->mNormals[i].z;
vertex.Normal = vector;
// Texture Coordinates
if (mesh->mTextureCoords[0]) // Does the mesh contain texture coordinates?
{
glm::vec2 vec;
// A vertex can contain up to 8 different texture coordinates. We thus make the assumption that we won't
// use models where a vertex can have multiple texture coordinates so we always take the first set (0).
vec.x = mesh->mTextureCoords[0][i].x;
vec.y = mesh->mTextureCoords[0][i].y;
vertex.TexCoords = vec;
// tangent
if (mesh->mTangents)
{
vector.x = mesh->mTangents[i].x;
vector.y = mesh->mTangents[i].y;
vector.z = mesh->mTangents[i].z;
vertex.Tangent = vector;
}
// bitangent
if (mesh->mBitangents)
{
vector.x = mesh->mBitangents[i].x;
vector.y = mesh->mBitangents[i].y;
vector.z = mesh->mBitangents[i].z;
vertex.Bitangent = vector;
}
}
else
{
if (step == 1)
vertex.TexCoords = glm::vec2(0.0f, 0.0f);
else if (step == 2)
vertex.TexCoords = glm::vec2(1.0f, 0.0f);
else
{
vertex.TexCoords = glm::vec2(0.5f, 1.0f);
step = 0;
}
}
vertices.push_back(vertex);
if (i % 100000 == 0)
{
emit verticesProcessed(static_cast<float>(i) / nbVertices * 100.0f);
}
}
// Now wak through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices.
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
{
aiFace face = mesh->mFaces[i];
// Retrieve all indices of the face and store them in the indices vector
for (unsigned int j = 0; j < face.mNumIndices; j++)
{
indices.push_back(face.mIndices[j]);
}
}
// Process materials
GLMaterial mat = GLMaterial::DEFAULT_MAT();
if (mesh->mMaterialIndex != 0)
{
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
// We assume a convention for sampler names in the shaders. Each diffuse texture should be named
// as 'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER.
// Same applies to other texture as the following list summarizes:
// Diffuse: texture_diffuseN
// Specular: texture_specularN
// Normal: texture_normalN
// ADS Maps
// 1. diffuse maps
vector<Texture> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end());
// 2. specular maps
vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
// 3. emissive maps
vector<Texture> emissiveMaps = loadMaterialTextures(material, aiTextureType_EMISSIVE, "texture_emissive");
textures.insert(textures.end(), emissiveMaps.begin(), emissiveMaps.end());
// 4. normal maps
std::vector<Texture> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
textures.insert(textures.end(), normalMaps.begin(), normalMaps.end());
// 5. height maps
std::vector<Texture> heightMaps = loadMaterialTextures(material, aiTextureType_DISPLACEMENT, "texture_height");
textures.insert(textures.end(), heightMaps.begin(), heightMaps.end());
// 6. opacity maps
std::vector<Texture> opacityMaps = loadMaterialTextures(material, aiTextureType_OPACITY, "texture_opacity");
textures.insert(textures.end(), opacityMaps.begin(), opacityMaps.end());
// PBR Maps
// 1. albedo maps
vector<Texture> albedoPBRMaps = loadMaterialTextures(material, aiTextureType_BASE_COLOR, "albedoMap");
textures.insert(textures.end(), albedoPBRMaps.begin(), albedoPBRMaps.end());
// 2. specular maps
vector<Texture> metalicPBRMaps = loadMaterialTextures(material, aiTextureType_METALNESS, "metallicMap");
textures.insert(textures.end(), metalicPBRMaps.begin(), metalicPBRMaps.end());
// 3. roughness maps
vector<Texture> roughnessPBRMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE_ROUGHNESS, "roughnessMap");
textures.insert(textures.end(), roughnessPBRMaps.begin(), roughnessPBRMaps.end());
// 4. emissive maps
vector<Texture> emissivePBRMaps = loadMaterialTextures(material, aiTextureType_EMISSION_COLOR, "emissiveMap");
textures.insert(textures.end(), emissivePBRMaps.begin(), emissivePBRMaps.end());
// 5. normal maps
std::vector<Texture> normalPBRMaps = loadMaterialTextures(material, aiTextureType_NORMAL_CAMERA, "normalMap");
textures.insert(textures.end(), normalPBRMaps.begin(), normalPBRMaps.end());
// 6. AO maps
std::vector<Texture> aoPBRMaps = loadMaterialTextures(material, aiTextureType_AMBIENT_OCCLUSION, "aoMap");
textures.insert(textures.end(), aoPBRMaps.begin(), aoPBRMaps.end());
aiColor3D color(0.f, 0.f, 0.f);
float opacity = 1.0f;
material->Get(AI_MATKEY_OPACITY, opacity);
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_AMBIENT, color))
{
mat.setAmbient(QVector3D(color.r, color.g, color.b));
}
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_DIFFUSE, color))
{
mat.setDiffuse(QVector3D(color.r, color.g, color.b));
mat.setAlbedoColor(QVector3D(color.r, color.g, color.b));
}
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_SPECULAR, color))
{
mat.setSpecular(QVector3D(color.r, color.g, color.b));
bool grayScale = (color.r == color.g && color.g == color.b && color.r == color.b);
float intensity = 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b;
if (grayScale)
mat.setMetalness(intensity > 0.04f ? 0.04f : intensity); // limit to 4% for dielectrics
else
mat.setMetalness(intensity);
mat.setRoughness(1.0f - intensity);
}
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_EMISSIVE, color))
{
mat.setEmissive(QVector3D(color.r, color.g, color.b));
}
if (AI_SUCCESS == material->Get(AI_MATKEY_OPACITY, opacity))
{
mat.setOpacity(opacity);
}
}
// Return a mesh object created from the extracted mesh data
return new AssImpMesh(_prog, QFileInfo(QString(_path.data())).baseName(), vertices, indices, textures, mat);
}
// Checks all material textures of a given type and loads the textures if they're not loaded yet.
// The required info is returned as a Texture struct.
vector<Texture> AssImpModelLoader::loadMaterialTextures(aiMaterial* mat, aiTextureType type, string typeName)
{
vector<Texture> textures;
for (unsigned int i = 0; i < mat->GetTextureCount(type); i++)
{
aiString str;
mat->GetTexture(type, i, &str);
// Check if texture was loaded before and if so, continue to next iteration: skip loading a new texture
GLboolean skip = false;
for (unsigned int j = 0; j < _loadedTextures.size(); j++)
{
if (_loadedTextures[j].path == str)
{
textures.push_back(_loadedTextures[j]);
skip = true; // A texture with the same filepath has already been loaded, continue to next one. (optimization)
break;
}
}
if (!skip)
{
// If texture hasn't been loaded already, load it
Texture texture;
texture.id = textureFromFile(str.C_Str(), this->directory);
texture.type = typeName;
texture.path = str;
textures.push_back(texture);
this->_loadedTextures.push_back(texture); // Store it as texture loaded for entire model, to ensure we won't unnecesery load duplicate textures.
}
}
return textures;
}
unsigned int AssImpModelLoader::textureFromFile(const char* path, string directory)
{
//Generate texture ID and load texture data
string filename = string(path);
filename = directory + '/' + filename;
unsigned int textureID;
glGenTextures(1, &textureID);
QImage texImage;
if (!texImage.load(QString(filename.c_str())))
{ // Load first image from file
qWarning("Could not read image file, using single-color instead.");
QImage dummy(128, 128, static_cast<QImage::Format>(5));
dummy.fill(Qt::white);
texImage = dummy;
}
else
{
texImage = QGLWidget::convertToGLFormat(texImage); // flipped 32bit RGBA
}
// Assign texture to ID
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texImage.width(), texImage.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage.bits());
glGenerateMipmap(GL_TEXTURE_2D);
// Parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
return textureID;
}
QString AssImpModelLoader::getErrorMessage() const
{
return _errorMessage;
}