Introduction
Shadow map is a technique that generate shadow. By using a frame buffer to store a texture that indicate depth from the light position, we should able to compare the distance between the fragment in the camera and the fragment depth in the depth map. If the distance after the transformation is longer than the depth on the depth map, that means the fragment is below some object from the “light perspective”.
Implementation
Think the light as a camera. We need a matrix that same as the View matrix to transform everything from the world space to the “Light Space” to get a view from the camera. Think about a space that origin point is at the light and the -Z axis is point towards from the light position to the objects. You can generate this matrix by using the LookAt function from the GLM or us a “cheat” technique that using the Y axis(pointing upward) and the Looking at direct to calculate the X axis that in the Light Space(using cross product) in world space(The X base of the Light space defined in the World Space). Than using the X axis(Light Space) and the -Z axis(Light Space) to get the Y axis in the world space. Than, using those three vector as base to construct a matrix that transfer from the world space to the Light Space.
The second step is using a frame buffer to store the depth that in the light space. After that, we need compare the fragment depth in the view space(transfer the vertex to the light space and compare) to the fragment depth of the light space. If the distance is way further(avoid alias error) than the distance, that means that fragment should be cover by a shadow.