I'm not sure if I have a bug in my code or not. It seems like it's a bug, or at least a major shortfall. Here's two images of the same model at slightly different angles (by moving the camera):
As we can see, the diffuse shading is wildly different from moving the camera just a bit. I would expect this effect from the specular, but not the diffuse (and removing the specular component has no noticeable affect on this shading).
The relevant part of the fragment shader (which removing will remove this issue, but also remove all diffuse shading) is:
clamp(dot(normal, lightDir), 0, 1)
normal is the normalized normal vector in camera space (found with
(VP * M * vec4(vertexNormal_modelspace,0)).xyz) and
lightDir is the normalized light direction vector, which is found with:
vec3 vertexPosition_cameraspace = (VP * M * vec4(vertexPosition_modelspace, 1)).xyz; eyeDirection_cameraspace = vec3(0, 0, 0) - vertexPosition_cameraspace; vec3 lightPosition_cameraspace = (VP * vec4(lightPos, 1)).xyz; lightDirection_cameraspace = lightPosition_cameraspace + eyeDirection_cameraspace;
The above clamped dot product is multiplied by the diffuse color and light color. It seems to be exactly what the Wikipedia page describes (they don't clamp it, but describe the need for it in the text).
Most of the Phong shading code is directly or heavily drawn from this site.
Is the Phong model supposed to result in such camera angle dependent diffuse shading? If not, any ideas as to what might be wrong? If so, what alternatives are there that don't have this effect?
What I want to achieve is essentially the diffuse shading, but without it changing on camera angle, which seems very unrealistic to me. Instead, only the angle between the light source and the normal would matter (and these don't change when we move the camera).
This is homework.