From 8de75c7c47c9701caab299b801abd4d27e87c342 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 11 Mar 2018 14:16:23 +0100 Subject: [PATCH] Added specular highlights --- data/shaders/fs_default.glsl | 15 +++++++++++++-- data/shaders/vs_default.glsl | 9 +++++++-- src/modules/RenderModule.cc | 12 +++++++----- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/data/shaders/fs_default.glsl b/data/shaders/fs_default.glsl index c512d00..e7c4656 100644 --- a/data/shaders/fs_default.glsl +++ b/data/shaders/fs_default.glsl @@ -2,21 +2,32 @@ uniform vec4 uColor; uniform vec3 uLightDirection; +uniform vec3 uViewPosition; smooth in vec4 ioFragColor; in vec3 ioNormal; +in vec3 ioFragPosition; out vec4 outColor; void main() { + // ambient lighting float ambient_strength = 0.1; - vec4 ambient = ambient_strength * ioFragColor; + // diffuse lighting vec3 norm = normalize(ioNormal); vec3 light_dir = normalize(uLightDirection); float diff = max(dot(norm, light_dir), 0.0); vec4 diffuse = diff * ioFragColor; - outColor = ambient + diffuse; + // specular lighting + vec3 view_dir = normalize(uViewPosition - ioFragPosition); + vec3 reflect_dir = reflect(-light_dir, norm); + + float specular_strength = 0.5; + float spec = pow(max(dot(view_dir, reflect_dir), 0.0), 32); + vec4 specular = specular_strength * spec * vec4(1.0, 1.0, 1.0, 1.0); + + outColor = ambient + diffuse + specular; } diff --git a/data/shaders/vs_default.glsl b/data/shaders/vs_default.glsl index 179efcc..29d3b7f 100644 --- a/data/shaders/vs_default.glsl +++ b/data/shaders/vs_default.glsl @@ -6,14 +6,19 @@ in vec3 inNormal; in vec2 inUV; in vec4 inColor; -uniform mat4 uModelViewProj; +uniform mat4 uModelMatrix; +uniform mat4 uViewMatrix; +uniform mat4 uProjectionMatrix; uniform vec3 uLightDirection; +uniform vec3 uViewPosition; smooth out vec4 ioFragColor; out vec3 ioNormal; +out vec3 ioFragPosition; void main() { - gl_Position = uModelViewProj * inCoord; + gl_Position = uProjectionMatrix * uViewMatrix * uModelMatrix * inCoord; ioFragColor = inColor; ioNormal = inNormal; + ioFragPosition = (uModelMatrix * inCoord).xyz; } diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index 878f4d7..1ed1724 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -465,13 +465,15 @@ void Renderer::RenderGl() { // Unit cube glUseProgram(mDefaultProgram.mProgramId); - model_view_projection = - TranslateMat44(3.0f, 0.0f, 1.0f) - * mCamera.mViewMatrix - * mCamera.mProjectionMatrix; - mDefaultProgram.SetMat44("uModelViewProj", model_view_projection); + model_matrix = TranslateMat44(3.0f, 0.0f, 1.0f); + + mDefaultProgram.SetMat44("uModelMatrix", model_matrix); + mDefaultProgram.SetMat44("uViewMatrix", mCamera.mViewMatrix); + mDefaultProgram.SetMat44("uProjectionMatrix", mCamera.mProjectionMatrix); + mDefaultProgram.SetVec4("uColor", Vector4f (1.0f, 0.0f, 0.0f, 1.0f)); mDefaultProgram.SetVec3("uLightDirection", mLight.mDirection); + mDefaultProgram.SetVec3("uViewPosition", mCamera.mEye); gVertexArray.Bind(); gUnitCubeMesh.Draw(GL_TRIANGLES);