YoggieEngine/BarinkEngine/graphics/ModelImporter.cpp

94 lines
2.5 KiB
C++
Raw Normal View History

#include "AssetManager/ModelImporter.h"
2022-07-09 20:21:56 +00:00
BarinkEngine::SceneObject* BarinkEngine::ModelImporter::Import(const std::string path)
{
2022-07-09 20:21:56 +00:00
SceneObject* root = new SceneObject(std::string(path), nullptr);
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode;
std::vector<BarinkEngine::Mesh> meshes = processNode(currentNode, scene);
return root;
}
2022-07-09 20:21:56 +00:00
std::vector<BarinkEngine::Mesh> BarinkEngine::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;
}
2022-07-09 20:21:56 +00:00
BarinkEngine::Mesh BarinkEngine::ModelImporter::processMesh(aiMesh* mesh, const aiScene* scene) {
std::vector<unsigned int> indices;
std::vector<BarinkEngine::Vertex> vertices;
ProcessVertices(mesh, vertices);
ProcessIndices(mesh, indices);
BarinkEngine::Mesh result;
result.vertices = vertices;
result.elements = indices;
return result;
}
void ProcessVertices(aiMesh* mesh, std::vector<BarinkEngine::Vertex>& out_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;
}
out_vertices.push_back(v);
}
}
void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& out_indices) {
// 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++) {
out_indices.push_back(face.mIndices[j]);
}
}
}