Fog of War

Hi, atm I am implementing a fog of war system like in wc3 and other RTS games.

I added another texture to the terrain so I am able to draw directly over the other terrain textures.

When a doodad should be darkend I simply change the ambient color to black.

This works but I want the fog to fade in/out when a unit walks away and not that the fog is directly removed. (this looks ugly)

Any ideas?

Just use an RBGA texture, which is black by itself and adjust the alpha value according to the units distance. You just need to think about how to exactly determine which unit removes the fog to which amount etc.

I would need to redraw the texture manually every frame but I think this would be a slow solution.

Maybe there is a way to add a fading feature to the material?

Why you have to redraw it? Just update it when the fog changes… This you will inevitably have to do…

@ogerlord said:
I would need to redraw the texture manually every frame but I think this would be a slow solution.
Maybe there is a way to add a fading feature to the material?

actually you don't have to fade at all, just use a very downsized texture like 32x32 or 64x64 (black and white, in modulate mode). white parts will be transparent.

The default mag filtering for a texture is bi-linear so if you map a very small texture on a big area, each pixel of the texture will be smoothed and will produce something like the effect you're looking for

Here is a picture of how id do something similar in one of my project.
I use a 64 x 64 map to color cells on a gird
http://i.imgur.com/2Mo6y.jpg
As you can see the cell color is very smooth because of the filtering.
Also having a small map use less memory and its creation cost is lighter.

This won’t be slow, believe me. If you are afraid of the performance, then update your fog only every x’th frame or sth.

Just update it when the fog changes

But every time a unit walks the fog changes and therefore I need to change some pixels. Atm I dont update the fog every frame but this cant look smooth.

The default mag filtering for a texture is bi-linear so if you map a very small texture on a big area, each pixel of the texture will be smoothed and will produce something like the effect you’re looking for

Thats not my problem. When I change a pixel the fog directly dissapears instead of a fading out.

Btw I tried updating the fog every frame and it works but now it is a bit laggy. :/

Well then you do it wrong, i.e. in a slow way. I also use something like this and it works very fast for me.



A naive solution from my head which should work rather well:

Start out with a black texture

for every unit you have

set the alpha value at the units position in the texture to fully transparent

compute in a circle around the unit a falling transparency value and add it to the corresponding value in the texture



Tadaaaa and you will automatically have fading

ohhhhhh ok i didn’t get your issue.



Split your map in areas (grid or what ever), each time a unit is approaching a fogged area the area reveals entirely.

If you update the texture each time a unit walks, yeah i guess you’ll have performance issues.



For your fading out issue, what you could do is use 2 textures and alternate them, each time an area is revealed you render a new texture of the fog, apply it over the old texture, then smoothly lower the alpha of the old texture over few frames until it completely fades out, revealing the new texture.

Well then you do it wrong, i.e. in a slow way. I also use something like this and it works very fast for me.

Or my fogmap was with 512*512 pixels to large. :)
Atm I have an 2d array with the fog states and every third frames I check the areas which have changed and add them to a list that these pixels should fade. If someone is intrested I can post my code.


The default mag filtering for a texture is bi-linear so if you map a very small texture on a big area, each pixel of the texture will be smoothed and will produce something like the effect you’re looking for

Is it possible to increase this effect or to draw a circle for each pixel instead of a smoothed square?
@ogerlord said:
Is it possible to increase this effect or to draw a circle for each pixel instead of a smoothed square?

well it will always be a smoothed square whatever you do.

Just paint the texture (i.e. fill the byte buffer with the right values) in another thread and shove it to the openGL thread to update it. It should work perfectly without stuttering for your 512x512 texture.