Common problems of shadow map in OpenGL

Shadow maps are a common technique in games,
The principle is to view the entire scene from the light space with the light viewing matrix (binding the depth cache with the frame cache), and the resulting scene is not rendered
The second drawing, the depth buffer into the shader, all point transformation to the light space, the z coordinates, and the depth of the original cache value contrast, if the resulting value is less than the value of the depth buffer is proved that the pixels in the most close to the rear of the light source location, so this point can’t render, reason for shadow covered place.
This is what I get, the light source is rotated around (0,0,0) with a radius of 50. In the middle is a physical model. The entire lighting system is the simplest (an improved version of Feng’s lighting).

Problem 1. Boundary problem: there is a plane in front of the light source, the space outside the plane is the area affected by light, and the area inside the plane is the area not affected by light.

The problem is very simple. In the light space analysis, the light space, like the view space, has a cone:

The areas outside the cone that are not located on the upper ledge are all clipped out of the light space.
To make the cone black outside the edge, since the second time the shader calls the depth cache, it will also call some areas in the depth cache, which will result in the wrong result on the boundary.
To solve this problem, when setting the depth cache, you need to set all colors outside the bounds to black :


And that will give you the right answer.
Problem 2. Texture error
Textures are rendered in striped red, blue and green colors.
This is because the texture is not formatted correctly, the image is formatted in RGBA format, but the texture is formatted in RGB format, which will result in the wrong result:

As shown in the figure above, setting the image format to GL_RGBA will solve this problem when reading the image from the library

Problem 3. Follow the tutorial, but there are no shadows, the whole scene is bright
If there is no compiler error in the shader, please check whether the two matrix Settings are consistent (that is, whether the matrix of optical space is directly passed into the second shader).
In the second shading, set the observation matrix to the light space matrix to see if the whole scene can be seen in the light space observation matrix.
If the whole scene is not visible in light space, the entire depth cache is infinite (i.e. 1.0F), and all the points are in front of the occlusion, so you will see the whole scene lit up.
Question 4. According to the tutorial to write, there is no shadow, the whole scene is dark
The problem may be that your light source is so close to the model that the whole scene is completely blocked by the model from the light space. Another possibility is that the first rendering of the depth cache was not written,
This also results in the equivalent of a large flat surface in front of you.
Question 5. Where there is shadow, there is light, and where there is light, there is shadow
The reason for this problem is that there is a logic error in the shader of the second rendering, and the shadow is 1, which means there is a shadow (for example in Learnopengl tutorial). Therefore, it is necessary to use (1-shadow) in the final calculation of Feng’s illumination.
Multiplied by stolid and ambient, not shadow
Problem six. A layer of gray, according to the tutorial to eliminate but not the effect
The gray is caused by the principle defect of Shadowmap. It is possible that multiple adjacent pixels of the rendered graph correspond to the depth value in the same depth cache, resulting in the wrong corresponding of the depth value, so there will be streaks.
If the tutorial minus the value of a fixed doesn’t work, minus the value can be more a little bit small. This means that you draw volume is very big, I draw when I was led to this situation, this is because the tutorial is a very small unit to draw, so it’s bigger than the offset, but I draw the unit is very big, (100.0 f) so I need to reduce the value of the more, you can test several times, each time reduce an order of magnitude, by the way, I use a value of 0.00001, this is a very small value, the results after the application of the this value is true.

There are some other problems, such as the resolution is not enough to lead to the edge is not clear and so on, this kind of problem can find some anti-aliasing method to solve, is to take the average edge.
If you have a problem can leave a message at the bottom, you can also find me to source.
(to be continued….)


Read More: