started fixing stuff with model imports
This commit is contained in:
@@ -12,11 +12,13 @@
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include "glm/ext/matrix_transform.hpp"
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<Model>> ModelLoader::models;
|
||||
|
||||
Mesh process_ai_mesh(aiMesh* aiMesh);
|
||||
void process_ai_node(aiNode* node, const aiScene* scene, Model* model);
|
||||
|
||||
Mesh process_ai_mesh(const aiMesh* aiMesh);
|
||||
Material process_ai_material(aiMaterial* mat);
|
||||
|
||||
std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
|
||||
@@ -25,9 +27,8 @@ std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
|
||||
|
||||
const aiScene* scene = importer.ReadFile(_path.data(),
|
||||
aiProcess_Triangulate |
|
||||
aiProcess_GenSmoothNormals |
|
||||
aiProcess_FlipUVs |
|
||||
aiProcess_CalcTangentSpace);
|
||||
aiProcess_GenNormals |
|
||||
aiProcess_FlipUVs);
|
||||
|
||||
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
|
||||
{
|
||||
@@ -37,64 +38,65 @@ std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
|
||||
|
||||
auto model = std::make_shared<Model>();
|
||||
|
||||
model->materials.resize(scene->mNumMaterials);
|
||||
for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
|
||||
aiMaterial* mat = scene->mMaterials[i];
|
||||
model->materials[i] = std::make_shared<Material>(process_ai_material(mat));
|
||||
}
|
||||
|
||||
model->meshes.resize(scene->mNumMeshes);
|
||||
for (unsigned int i = 0; i < scene->mNumMeshes; i++) {
|
||||
aiMesh* mesh = scene->mMeshes[i];
|
||||
model->meshes[i] = std::make_shared<Mesh>(process_ai_mesh(mesh));
|
||||
}
|
||||
process_ai_node(scene->mRootNode, scene, model.get());
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
void process_ai_node(aiNode* node, const aiScene* scene, Model* model)
|
||||
void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, Model* model)
|
||||
{
|
||||
for (unsigned int i = 0; i < node->mNumChildren; i++) {
|
||||
auto t = node->mTransformation;
|
||||
transform = transform * glm::mat4(nTransform)
|
||||
|
||||
for (unsigned int i = 0; i < node->mNumMeshes; i++)
|
||||
{
|
||||
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
||||
model->meshes.push_back(std::make_shared<Mesh>(process_ai_mesh(mesh)));
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < node->mNumChildren; i++)
|
||||
{
|
||||
process_ai_node(node->mChildren[i], scene, model);
|
||||
}
|
||||
}
|
||||
|
||||
Mesh process_ai_mesh(const aiMesh* aiMesh)
|
||||
Mesh process_ai_mesh(aiMesh* aiMesh)
|
||||
{
|
||||
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);
|
||||
auto indices = std::vector<unsigned int>();
|
||||
|
||||
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);
|
||||
positions[3 * i] = position.x;
|
||||
positions[3 * i + 1] = position.y;
|
||||
positions[3 * i + 2] = 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);
|
||||
aiVector3D norm = aiMesh->mNormals[i];
|
||||
normals[3 * i] = norm.x;
|
||||
normals[3 * i + 1] = norm.y;
|
||||
normals[3 * i + 2] = norm.z;
|
||||
}
|
||||
else
|
||||
{
|
||||
normals.push_back(0);
|
||||
normals.push_back(0);
|
||||
normals.push_back(0);
|
||||
normals[3 * i] = 0;
|
||||
normals[3 * i + 1] = 0;
|
||||
normals[3 * i + 2] = 0;
|
||||
}
|
||||
|
||||
if (aiMesh->HasTextureCoords(0))
|
||||
{
|
||||
uvs.push_back(aiMesh->mTextureCoords[0][i].x);
|
||||
uvs.push_back(aiMesh->mTextureCoords[0][i].y);
|
||||
uvs[2 * i] = aiMesh->mTextureCoords[0][i].x;
|
||||
uvs[2 * i + 1] = aiMesh->mTextureCoords[0][i].y;
|
||||
}
|
||||
else
|
||||
{
|
||||
uvs.push_back(0);
|
||||
uvs.push_back(0);
|
||||
uvs[2 * i] = 0;
|
||||
uvs[2 * i + 1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user