2022-06-04 23:44:54 +00:00
|
|
|
#include "AssetManager/ModelImporter.h"
|
|
|
|
|
|
|
|
|
2022-07-09 20:21:56 +00:00
|
|
|
BarinkEngine::SceneObject* BarinkEngine::ModelImporter::Import(const std::string path)
|
2022-06-04 23:44:54 +00:00
|
|
|
{
|
2022-07-09 20:21:56 +00:00
|
|
|
|
|
|
|
SceneObject* root = new SceneObject(std::string(path), nullptr);
|
2022-07-09 19:22:50 +00:00
|
|
|
|
2022-07-08 19:35:14 +00:00
|
|
|
Assimp::Importer importer;
|
|
|
|
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs);
|
2022-06-04 23:44:54 +00:00
|
|
|
|
2022-07-08 19:35:14 +00:00
|
|
|
aiNode* currentNode = scene->mRootNode;
|
2022-06-04 23:44:54 +00:00
|
|
|
|
2022-07-09 19:22:50 +00:00
|
|
|
std::vector<BarinkEngine::Mesh> meshes = processNode(currentNode, scene);
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
2022-06-04 23:44:54 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 20:21:56 +00:00
|
|
|
std::vector<BarinkEngine::Mesh> BarinkEngine::ModelImporter::processNode(aiNode* node, const aiScene* scene)
|
2022-06-04 23:44:54 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
std::vector<BarinkEngine::Mesh> meshes;
|
2022-07-08 19:35:14 +00:00
|
|
|
|
2022-06-04 23:44:54 +00:00
|
|
|
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) {
|
2022-06-04 23:44:54 +00:00
|
|
|
std::vector<unsigned int> indices;
|
|
|
|
std::vector<BarinkEngine::Vertex> vertices;
|
|
|
|
|
2022-07-08 19:35:14 +00:00
|
|
|
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) {
|
2022-06-04 23:44:54 +00:00
|
|
|
// 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;
|
2022-07-08 19:35:14 +00:00
|
|
|
|
2022-06-04 23:44:54 +00:00
|
|
|
v.vertices = vector;
|
2022-07-08 19:35:14 +00:00
|
|
|
|
2022-06-04 23:44:54 +00:00
|
|
|
if (mesh->mTextureCoords[0]) {
|
|
|
|
|
|
|
|
glm::vec2 texCoord;
|
|
|
|
|
|
|
|
texCoord.x = mesh->mTextureCoords[0][i].x;
|
|
|
|
texCoord.y = mesh->mTextureCoords[0][i].y;
|
|
|
|
|
|
|
|
v.uv = texCoord;
|
|
|
|
|
|
|
|
}
|
2022-07-08 19:35:14 +00:00
|
|
|
|
|
|
|
out_vertices.push_back(v);
|
2022-06-04 23:44:54 +00:00
|
|
|
}
|
2022-07-08 19:35:14 +00:00
|
|
|
}
|
2022-06-04 23:44:54 +00:00
|
|
|
|
2022-07-08 19:35:14 +00:00
|
|
|
void ProcessIndices(aiMesh* mesh, std::vector<unsigned int>& out_indices) {
|
2022-06-04 23:44:54 +00:00
|
|
|
// 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++) {
|
2022-07-08 19:35:14 +00:00
|
|
|
out_indices.push_back(face.mIndices[j]);
|
2022-06-04 23:44:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-04 09:10:26 +00:00
|
|
|
}
|