Adding shadows to the scene
Lighting is one of the most important operations performed by 3D applications. Unfortunately, due to the specifics of graphics libraries and the graphics hardware itself, lighting calculations have one major drawback--they don't have information about positions of all drawn objects. That's why generating shadows requires a special approach and advanced rendering algorithms.
There are several popular techniques targeted at efficient generation of natural looking shadows. Now we will learn about a technique called shadow mapping.
An example of an image generated with this recipe looks like:

Getting ready
The shadow mapping technique requires us to render a scene twice. Firstly, we render objects that cast shadows. They are rendered from the light's point of view. This way we store depth values in a depth attachment (color values are not required).
Then, in the second step, we render the scene as we normally do, from the camera's point of view. Inside shaders we use the...