Fragment lighting

I think all of the glsl examples of fragment lighting I’ve seen use a fixed point light source.

I need a fragment shader does performs lighting with a variably positioned light.
My understanding of lighting calculations requires me to calculate the vector between the fragment position and light position.

the problem is gl_FragCoord is in screen space while the lightSource[i] is in object space.

anyone have any hints/pointers/code that can peform fragment lighting with a moving light?

thanks.

The vector from the surface to the light can be linearly interpolated as long as you don’t normalize it. Hence, you can calculate it in the vertex shader and normalize it in the fragment shader. Once you do this, you don’t need to know the fragment position in the fragment shader anymore.

If you really do need the position for something, just use the same trick: calculate it in the vertex shader and interpolate it.

– Tom

The vector calculated in the Vertex Shader will be the vector from the light to that vertex.

are you saying glsl automatically interpolates all varying variables on their way to the fragment shader?

[EDIT]

wow, look at that…the vector does get interpolated.
this changes a lot. Can’t believe i didn’t know that.

hoo-wa. thanks a lot Tom.

Originally posted by Aeluned:
[b]The vector calculated in the Vertex Shader will be the vector from the light to that vertex.

are you saying glsl automatically interpolates all varying variables on their way to the fragment shader?
[/b]
Yep… all varying’s are interpolated and perspective corrected.

yooyo

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.