3#include <ModelLoading.h>
4#include <glm/gtc/type_ptr.hpp>
13uint32_t ModelLoader::mesh_index = 0;
14ModelPrimitives ModelLoader::LoadModelAssimp(
const char* file_name)
17 std::string_view file_name_sv(file_name);
18 Assimp::Importer importer;
19 const aiScene* scene = importer.ReadFile(file_name, USING_ASSIMP_DEFAULT_FLAGS);
22 std::cout <<
"Failed to load scene:\t" << file_name <<
'\n';
23 size_t last_slash_pos = file_name_sv.rfind(
"/") + 1;
24 std::string file_directory(file_name_sv.substr(0, last_slash_pos));
27 data.meshes.reserve(scene->mNumMeshes);
29 for(
size_t mesh = 0; mesh < scene->mNumMeshes; ++mesh)
31 std::vector<fpr::Vertex> mesh_vertices;
32 std::vector<uint32_t> mesh_indices;
33 aiMesh* current_mesh = scene->mMeshes[mesh];
35 aiMaterial* material = scene->mMaterials[current_mesh->mMaterialIndex];
36 bool using_normals = current_mesh->HasNormals();
48 for(
auto& [name, type] : ASSIMP_TEXTURE_TYPES)
51 if(material->GetTexture(type, 0, &mat_name) == aiReturn_SUCCESS)
52 printf(
"%s\t %s\n", name, mat_name.C_Str());
56 material->GetTexture(aiTextureType_DIFFUSE, 0, &albedo_path);
58 std::string full_albedo_path(file_directory);
59 full_albedo_path.append(albedo_path.C_Str());
62 for(
size_t vert = 0; vert < current_mesh->mNumVertices; ++vert)
65 vertex.position.x = current_mesh->mVertices[vert].x;
66 vertex.position.y = current_mesh->mVertices[vert].y;
67 vertex.position.z = current_mesh->mVertices[vert].z;
69 if(current_mesh->HasTextureCoords(0))
71 vertex.uv.x = current_mesh->mTextureCoords[0][vert].x;
72 vertex.uv.y = current_mesh->mTextureCoords[0][vert].y;
77 vertex.normal.x = current_mesh->mNormals[vert].x;
78 vertex.normal.y = current_mesh->mNormals[vert].y;
79 vertex.normal.z = current_mesh->mNormals[vert].z;
81 if(current_mesh->HasTangentsAndBitangents())
83 vertex.tangent.x = current_mesh->mTangents[vert].x;
84 vertex.tangent.y = current_mesh->mTangents[vert].y;
85 vertex.tangent.z = current_mesh->mTangents[vert].z;
87 mesh_vertices.push_back(vertex);
91 for(
size_t face = 0; face < current_mesh->mNumFaces; ++face)
93 aiFace* current_face = ¤t_mesh->mFaces[face];
95 mesh_indices.push_back(current_face->mIndices[0]);
96 mesh_indices.push_back(current_face->mIndices[1]);
97 mesh_indices.push_back(current_face->mIndices[2]);
100 data.indices.insert(std::end(data.indices), std::begin(mesh_indices), std::end(mesh_indices));
101 data.vertices.insert(std::end(data.vertices), std::begin(mesh_vertices), std::end(mesh_vertices));
103 vk::DeviceSize index_offset = data.indices.size() - mesh_indices.size();
104 vk::DeviceSize vertex_offset =
sizeof Vertex * data.vertices.size() -
sizeof Vertex * mesh_vertices.size();
106 data.meshes.emplace_back(
108 (vk::DeviceSize)index_offset,
109 (uint32_t)mesh_indices.size(),
111 (uint32_t)mesh_vertices.size(),