The advantages of the Board buffer in OpenGL)

Source: Internet
Author: User

Technical Basis

 

Stenciling mask
1. pixel-by-pixel testing similar to deep Buffering
2. When compared with the value in the masked buffer, the test fails.
3. In the following cases, specific operations will be performed:
-Mask test failed
-An error occurred while performing the deep test.
-Deep test passed
4. Provides meticulous control over object and element updates.

 

Related APIs in OpenGL
Glenabe/gldisable (gl_stencil_test );
Glstencilfunc (function, reference, mask );
Glstencilop (stencil_fail, depth_fail, depth_pass );
Glstencilmask (mask );
Glclear (... | gl_stencil_buffer_bit );

 

Requirements for mask buffer zone
If the mask is used, a sufficient number of digits are required.
The implementation should support a mask ranging from 0 to 32
8, 4, 1 is usually available
Easy to use for glut programs:
Fig );
Valley createwindow ("stencel example ");

 

Mask Test
The test is to compare the reference value with the masked buffer value of the pixel.
Comparison functions are the same as those for deep testing:
Nerver, always
Less, lequal
Greater, gequal
Equal, notequal

 

Bit mask control comparison process
(Ref & Mask) OP (svalue & Mask ))

 

Mask operation
Influence on mengban
-Mask test failed
-An error occurred while performing the deep test.
-Deep test passed
Possible operations
-Add, subtract (saturates)
-Add, subtract (WRAP, dx6 option)
-Preserve and replace
-Clear 0, reverse
This is how the mask buffer value is controlled.

 

Mask buffer write mask
Bit mask controls how the mask value is written back into the mask buffer zone
It is also used to clear
Compared with the write mask, the mask value can be considered as multiple subareas.

 

Dx6 d3d API
Functions exactly the same as OpenGL
Setrenderstate Control update:
-D3drenderstate_stencilenable
-D3drenderstate_stencilfunc
-D3drenderstate_stencilref
-D3drenderstate_stencilmask
-D3drenderstate_stencilwritemask
-D3drenderstate_stencilfail
-D3drenderstate_stencilzfail
-D3drenderstate_stencilpass

 

Dx6 topic
Remember the potential bit of the query!
Add a depth/mask interface (Interface)

 

Performance
In today's 32-bit graphics acceleration mode, the 24-bit depth plus the 8-bit mask is encapsulated in the same memory word.
Such as Riva TNT
Performance tips: if you use a deep test, the masked test will not bring any negative news.
Do not think that the mask test is "expensive". In fact, if you have already conducted a deep test, the mask is "free ".

 

Plane reflection:

 

Draw an object twice, and use glscalef (1,-1,-1) for the second reflection on the plane
The second painting can only be performed on the inside of the reflector. the excess part should be laid, and the mask buffer zone should be used.
1. Mask 0.
2. Set the mask to 1 to draw a reflection plane polygon.
3. Draw a reflection image only at the top of the mask.
This idea of plane reflection can be used recursively. This requires more masked positions.

 

