Dealing with multiple lights in the shader?

Started by
0 comments, last by C0lumbo 11 years ago

Hello,

Right now I only deal with one light, a directional one (like described in http://www.arcsynthesis.org/gltut/Illumination/Tutorial%2009.html). I provide a Vec4 'lightIntensity' and Vec3 'directionToLight' to the vertex shader and proceed to apply lightning. Not very flexible and soon I want to add more lights and of different types aswell.

So my question is what is the best practice when dealing with multiple lights and of multiple lightning types?

Advertisement

Three common solutions:

1. Use deferred shading.

2. Compile permutations of your shader for each combination of light states (beware, you can end up with a huge number of shaders this way)

3. Use for-loops and if-branches in your shader to handle the various lighting states.

If you're targeting desktop hardware, then #1 is best but #3 is fine - you might read about branches being expensive but on any vaguely modern desktop hardware branching on a uniform is pretty cheap. If you're targeting mobile, I'd recommend #2 but you might have to take steps (i.e. make assumptions) to keep the permutation count down.

This topic is closed to new replies.

Advertisement