fix shaders for proper lighting computation

This commit is contained in:
Josh Holtrop 2011-05-05 13:56:57 -04:00
parent 2e2b8225be
commit 59d0d66c44
2 changed files with 6 additions and 6 deletions

View File

@ -2,7 +2,7 @@
uniform vec4 ambient, diffuse, specular; uniform vec4 ambient, diffuse, specular;
uniform float shininess; uniform float shininess;
varying vec3 eye_pos_i; varying vec3 pos_i;
varying vec3 normal_i; varying vec3 normal_i;
void main(void) void main(void)
@ -11,7 +11,7 @@ void main(void)
vec4 color; vec4 color;
float NdotL, RdotEye; float NdotL, RdotEye;
lightDir = vec3(-0.1, 0, -0.9); lightDir = normalize(vec3(-0.1, 0, -0.9));
color = ambient; /* ambient light */ color = ambient; /* ambient light */
n = normalize(normal_i); n = normalize(normal_i);
@ -22,10 +22,10 @@ void main(void)
/* diffuse component */ /* diffuse component */
color += diffuse * NdotL; color += diffuse * NdotL;
/* specular component */ /* specular component */
RdotEye = dot(normalize(eye_pos_i), normalize(reflect(-lightDir, n))); RdotEye = dot(normalize(-pos_i), normalize(reflect(-lightDir, n)));
if (RdotEye > 0.0) if (RdotEye > 0.0)
{ {
color += specular * pow(RdotEye, shininess); color += clamp(specular * pow(RdotEye, shininess), 0.0, 1.0);
} }
} }

View File

@ -2,12 +2,12 @@
attribute vec3 pos; attribute vec3 pos;
attribute vec3 normal; attribute vec3 normal;
varying vec3 eye_pos_i; varying vec3 pos_i;
varying vec3 normal_i; varying vec3 normal_i;
void main(void) void main(void)
{ {
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1); gl_Position = gl_ModelViewProjectionMatrix * vec4(pos, 1);
eye_pos_i = vec3(gl_Position.x, gl_Position.y, gl_Position.z); pos_i = vec3(gl_Position.x, gl_Position.y, gl_Position.z);
normal_i = gl_NormalMatrix * normal; normal_i = gl_NormalMatrix * normal;
} }