Sign in to follow this  
KaiserJohan

Dealing with multiple lights in the shader?

Recommended Posts

KaiserJohan    2317

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? 

Share this post


Link to post
Share on other sites
C0lumbo    4411

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this