Files
RayTracer/src/util/ModelLoader.cpp
T

43 lines
1.3 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-25 19:14:22 +01:00
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(std::string(filename), aiProcess_Triangulate);
2025-01-26 20:59:51 +01:00
std::cout << importer.GetErrorString() << std::endl;
2025-01-25 19:14:22 +01:00
std::vector<PModel> result;
for (int m = 0; m < scene->mNumMeshes; ++m)
{
PModel model = std::make_unique<Model>();
const aiMesh* mesh = scene->mMeshes[m];
AABB aabb;
for (int v = 0; v < mesh->mNumVertices; ++v)
2025-01-23 16:10:28 +01:00
{
2025-01-25 19:14:22 +01:00
auto aiVert = mesh->mVertices[v];
model->positions.push_back(glm::vec3(aiVert.x, aiVert.y, aiVert.z));
2025-01-27 14:10:05 +01:00
if (mesh->HasTextureCoords(0))
{
model->texCoords.push_back(glm::vec2(mesh->mTextureCoords[0][v].x, mesh->mTextureCoords[0][v].y));
}
else
{
model->texCoords.push_back(glm::vec2(0, 0));
}
2025-01-25 19:14:22 +01:00
aabb.adjust(model->positions.back());
2025-01-23 16:10:28 +01:00
}
2025-01-25 19:14:22 +01:00
for (int i = 0; i < mesh->mNumFaces; ++i)
{
auto face = mesh->mFaces[i];
model->indices.push_back(glm::uvec3(face.mIndices[0], face.mIndices[1], face.mIndices[2]));
}
model->boundingBox = aabb;
result.push_back(std::move(model));
}
return result;
2025-01-23 16:00:29 +01:00
}