From 366ab195448d9bae611f99c3f1b6e4660ad97e27 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 30 Sep 2022 17:01:14 -0400 Subject: [PATCH] Make normal maps optional --- examples/dev_testing/src/main.cpp | 2 +- resources/shaders/core/3d/fragment_core.glsl | 16 ++++++++++------ src/gfx/renderer.cpp | 5 ++--- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index ed4d271..0b11aa1 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -172,7 +172,7 @@ int main(int argc, char *argv[]) { // entity.add_component("examples/dev_testing/resources/viper/viper.obj"); // entity.add_component("examples/dev_testing/resources/halot/chief.fbx"); - //entity.add_component("examples/dev_testing/resources/planks/planks.fbx", simpleengine::gfx::ModelProcessingFlags::MdlProcFlag_CALCULATE_TANGENT_SPACE); + // 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/scientist/scientist.fbx"); diff --git a/resources/shaders/core/3d/fragment_core.glsl b/resources/shaders/core/3d/fragment_core.glsl index 18ab10d..218f012 100644 --- a/resources/shaders/core/3d/fragment_core.glsl +++ b/resources/shaders/core/3d/fragment_core.glsl @@ -24,6 +24,7 @@ struct Material { sampler2D specular_map; // TODO: Make Optional + bool has_normal_map; sampler2D normal_map; float ambient_strength; @@ -53,11 +54,14 @@ vec3 calculate_lighting() { //float ambient_strength = 0.1; vec3 ambient = u_material.ambient_strength * u_light_color; - // Apply the normal map to the lighting. - vec3 normal = vs_normal; - normal = texture(u_material.normal_map, vs_texcoord).rgb; - normal = normal * 2.0 - 1.0; - normal = normalize(vs_tbn * normal); + vec3 normal = vs_world_normal; + + // Check if the normal map is set before trying to apply it. + if (u_material.has_normal_map) { + normal = texture(u_material.normal_map, vs_texcoord).rgb; + normal = normal * 2.0 - 1.0; + normal = normalize(vs_tbn * normal); + } // Diffuse vec3 norm = normalize(normal); @@ -72,7 +76,7 @@ vec3 calculate_lighting() { float spec = pow(max(dot(view_dir, reflect_dir), -0.f), 32 * u_material.shine_factor); vec3 specular = specular_strength * (spec * u_material.specular_strength) * u_light_color; - // Check if the specular map is set before trying to set it + // Check if the specular map is set before trying to apply it. if (u_material.has_specular_map) { specular = specular * texture(u_material.specular_map, vs_texcoord).r; } diff --git a/src/gfx/renderer.cpp b/src/gfx/renderer.cpp index 9a770b6..e336daa 100644 --- a/src/gfx/renderer.cpp +++ b/src/gfx/renderer.cpp @@ -138,14 +138,13 @@ namespace simpleengine::gfx { if (normal_maps != material->textures.end()) { auto normal = normal_maps->second.front(); - //shader.set_uniform_int("u_material.has_normal_map", 1, false); + shader.set_uniform_int("u_material.has_normal_map", 1, false); shader.set_uniform_int("u_material.normal_map", 2, false); glActiveTexture(GL_TEXTURE2); normal->bind(); } else { - //shader.set_uniform_int("u_material.has_normal_map", 0, false); - std::cout << "No normal map for model!" << std::endl; + shader.set_uniform_int("u_material.has_normal_map", 0, false); } }