Coordinate lighting system for each fragment

I am developing an OpenGL 2.1 application using shaders and I have a problem with my fragment lighting. The lighting is correct when I bootstrap my scene, but when I move around the stage, the lighting moves with the "camera" rather than staying in a static place. For example, if I put my light to the right, the right side of the objects will be highlighted. If you 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 (and not on the left, as it is now). I assume that I am calculating lighting in the wrong coordinate system, and this causes the wrong behavior. I calculate the lighting as follows ...

In the vertex shader ...

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

where vertexNormal is normal in an 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, for example, (100,0, 10,0, 0,0), which will put the light on the right side of the world, as described above. The part I don't know about is the gl_NormalMatrix part. I’m not quite sure what the matrix is ​​and what coordinate space it brings to my normal life (I assume this is the space of the world). If the normal fits into world space, then I realized that the problem is that ECPosition is in the 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 to put ECPosition into world space, multiplying it by my own Matrix model, which contains only the transformations that I do to get the coordinates in world space, but that did not work. Let me know if I need to provide other information about my shaders or code.

+7
source share
1 answer

gl_NormalMatrix converts your normal into the eye space (see this tutorial for more details).

I think that in order for your light to be in a static world position, you must transform your LightPosition into eye space, as well as by multiplying it by the current view matrix.

+6
source

All Articles