Plane shadow: Creates a shadow transformation matrix.
Principle
1. The equation of the ground plane is AX + by + cz + D = 0, and the shadow of the light source at the origin, a little space (sx, Sy, SZ, 1:
Point coordinates on the straight line between the origin point and the spatial point are (N * Sx, N * Sy, N * SZ, 1 );
The intersection of this line and the ground plane is (M * Sx, M * Sy, M * SZ, 1), and it should satisfy the ground plane equation, so
M (A * SX + B * SY + C * sz) + d = 0; then M =-D/(A * SX + B * SY + C * sz );
Intersection coordinate {-D/(A * SX + B * SY + C * SZ} (sx, Sy, SZ)
Transformation Matrix
-D, 0, 0, 0
0,-D, 0, 0
0, 0, 0,-D,
0, 0, 0,-d
2. the light source is infinitely far away, and the direction is D (dx, Dy, Dz). The space is a bit of S (sx, Sy, SZ, 1) shadow:
This line is S + nd, and the intersection with the ground plane is:
A (SX + N. DX) + B (SY + N. dy) + C (SZ + N. DZ) + d = 0 --- n =-(. SZ + B. SY + C. SZ + d)/(dx + dy + Dz)
Intersection is (sx, Sy, SZ) + N * (dx, Dy, Dz)
Transformation Matrix
-D, 0, 0, 0
0,-D, 0, 0
0, 0,-D, 0
0, 0, 0,-d
Code:
Void shadowmatrix (glfloat shadowmat [4] [4], glfloat groundplane [4], glfloat lightpos [4])
{
Glfloat dot;
/* Point product of the light source vector and the plane normal vector */
Dot = groundplane [x] * lightpos [x] +
Groundplane [y] * lightpos [y] +
Groundplane [Z] * lightpos [Z] +
Groundplane [w] * lightpos [w];
Shadowmat [0] [0] = dot-lightpos [x] * groundplane [x];
Shadowmat [1] [0] = 0.f-lightpos [x] * groundplane [y];
Shadowmat [2] [0] = 0.f-lightpos [x] * groundplane [Z];
Shadowmat [3] [0] = 0.f-lightpos [x] * groundplane [w];
Shadowmat [x] [1] = 0.f-lightpos [y] * groundplane [x];
Shadowmat [1] [1] = dot-lightpos [y] * groundplane [y];
Shadowmat [2] [1] = 0.f-lightpos [y] * groundplane [Z];
Shadowmat [3] [1] = 0.f-lightpos [y] * groundplane [w];
Shadowmat [x] [2] = 0.f-lightpos [Z] * groundplane [x];
Shadowmat [1] [2] = 0.f-lightpos [Z] * groundplane [y];
Shadowmat [2] [2] = dot-lightpos [Z] * groundplane [Z];
Shadowmat [3] [2] = 0.f-lightpos [Z] * groundplane [w];
Shadowmat [x] [3] = 0.f-lightpos [w] * groundplane [x];
Shadowmat [1] [3] = 0.f-lightpos [w] * groundplane [y];
Shadowmat [2] [3] = 0.f-lightpos [w] * groundplane [Z];
Shadowmat [3] [3] = dot-lightpos [w] * groundplane [w];
}

 

Basic shadow rendering method:
/* Render 50% black shadow color on top of whatever
The floor appearance is .*/
Glable (gl_blend );
Glblendfunc (gl_src_alpha,
Gl_one_minus_src_alpha );
Gldisable (gl_lighting);/* force the 50% black .*/
Glcolor4f (0.0, 0.0, 0.0, 0.5 );
Glpushmatrix ();
/* Project the shadow .*/
Glmultmatrixf (glfloat *) floorshadow );
Drawdinosaur ();
Glpopmatrix ();
Not so easy 1
Obviously, the above Basic Drawing code is not masked.
This will lead to a doubling of the mixing phenomenon, so that black blocks appear inside the gray shadow. This is generated when some shadow pixels are drawn multiple times.
The solution is to use the mask to record the pixels that have been drawn:
1. Clear mengban 0
2. Draw a ground plane with a mask of 1
3. If the mask is 1, draw a shadow.
4. If the shadow passes the mask test, set the mask to 2.
Therefore, the double mixing disappears.
Not so easy 2
Even if double mixing is eliminated, other problems may occur.
The depth buffer Z value conflicts with the object shadow, and may be painted on the ground surface in the place where the shadow is drawn.
The solution is to slightly increase the shadow Z.
Halo Effect
1. Mask buffer is cleared;
2. Draw an object. The mask of the part where the object is located is set to 1.
3. Use glscalef to zoom in the object
4. Draw an object again in a region other than mask 1
Shadow body
This is the prism formed by the link between the light source and the edge vertex of the object.
Use a mask to mark whether the pixel is inside or outside the shadow.
Draw two times: the pixels of the illumination shadow outside the body, and the pixels of the illumination shadow inside the body.
The Shadow projection may partially interwork with the projection of other objects (Note: Two-Dimensional processing ),
That is to say, two-dimensional cropping is required to distinguish the pixels of objects inside and outside the shadow.
Calculation of object contour
We need to find the outline of an object to expand its shadow. This process is like silhouette for them.
Triangle has the simplest Contour
Contour Calculation for complex objects
-The feedback mechanism of OpenGL and the tesselator of glu1.2 can help you.
Dynamic shadow is cool!
Masked shadow Algorithm
1. Draw the scene depth value and write it into the depth buffer.
2. When a shadow is drawn on a masked plate, if the deep test passes, the masked position is reversed.
-Glstencilop (gl_keep, gl_keep, gl_invert );
3. decimal number of pixels in the shadow outside the body
4. pixels in the shadow body are reversed odd times.
Reverse example [p35]
The light source is on top, and the object and its shadow are on the front,
Point between viewpoint and shadow
Point in the shadow body: reversed once, 1
Point of shadow in vitro: reversed twice, 0
Draw shadow inside and outside
1. Disable the lighting computation and only update pixels with odd template bits.
2. Open the lighting computing Rendering scenario and update only the pixels with even template bits.
3. Shadow is drawn.
Multiple objects and multiple light sources
Count of inbound and outbound traffic
The area in the scenario can be divided into different shadow levels, and 0 is not in any shadow,
It is 1 in the shadow body of an object and 2 in the intersection area of the shadow body of the two objects,
In the intersection area of the three shadow objects, It is 3 ......
The process of entering the shadow and leaving the shadow appears along the light direction. We can count the shadow.
Mask count
1. Use gl_incr mask on the right shadow polygon.
2. Use gl_decr mask for the back-to-shadow Polygon
3. Draw twice
4. The shadow from each polygon can be slightly expanded.
5. Observe the cracks
6. high pixel filling rate consumption
Shake shadow body
The shadow is drawn multiple times, and the light source is slightly jittered between times. The cumulative buffer adds up each time. In this way, software shadows with soft edges can be generated.
Usage of other masks
1. Digital dissolution effect: the mask is saved in the mask buffer, and the mask is used to test the masks in two scenarios.
2. process common geometries, such as labels.
A z-conflict occurs in the total plane ry. For example, when a box with a label Paper is drawn, the label texture and the surface of the box may replace each other randomly. You can use the mask test to correct the problem.
3. measure the depth complexity.
Use the mask to calculate the number of pixel updates and generate a color code. In this way, different colors can be used to draw parts of an object in different depths.
4. ry Modeling
Using two or more intersection Geometries for addition, subtraction, and intersection operations, a new complex ry is called the primary ry.
The intersection section must be specially processed during painting. This information can be obtained using the mask buffer.
Conclusion
Mask testing = improving visual quality and unique results
Today's masked games:
-Quake 3: Terror Volume Shadow
-Unreal: evil shadow effect

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.