Multiple Lights - How to design?

Started by
2 comments, last by FelixK15 11 years, 7 months ago
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.

Visit my blog, follow me on twitter or check out my bitbucket repositories.

Advertisement
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.
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.
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?

Visit my blog, follow me on twitter or check out my bitbucket repositories.

This topic is closed to new replies.

Advertisement