Results 1 to 5 of 5

Thread: directional light confusion

  1. #1
    Member Contributor
    Join Date
    Feb 2009
    Posts
    64

    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!

  2. #2
    Member Contributor
    Join Date
    Feb 2009
    Posts
    64

    Re: directional light confusion

    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

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,651

    Re: directional light confusion

    Quote Originally Posted by mokafolio
    I just have a constant (normalized) direction vector in my shader ... 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 ... Unfortunatelly the results i get look a little bit weird
    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.

  4. #4
    Member Contributor
    Join Date
    Feb 2009
    Posts
    64

    Re: directional light confusion

    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):
    Code :
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(...);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        //simple camera transforms
        glTranslatef(...);
        glRotatef(...);
        //save light position in worldspace
        glGetDoublev( GL_MODELVIEW_MATRIX, modelView);
        lightPosModelView = transform_vector(modelView, lightPos);
        glutSolidTeapot();

    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!

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,651

    Re: directional light confusion

    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.

Similar Threads

  1. Directional light shaders
    By M//Hax in forum OpenGL: GLSL
    Replies: 15
    Last Post: 12-21-2010, 12:57 PM
  2. Directional Light
    By yuzi in forum COLLADA
    Replies: 1
    Last Post: 09-27-2010, 03:03 PM
  3. Directional light
    By ptigibus in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-02-2004, 03:49 AM
  4. Specular light not directional?
    By in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 08-12-2002, 09:03 AM
  5. Directional Light
    By Xero_Tolerance in forum OpenGL: Advanced Coding
    Replies: 18
    Last Post: 04-16-2002, 12:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean