-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_loading.cpp
More file actions
276 lines (230 loc) · 11.5 KB
/
model_loading.cpp
File metadata and controls
276 lines (230 loc) · 11.5 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
#include <spdlog/spdlog.h>
#include <assimp/postprocess.h>
#include <filesystem>
#include "model_loading.hpp"
namespace model_loading {
glm::vec3 assimp_to_glm_3d_vector(aiVector3D assimp_vector) {
return {assimp_vector.x, assimp_vector.y, assimp_vector.z};
}
std::vector<draw_info::IndexedVertexPositions> parse_model_into_ivps(const std::string &model_path) {
Assimp::Importer importer;
const aiScene *scene =
importer.ReadFile(model_path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
std::cerr << "Error: Assimp - " << importer.GetErrorString() << std::endl;
}
RecIvpCollector ric;
ric.rec_process_nodes(scene->mRootNode, scene);
return ric.ivps;
}
std::vector<draw_info::IVPTextured> parse_model_into_ivpts(const std::string &model_path, bool flip_uvs) {
Assimp::Importer importer;
unsigned int flags = aiProcess_Triangulate | aiProcess_CalcTangentSpace;
if (flip_uvs) {
flags |= aiProcess_FlipUVs;
}
const aiScene *scene = importer.ReadFile(model_path, flags);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
std::cerr << "Error: Assimp - " << importer.GetErrorString() << std::endl;
}
RecIvptCollector ric(model_path);
ric.rec_process_nodes(scene->mRootNode, scene);
return ric.ivpts;
}
std::vector<draw_info::IVPNTextured> parse_model_into_ivpnts(const std::string &model_path, bool flip_uvs) {
Assimp::Importer importer;
unsigned int flags = aiProcess_Triangulate | aiProcess_CalcTangentSpace;
if (flip_uvs) {
flags |= aiProcess_FlipUVs;
}
const aiScene *scene = importer.ReadFile(model_path, flags);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
std::cerr << "Error: Assimp - " << importer.GetErrorString() << std::endl;
}
RecIvpntCollector ric(model_path);
ric.rec_process_nodes(scene->mRootNode, scene);
return ric.ivpnts;
}
/// @brief Extracts the directory path of a given asset path using std::filesystem.
/// @param asset_path The full path of the asset.
/// @return The directory part of the asset path.
std::string get_directory_of_asset(const std::string &asset_path) {
std::filesystem::path path(asset_path);
// Convert parent_path to string and append the preferred separator
return path.parent_path().string() + std::string(1, std::filesystem::path::preferred_separator);
}
void RecIvpCollector::rec_process_nodes(aiNode *node, const aiScene *scene) {
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
unsigned int mesh_index = node->mMeshes[i];
aiMesh *mesh = scene->mMeshes[mesh_index];
this->ivps.push_back(process_mesh_ivps(mesh, scene));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
this->rec_process_nodes(node->mChildren[i], scene);
}
}
void RecIvptCollector::rec_process_nodes(aiNode *node, const aiScene *scene) {
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
unsigned int mesh_index = node->mMeshes[i];
aiMesh *mesh = scene->mMeshes[mesh_index];
this->ivpts.push_back(process_mesh_ivpts(mesh, scene, directory_to_model));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
this->rec_process_nodes(node->mChildren[i], scene);
}
}
RecIvptCollector::RecIvptCollector(const std::string &model_path) {
directory_to_model = get_directory_of_asset(model_path);
}
RecIvpntCollector::RecIvpntCollector(const std::string &model_path) {
directory_to_model = get_directory_of_asset(model_path);
std::cout << "model path: " << model_path << " directory to model: " << directory_to_model << std::endl;
}
void RecIvpntCollector::rec_process_nodes(aiNode *node, const aiScene *scene) {
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
unsigned int mesh_index = node->mMeshes[i];
aiMesh *mesh = scene->mMeshes[mesh_index];
this->ivpnts.push_back(process_mesh_ivpnts(mesh, scene, directory_to_model));
}
for (unsigned int i = 0; i < node->mNumChildren; i++) {
this->rec_process_nodes(node->mChildren[i], scene);
}
}
draw_info::IndexedVertexPositions process_mesh_ivps(aiMesh *mesh, const aiScene *scene) {
std::vector<glm::vec3> vertices = process_mesh_vertex_positions(mesh);
std::vector<unsigned int> indices = process_mesh_indices(mesh);
std::string name = mesh->mName.C_Str();
draw_info::IndexedVertexPositions ivp{indices, vertices};
ivp.name = name;
return ivp;
};
draw_info::IVPTextured process_mesh_ivpts(aiMesh *mesh, const aiScene *scene, const std::string &directory_to_model) {
std::vector<glm::vec3> vertices = process_mesh_vertex_positions(mesh);
std::vector<unsigned int> indices = process_mesh_indices(mesh);
std::vector<glm::vec2> texture_coordinates = process_mesh_texture_coordinates(mesh);
std::vector<TextureInfo> texture_data = process_mesh_materials(mesh, scene, directory_to_model);
std::string main_texture = texture_data[0].path;
std::filesystem::path fs_path = main_texture;
// Convert to the preferred format for the operating system
std::string texture_native_path = fs_path.make_preferred().string();
// TODO: this needs to be done in more places now that other things accept names
std::string name = mesh->mName.C_Str();
draw_info::IVPTextured ivpt{indices, vertices, texture_coordinates, texture_native_path};
ivpt.name = name;
return ivpt;
};
draw_info::IVPNTextured process_mesh_ivpnts(aiMesh *mesh, const aiScene *scene, const std::string &directory_to_model) {
std::vector<glm::vec3> vertices = process_mesh_vertex_positions(mesh);
std::vector<glm::vec3> normals = process_mesh_normals(mesh);
std::vector<unsigned int> indices = process_mesh_indices(mesh);
std::vector<glm::vec2> texture_coordinates = process_mesh_texture_coordinates(mesh);
std::vector<TextureInfo> texture_data = process_mesh_materials(mesh, scene, directory_to_model);
std::string main_texture = texture_data[0].path;
std::filesystem::path fs_path = main_texture;
// Convert to the preferred format for the operating system
std::string texture_native_path = fs_path.make_preferred().string();
draw_info::IVPNTextured ivpnt{indices, vertices, normals, texture_coordinates, texture_native_path};
// ivpnt.name = mesh->mName.C_Str();
return ivpnt;
};
std::vector<glm::vec3> process_mesh_vertex_positions(aiMesh *mesh) {
std::vector<glm::vec3> vertices;
/*spdlog::get(Systems::asset_loading)->info("This mesh has {} vertex_positions", mesh->mNumVertices);*/
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
glm::vec3 vertex = {assimp_to_glm_3d_vector(mesh->mVertices[i])};
vertices.push_back(vertex);
}
return vertices;
}
std::vector<TextureInfo> process_mesh_materials(aiMesh *mesh, const aiScene *scene,
const std::string &directory_to_asset_being_loaded) {
std::vector<TextureInfo> textures;
std::cout << "processing mesh materials: " << directory_to_asset_being_loaded << std::endl;
bool mesh_has_materials = mesh->mMaterialIndex >= 0;
if (mesh_has_materials) {
aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
std::vector<TextureInfo> diffuse_maps_texture_info = get_texture_info_for_material(
material, aiTextureType_DIFFUSE, TextureType::DIFFUSE, directory_to_asset_being_loaded);
textures.insert(textures.end(), diffuse_maps_texture_info.begin(), diffuse_maps_texture_info.end());
for (auto &texture : textures) {
std::cout << texture.path << std::endl;
}
if (diffuse_maps_texture_info.size() == 0) {
/*spdlog::get(Systems::asset_loading)->warn("This material doesn't have any diffuse maps");*/
}
std::vector<TextureInfo> specular_maps_texture_info = get_texture_info_for_material(
material, aiTextureType_SPECULAR, TextureType::SPECULAR, directory_to_asset_being_loaded);
textures.insert(textures.end(), specular_maps_texture_info.begin(), specular_maps_texture_info.end());
if (specular_maps_texture_info.size() == 0) {
/*spdlog::get(Systems::asset_loading)->info("This material doesn't have any specular maps");*/
}
}
return textures;
}
std::vector<TextureInfo> get_texture_info_for_material(aiMaterial *material, aiTextureType type,
TextureType texture_type,
const std::string &directory_to_asset_being_loaded) {
std::vector<TextureInfo> textures;
// loop through the textures of the specified type
for (unsigned int i = 0; i < material->GetTextureCount(type); i++) {
aiString texture_path;
material->GetTexture(type, i, &texture_path);
// construct the asset path
std::string asset_path = directory_to_asset_being_loaded + std::string(texture_path.C_Str());
// create the texture info and add it to the vector
TextureInfo texture{texture_type, asset_path.c_str()};
textures.push_back(texture);
}
// if no diffuse textures are found, add the missing texture
if (textures.empty() && texture_type == TextureType::DIFFUSE) {
std::cout << "no diffuse textures found, using missing texture" << std::endl;
std::string missing_texture_path = "assets/images/missing_texture.png";
TextureInfo missing_texture{texture_type, missing_texture_path.c_str()};
textures.push_back(missing_texture);
}
return textures;
}
std::vector<glm::vec2> process_mesh_texture_coordinates(aiMesh *mesh) {
std::vector<glm::vec2> texture_coordinates;
// there is a better way to do this at some point
bool mesh_has_texture_coordinates = mesh->mTextureCoords[0] != nullptr;
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
glm::vec2 texture_coordinate;
if (mesh_has_texture_coordinates) {
texture_coordinate = glm::vec2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y);
} else {
/*spdlog::get(Systems::asset_loading)->warn("This mesh doesn't have texture coordinates!");*/
texture_coordinate = glm::vec2(0.0f, 0.0f);
}
texture_coordinates.push_back(texture_coordinate);
}
return texture_coordinates;
}
std::vector<glm::vec3> process_mesh_normals(aiMesh *mesh) {
std::vector<glm::vec3> normals;
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
if (mesh->HasNormals()) {
normals.push_back(assimp_to_glm_3d_vector(mesh->mNormals[i]));
} else {
/*spdlog::get(Systems::asset_loading)->warn("This mesh doesn't have texture coordinates!");*/
normals.push_back(glm::vec3(0, 0, 0));
}
}
return normals;
}
std::vector<unsigned int> process_mesh_indices(aiMesh *mesh) {
std::vector<unsigned int> indices;
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
if (face.mNumIndices < 3) {
std::cout << "there is a face with less than 3 sides, so you have degenerate geom in your export"
<< std::endl;
}
assert(face.mNumIndices == 3); // if this is false we are not working with triangles
for (unsigned int j = 0; j < face.mNumIndices; j++) {
indices.push_back(face.mIndices[j]);
}
}
return indices;
}
} // namespace model_loading