Shadowmap & shadowvolume

Source: Internet
Author: User

Shadowmap]

The reason why an object is in a shadow is that there is a shadow between it and the light source, orThe distance between the shadow and the light source is closer than that of the object.This is the basic principle of the Shadow ing algorithm.

Pass1: rendering the entire scene in the light source or coordinate system.Obtain the depth map of all objects relative to the light source.(That is, the shadow map), that is, the value of each pixel in this image represents the depth value of fragment closest to the light source in the scene. In this pass, we are only interested in the depth value of the pixel, so we can turn off all the illumination calculations and open the render state of Z-test and Z-write.

Pass2: restores the viewpoint to its original normal position, renders the entire scene, calculates the distance between the viewpoint and the light source for each pixel, and then compares the value with the corresponding value in depth map, to determine whether the pixel point is in the shadow. Then, based on the comparison results, different illumination calculations are performed for shadowed fragment and lighted fragment respectively, so that the shadow effect can be obtained.

From the above analysis, we can see that the rendering of depth map is only related to the position of the light source and the position of the object in the scene, no matter how the viewpoint moves,As long as the relationship between the light source and the object remains unchanged, the shadow map can be reused.Therefore, shadow ing is a wise choice for scenarios without dynamic light sources.

Shadowvolume]

1. Z-pass algorithm.

Z-Pass is a standard algorithm used to determine whether a pixel is shadow volume. The principle is:

Pass1: Enable Z-buffer write: renders the entire scene and obtains the depth map of all objects. Note that the difference between depth map and shadowmapping is that the depth map in shadow volume is obtained from the actual viewpoint, whileDepth Map is a light source..

Pass2: Disable Z-buffer write, enable stencel buffer write, and then render all shadow volume. For the front face of shadow volume (both the front face of the viewpoint ),If the result of depth test is pass, Then the value of stenpencil corresponding to this pixel is added with 1, otherwise the value of stenpencil remains unchanged. For the back face of shadow volume (far from the viewpoint side ),If the result of depth test is pass, Stencel value minus 1, otherwise it remains unchanged.

In a simple sentence, the algorithm of Z-Pass is to draw a line of sight from the point of view to the object. When this ray enters shadow volume, the value of stencel is increased by 1, when this ray leaves shadowvolume, the value of stencer is reduced by 1. If the stencel value is zero, it indicates that the number of times the shadow volume enters and leaves is equal. Naturally, it indicates that the object is not in shadow volume.

  

Pass3, otherwise, it is outside the shadow volume, and then the shadow effect is drawn accordingly.

2. Z-pass problems.

The above discussion is based on the situation where the viewpoint is outside shadow volume. When this condition can be met, the Z-pass algorithm works well. However, once the viewpoint enters the shadow volume, the Z-pass algorithm will immediately become invalid.

  

3. Z-fail algorithm.

The Z-fail algorithm was independently invented by John Carmack, Bill Bilodeau, and Mike songy to solve the failure of the Z-pass algorithm after the viewpoint enters shadow volume.

Pass1: Enable Z-Write/Z-test. render the entire scenario and obtain the depth map. (This step is exactly the same as that of Z-Pass)

Pass2: Disable Z-write, enable Z-test/stenpencil-write. Render shadow volume. For its back face, ifThe result of Z-test is fail., Stenpencil value plus 1. If the result of Z-test is pass, the stenpencil value remains unchanged. For front face, ifThe result of Z-test is fail, and the stencel value is reduced by 1.If the result is pass, the value of stencer remains unchanged.

  

The purpose of the Z-fail algorithm is to solve the failure of the Z-pass algorithm after the viewpoint enters shadow volume.

Reference: http://blog.163.com/wmk_2000_ren/blog/static/138846192201019114117466/

 

  

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.