Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / 3D Theory & Graphics / help, diffuse lighting seems view dependant Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

March 14, 2005, 09:14 PM

I am trying to write a simple diffuse-only, per-vertex lighting program in glsl. I have read various tutorials, and writing such a shader should be fairly simple. However, when I try, the lighting seems view dependant, ex. when I move my view, the lighting seems to follow. Has anyone else had similar problems?

Here is my vertex shader code:

  2. varying vec2 texcoord;
  3. varying vec3 lightpos;
  4. varying vec3 a2;
  5. varying vec3 normal;
  6. void main(void)
  7. {
  8.         a2 = ftransform().xyz;
  9.         lightpos = gl_LightSource[0];
  10.         texcoord = gl_MultiTexCoord0.xy;
  11.         normal = gl_NormalMatrix*;
  12.         gl_Position = ftransform();
  13. }

...and here is the fragment shader code:

  2. varying vec2 texcoord;
  3. uniform sampler2D diffuse_map;
  4. varying vec3 lightpos;
  5. varying vec3 viewvect;
  6. varying vec3 normal;
  7. varying vec3 a2;
  8. void main (void)
  9. {
  10.         vec4 ambient = vec4(1.0, 1.0, 1.0, 1.0);
  11.         vec3 lightvec = normalize(lightpos - a2);
  12.         vec4 txc = texture2D(diffuse_map, texcoord);
  13.         vec4 ndotl;
  14.         ndotl.rgb = max(0, dot(lightvec, normal));
  15.         ndotl.a = 1.0;
  16.         gl_FragColor = txc * ndotl + txc*ambient;
  17. }


March 14, 2005, 11:22 PM

Are you updating the light source position every frame from the main app? Remember, the GL_LIGHT position is stored in eye coordinates, so if you do not update it every frame, the lighting will change whenever the camera location or angle changes.

Tamas Schlagl

March 15, 2005, 06:44 AM

  2.         a2 = ftransform().xyz;
  3.         ...
  4.         gl_Position = ftransform();

This means you use same matrix for project the vertices to screen space and for rotate to world position (for lighting)... this is the problem.


March 15, 2005, 09:54 PM

Ahh yes...if you want to do lighting in eye space (which it looks like) you should have a2 = gl_ModelviewMatrix * gl_Vertex

This thread contains 4 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.