Files
RayTracer/src/util/ModelLoader.cpp
T

37 lines
1.2 KiB
C++
Raw Normal View History

2025-01-23 15:55:24 +01:00
#include "ModelLoader.h"
2025-01-23 16:10:28 +01:00
#include <assimp/Importer.hpp>
#include <assimp/config.h>
#include <assimp/material.h>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
2025-01-23 15:55:24 +01:00
2025-01-23 16:10:28 +01:00
std::vector<PModel> ModelLoader::loadModel(std::string_view filename)
2025-01-23 16:00:29 +01:00
{
2025-01-23 16:10:28 +01:00
Assimp::Importer importer;
2025-01-23 17:19:53 +01:00
const aiScene* scene = importer.ReadFile(std::string(filename), aiProcess_Triangulate);
std::vector<PModel> result;
2025-01-23 16:10:28 +01:00
for (int m = 0; m < scene->mNumMeshes; ++m)
{
2025-01-23 17:19:53 +01:00
PModel model = std::make_unique<Model>();
2025-01-23 16:10:28 +01:00
const aiMesh* mesh = scene->mMeshes[m];
2025-01-23 17:19:53 +01:00
AABB aabb;
2025-01-23 16:10:28 +01:00
for (int v = 0; v < mesh->mNumVertices; ++v)
{
auto aiVert = mesh->mVertices[v];
2025-01-23 17:19:53 +01:00
model->positions.push_back(glm::vec3(aiVert.x, aiVert.y, aiVert.z));
aabb.adjust(model->positions.back());
2025-01-23 16:10:28 +01:00
}
for (int i = 0; i < mesh->mNumFaces; ++i)
{
auto face = mesh->mFaces[i];
2025-01-23 17:19:53 +01:00
model->indices.push_back(face.mIndices[0]);
model->indices.push_back(face.mIndices[1]);
model->indices.push_back(face.mIndices[2]);
2025-01-23 16:10:28 +01:00
}
2025-01-23 17:19:53 +01:00
model->boundingBox = aabb;
result.push_back(std::move(model));
2025-01-23 16:10:28 +01:00
}
2025-01-23 17:19:53 +01:00
return result;
2025-01-23 16:00:29 +01:00
}