rendering with multiple lights

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

Recommended Posts

I'm working on rendering a scene with more than one light for the first time, and I'm running into what I figure must be a simple problem. I'm doing everything brute force right now with a loop that looks like:
foreach light
foreach object
draw object with appropriate technique for light type

The problem is I'm doing this all into the back buffer, so every light I render basically overwrites the results from all the previous lights and I only see the result from the last one. There must be a standard way to handle this though, right? Do I need to draw each light into its own render target and combine them at the end? Or is there some way to set the back buffer (or any render target for that matter) to allow the colors from each pass to accumulate/add (rather than just replacing the whole pixel each time something draws to it). Thanks for any advice anyone can offer. [Edited by - My_Mind_Is_Going on March 6, 2010 5:49:08 PM]

Share on other sites
You need to turn alpha blending on and configure it for additive blending using D3DBLEND_ONE for DESTBLEND and SRCBLEND renderstates.

	m_pDirect3DDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );	m_pDirect3DDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_ONE );	m_pDirect3DDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );

Share on other sites
Thanks, that pretty much did the trick. There seems to be a problem now though where objects that are mostly in the dark are ending up partially transparent. I tried hacking my shaders to always output alpha of 1.0, but it didn't seem to make a difference. Any idea what could be causing this?

Share on other sites
Have you got a screenshot?

Share on other sites
First here's a screen shot near a light, everything looks fine:

Now in the dark things look like they're blending with each other. You can see the character and the box are both partially transparent and blending together.

Share on other sites
I'm afraid i can't think of anything off the top of my head. For debugging, i'd try outputting constant colour from the shader to be sure of what is being written to the target. I'd also try using D3DBLEND_ONE and D3DBLEND_NONE both ways around for the source and dest blending states to see if i always get one or the other.

Share on other sites
Are you shure, that you just render the light value?

I mean you could do it like this:

Render the unlit mesh, with textures and the other foo (With Alpha 1, totally visible)

For each light ->
Render the mesh again, but just with lighting, no Textures, Alpha=Lighting

Maybe thats what you want :)

Share on other sites
I'm not sure this is specifically the problem I'm seeing, but it doesn't seem like just turning on alpha blending and running through that loop I mentioned is the right approach. I don't think I want the results of a particular light's pass to be blending relative to itself.

For example lets say the back buffer is completely cleared, and I draw the ground mesh into it. Then I draw an object that's on top of the ground. Instead of it just replacing the existing pixels in the back buffer, now it's going to be adding to the ground pixels that it's really in front of (and should be completely overwriting).

What I think I need to do is draw each light's pass with no blending and then somehow blend the different passes together... but this would require having multiple render targets right?

I don't this is the problem I'm currently seeing though, because if everything was working as I understand it, I would just see a lot of blown out areas where too many things added in the same place. I'm not seeing that however, I'm seeing areas that unexpectedly seem to be doing a blend operation that isn't just final_color = source_color + dest_color.

EDIT: On second thought, maybe that is what I'm seeing. Areas where two things are drawing on top of each other appear brighter, so it looks like the object in the back is showing through.

Share on other sites
uhm... Did you try to disable your zBuffer before drawing the Light-Stuff?

Share on other sites
Quote:
 Original post by WuTzuhm... Did you try to disable your zBuffer before drawing the Light-Stuff?

I'm not sure I understand how that will help. The problem is that I have too many things passing the depth test and adding their color to the back buffer. I need to have only the front-most objects actually add and everything else do nothing.

Maybe an early z-pass can help so everything fails the depth test except the thing that's actually supposed to draw at each pixel.

I guess what I'm wondering more than anything (since I can probably always cook up something that will work) is what the standard technique is to render multiple lights.

Share on other sites
Yeah, looks like an early z-fill pass did the trick. Anyway, thanks for the help guys.