Notes for using template Buffering
----- Author: Xiong bin 2007.12.30 xiongbincsu@yahoo.com.cn
Maybe you have used the template buffer, but do you actually know its correct usage?
I have summarized the following questions based on my practice and hope to help you:
1. Steps for using the template Buffer
2. What are the meanings of parameters in glstencilfunc and how to choose proper parameters?
3. Significance and selection of parameters in glstencilop.
========================================================== ======================================
Question 1:
In short:
1) Specify the default template value and use it to clear the template buffer,
2) Enable the template test function, specify the template comparison function, and specify the operation method for the template value at the corresponding position after the template test fails.
3) use the drawing command to draw a template image.
4) draw other images
The following details:
If you want to use a template buffer, first you need to clean up the invalid data in the original buffer, just like clearing the color buffer before using it. This is when you call glclear () add gl_stencil_buffer_bit. that is, like this:
Glclear (gl_color_buffer_bit | gl_stencil_buffer_bit)
Similar to the color buffer, you must specify a default fill value before using this function. You can specify glclearstenpencil (0) as follows );
Of course, you do not need to use 0, just as you do not have to specify glclearcolor (, 0) when clearing the color buffer. You need to set it according to your needs.
After setting the default value and filling the template buffer with this default value, you can use it where you need it:
Glenable (gl_stencil_test );
Next we will generate our template. In fact, we will use a group of drawing commands to plot an image and use it as a template. for example, if we want to generate a hollow window, we can plot the blank part of the window as a template, so that during subsequent drawing, the image that falls into this template is not drawn, so it forms a hollow-out .. well, it's far away. It just tells you what a template is ....... oh, it seems that this is not necessary ???
Because we use drawing commands when creating a template, sometimes we do not want this template to be seen by others, (a picture to be seen equals to the color buffer written to it and finally switched to the screen ).. to prevent the template creation code from being displayed as a graph, we need to disable the write of the color buffer. This can be done through the following statement:
Glcolormask (gl_false, gl_false); this prevents all color channels from writing
Similarly, if we do not want the template code to affect the depth buffer, we can call gldisable (gl_depth_test); To disable writing in the depth cache.
After setting these settings, we are ready to start drawing the template,
Before creating a template, We need to specify a template value for the image drawn by the template itself. Why? Because the template test is similar to the deep test, it compares the value of the existing template buffer with the template value of the image to be drawn, then, just draw those images that make the comparison result true...
Now all the values in the template buffer are default values, so we need to specify a value for the template itself. In the future, you can compare the template value of the image you want to draw with this value to make a trade-off...
How can I assign a template value to the image to be drawn and compare it? This is done through the two functions described in question 2 and 3. Please continue to look at them...
Question 2. glstencilfunc usage
The original form of glstencilfunc is: glstencilfunc (glenum func, glint ref, gluint mask );
It is used to specify the comparison function for template testing, these functions are used to compare the "template value of the currently drawn image" with the "template value to be written" to get a true or false value. This comparison function is specified by the first parameter, it is an enumerated value, which can be gl_greater or gl_less. The second is the template value of the drawing function drawn by this function, it is used to compare with the template value of the target location to be drawn with the function specified by the first parameter. The result is displayed .. however, this comparison does not directly compare the two values. If the template value of the target location to be drawn is B and the second parameter is A, the comparison is as follows, perform the and operation on parameters a and mask, and perform the operation on parameters B and mask at the same time, and then compare the result by the specified comparison function func,
That is, (ref & Mask) OP (svalue & Mask ))
If the comparison structure between A and B is true, write, false, and not write...
Question 3: glstencilop usage
The original form of the glstencilop () function is: glstencilop (glenum fail, glenum zfail, glenum zpss );
It is used to specify how to modify the template value at the position to be written if the template test fails (or the deep test fails) when some elements are being drawn. the three parameters are specified respectively. When the template test fails, how to modify the template, how to modify the template test fails, and how to modify the template if the test is successful .... the following two parameters can be used to modify the template value even if the template test passes ..
Their optional values are: gl_keep (keep unchanged), gl_dec (decrease), gl_incr (Increase), gl_replace (replace ),
Note that these operations are performed when each slice is written. If you use gl_incr, you may only draw one model. However, this model has several overlapping patches in depth, so each time you write these patches, it will cause a gl_incr, that is, the template value + 1. If you have not noticed this problem, then you use gl_equal in the test function glstencilfunc to get incorrect results .. if you want to use gl_equal, we recommend that you use gl_replace in this function. This variable causes the function to replace the template value of the current position with the ref value specified in glstencilfunc, this will not affect multiple operations ....