Fog question

Started by
14 comments, last by MARS_999 17 years, 6 months ago
Quote:Original post by Exorcist
MARS_999, have you considered doing the fog as post-render fullscreen pass?

If you can capture the z-buffer to a texture, then you can use that in a fragment shader to computer distance fog on a per-pixel basis, as an overlay to the scene.


Interesting, I have just gotten a depthbuffer read into a FBO and have the scene rendered into a texture. So now do you want me to just render a single quad? like a overlay for UI or some other method? Thanks for the tip hopefully it works out like I need it to.
Advertisement
Exactly, if the FBO contains a copy of the depth buffer, pixel for pixel, then you can use it as a texture on a single fullscreen quad which stresses across the screen. Then in your shader, you output a colour where you pick the alpha based off the depth off each pixel. If you want to not colour over a skybox etc, just kill the fragment if the depth is 1.0.
Quote:Original post by Exorcist
Exactly, if the FBO contains a copy of the depth buffer, pixel for pixel, then you can use it as a texture on a single fullscreen quad which stresses across the screen. Then in your shader, you output a colour where you pick the alpha based off the depth off each pixel. If you want to not colour over a skybox etc, just kill the fragment if the depth is 1.0.


Hmmm think I am close but must be missing something. The quad is all foggy, but not what I was looking for. Maybe its my shader?

uniform sampler2D fogDepthmap;void main(void){	vec4 color = texture2D(fogDepthmap, gl_TexCoord[0].xy);	if(color.a >= 1.0)		discard;	gl_FragColor = color;}


Thanks

Update,
Exorcist I am not sure if I pointed this out but this is for a RTS sytle game and starting to think this method only works for FPS? Thanks again for the help so far.

[Edited by - MARS_999 on October 19, 2006 1:05:24 AM]
The problem is that you're not doing anything with the color you just read. If fogDepthmap contains the depth buffer, you'll need to use it's values to calculate how much fog you'll output. Right now you are just returning the depth buffer values.

You must return a constant fog color with an alpha value that is calculated based on the depth value stored on the depth buffer (the higher the distance, the higher the alpha value). You also need to turn on alpha blending before drawing the full screen quad.
Actually, it sounds like you most likely just aren't using the hardware fog correctly.

Volumetric fog *might* be what you're looking for, but from all your descriptions, it doesn't sound like it.



Ah, you say you're making an rts - is it an isometric type view (viewed from above for the most part)?
If so you might want to apply volumetric fog - but if you have the equivalent of a heightmap, you can always do a cheap hack by applying vertex fog based on vertex absolute height.
This would give a radiation fog type effect.
The exact math would be to perform a raytrace through the fog volume (which, in this case, would be a rectangular cube-like shape) and calculating the fog volume's near hit and far hit...

Actually, if you're not doing volumetric zones, this become *very* easy in a pixel (or vertex for that matter) shader. If the camera's absolute height is above the volume, calculate the distance to the height=fog volume height, using the view normal, then use the current pixel's z-buffer value (or vertex transformed xyz to find the distance). Subtract the dist to volume from the dist to vertex/pixel, and viola! nice murky foggy stuff :)


-M
I would like to thank everyone here for help so far. I would like to point out that I am not trying to add fog to my overall terrain but in Thr33d's pic where the sky line meets the terrain I am trying to cover that up with a fog wall if you will. I would also like to have the fog wall have a depth to the fog so you can barely see units/terrain in the distance. So far I have a wall that is in front of this skyline and where the terrain meet, but looks to old school where the fog wall hits the terrain where its sliced through at it looks like a hard line.

This topic is closed to new replies.

Advertisement