Sign in to follow this  

Multiple Lights - How to design?

This topic is 1930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!

I'm currently learning Direct3D 11 via the book 3D Game Programming with DirectX11 from Frank D. Luna.

I just finished the chapter about lighting and I was wondering myself how I could implement a system that supports multiple lights.

The current approach I find most straight-forward is to have an array of current active lights.
Whenever an objects gets drawn, it first gets drawn with its initial vertex and pixel shader and then gets redrawn for each light
in the array. The system now replaces the pixelshader of the object with a pixelshader for a specific light type (e.g. if the current light is a spotlight, set the spotlight pixelshader - and so forth) - the vertex shader stays untouched.

The question I ask myself is, if that is even a reasonable approach?

That system doesn't even work very well as all the pixels that gets redrawn via the light shaders are getting rejected by the depth test. I assume that is because the object is redrawn at the exact same position as it was drawn before and the depth of the new pixel has the same depth as the old pixel (is that correct?). The solution for this would be to disable depth testing, but as I said before I don't know if that is even a reasonable approach.

Can someone fill the gap in my mind and give me an advice or two on how to implement multiple lights well?

Thanks in advance. Edited by FelixK15

Share this post


Link to post
Share on other sites
You don't need to redraw your objects for every light.
You can follow this algorithm:

[source lang="java"]Color finalColor;
for every light
if(light is spotLight)
finalColor += shadeSpotLight(currentLight);
else if (light is omni)
finalColor += shadeOmniLight(currentLight);
...[/source]

This should be implemented in your pixel shader.

When you became familiar with shaders You can use a Deferred Lighting technique which is much more appropriate for multiple lights.

Share this post


Link to post
Share on other sites
What you're describing is called "multipass lighting", and it's definitely feasible. A lot of games have used it, including pretty much every UE3 game. While it's easy to implement, the downside is that you waste a lot of performance re-rendering the mesh for each. On DX11 hardware you have a lot of flexibility, and you can use that do something more optimal. Like the above poster mentioned you can loop over lights in the pixel shader, or you can use deferred rendering.

Share this post


Link to post
Share on other sites
Thanks for your replies!

I read a bit about deferred rendering, but being new in the field of graphic programming and shader programming I think its a bit too heavy to begin with.

But what happens to the initial pixel shader if I replace is with the "lighting pixel shader"? Wouldn't it be reasonable to draw the geometry with its initial pixel shader and then redraw it with the "lighting pixel shader" and then blend those two?

Share this post


Link to post
Share on other sites
Sign in to follow this