Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: My lighting is way off

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    22

    My lighting is way off

    imgur.com/hWE1vxS.png

    Code :
    dot(
    normalize( Light1Position - vec3(modelViewProjectionMatrix * position) ),
    normalize( normalMatrix * normal )
    )

    Light1Position is the location of the red dot and I'm translating the spheres using the modelview matrix. How can I make the lighting look correct? Also, I'd like the lighting intensity to decrease with distance.

    Are there any solid tutorials online for this?

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by Rakehell View Post
    Code :
    dot(
    normalize( Light1Position - vec3(modelViewProjectionMatrix * position) ),
    normalize( normalMatrix * normal )
    )
    The projection matrix shouldn't be involved in lighting calculations, i.e. you should just use the model-view matrix.

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    22
    Quote Originally Posted by GClements View Post
    The projection matrix shouldn't be involved in lighting calculations, i.e. you should just use the model-view matrix.
    I tried that too and it didn't work. It looks as if the light is coming from the camera, not from the red dot.

  4. #4
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by Rakehell View Post
    I tried that too and it didn't work. It looks as if the light is coming from the camera, not from the red dot.
    That doesn't change the fact that the projection matrix should not be used in lighting calculations.

    Using the projection matrix is just one of many possible mistakes. Just because you eliminate that particular mistake, it doesn't meant that there won't be others.

    Other common mistakes include: mixing coordinate systems (e.g. using a vertex position specified in one with a light or eye position specified in another), calculating the normals incorrectly (e.g. having them point inward rather than outward), transforming normals incorrectly (if you have non-uniform scaling or shear, the normal matrix needs to be the inverse of the transpose of the model-view matrix), failing to normalise vectors, performing dot products on 4D vectors (i.e. failing to divide by w first).

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    22
    It appears to be calculating in eye space and I'm not sure how to avoid doing that.

  6. #6
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by Rakehell View Post
    It appears to be calculating in eye space and I'm not sure how to avoid doing that.
    Calculating in eye space (with the model-view transformation applied) is fine (and fairly typical), so long as everything is eye space: vertex position, light position, eye position, normals.
    Calculating in screen space (with both the model-view and projection transformations applied) won't work with a perspective projection.

  7. #7
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    22
    My lighting shader looks something like this one, but the comments say the light position needs to be in eye coords.
    http://www.gamedev.net/page/resource...-shaders-r2861

    I have all of my positions in absolute space.

  8. #8
    Member Regular Contributor Nowhere-01's Avatar
    Join Date
    Feb 2011
    Location
    Novosibirsk
    Posts
    251
    Quote Originally Posted by Rakehell View Post
    I have all of my positions in absolute space.
    Code :
    dot( normalize( viewMatrix * Light1Position - vec3(modelViewMatrix * position)), normalize( normalMatrix * normal ) )
    try it like that.
    also, you can premultiply light position with view matrix before passing it to shaders.
    and storing position in world-space(or object-space?) is not very convenient.

  9. #9
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    22
    I subtracted the light position from the camera and now it looks better from some angles but not others.

  10. #10
    Junior Member Newbie
    Join Date
    Jul 2013
    Posts
    22
    Quote Originally Posted by Nowhere-01 View Post
    Code :
    dot( normalize( viewMatrix * Light1Position - vec3(modelViewMatrix * position)), normalize( normalMatrix * normal ) )
    try it like that.
    also, you can premultiply light position with view matrix before passing it to shaders.
    and storing position in world-space(or object-space?) is not very convenient.
    I'm googling how to calculate the view matrix, but it seems nontrivial.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •