Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 15

Thread: My lighting is way off

Hybrid View

  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.

Posting Permissions

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