Implement reflection using stencel Buffer

Source: Internet
Author: User
Tags reflector
It is not easy to understand stencel buffer. Correct and flexible use is more skillful. Stencel is widely used in reflection, refraction, and shodow creation. There are also some examples on the Internet. There is a fixed pattern for planar reflection, but it is impossible to create more complex and variable scenarios without knowing why. Let's talk about the simplest planar reflection. Beware that reflection will reverse vertex ordering for the faces in the object, so the face culling state for the reflection scene must be set to the reverse of the state for the original scene. It must be noted that reflection will reverse the vertex order of the object surface. Therefore, in the reflection scenario, the culling state of the object surface must be set to the opposite value of the culling state in the original scenario. How can we get a virtual object relative to reflector (this is an operation independent of the point of view, only considering the relative position of the object and the reflection surface )? This process can be divided into three steps: 1, Transform the object to the local coordinate system of the reflector, then translate the reflector to the origin, and rotate the reflector to overlap with the XY plane. 2, Glscalef (1.0f, 1.0f,-1.0f ); 3, The first step is to transform the inverse operation to bring the reflector back to its original position. Related Code Column and below. Through the reflector matrix, we can also obtain that virtual object: P is a point on the reflection plane, and V is a vector of the vertical reflection plane. Multiply the vertex of an object by R to obtain the reflected object. We can make a small example. Assume that the reflector is the XY plane, then v = (, 1); take any point on the reflector as P = (, 0), then P · V = 0; the reflected matrix R is: | 1 0 0 0 | 0 1 0 0 | 0 0 0-1 0 | 0 0 0 1 |, when an object vertex is multiplied by R, it only changes its z direction, which is the same as glscalef (1.0f, 1.0f,-1.0f) in the second step of the above method. From this we can know that the above two types of virtual objects have the same effect. After obtaining the virtual vertex of the reflected object, it is time to render the reflected object in the scene. The main task is to cut the reflected object so that it can only be visible in the reflector. There are two methods: the use of the template cache (stencel buffer), or the projection of the texture map to the reflector, the reflection image is cut using the reflector boundary. Using the template cache to cut reflected objects is better understood. // Set the stencel buffer in the reflector region to 1, and the buffer in other regions to 0. glcolormask (,); glable (gl_stencil_test); glstencilfunc (gl_always, 1, 1); glstencilop (gl_replace, gl_replace, gl_replace); gldisable (gl_depth_test); glpushmatrix (); Glmultmatrixd (reflector-> matrix ()); Drawfloor (); Drawfloor2 (); Glpopmatrix (); // render virtual object to convert the object to the virtualobject-> setparentframe (dynamic_cast <frame *> (reflector) in the local coordinate system of the reflection plane )); virtualobject2-> setparentframe (dynamic_cast <frame *> (reflector); virtualobject-> setpositionandorientation (Object-> position (), object-> orientation ()); virtualobject2-> setpositionandorientation (Object-> position (), object-> orientation ()); // Glscalef (1.f, 1.f,-1.f) Reflection process // Glpushmatrix ()/glpopmatrix () Step 3 Glpushmatrix (); Glmultmatrixd (reflector-> matrix ()); Glscalef (1.f, 1.f,-1.f ); Glmultmatrixd (virtualobject-> matrix ()); Drawobject (); Glpopmatrix (); Glpushmatrix (); Glmultmatrixd (reflector-> matrix ()); Glscalef (1.f,-1.f, 1.f );// Note: drawfloor2 () is drawn on the xz plane, so it is reflected in the Y direction. Glmultmatrixd (virtualobject2-> matrix ()); Drawobject (); Glpopmatrix (); Gldisable (gl_stencil_test ); // Other aspects of the rendering scenario Glable (gl_blend ); Glcolor4f (1.f, 1.f, 1.f, 0.8f ); Glblendfunc (gl_src_alpha, gl_one_minus_src_alpha ); Glpushmatrix (); Glmultmatrixd (reflector-> matrix ()); Drawfloor (); Drawfloor2 (); Glpopmatrix (); Gldisable (gl_blend ); Glpushmatrix (); Glmultmatrixd (Object-> matrix ()); Drawobject (); Glpopmatrix (); The implementation is as follows: When an object passes through the reflector, the corresponding virtual object will also pass through the reflector. The nehe solution to this problem is to define a clip plane (the plane is determined by the coefficient of the equation AX + by + cz + D = 0). If the reflector is not a special plane, the coefficients A, B, C, and D are difficult to determine. You can consider adding collision detection so that it cannot pass through the reflective surface. See also: Http://www.bluevoid.com/opengl/sig00/advanced00/notes/node164.html

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.