From ce8886ade15ffdfba1e67136037ed91d73d404e7 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Wed, 12 Oct 2022 23:35:39 -0400 Subject: [PATCH] Always calculate tangent space for Models --- examples/dev_testing/src/main.cpp | 5 +- include/simpleengine/gfx/model.h | 3 +- resources/shaders/core/3d/fragment_core.glsl | 3 +- src/gfx/model.cpp | 53 +------------------- 4 files changed, 5 insertions(+), 59 deletions(-) diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index eb334c4..ca77c59 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -78,7 +78,6 @@ int main(int argc, char *argv[]) { se::ecs::Entity other_e = scene->create_entity(); other_e.add_component("examples/dev_testing/resources/transparent_window.fbx", - se::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE | se::gfx::ModelProcessingFlags::MdlProcFlag_TRANSPARENT); auto &other_transform = other_e.add_component(); other_transform.translate(5.f, 0.5f, 1.f); @@ -89,15 +88,13 @@ int main(int argc, char *argv[]) { // entity.add_component("examples/dev_testing/resources/planks/planks.fbx", simpleengine::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE); //entity.add_component("examples/dev_testing/resources/bricks/bricks.fbx", simpleengine::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE); entity.add_component("examples/dev_testing/resources/transparent_window.fbx", - se::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE | se::gfx::ModelProcessingFlags::MdlProcFlag_TRANSPARENT); auto &transform_comp = entity.add_component(); transform_comp.translate(4.f, 0.f, 0.f); se::ecs::Entity brick_e = scene->create_entity(); - brick_e.add_component("examples/dev_testing/resources/bricks/bricks.fbx", - se::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE); + brick_e.add_component("examples/dev_testing/resources/bricks/bricks.fbx"); auto &brick_transf = brick_e.add_component(); brick_transf.translate(6.f, -0.5f, 1.f); diff --git a/include/simpleengine/gfx/model.h b/include/simpleengine/gfx/model.h index db8cbd4..b6beab0 100644 --- a/include/simpleengine/gfx/model.h +++ b/include/simpleengine/gfx/model.h @@ -15,8 +15,7 @@ namespace simpleengine::gfx { MdlProcFlag_NONE = 0x0, MdlProcFlag_FLIP_TEX_COORDS_VERTICALLY = 0x1, MdlProcFlag_FLIP_TEX_COORDS_HORIZONTALLY = 0x2, - MdlProcFlag_CALCULATE_TANGENT_SPACE = 0x4, - MdlProcFlag_TRANSPARENT = 0x8, + MdlProcFlag_TRANSPARENT = 0x4, }; /** diff --git a/resources/shaders/core/3d/fragment_core.glsl b/resources/shaders/core/3d/fragment_core.glsl index 0751d1c..cd76c7b 100644 --- a/resources/shaders/core/3d/fragment_core.glsl +++ b/resources/shaders/core/3d/fragment_core.glsl @@ -26,8 +26,7 @@ struct Material { bool has_specular_map; sampler2D specular_map; - - // TODO: Make Optional + bool has_normal_map; sampler2D normal_map; diff --git a/src/gfx/model.cpp b/src/gfx/model.cpp index 956ea1d..09d57c4 100644 --- a/src/gfx/model.cpp +++ b/src/gfx/model.cpp @@ -34,12 +34,8 @@ namespace simpleengine::gfx { void Model::load_model(std::string path) { Assimp::Importer importer; - if (model_processing_flags & ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE) { - additional_assimp_flags |= aiProcess_CalcTangentSpace; - } - // assimp post processing options: http://assimp.sourceforge.net/lib_html/postprocess_8h.html - const aiScene *scene = importer.ReadFile(path, additional_assimp_flags | aiProcess_Triangulate | aiProcess_FlipUVs); + const aiScene *scene = importer.ReadFile(path, aiProcess_GenNormals | aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_FlipUVs); if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) { std::cout << "ERROR::ASSIMP::" << importer.GetErrorString() << std::endl; @@ -141,9 +137,6 @@ namespace simpleengine::gfx { std::vector> normal_maps = load_material_texture(processed_textures, material, loading_type); if (!normal_maps.empty()) { textures.emplace(loading_type, normal_maps); - - // Force calculation of tangent space. - model_processing_flags |= MdlProcFlag_CALCULATE_TANGENT_SPACE; } // TODO Handle other types of texture maps @@ -180,10 +173,7 @@ namespace simpleengine::gfx { Mesh se_mesh(vertices, indicies, mat); - if (!mesh->HasNormals()) { - se_mesh.calculate_normals(); - } - + // Store the tangents in the mesh if (mesh->HasTangentsAndBitangents()) { se_mesh.tangents = tangents; } @@ -255,43 +245,4 @@ namespace simpleengine::gfx { mesh.calculate_tangents(); } } - - /* std::vector> calculate_tangent_space( - std::vector vertices, std::vector indices) { - - std::vector> tangents; - tangents.resize(vertices.size()); - - for (int i = 0; i < indices.size(); i += 3) { - int index0 = indices[i]; - int index1 = indices[i+1]; - int index2 = indices[i+2]; - - LitVertex& lit_vertex0 = vertices[index0]; - LitVertex& lit_vertex1 = vertices[index1]; - LitVertex& lit_vertex2 = vertices[index2]; - - glm::vec3 pos0 = lit_vertex0.position; - glm::vec3 pos1 = lit_vertex1.position; - glm::vec3 pos2 = lit_vertex2.position; - - // Edges of the triangle : postion delta - glm::vec3 delta_pos1 = pos1 - pos0; - glm::vec3 delta_pos2 = pos2 - pos0; - - // UV delta - glm::vec2 delta_uv1 = lit_vertex1.tex_coord - lit_vertex0.tex_coord; - glm::vec2 delta_uv2 = lit_vertex2.tex_coord - lit_vertex0.tex_coord; - - float r = 1.0f / (delta_uv1.x * delta_uv2.y - delta_uv1.y * delta_uv2.x); - glm::vec3 tangent = (delta_pos1 * delta_uv2.y - delta_pos2 * delta_uv1.y)*r; - glm::vec3 bitangent = (delta_pos2 * delta_uv1.x - delta_pos1 * delta_uv2.x)*r; - - tangents[0] = {tangent, bitangent}; - tangents[1] = {tangent, bitangent}; - tangents[2] = {tangent, bitangent}; - } - - return tangents; - } */ } // namespace simpleengine::gfx \ No newline at end of file