From d1f5ec7479eac5a3ad24dd0758560919e5fce03e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 14 May 2012 02:09:22 +0200 Subject: [PATCH] Phong shader: finally fixed specular highlights. The highlights are now based on camera position, which is finally the way it should be since forever. --- src/Shaders/PhongShader.frag | 3 ++- src/Shaders/PhongShader.vert | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Shaders/PhongShader.frag b/src/Shaders/PhongShader.frag index b70fb8f04..230af1736 100644 --- a/src/Shaders/PhongShader.frag +++ b/src/Shaders/PhongShader.frag @@ -8,6 +8,7 @@ uniform float shininess = 80.0; in vec3 transformedNormal; in vec3 lightDirection; +in vec3 cameraDirection; out vec4 color; @@ -25,7 +26,7 @@ void main() { /* Add specular color, if needed */ if(intensity != 0) { vec3 reflection = reflect(-normalizedLightDirection, normalizedTransformedNormal); - float specularity = pow(max(0.0, dot(normalizedTransformedNormal, reflection)), shininess); + float specularity = pow(max(0.0, dot(normalize(cameraDirection), reflection)), shininess); color.rgb += specularColor*specularity; } diff --git a/src/Shaders/PhongShader.vert b/src/Shaders/PhongShader.vert index 2a8ef00a0..5b73ccd79 100644 --- a/src/Shaders/PhongShader.vert +++ b/src/Shaders/PhongShader.vert @@ -9,6 +9,7 @@ in vec3 normal; out vec3 transformedNormal; out vec3 lightDirection; +out vec3 cameraDirection; void main() { /* Transformed vertex position */ @@ -21,6 +22,9 @@ void main() { /* Direction to the light */ lightDirection = normalize(light - transformedVertex); + /* Direction to the camera */ + cameraDirection = -transformedVertex; + /* Transform the vertex */ gl_Position = projectionMatrix*transformedVertex4; }