From 576d4030b86d678865da99756719072d41802b39 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Thu, 9 Dec 2021 16:24:26 -0500 Subject: [PATCH] Add specular lighting --- .../resources/shaders/fragment_core.glsl | 22 +++++++++++++++++-- .../resources/shaders/vertex_core.glsl | 9 ++------ examples/dev_testing/src/main.cpp | 2 ++ include/simpleengine/gfx/texture.h | 2 ++ src/gfx/textured_model.cpp | 2 ++ 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/examples/dev_testing/resources/shaders/fragment_core.glsl b/examples/dev_testing/resources/shaders/fragment_core.glsl index 68976ea..d5fe2e6 100644 --- a/examples/dev_testing/resources/shaders/fragment_core.glsl +++ b/examples/dev_testing/resources/shaders/fragment_core.glsl @@ -5,14 +5,18 @@ in mat4 vs_transform; in vec2 vs_texcoord; in vec3 vs_normal; in vec3 vs_to_light; +in vec3 vs_to_camera; uniform bool texture_is_set; uniform sampler2D vs_texture; uniform vec3 light_color; +uniform float shine_damper; +uniform float reflectivity; out vec4 fs_color; void main() { + // Lighting vec3 unit_normal = normalize(vs_normal); vec3 unit_light_vector = normalize(vs_to_light); @@ -20,9 +24,23 @@ void main() { float brightness = max(dot_prod, 0.f); vec3 diffuse = brightness * light_color; + // Specular lighting + // only do all this math is reflectivity is > 0 + vec3 final_specular = vec3(0.f); + if (reflectivity > 0) { + vec3 unit_vector_to_camera = normalize(vs_to_camera); + vec3 light_direction = -unit_vector_to_camera; + vec3 reflected_light_dir = reflect(light_direction, unit_normal); + float specular_factor = dot(reflected_light_dir, unit_vector_to_camera); + specular_factor = max(specular_factor, 0.f); + float damped_specular = pow(specular_factor, shine_damper); + final_specular = damped_specular * reflectivity * light_color; + } + if (texture_is_set) { - fs_color = vec4(diffuse, 1.f) * texture(vs_texture, vs_texcoord); + //fs_color = vec4(0.5 * unit_normal + vec3(0.5), 1.f); // Visualize normals + fs_color = vec4(diffuse, 1.f) * texture(vs_texture, vs_texcoord) + vec4(final_specular, 1.f); } else { - fs_color = vec4(diffuse, 1.f); + fs_color = vec4(diffuse, 1.f) + vec4(final_specular, 1.f); } } \ No newline at end of file diff --git a/examples/dev_testing/resources/shaders/vertex_core.glsl b/examples/dev_testing/resources/shaders/vertex_core.glsl index b99b0f3..42b9be3 100644 --- a/examples/dev_testing/resources/shaders/vertex_core.glsl +++ b/examples/dev_testing/resources/shaders/vertex_core.glsl @@ -9,6 +9,7 @@ out vec2 vs_texcoord; out mat4 vs_transform; out vec3 vs_normal; out vec3 vs_to_light; +out vec3 vs_to_camera; uniform mat4 transform_matrix; uniform mat4 view_matrix; @@ -26,11 +27,5 @@ void main() { vs_normal = (transform_matrix * vec4(vertex_normal, 0.f)).xyz; vs_to_light = light_position - world_pos.xyz; - - /* vs_position = vec4(transform_matrix * vec4(vertex_position, 1.f)).xyz; - vs_transform = transform_matrix; - vs_color = vertex_color; - vs_texcoord = vertex_texcoord; - - gl_Position = projection_matrix * view_matrix * transform_matrix * vec4(vertex_position, 1.0f); */ + vs_to_camera = (inverse(view_matrix) * vec4(0.f, 0.f, 0.f, 1.f)).xyz - world_pos.xyz; } \ No newline at end of file diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index fedd4f9..13be6c7 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -52,6 +52,8 @@ int main(int argc, char *argv[]) { game.add_event(light); simpleengine::gfx::Texture white_texture("resources/white_texture.png"); + /* white_texture.shine_damper = 10; + white_texture.reflectivity = 1; */ auto dragon = std::make_shared(game.get_window(), core_shader, white_texture, "resources/dragon.obj"); dragon->translate(0.f, -5.f, -25.f); game.add_event(dragon); diff --git a/include/simpleengine/gfx/texture.h b/include/simpleengine/gfx/texture.h index 527a35e..a1c0fc2 100644 --- a/include/simpleengine/gfx/texture.h +++ b/include/simpleengine/gfx/texture.h @@ -29,6 +29,8 @@ namespace simpleengine::gfx { int height; int width; int channels; + float shine_damper = 1.f; + float reflectivity = 0.f; /** * @brief Construct a new Texture object from a path. diff --git a/src/gfx/textured_model.cpp b/src/gfx/textured_model.cpp index 3036ec7..f6148d4 100644 --- a/src/gfx/textured_model.cpp +++ b/src/gfx/textured_model.cpp @@ -19,6 +19,8 @@ namespace simpleengine::gfx { void TexturedModel::render(GLFWwindow* target) { shader.use(); shader.set_uniform_matrix_4f("transform_matrix", transform_matrix, false); + shader.set_uniform_float("shine_damper", texture.shine_damper, false); + shader.set_uniform_float("reflectivity", texture.reflectivity, false); // When binding to the texture, tell the shader if the texture is set or not. shader.set_uniform_int("texture_is_set", (GLint) true, false);