Fog with multipass lighting

Started by
6 comments, last by Galshin 17 years, 8 months ago
I have a multipass lighting engine, and I am having trouble implementing fog. The only way I have come up with is to render the distance for each pixel into a texture, then after the scene is render do a post process effect that adds the fog color based on the distance written into that initial texture... I do this because you can't just compute the fog value in each light pass, because the effect gets added... and it doesn't look right at all. There are problems with this: 1) Alpha polygons don't get fogged right... e.g. if there is a glass window in front of you, the fog value at that point is really close to the camera, and therefore not fogged... BUT because its a post-process effect, all objects behind the window are also not fogged, even if they are really distant. 2) Additive polies that don't write to the zbuffer will get fogged with whatever depth is written by the objects behind them... so if you have an additive particle effect right in front of you, but the only geometry behind it is really far away... the particles will get fogged.. How are people doing fog with multipass lighting??
Advertisement
NM... I figured it out :p
I've actually come across this problem myself and haven't really found a good solution. The best thing I can suggest is to consider your level design. Beyond that, it seems like you'd actually have to start peeling the scene, rendering (entirely, lighting and all) each slice at a time. This still presents problems, though (intersections within that slice, for instance, you'd actually have to intelligently slice the scene via something like the geometry itself).
Ooooh, ya replied right as I was typing. :D How did you solve it?
Here's the way to do fogging in a multipass scenario:

Each pass after the first one in a lighting shader will most likely be additively blended over the first, so for each subsequent pass, make the fog black, with the same settings(you also try moving the fog in a bit closer for these passes). This will fade out each additive pass in the distance, so the correct fog color will be visible. Additionally, you could also fade out the color using the pixel shader.

Quote:
Here's the way to do fogging in a multipass scenario:

Each pass after the first one in a lighting shader will most likely be additively blended over the first, so for each subsequent pass, make the fog black, with the same settings(you also try moving the fog in a bit closer for these passes). This will fade out each additive pass in the distance, so the correct fog color will be visible. Additionally, you could also fade out the color using the pixel shader.

What happens when you want to turn fog off? Or when you want to try out different fog equations? I think you will need to alter the shaders in case to do that with this method. This is because, if you are using fragment programs, you have to do the fog by hand inside the shader. Am i missing something?

I think the easiest way of doing fog in a multipass renderer is by adding one more pass. Fog usually is (i think) a linear interpolation between fog color and the actual scene color. The only thing that changes is the way you calculate the fog factor.
If this is the case, then you can render the scene one more time with alpha blending (SRC_ALPHA, ONE_MINUS_SRC_ALPHA) where source alpha is the fog factor for the vertex/pixel and source color is the fog color for the vertex/pixel. dest color is the scene color. This way you can switch between different fog equations by altering this pass, instead of altering every lighting shader.

Maybe i'm wrong on that. We haven't implemented fog in our engine yet, but this is the way we are going to do it. Do you see any problems with this approach?

HellRaiZer
HellRaiZer
I dont think another pass is a good idea...

I have found my method to be the best way.
I solved it the way Matt described... works really well.

This topic is closed to new replies.

Advertisement