From 5c9310605acfe0fe06da56e59f0ad32ab8efea5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 16 Apr 2012 02:29:53 +0200 Subject: [PATCH] Finally fixed light intensity computation bug in PhongShader. Both vectors passed to dot() (and thus also reflect()) must be normalized to achieve desired result. --- src/Shaders/PhongShader.frag | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Shaders/PhongShader.frag b/src/Shaders/PhongShader.frag index f34a95cf3..ae99af121 100644 --- a/src/Shaders/PhongShader.frag +++ b/src/Shaders/PhongShader.frag @@ -18,14 +18,17 @@ void main() { /* Ambient color */ color.rgb = ambientColor*lightAmbientColor; + vec3 normalizedTransformedNormal = normalize(transformedNormal); + vec3 normalizedLightDirection = normalize(lightDirection); + /* Add diffuse color */ - float intensity = max(0.0, dot(transformedNormal, lightDirection)); + float intensity = max(0.0, dot(normalizedTransformedNormal, normalizedLightDirection)); color.rgb += diffuseColor*lightDiffuseColor*intensity; /* Add specular color, if needed */ if(intensity != 0) { - vec3 reflection = reflect(-lightDirection, transformedNormal); - float specularity = pow(max(0.0, dot(transformedNormal, reflection)), shininess); + vec3 reflection = reflect(-normalizedLightDirection, normalizedTransformedNormal); + float specularity = pow(max(0.0, dot(normalizedTransformedNormal, reflection)), shininess); color.rgb += specularColor*lightSpecularColor*specularity; }