revamped resource system
This commit is contained in:
@@ -10,16 +10,23 @@
|
||||
#include <assimp/postprocess.h>
|
||||
#include <assimp/scene.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<Model>> ModelLoader::models;
|
||||
|
||||
void process_ai_node(aiNode* node, const aiScene* scene, Model* model);
|
||||
Mesh process_ai_mesh(aiMesh* aiMesh, 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)
|
||||
{
|
||||
Assimp::Importer importer;
|
||||
|
||||
const aiScene* scene = importer.ReadFile(_path.data(),
|
||||
/*aiProcess_CalcTangentSpace |*/
|
||||
aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs |
|
||||
aiProcess_Triangulate |
|
||||
aiProcess_GenSmoothNormals |
|
||||
aiProcess_FlipUVs |
|
||||
aiProcess_CalcTangentSpace);
|
||||
|
||||
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
|
||||
@@ -30,21 +37,29 @@ std::shared_ptr<Model> ModelLoader::load_from_file(std::string_view _path)
|
||||
|
||||
auto model = std::make_shared<Model>();
|
||||
|
||||
process_ai_node(scene->mRootNode, scene, model.get());
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
for (unsigned int i = 0; i < node->mNumChildren; i++) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Mesh process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, Model* model)
|
||||
Mesh process_ai_mesh(const aiMesh* aiMesh)
|
||||
{
|
||||
auto positions = std::vector<float>(aiMesh->mNumVertices * 3);
|
||||
auto uvs = std::vector<float>(aiMesh->mNumVertices * 2);
|
||||
@@ -92,3 +107,48 @@ Mesh process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, Model* model)
|
||||
|
||||
return {positions, uvs, normals, indices};
|
||||
}
|
||||
|
||||
Material process_ai_material(aiMaterial *mat) {
|
||||
aiString matName;
|
||||
aiReturn ret = mat->Get(AI_MATKEY_NAME, matName);
|
||||
if (ret != AI_SUCCESS) {
|
||||
spdlog::error("Could not find material name");
|
||||
}
|
||||
|
||||
aiColor3D ambientColor {0.f, 0.f, 0.f};
|
||||
ret = mat->Get(AI_MATKEY_COLOR_DIFFUSE, ambientColor);
|
||||
if (ret != AI_SUCCESS) {
|
||||
spdlog::error("Could not find ambient color for material: {}", matName.C_Str());
|
||||
ambientColor = {.3f, .3f, .3f};
|
||||
}
|
||||
|
||||
aiColor3D diffuseColor {1.f, 1.f, 1.f};
|
||||
ret = mat->Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor);
|
||||
if (ret != AI_SUCCESS) {
|
||||
spdlog::error("Could not find diffuse color for material: {}", matName.C_Str());
|
||||
diffuseColor = {1.f, 1.f, 1.f};
|
||||
}
|
||||
|
||||
aiColor3D specularColor {1.f, 1.f, 1.f};
|
||||
ret = mat->Get(AI_MATKEY_COLOR_SPECULAR, specularColor);
|
||||
if (ret != AI_SUCCESS) {
|
||||
spdlog::error("Could not find specular color for material: {}", matName.C_Str());
|
||||
specularColor = {1.f, 1.f, 1.f};
|
||||
}
|
||||
|
||||
float shininess = 32.f;
|
||||
ret = mat->Get(AI_MATKEY_SHININESS, shininess);
|
||||
if (ret != AI_SUCCESS) {
|
||||
spdlog::error("Could not find shininess for material: {}", matName.C_Str());
|
||||
shininess = 32.f;
|
||||
}
|
||||
|
||||
Material material;
|
||||
material.name = matName.C_Str();
|
||||
material.phong.ambient = glm::vec3{ambientColor.r, ambientColor.g, ambientColor.b};
|
||||
material.phong.diffuse = glm::vec3{diffuseColor.r, diffuseColor.g, diffuseColor.b};
|
||||
material.phong.specular = glm::vec3{specularColor.r, specularColor.g, specularColor.b};
|
||||
material.phong.shininess = shininess;
|
||||
|
||||
return material;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user