Original Post
I just finished writing a phong shader to handle directional and positional light from a single source but it's not working. The fragments are only receiving the ambient light. I have tried several directions but nothing is working. What I need is for someone to proof read them to make sure they are correct. If they are, it could be a problem with the way I'm setting light and material parameters. Vertex shader Fragment shader Thanks. [Edited by - biggoron on January 23, 2008 4:43:15 PM]
varying vec3 normal, eyeVec, lightDir;
void main(void)
{
vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
normal = normalize(gl_NormalMatrix * gl_Normal);
eyeVec = -vVertex;
lightDir = gl_LightSource[0].position.xyz;
// Calculate positional direction
if(gl_LightSource[0].position.w == 1.0) {
lightDir += eyeVec;
}
gl_Position = ftransform();
}
varying vec3 normal, eyeVec, lightDir;
void main(void)
{
vec4 colour;
vec4 amb;
float lambert;
amb = (gl_LightSource[0].ambient * gl_FrontMaterial.ambient) + (gl_LightModel.ambient * gl_FrontMaterial.ambient);
vec3 N, L;
N = normalize(normal);
L = normalize(lightDir);
lambert = dot(N, L);
colour = amb;
// In light, calculate diffuse and specular terms
if(lambert > 0.0) {
vec4 diffuse, specular;
vec3 R = reflect(-L, N);
diffuse = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * lambert;
specular = gl_LightSource[0].specular * gl_FrontMaterial.specular * pow(max(dot(R, normalize(eyeVec)), 0.0), gl_FrontMaterial.shininess);
colour += diffuse + specular;
}
gl_FragColor = colour;
}