added models and stuff
This commit is contained in:
@@ -17,10 +17,11 @@
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<Model>> ModelManager::models;
|
||||
|
||||
void process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, glm::mat4 transform, Mesh& mesh);
|
||||
void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, Model* model);
|
||||
void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, ModelNode& model);
|
||||
|
||||
void process_ai_material(aiMaterial* aiMat, const aiScene* scene, Material& mat, const std::filesystem::path& modelDirectory);
|
||||
void process_ai_mesh(const aiMesh* aiMesh, Mesh& mesh);
|
||||
|
||||
void process_ai_material(const aiMaterial* aiMat, Material& mat, const std::filesystem::path& modelDirectory);
|
||||
void process_ai_material_diffuse(const aiMaterial *aiMat, Material& mat, const std::filesystem::path& modelDirectory);
|
||||
void process_ai_material_specular(const aiMaterial *aiMat, Material& mat, const std::filesystem::path& modelDirectory);
|
||||
|
||||
@@ -38,7 +39,7 @@ std::shared_ptr<Model> ModelManager::load_from_file(std::string_view _path, bool
|
||||
const aiScene* scene = importer.ReadFile(_path.data(),
|
||||
aiProcess_Triangulate |
|
||||
aiProcess_GenSmoothNormals |
|
||||
aiProcess_FlipUVs);
|
||||
aiProcess_RemoveRedundantMaterials);
|
||||
|
||||
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
|
||||
{
|
||||
@@ -48,14 +49,25 @@ std::shared_ptr<Model> ModelManager::load_from_file(std::string_view _path, bool
|
||||
|
||||
auto model = std::make_shared<Model>();
|
||||
|
||||
std::filesystem::path modelPath = {_path};
|
||||
|
||||
model->materials.resize(scene->mNumMaterials);
|
||||
const std::filesystem::path modelPath = {_path};
|
||||
for (unsigned int i = 0; i < scene->mNumMaterials; i++) {
|
||||
aiMaterial* aiMat = scene->mMaterials[i];
|
||||
aiString matName;
|
||||
if (aiMat->Get(AI_MATKEY_NAME, matName) && strcmp(matName.data, "DefaultMaterial") == 0) {
|
||||
continue;
|
||||
}
|
||||
Material mat{};
|
||||
process_ai_material(aiMat, scene, mat, modelPath.parent_path());
|
||||
model->materials[i] = std::make_shared<Material>(mat);
|
||||
process_ai_material(aiMat, mat, modelPath.parent_path());
|
||||
model->materials.push_back(std::make_shared<Material>(mat));
|
||||
}
|
||||
|
||||
model->meshes.resize(scene->mNumMeshes);
|
||||
for (unsigned int i = 0; i < scene->mNumMeshes; i++) {
|
||||
aiMesh* aiMesh = scene->mMeshes[i];
|
||||
Mesh mesh{};
|
||||
mesh.materialId = aiMesh->mMaterialIndex;
|
||||
process_ai_mesh(aiMesh, mesh);
|
||||
model->meshes.push_back(std::make_shared<Mesh>(mesh));
|
||||
}
|
||||
|
||||
auto transform = glm::identity<glm::mat4>();
|
||||
@@ -63,32 +75,31 @@ std::shared_ptr<Model> ModelManager::load_from_file(std::string_view _path, bool
|
||||
transform = zUpMatrix;
|
||||
}
|
||||
|
||||
process_ai_node(scene->mRootNode, scene, transform, model.get());
|
||||
process_ai_node(scene->mRootNode, scene, transform, model->root);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
void process_ai_node(aiNode* node, const aiScene* scene, glm::mat4 transform, Model* model)
|
||||
void process_ai_node(aiNode* aiNode, const aiScene* scene, glm::mat4 transform, ModelNode* node)
|
||||
{
|
||||
const auto t = node->mTransformation;
|
||||
const auto t = aiNode->mTransformation;
|
||||
transform = transform * glm::mat4(t.a1, t.a2, t.a3, t.a4, t.b1, t.b2, t.b3, t.b4, t.c1, t.c2, t.c3, t.c4, t.d1, t.d2, t.d3, t.d4);
|
||||
|
||||
for (unsigned int i = 0; i < node->mNumMeshes; i++)
|
||||
{
|
||||
aiMesh* aiMesh = scene->mMeshes[node->mMeshes[i]];
|
||||
Mesh mesh{};
|
||||
mesh.materialId = aiMesh->mMaterialIndex;
|
||||
process_ai_mesh(aiMesh, scene, transform, mesh);
|
||||
model->meshes.push_back(std::make_shared<Mesh>(mesh));
|
||||
node->transform = transform;
|
||||
|
||||
for (unsigned int i = 0; i < aiNode->mNumMeshes; i++) {
|
||||
node->meshIndices.push_back(aiNode->mMeshes[i]);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < node->mNumChildren; i++)
|
||||
for (unsigned int i = 0; i < aiNode->mNumChildren; i++)
|
||||
{
|
||||
process_ai_node(node->mChildren[i], scene, transform, model);
|
||||
ModelNode newNode{};
|
||||
process_ai_node(aiNode->mChildren[i], scene, transform, newNode);
|
||||
node->children.push_back(newNode);
|
||||
}
|
||||
}
|
||||
|
||||
void process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, glm::mat4 transform, Mesh& mesh)
|
||||
void process_ai_mesh(const aiMesh* aiMesh, Mesh& mesh)
|
||||
{
|
||||
auto positions = std::vector<float>(aiMesh->mNumVertices * 3);
|
||||
auto uvs = std::vector<float>(aiMesh->mNumVertices * 2);
|
||||
@@ -97,7 +108,7 @@ void process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, glm::mat4 transform,
|
||||
|
||||
for (unsigned int i = 0; i < aiMesh->mNumVertices; i++)
|
||||
{
|
||||
auto position = transform * glm::vec4(aiMesh->mVertices[i].x, aiMesh->mVertices[i].y, aiMesh->mVertices[i].z, 1.0f);
|
||||
auto position = glm::vec4(aiMesh->mVertices[i].x, aiMesh->mVertices[i].y, aiMesh->mVertices[i].z, 1.0f);
|
||||
positions[3 * i] = position.x;
|
||||
positions[3 * i + 1] = position.y;
|
||||
positions[3 * i + 2] = position.z;
|
||||
@@ -138,7 +149,7 @@ void process_ai_mesh(aiMesh* aiMesh, const aiScene* scene, glm::mat4 transform,
|
||||
mesh = {positions, uvs, normals, indices};
|
||||
}
|
||||
|
||||
void process_ai_material(aiMaterial* aiMat, const aiScene* scene, Material& mat, const std::filesystem::path& modelDirectory) {
|
||||
void process_ai_material(const aiMaterial* aiMat, Material& mat, const std::filesystem::path& modelDirectory) {
|
||||
aiString matName;
|
||||
aiReturn ret = aiMat->Get(AI_MATKEY_NAME, matName);
|
||||
if (ret != AI_SUCCESS) {
|
||||
|
||||
Reference in New Issue
Block a user