2022-06-04 23:44:54 +00:00
|
|
|
#include "AssetManager/ModelImporter.h"
|
|
|
|
|
|
|
|
|
2022-06-19 18:01:31 +00:00
|
|
|
std::vector<BarinkEngine::Mesh> ModelImporter::Import(std::string path)
|
2022-06-04 23:44:54 +00:00
|
|
|
{
|
|
|
|
Assimp::Importer importer;
|
2022-06-19 18:01:31 +00:00
|
|
|
const aiScene* scene = importer.ReadFile(path.c_str(), aiProcess_Triangulate | aiProcess_FlipUVs);
|
2022-06-04 23:44:54 +00:00
|
|
|
aiNode* currentNode = scene->mRootNode;
|
|
|
|
|
2022-06-19 18:01:31 +00:00
|
|
|
return processNode(currentNode, scene);
|
2022-06-04 23:44:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<BarinkEngine::Mesh> ModelImporter::processNode(aiNode* node, const aiScene* scene) {
|
|
|
|
std::vector<BarinkEngine::Mesh> meshes;
|
|
|
|
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
|
|
|
|
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
|
|
|
meshes.push_back(processMesh(mesh, scene));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < node->mNumChildren; i++) {
|
|
|
|
auto m2 = processNode(node->mChildren[i], scene);
|
|
|
|
|
|
|
|
for(auto m : m2) {
|
|
|
|
meshes.push_back(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return meshes;
|
|
|
|
}
|
|
|
|
|
|
|
|
BarinkEngine::Mesh ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) {
|
|
|
|
std::vector<unsigned int> indices;
|
|
|
|
std::vector<BarinkEngine::Vertex> vertices;
|
|
|
|
|
|
|
|
// Process vertices
|
|
|
|
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
|
|
|
|
BarinkEngine::Vertex v{};
|
|
|
|
glm::vec3 vector;
|
|
|
|
vector.x = mesh->mVertices[i].x;
|
|
|
|
vector.y = mesh->mVertices[i].y;
|
|
|
|
vector.z = mesh->mVertices[i].z;
|
|
|
|
|
|
|
|
v.vertices = vector;
|
|
|
|
|
|
|
|
if (mesh->mTextureCoords[0]) {
|
|
|
|
|
|
|
|
glm::vec2 texCoord;
|
|
|
|
|
|
|
|
texCoord.x = mesh->mTextureCoords[0][i].x;
|
|
|
|
texCoord.y = mesh->mTextureCoords[0][i].y;
|
|
|
|
|
|
|
|
v.uv = texCoord;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
vertices.push_back(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process Indices
|
|
|
|
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
|
|
|
|
aiFace face = mesh->mFaces[i];
|
|
|
|
if (face.mNumIndices < 3)
|
|
|
|
continue;
|
|
|
|
for (unsigned int j = 0; j < face.mNumIndices; j++) {
|
|
|
|
indices.push_back(face.mIndices[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BarinkEngine::Mesh result;
|
|
|
|
|
|
|
|
|
|
|
|
result.vertices = vertices;
|
|
|
|
result.elements = indices;
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
2022-05-04 09:10:26 +00:00
|
|
|
}
|