started on assimp importing and texture stuff

This commit is contained in:
2026-05-01 20:06:54 -05:00
parent cb1d8f753d
commit f9010eea81
20 changed files with 8673 additions and 42 deletions

94
src/ModelLoader.cpp Normal file
View File

@@ -0,0 +1,94 @@
//
// Created by lbmas on 4/29/2026.
//
#include <spdlog/spdlog.h>
#include "ModelLoader.h"
#include <assimp/Importer.hpp>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
void process_ai_node(aiNode* node, const aiScene* scene, Model* model);
Mesh process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, Model* model);
std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
{
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(_path.data(),
/*aiProcess_CalcTangentSpace |*/
aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs |
aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
spdlog::error("failed to load model {}: {}", _path, importer.GetErrorString());
return nullptr;
}
auto model = std::make_shared<Model>();
process_ai_node(scene->mRootNode, scene, model.get());
return model;
}
void process_ai_node(aiNode* node, const aiScene* scene, Model* model)
{
for (unsigned int i = 0; i < node->mNumMeshes; i++)
{
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
model->meshes.push_back(process_ai_mesh(mesh, scene, model));
}
}
Mesh process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, Model* model)
{
auto positions = std::vector<float>(aiMesh->mNumVertices * 3);
auto uvs = std::vector<float>(aiMesh->mNumVertices * 2);
auto normals = std::vector<float>(aiMesh->mNumVertices * 3);
auto indices = std::vector<unsigned int>(aiMesh->mNumFaces * 3);
for (unsigned int i = 0; i < aiMesh->mNumVertices; i++)
{
auto position = aiMesh->mVertices[i];
positions.push_back(position.x);
positions.push_back(position.y);
positions.push_back(position.z);
if (aiMesh->HasNormals())
{
normals.push_back(aiMesh->mNormals[i].x);
normals.push_back(aiMesh->mNormals[i].y);
normals.push_back(aiMesh->mNormals[i].z);
}
else
{
normals.push_back(0);
normals.push_back(0);
normals.push_back(0);
}
if (aiMesh->HasTextureCoords(0))
{
uvs.push_back(aiMesh->mTextureCoords[0][i].x);
uvs.push_back(aiMesh->mTextureCoords[0][i].y);
}
else
{
uvs.push_back(0);
uvs.push_back(0);
}
}
for(unsigned int i = 0; i < aiMesh->mNumFaces; i++)
{
const aiFace face = aiMesh->mFaces[i];
for(unsigned int j = 0; j < face.mNumIndices; j++)
indices.push_back(face.mIndices[j]);
}
return {positions, uvs, normals, indices};
}