Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am attempting to create a GLSL shader for simple, per-fragment directional light. So far, after following many tutorials, I have continually ran into the issue: my light is specified in world coordinates, however, the shader treats the light's position as being in eye space, thus, the light direction changes when I move the camera. My question is, how to I transform a directional light position such as (50, 50, 50, 0) into eye space, or, would doing things this way be the incorrect approach to the problem?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You have two options. The easiest in my opinion for a variety of reasons is to just multiply your light data by the view/camera matrix so that they're in view space.

The second option is that in your vertex shader you can generate outputs multiplied by only the model transformation matrix in addition to the usual fully projected outputs (similar to how you can generate outputs with only the model and view matrices to get outputs in view space).

Yet another option is to take the NDC-space data you have in the fragment shader and the inverse of your projection and view matrices to recalculate world-space coordinates. Just remember that you need to do the whole W divide step as NDC-space is more than just a transformation by the projection matrix.

I personally just calculate light data in view-space and add extra outputs to the vertex shader which are in eye-space, since it makes the math easier and slightly faster (you generally don't have enough lights to make the matrix multiplication on the CPU be a big deal, and with a deferred shader pipeline, you can do it on the GPU anyway).

share|improve this answer
    
So, by light data, I would understand that to be the direction; IE. doing modelview * light_pos on the CPU side, correct? –  Seven Hundred and Forty-Two Jun 29 '13 at 20:46
    
Not model view, but your camera matrix. The light had no relation to your model. You also need to transform the lights position for point and spot lights. –  Sean Middleditch Jun 29 '13 at 21:07
    
Ah! Does the camera matrix include the frustrum? –  Seven Hundred and Forty-Two Jun 29 '13 at 21:32
    
It's implicitly part of the concatenated camera and projection matrices. One quick Google search gives the following page, unsure how good it is: lighthouse3d.com/tutorials/view-frustum-culling –  Sean Middleditch Jun 29 '13 at 21:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.