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;
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.
Nitrex88
source share