Well I am normalizing the eye vector with the line:
vec3 eyeVec = normalize(-eyePos.xyz);
But remember that my problem is that the lights look bad when things are normalized (just like the code you gave Hummus).
Here is my entire shader:
/**** VERTEX SHADER ****/
varying vec3 norm;
varying vec4 lightVec[1];
varying vec3 halfAngle[1];
void main()
{
vec4 eyePos = gl_ModelViewMatrix * gl_Vertex;
gl_FrontColor = gl_FrontMaterial.diffuse;
gl_BackColor = gl_BackMaterial.diffuse;
gl_FrontSecondaryColor = gl_FrontMaterial.specular;
gl_BackSecondaryColor = gl_BackMaterial.specular;
vec3 eyeVec = normalize(-eyePos.xyz);
norm = normalize(gl_NormalMatrix * gl_Normal);
vec4 light;
light = gl_LightSource[0].position - eyePos;
/******
This is the important line. If this line is commented out, things look nice, if it stays in, things look wierd
*******/
light.xyz = normalize(light.xyz);
lightVec[0].xyz = light.xyz;
halfAngle[0] = (light.xyz + eyeVec) * 0.5;
gl_Position = ftransform();
}
/**** FRAGMENT SHADER ****/
varying vec3 norm;
varying vec4 lightVec[1];
varying vec3 halfAngle[1];
void main()
{
vec4 outcol = vec4(0.0, 0.0, 0.0, 0.0);
vec4 tempC;
vec3 lightV;
float lightDot;
vec3 normal = normalize(norm);
lightV = normalize(lightVec[0].xyz);
tempC = vec4(0.0,0.0,0.0,0.0);
lightDot = max(dot(lightV, normal), 0.0);
tempC += lightDot * gl_Color ;
tempC += pow(max(dot(normalize(halfAngle[0]), normal), 0.0), gl_FrontMaterial.shininess) * gl_SecondaryColor ;
tempC *= gl_LightSource[0].diffuse;
outcol += tempC;
outcol.xyz += vec3(gl_LightModel.ambient * gl_FrontMaterial.ambient);
gl_FragColor.xyz = outcol.xyz;
gl_FragColor.a = gl_Color.a ;
}
Here are some videos of the results I get.
This is a single quad, 8x8 units wide centerd at (0,0,0). The camera is at (0,0,10). The light is at (x,0,1) and travels from about -5 to 5 in the x direction.
This video is when both light and eyeVec are normalized in the vertex shader. These are the bad results I’m getting when I’m doing the right thing. Notice how you can see the tesselated triagles as the light moves across. This is why the post is titled ‘interpation precision’
normalized
Now, here is a video where the line
light.xyz = normalize(light.xyz);
is commented out.
unnormalized
And here are some extra examples, in this case
instead of a quad, the geoemtry is a 10x10 grid of quads, the geometry is still 8x8 units wide though and the light/camera are at the same position.
normalized-grid
unnormalized-grid
Is there anything obvious I’m doing wrong in the shader? I must be missing something here but I can’t figure out what. Thanks for all the help.
Malcolm Bechard