Advertisement Jump to content
Sign in to follow this  

OpenGL Lights in view space

This topic is 1767 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, I'm having some trouble figuring how to properly transform my vectors to perform lighting in eye space.

Right now I'm trying to do a directional light in glsl/ opengl.

What I'm doing now:

  • Transform light position by the inverse view matrix, as to transform it to eye space. This is sent as a uniform to glsl.
  • In vertex shader, transform normal by mat3(view) * mat3(world). (I know i could send this as a uniform)
  • The vertex in eye space is transformed by view * world and sent over the fragment shader along with the normal vector.
  • In the fragment shader, the vector from eye to vertex is calculated as just (-position).

Would really appreciate some help on this.

Share this post

Link to post
Share on other sites

I am sorry but I have been unable to follow your explanation of what you are doing... Anyway, let's see if I can help.


Regardless of where and when you do your lighting what matters is that all the elements involve are in the same space. In your case you want eye space, so let's do it that way smile.png


  • Get the light in eye space: (viewMat) * (worldMat) * lightPos <-- do it in the host program, since this doesn't change for each primitive
  • Get the vertex normal in eye space: traspose( inverse( mat3(viewMat) * mat3(worldMat) ) ) * normal <-- do it in the vertex program, since this changes per-vertex
  • Compute the lighting using whatever lighting model you want <-- do this in the fragment, if you want accurate per-pixel lighting.

Now: if you are totally sure that you are not using non-uniform scaling anywhere, you can transform your vertex normal with just mat3(viewMat) * mat3(worldMat) * normal.


By the way, of course the vector from the eye to the vertex is -position in eye space. Just don't forget to normalize it (and the others!) when computing your lighting in case you need it (highly likely).


EDIT: Info about the transformation of normals ->

Edited by Javier Meseguer de Paz

Share this post

Link to post
Share on other sites

Thanks, everything looks good now  if I calculate my direction as .(lightpos - position).

However, to get a directional light it's supposed to be calculated as just normalize(lightpos). but if I change the code to be like that, the light never really shows up, no matter how i rotate the camera.


EDIT: Never mind this, I got directional lights to work by realizing i was trying to transform it as a homogenous vector when it should be a regular vector, causing it to look wrong.

Edited by Doublefris

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!