First of all, we needed to render a shadow map, using a different camera (the view and projection of the light). Since it's supposed to represent sunlight, we're using a orthographic projection, simulating directional light.
We create the shadowmap by drawing every level element and simply returning a depthmap.
Shadowmap:
By then looking up the depth of the elements in the pixel shader (in the normal draw) and comparing them to the depth stored in the depthmap, we can determine if the pixel is in shadow or not
result:
Now we have 2 problems here:
1. the lookup isn't very accurate, because of some precision problems using floats
2. the edges of the shadows look too jaggy, we need to soften it a bit more.
The solution of the first problem: we have 4 channels (r,g,b,a) when we write to our shadow map, but we only use one of them. So, i splitted up the shadow depths in 3 parts, by first multiplying the value by 3; that way, we can use the channels like this:
red channel for the range (0-1)
green channel for the range(1-2)
blue channel for the range (2-3)
Then we get something like this:
the second problem was solved by blurring the lookup of the shadow map this was done by taking the average of surrounding pixels instead of just one pixel
result: