• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Lights in view space

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Doublefris  Members

Posted 20 March 2014 - 08:06 AM

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.

### #2Javier Meseguer de Paz  Members

Posted 20 March 2014 - 10:01 AM

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

• 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/

Edited by Javier Meseguer de Paz, 20 March 2014 - 10:04 AM.

“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

### #3Doublefris  Members

Posted 20 March 2014 - 02:19 PM

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, 20 March 2014 - 03:47 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.