Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

per-fragment lighting coordinate system

I'm developing an OpenGL 2.1 application using shaders and I'm having a problem with my per-fragment lighting. The lighting is correct when my scene initial loads, but as I navigate around the scene, the lighting moves around with the "camera", rather than staying in a static location. For example, if I place my light way off to the right, the right side of objects will be illuminated. If I then move the camera to the other side of the object and point in the opposite direction, the lighting is still on the right side of the object (rather than being on the left, like it should now). I assume that I am calculating the lighting in the wrong coordinate system, and this is causing the wrong behavior. I'm calculating lighting the following way...

In vertex shader...

ECPosition = gl_ModelViewMatrix * gl_Vertex;
WCNormal = gl_NormalMatrix * vertexNormal;

where vertexNormal is the normal in object/model space.

In the fragment shader...

float lightIntensity = 0.2; //ambient
lightIntensity += max(dot(normalize(LightPosition - vec3(ECPosition)), WCNormal), 0.0) * 1.5; //diffuse

where LightPosition is, for example, (100.0, 10.0, 0.0), which would put the light on the right side of the world as described above. The part that I'm unsure of, is the gl_NormalMatrix part. I'm not exactly sure what this matrix is and what coordinate space it puts my normal into (I assume world space). If the normal is put into world space, then I figured the problem was that ECPosition is in eye space while LightPosition and WCNormal are in world space. Something about this doesn't seem right but I can't figure it out. I also tried putting ECPosition into world space my multiplying it by my own modelMatrix that only contains the transformations I do to get the coordinate into world space, but this didn't work. Let me know if I need to provide other information about my shaders or code.

like image 636
Nitrex88 Avatar asked Jul 12 '11 20:07

Nitrex88


1 Answers

gl_NormalMatrix transforms your normal into eye-space (see this tutorial for more details).

I think in order for your light to be in a static world position you ought to be transforming your LightPosition into eye-space as well by multiplying it by your current view matrix.

like image 168
bosmacs Avatar answered Oct 30 '22 17:10

bosmacs