YoggieEngine/BarinkEngine/graphics/ModelImporter.cpp

133 lines
3.4 KiB
C++
Raw Normal View History

#include "AssetManager/ModelImporter.h"
void ModelImporter::ImportFBX(std::string path)
{
//spdlog::warn("ImportFBX not implemented!");
}
void ModelImporter::ImportBlend(std::string path)
{
//spdlog::warn("ImportBlend not implemented!");
}
void ModelImporter::ImportGLTF(std::string path)
{
//spdlog::warn("ImportGLTF not implemented!");
}
void ModelImporter::ImportOBJ(std::string path)
{
//spdlog::warn("ImportOBJ not implemented!");
}
void ModelImporter::Import(std::string path)
{
//spdlog::warn("Import not implemented!");
}
std::vector<BarinkEngine::Mesh> ModelImporter::Test() {
/*
spdlog::info("====== Tiny GLTF ======");
tinygltf::Model loadedModel;
tinygltf::TinyGLTF loader;
std::string error;
std::string warn;
bool ret = loader.LoadASCIIFromFile(&loadedModel, &error, &warn, "./Build/SandboxApplication/Debug/sponza.gltf");
if (!warn.empty())
spdlog::warn("TinyGLTF Warning: {}", warn);
if (!error.empty())
spdlog::error("TinyGLTF Error: {}", error);
if (!ret) {
spdlog::error("TinyGLTF Error: Failed to parse glTF");
exit(-1);
}
spdlog::info("Meshes in model: {}", loadedModel.meshes.size());
spdlog::info("Primitives in mesh: {}", loadedModel.meshes[0].primitives.size());
*/
//spdlog::info("======= Assimp ======");
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("build/SandboxApplication/Debug/Models/Cube.obj", aiProcess_Triangulate | aiProcess_FlipUVs);
aiNode* currentNode = scene->mRootNode;
return processNode(currentNode, scene);
}
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;
}