OpenGL Diffuse Lighting Shader Bug?

Posted by anon on Stack Overflow See other posts from Stack Overflow or by anon
Published on 2010-03-13T09:42:40Z Indexed on 2010/03/13 9:45 UTC
Read the original article Hit count: 376

Filed under:

The Orange book, section 16.2, lists implementing diffuse lighting as:

void main()
{
  vec3 N = normalize(gl_NormalMatrix * gl_Normal);
  vec4 V = gl_ModelViewMatrix * gl_vertex;
  vec3 L = normalize(lightPos - V.xyz);
  gl_FrontColor = gl_Color * vec4(max(0.0, dot(N, L));
}

However, when I run this, the lighting changes when I move my camera. On the other hand, when I change

  vec3 N = normalize(gl_NormalMatrix * gl_Normal);

to vec3 N = normalize(gl_Normal);

I get diffuse lighting that works like the fixed pipeline.

What is this gl_NormalMatrix, what did removing it do, ... and is this a bug in the orange book ... or am I setting up my OpenGl code improperly?

© Stack Overflow or respective owner

Related posts about vertex-shader