The main function of the template test is to discard a subset of the pieces, compared to the depth detection, the template test proposed a relatively small number of pieces. The template test occurs after the trim test, before the depth test.
Code hints that are important when using template testing:
1 1. Glclear (gl_stencil_buffer_bit); 2 //enable template Test 3 2.glEnable (gl_stencil_test);
In most cases your stencil mask (stencil mask) is written as 0x00 or 0xFF on the line.
1 // 0xFF = = 0b11111111, at which point the template value is bitwise and the result of the operation is the template value, the template buffer can be written 2 glstencilmask (0xFF); 3 4 // 0x00 = = 0b00000000 = = 0, at this time, the template value with which the bitwise AND operation result is 0, the template buffer is not writable 5 Glstencilmask (0x00);
Glstencilmask allows us to set a bit mask (bitmask) for the template value, which determines whether the buffer is writable with the bitwise and (and) operation of the template value. The default bit mask is 1, so it doesn't affect the output, but if we set it to 0x00, all the write depth buffers end up being 0.
In addition, Glstencilfunc and glstencilop can be used to determine how template tests pass or fail and how template buffering is affected.
1 void Glstencilfunc (glenum func, glint ref, Gluint mask);
- Func: Sets the template test action. This test action is applied to the stored template values and Glstencilfunc ref values, and the options available are:
Gl_never, Gl_lequal, Gl_greater, Gl_gequal, Gl_equal, Gl_notequal, Gl_always. Their semantics are similar to depth buffers.
- Ref: Specifies the reference value for the template test. The content of the template buffer is compared to this value.
- Mask: Specifies a mask that, before the template test compares reference values and stored template values, operates with a bitwise and (and) operation with an initial setting of 1.
The code snippet is as follows:
1 glstencilfunc (gl_equal, 1, 0xFF); 2 //It tells OpenGL that whenever a fragment template value equals (gl_equal) A reference value of 1, the fragment can be drawn through the test, otherwise it will be discarded.
Glstencilfunc only describes what OpenGL does with template buffering, not how we update the buffers. This requires a Glstencilop method.
1 void glstencilop (glenum sfail, Glenum dpfail, Glenum dppass);
- Sfail: The action to take if the template test fails.
- Dpfail: The action to take if the template test passes, but the depth test fails.
- Dppass: If both the depth test and the template test are passed, the action will be taken.
The Glstencilop function is set to (Gl_keep, Gl_keep, Gl_keep) by default, so the template buffer retains its value for any result of any test.
The default behavior does not update the template buffers, so if you want to write to the template buffer, you must specify at least one different action as any option.
Note: three-dimensional masking
1 Gldepthmask (gl_true); // Enable write depth value 2 glcolormask (Gl_false, Gl_false, Gl_false, gl_false); 3 Glstencilfunc (gl_always, 1, 0xFF); 4 glstencilop (Gl_replace, Gl_keep, gl_keep); 5 Glstencilmask (0xFF);
Template testing in OpenGL ES