-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathModel.cpp
More file actions
248 lines (197 loc) · 8.2 KB
/
Model.cpp
File metadata and controls
248 lines (197 loc) · 8.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
#include "Model.h"
#include "Core/RenderSystem.h"
#include <assimp/scene.h>
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include "ResourceManager.h"
/***********************************************************************************/
Model::Model(const std::string_view Path, const std::string_view Name, const bool flipWindingOrder, const bool loadMaterial) : m_name(Name), m_path(Path) {
if (!loadModel(Path, flipWindingOrder, loadMaterial)) {
std::cerr << "Failed to load: " << Name << '\n';
}
}
/***********************************************************************************/
Model::Model(const std::string_view Name, const std::vector<Vertex>& vertices, const std::vector<GLuint>& indices, const PBRMaterialPtr& material) noexcept : m_name(Name) {
m_meshes.emplace_back(vertices, indices, material);
}
/***********************************************************************************/
Model::Model(const std::string_view Name, const Mesh& mesh) noexcept : m_name(Name) {
m_meshes.push_back(mesh);
}
/***********************************************************************************/
void Model::AttachMesh(const Mesh mesh) noexcept {
m_meshes.push_back(mesh);
}
/***********************************************************************************/
void Model::Scale(const glm::vec3& scale) {
m_scale = scale;
m_aabb.scale(scale, glm::vec3(0.0f));
}
/***********************************************************************************/
void Model::Rotate(const float radians, const glm::vec3& axis) {
m_radians = radians;
m_axis = axis;
}
/***********************************************************************************/
void Model::Translate(const glm::vec3& pos) {
m_position = pos;
m_aabb.translate(pos);
}
/***********************************************************************************/
glm::mat4 Model::GetModelMatrix() const {
const auto scale = glm::scale(glm::mat4(1.0f), m_scale);
const auto translate = glm::translate(glm::mat4(1.0f), m_position);
return scale * translate;
}
/***********************************************************************************/
void Model::Delete() {
for (auto& mesh : m_meshes) {
mesh.VAO.Delete();
}
}
/***********************************************************************************/
bool Model::loadModel(const std::string_view Path, const bool flipWindingOrder, const bool loadMaterial = true) {
#ifdef _DEBUG
std::cout << "Loading model: " << m_name << '\n';
#endif
std::cout << sizeof(PBRMaterial) << '\n';
Assimp::Importer importer;
const aiScene* scene = nullptr;
if (flipWindingOrder) {
scene = importer.ReadFile(Path.data(), aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_GenUVCoords |
aiProcess_SortByPType |
aiProcess_RemoveRedundantMaterials |
aiProcess_FindInvalidData |
aiProcess_FlipUVs |
aiProcess_FlipWindingOrder | // Reverse back-face culling
aiProcess_CalcTangentSpace |
aiProcess_OptimizeMeshes |
aiProcess_SplitLargeMeshes);
}
else {
scene = importer.ReadFile(Path.data(), aiProcess_Triangulate |
aiProcess_JoinIdenticalVertices |
aiProcess_GenUVCoords |
aiProcess_SortByPType |
aiProcess_RemoveRedundantMaterials |
aiProcess_FindInvalidData |
aiProcess_FlipUVs |
aiProcess_CalcTangentSpace |
aiProcess_GenSmoothNormals |
aiProcess_ImproveCacheLocality |
aiProcess_OptimizeMeshes |
aiProcess_SplitLargeMeshes);
}
// Check if scene is not null and model is done loading
if (!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
std::cerr << "Assimp Error for " << m_name << ": " << importer.GetErrorString() << '\n';
importer.FreeScene();
return false;
}
m_path = Path.substr(0, Path.find_last_of('/')); // Strip the model file name and keep the model folder.
m_path += "/";
processNode(scene->mRootNode, scene, loadMaterial);
importer.FreeScene();
return true;
}
/***********************************************************************************/
void Model::processNode(aiNode* node, const aiScene* scene, const bool loadMaterial) {
// Process all node meshes
for (auto i = 0; i < node->mNumMeshes; ++i) {
auto* mesh = scene->mMeshes[node->mMeshes[i]];
m_meshes.push_back(processMesh(mesh, scene, loadMaterial));
}
// Process their children via recursive tree traversal
for (auto i = 0; i < node->mNumChildren; ++i) {
processNode(node->mChildren[i], scene, loadMaterial);
}
}
/***********************************************************************************/
Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene, const bool loadMaterial) {
std::vector<Vertex> vertices;
glm::vec3 min(std::numeric_limits<float>::max()), max(std::numeric_limits<float>::lowest());
for (auto i = 0; i < mesh->mNumVertices; ++i) {
Vertex vertex;
if (mesh->HasPositions()) {
vertex.Position.x = mesh->mVertices[i].x;
vertex.Position.y = mesh->mVertices[i].y;
vertex.Position.z = mesh->mVertices[i].z;
// Construct bounding box
if (vertex.Position.x < min.x) min.x = vertex.Position.x;
if (vertex.Position.x > max.x) max.x = vertex.Position.x;
if (vertex.Position.y < min.y) min.y = vertex.Position.y;
if (vertex.Position.y > max.y) max.y = vertex.Position.y;
if (vertex.Position.z < min.z) min.z = vertex.Position.z;
if (vertex.Position.z > max.z) max.z = vertex.Position.z;
}
if (mesh->HasNormals()) {
vertex.Normal.x = mesh->mNormals[i].x;
vertex.Normal.y = mesh->mNormals[i].y;
vertex.Normal.z = mesh->mNormals[i].z;
}
if (mesh->HasTangentsAndBitangents()) {
vertex.Tangent.x = mesh->mTangents[i].x;
vertex.Tangent.y = mesh->mTangents[i].y;
vertex.Tangent.z = mesh->mTangents[i].z;
}
if (mesh->HasTextureCoords(0) && loadMaterial) {
// Just take the first set of texture coords (since we could have up to 8)
vertex.TexCoords.x = mesh->mTextureCoords[0][i].x;
vertex.TexCoords.y = mesh->mTextureCoords[0][i].y;
} else {
vertex.TexCoords = glm::vec2(0.0f);
}
vertices.push_back(vertex);
}
// Resize the bounding box
m_aabb.extend(min);
m_aabb.extend(max);
// Get indices from each face
std::vector<GLuint> indices;
for (auto i = 0; i < mesh->mNumFaces; ++i) {
const auto face = mesh->mFaces[i];
for (auto j = 0; j < face.mNumIndices; ++j) {
indices.emplace_back(face.mIndices[j]);
}
}
// Process material
// http://assimp.sourceforge.net/lib_html/structai_material.html
if (loadMaterial) {
if (mesh->mMaterialIndex >= 0) {
const auto* mat = scene->mMaterials[mesh->mMaterialIndex];
aiString name;
mat->Get(AI_MATKEY_NAME, name);
// Is the material cached?
const auto cachedMaterial = ResourceManager::GetInstance().GetMaterial(name.C_Str());
if (cachedMaterial.has_value()) {
return Mesh(vertices, indices, cachedMaterial.value());
}
// Get the first texture for each texture type we need
// since there could be multiple textures per type
aiString albedoPath;
mat->GetTexture(aiTextureType_DIFFUSE, 0, &albedoPath);
aiString metallicPath;
mat->GetTexture(aiTextureType_AMBIENT, 0, &metallicPath);
aiString normalPath;
mat->GetTexture(aiTextureType_HEIGHT, 0, &normalPath);
aiString roughnessPath;
mat->GetTexture(aiTextureType_SHININESS, 0, &roughnessPath);
aiString alphaMaskPath;
mat->GetTexture(aiTextureType_OPACITY, 0, &alphaMaskPath);
const auto newMaterial = ResourceManager::GetInstance().CacheMaterial(name.C_Str(),
m_path + albedoPath.C_Str(),
"",
m_path + metallicPath.C_Str(),
m_path + normalPath.C_Str(),
m_path + roughnessPath.C_Str(),
m_path + alphaMaskPath.C_Str());
++m_numMats;
return Mesh(vertices, indices, newMaterial);
}
}
return Mesh(vertices, indices);
}