Stencil Buffering and Shadow Body "go"

Source: Internet
Author: User

First, the template buffer

There are two types of actions associated with template buffering-comparison and update operations.

1. Compare operations

Stencil Test compares the values in reference and stencil buffer with the following formula:

(stencil Ref \ &mask) OP (stencil Buffer \ &mask)//left and right order not reversed

The corresponding commands in DX and OpenGL are:

Glstencil (Cmp_fun, ref, Mask) and Device->setrenderstate (D3drs_stencilfunc, Cmp_fun)

One of the Cmp_fun is the "OP" in the formula, the actual "" "," "", "〉=", "〈=" and other comparative operations. The so-called stencil test is the OP operation. The result of the OP operation corresponds to the state of the stencil test (stencil test has only pass, fail two states), the corresponding relationship is as follows:

OP results Stencil Test

True Pass

False fail

When OP is set to Always (Dx:d3dcmp_always | opengl:gl_always) operation, stencil test is always pass, regardless of the current value of stencil ref and stencil buffer.

2. Update operations

Stencil test has pass/fail two results, Z-buffer test also has pass/fail two results. An update to the value in stencil Buffer requires that you also indicate what kind of update is performed under what state (in fact, a total of four states combined by the result of the stencil test and Z-buffer test results 22). The update operation command format is as follows:

Dx:device->setrenderstate (State, OP)

Opengl:glstencilop (OP1, OP2, OP3)

OpenGL DX (state value)

Stencil Test pass Parameter op3 d3drs_stencilpass

Stencil Test fail parameter OP1 d3drs_stencilfail

Z Buffer Test fail parameter OP2 d3drs_stencilzfail

OpenGL (value of OP1/OP2/OP3) DX (value of OP)

Gl_keep D3dstencilop_keep

Gl_zero D3dstencilop_zero

Gl_replace d3dstencilop_replace//Replace the value of the corresponding position in the template buffer with the reference value

GL_INCR D3dstencilop_incrsat

GL_DECR D3dstencilop_decrsat

Gl_incr_wrap D3DSTENCILOP_INCR

Gl_decr_wrap D3DSTENCILOP_DECR

Note the two concepts of distinction-"Update the color buffer value after stencil test passes" and "update the template buffer value after stencil test passes".

3. Stencil Reference

Stencil reference has two main functions:

One is used as a value for comparison in the comparison operation, and the other is used in the update operation to replace the corresponding data (gl_replace/d3dstencilop_replace) in the template buffer.

4. Z-buffer Test and Stencil test sequencing

In OpenGL depth test (Z-buffer test) is after stencil test, see.

In DirectX, Z-buffer test is before stencil test, see. Here is DX9 data, DX11 after the rendering pipeline has a large change, do not know whether this order has changed.

Second, shadow Body (Shadow Volume)

The technique of projecting object projections through shadow bodies is described in many places, but is not intuitive enough. The technique of combining the shadow body (Shadow Volume) with the stencil test to form the shadow of an object is one of the most common methods, and is now combined to make a brief introduction.

Suppose that there is an object OB which, under the action of the light source, forms its own shadow W on the plane p, and the shadow in the corresponding area of the screen window s is w ' (for example). At this point the light source L with the OB intersection of the edge illumination direction extension, and the OB's back surface will form a bevel vl. The basic idea of Shadow volume is this: Mr. Mister becomes a VL entity and projects it onto S, but does not actually draw it (that is, when the VL is projected onto S, it does not really update the corresponding color buffers), but only by doing this to set the W ' on s The value of the template buffer corresponding to the zone, while the value in the template buffer corresponding to W ' is distinguished from the value in the template buffer corresponding to the other region. This allows the W ' area on s to be blackened to form the final shadow in the next process with the help of a template buffer.

The difference between the Stenclil buffer value of the W ' region and other regions through the projection process of the VL is based on the idea that a line of sight V1 with the VL front (facing the observer or camera) intersect at point B, and then intersect with the back of the VL (to the observer or the camera) at point C, Then there is no other object in the area V1, so VL does not have a shadow projection here, which means that the color on the intersection of V1 and S will be determined by the other objects in the scene; if there is another line of Sight v2 intersects the VL front at point E, The intersection with the plane p before the back of the VL intersects the point F, which means that point f must be in the shadow of the OB, then the color of the intersection d of V2 and s must be shaded. The following technical means are the realization of this principle.

First the front of the VL (for the observer or the camera) is projected on s (without updating the color buffer value), the template buffer value corresponding to the corresponding area of s (not drawn in) is added one at a time, and then the back of the VL (away from the viewer or camera) is projected again on s (without updating the color buffer value). At the same time, the template buffer value corresponding to its corresponding area on s (not drawn in) is reduced by one. Of course, all other objects in the scene are also rendered to s on this process.

When rendering the plane p, the test and write functions of the z-buffer are turned on, and when projecting the positive and back of the VL, the Z-buffer test function is turned on while the write function is turned off. This allows the values in the Z-buffer to be not updated when the VL is projected onto the s, while the plane p updates the Z-buffer value at render time. On the path that the V2 passes, since the plane p is in the triangles that make up the back of the VL, the triangles that make up the back of the VL are discarded during the Z-buffer test phase, so A reduction of the template buffer value for the corresponding area attached to the VL back projection operation is automatically canceled, resulting in the template buffer value corresponding to the F point being unchanged after the addition of an operation (in the case of a VL front projection), and in the path passed by the V1, because there is no other object barrier, Adding one to the template buffer, minus one operation will proceed as expected. In this way, the corresponding template buffer values for the W ' area and other regions are distinguished.

(Note that DX and OpenGL differ in order of stencil test and Z-buffer test, but both of the stencil Buffer values are set after Z-buffer Test and stencil test are complete.) )

Stencil Buffering and Shadow Body "go"

Related Article

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.