# draw fog of war using shaders

I am making a RTS game, and I'd like some advice on how to best render fog of wars, given what I'm already doing.

You can imagine this game as being a classic RTS like Age of Empires 2, where the fog of war will basically be handled by a 2D array telling if a given "tile" is explored or not. The specific things to consider here are :

1) I'm only doing a few draw calls to draw the whole screen, using shaders, and I'm not drawing "tile by tile" in a 2D loop

2) The whole map is much bigger than the screen, and the screen can move every frame or so

In that case, how could I draw the fog of war ? I have no issue maintaining on the CPU-side a 2D array giving the fog of war for each tile, but what would be the best way to actually display it dynamically ? thanks!

• Something I experimented with when another dev asked about fog of war ( gamedev.stackexchange.com/questions/71934/… ) was to create a mesh plane over the scene whose vertex alphas corresponded to visibility values. I then combined those with texture animation in the shader to create animated feathering at the edges of the fog. If that method may be of interest, I can expand it into an answer. – DMGregory Jun 1 '14 at 19:57

I'm not very sure of what type of fog you want to display, but if you want to do an AoEII fog, so a black fog, it would be something like that: And your code should be like that (it's pseudocode). Here I blit tile by tile but you can replace with your own technical:

Let fog[nTileX][nTileY] be your fog array
// values: 1 discovered tile and 0 undiscovered

For i = 0; i < nTileX; i++
For j = 0; j < nTileY; j++
If fog[i][j] == 1 Then
Blit your tile at pos (i, j)
Endif
Endfor
Endfor

For i = 0; i < nTileX; i++
For j = 0; j < nTileY; j++
If fog[i-1][j] == 0 Then
If fog[i][j-1] == 0 Then
Blit fog tile number 8
Elseif fog[i][j+1] == 0 Then
Blit fog tile number 5
Else
Blit fog tile number 1
Endif
Endif
If fog[i+1][j] == 0 Then
If fog[i][j-1] == 0 Then
Blit fog tile number 6
Elseif fog[i][j+1] == 0 Then
Blit fog tile number 7
Else
Blit fog tile number 3
Endif
Endif
If fog[i][j+1] == 0 Then
Blit fog tile number 2
Endif
If fog[i][j-1] == 0 Then
Blit fog tile number 4
Endif
Endfor
Endfor


If you want the tiles you already explored but where you are not present to be darkened, and with the building you saw when you explored, you can take a screenshot at each frame, and each new frame you add an alpha filter to it, and then you blit it after filling the screen of black.

• A fog tileset, quite cleaver. Probably how they did in AgeII. – glampert Jun 2 '14 at 1:15