Shadow volumes Generation Algorithm

Source: Internet
Author: User

I am a beginner in 3D. I am not a technical encyclopedia. I just want to talk about what I want to say on my blog. I will briefly talk about the shadow generation technology at the fastest speed. Currently, there are generally three types of technologies: planar shadow, shadow ing, and shadow volume. The former is similar to projection, and the calculation is the simplest. The disadvantage is that they can only draw shadows shot on the plane; shadow Mapping uses the depth map generated from the position of the light source along the normal line of the light source to detect whether the pixel in the scene is in the shadow, the disadvantage is that the light source is relatively fixed with the object location, and the computation accuracy is poor in extreme cases, which is not suitable for Dynamic Time scenarios with accurate pixels; shadow volume is currently the most suitable technology for accurately displaying dynamic time scenarios, and has the widest applicability. Its typical application example is Doom 3, which lacks the fact that the shadow volume introduces additional vertices and surfaces, it increases the storage and processing strength, and the shadow rendered at the same time is relatively hard. If you want to implement Soft Shadows, you still need to cooperate with other technologies.

Here we will jump forward quickly, so we will not talk much about perspective projection, depth test, stencel buffer and other concepts. The general steps of shadow volume are: Generate shadow volume (mesh) and shadow rendering, and generate shadow volume.AlgorithmThere are two other methods: one is the method used in the d3d SDK sample. First, separate/interpolation the object surface, and then use vertex shader for acceleration, copy the unstretched shadow volume to the extrusion along the light direction. The other is the software Renderer method commonly used in most tutorial. First, all the smooth surfaces are selected, add all edges to a list. If an edge already exists in the list, the edge is not added. Delete the same edges in the list, in this way, after all the smooth surfaces are processed, only the edge information of the object to the smooth contour is left in the list, and the contour is extended along the light vector direction, then complete the quad interpolation and front and back sealing (if z-fail is used. For shadow rendering, four passes are generally used, specifically Z-Pass/Z-fail:

Z-pass Algorithm
1. First turn off the light source and render the entire scence. It is dark, but the depth value is obtained.
2. Disable deep writing and render the front of the Shadow. If the depth test is passed, the template value is added to 1.
3. Then render the back of the Shadow. If the depth test succeeds, the template value is reduced by 1.
4. Enable deep write for the surface with the last template value not 0 in the shadow.
5. Use the template method to re-render the scence of the light, so that the shadow part is black.
6. Its fatal disadvantage is that when the viewpoint is in the shadow, the template count error will occur.
7. At the same time, the template count error may be caused by the z- near clip plane approaching.

Z-fail algorithm (John Carmack's reverse)
1. First turn off the light source, render the entire scence, and obtain the depth value.
2. Disable deep writing and render the back of the Shadow. If the depth test fails, the template value is added to 1.
3. render the front of the Shadow. If the depth test fails, the template value is reduced by 1.
4. The surface whose last template value is not 0 is in the shadow. Enable deep write.
5. Use the template method to re-render the scence of the light. The shadow part does not render the color.
6. Note that this algorithm requires that the shadow volume be closed, that is, it must be sealed before and after
7. This method is not defect-free and may cause template counting errors due to the proximity of Z-far clip plane.

Noteworthy aspects
1. Because Z-pass does not need to be sealed, the speed is faster than Z-fail, but it cannot be processed.
2. Quake 3 seems to use Z-pass shadow volume and planar shadow
3. to ensure sufficient robust, make sure that there is at least one problem in Z-near/Z-far. NVIDIA recommends Z-fail, use W = 0 to implement an infinite Z-far plane
4. Remember to use Z-fail to seal, and the normal of each side of the Shadow volume must correctly point out of the object, including front cap and back cap.
5. the above is a simple process, the shadow is very hard, you can slightly change it: first render it with ambient light, then calculate the template, then use the template to render the scene, and finally deepen the shadow with Alpha blend.

Next we will start to map a large area. First, let's take a look at the specific example of Z-fail (Z-Pass is simple, so we will not give an example): Let's look at a single-sided situation facing the light source from the outside. In the figure, the shadow volume is displayed in the dotted box, and the circle is the point light source. The surface adfb and the surface def are back faces, the surface ABC, aced, and cbfe are three front faces (note that the vertex sequence here uses the left-hand series ), to illustrate the problem (mainly to illustrate how to avoid Z-fighting when calculating the shadow volume), I intentionally painted the surface ABC in a little position under the yellow triangle, in addition, the gray shadow is in common with the plane, while the surface def is a little bit under the plane.

First, perform the depth test of two back faces: the pink part is the area added with 1 due to Z-fail in the stencel buffer; then perform the depth test of three front faces: the highlighted green is the area in the stencel buffer that is reduced by 1 due to Z-fail.

At last, the red-green area is offset by positive and negative values. Only the gray Triangle Area on the plane is left in the stencel buffer, that is, the position of the source Image Shadow. The above is an example from a single aspect. It is worth noting that the single aspect is different from the two back-to-back sides. Is the latter's calculation result normal? Continue to view the chart:

The red and green lines represent two back-to-back sides, while the coarse lines constitute a Volume Shadow. The red and green lines represent the projection of the Red and Green sides along the light direction. Generally, back cap is in an infinite distance, while front cap is worth noting. To avoid Z-fighting, it should be located after the red and green sides. Maybe you will say, "Hey, here, the front cap can be directly equal to the red face. "Yes, this can be done when the red face is the back of the eye, but when the eye position changes and the red face is no longer the back, simply taking the red face as the front cap will lead to Z-fighting. The figure shows that the template count is correct when the eye is in the shadow. Next let's take a look at the situation where there is only one single side and in the shadow:

As long as front cap pays attention to the Z-fighting situation, the calculation result is correct. Interestingly, when the eye is in the shadow, the back of the triangle has been removed. Therefore, in actual rendering, there is no depth information on the back of the triangle, but even so, the Z-fail template count is still correct.

Finally, let's take a look at the shadow example of a calculated block. We can see that when the front cap is the back, the calculation of the Shadow volume is very simple, so we don't need to consider its Z-fighting situation, you can directly use the forward surface of the square as the front cap. However, when the front cap is the front, it still faces the problem that the position needs to be fine-tuned.

To sum up, using the Z-fail method to calculate the shadow of a plane without a thickness is slightly different from that of an object with a thickness. When calculating the shadow volume, be sure to avoid the Z-fighting at the front cap, and slightly adjust the front cap position to move the front cap a little backward along the light direction; or .... and so on, there is a more concise and elegant way, the "depth test failure" in the Z-fail algorithm is interpreted as the pixel depth "> =" the depth of the position, not just ">", and zbufferfunction is set to campare. less than campare. lessequal, in this way, it completely avoids the Z-fighting problem, and fine-tuning everything can be avoided, whether it is a surface or an object, in any situation, you can directly use the object to the front surface as the front cap .... faint... it's time to make it clear after writing... so much nonsense .... extremely depressing-_-B .... close.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.