directional light confusion

Hello openGL friends,

I am currently implementing directional lights in GLSL (actually deferred but that should not matter) and I am confused about something.- The light works fine but right now the light direction is in viewspace so that it works kind of like a camera light (e.g: when i move the camera using glRotate or gluLookAt it stays in place relative to the camera, not the scene/world). That makes sense since I just have a constant (normalized) direction vector in my shader which does not get multiplied by any matrix.
However I was thinking that I could simply multiply the direction vector by gl_ModelViewMatrix in the vertex shader and normalize it in the fragment shader (how you would do it for point lights in world space, even though there its a position rather than a direction).- Unfortunatelly the results i get look a little bit weird so I was wondering if that is the correct approach. Any ideas on that?

If my idea is correct its propably something about my shader, I will post it then, if the way I had in mind is wrong anyways please correct me!

Thank you!

Okay, I think it works like i said:
multiply the light Direction with the modelviewmatrix in the vertex shader and normalize it in the fragmentshader. the only thing that was weird were my specular setting which made the impression that something about the light position was wrong.
it would be great though if someone could confirm that :slight_smile:

Doesn’t sound like it. Here’s why: ModelView in the shader is set to position the solid object in the world you are rendering. It transforms that object from its own object-space to the shared eye-space.

If you transform your constant light vector by ModelView, you are treating that vector as a direction in the object your rendering’s object space. If you’re rendering multiple objects, each with their own object space, they’ll all have their own directional light direction oriented in their own object space. In eye space, there won’t be a consistent position for the directional light. It will vary for each object space you’re rendering.

Simplest approach is to pre-transform the directional light vector to eye-space on the CPU every frame, and then just pass that into the shader.

hmm I only tested it with one glutSolidTeapot and I think it looked right.- Basically I also tried to send the transformed vector to the shader and it looked the same so I thought I could also do it in the shader. This is how I did it (PSEUDOCODE):

    //simple camera transforms
    //save light position in worldspace
    glGetDoublev( GL_MODELVIEW_MATRIX, modelView);
    lightPosModelView = transform_vector(modelView, lightPos);

and then in the lighting pass I simply send the light position to my shader.- so basically i setup my camera, save lightPos depending on that setup, and then render objects with their setup relative to the scene. Is that what you said?

Thank you!

Essentially, yes. Though you rendered your teapot with the same active object space as your light (there are no intervening MODELVIEW mods). So your light is still gonna be nailed to the teapot’s object space.