Lights in view space

Started by
1 comment, last by Doublefris 10 years ago

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.

Advertisement

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 -> http://www.lighthouse3d.com/tutorials/glsl-tutorial/the-normal-matrix/

“We should forget about small efficiencies, say about 97% of the time; premature optimization is the root of all evil” - Donald E. Knuth, Structured Programming with go to Statements

"First you learn the value of abstraction, then you learn the cost of abstraction, then you're ready to engineer" - Ken Beck, Twitter

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.

This topic is closed to new replies.

Advertisement