You can read more about stencil-testing in the OpenGL wiki or OpenGL docs.
The Test allows you to define how to compare the reference value
with the stencil buffer value, in order to set the conditions under which
the pixel will be drawn.
always -- Always pass
equal -- ref & mask == stencil & mask
never -- Never pass
less -- ref & mask < stencil & mask
greater -- ref & mask > stencil & mask
notEqual -- ref & mask != stencil & mask
lessOrEqual -- ref & mask <= stencil & mask
greaterOrEqual -- ref & mask >= stencil & maskDefines how to update the value in the stencil buffer.
Sets the stencil buffer value to ref from the stencil test.
Keeps the current stencil buffer value. Use this as a noop.
Sets the stencil buffer value to 0.
Increments the current stencil buffer value. Clamps to the maximum representable unsigned value.
Decrements the current stencil buffer value. Clamps to 0.
Bitwise inverts the current stencil buffer value.
Increments the current stencil buffer value. Wraps stencil buffer value to zero when incrementing the maximum representable unsigned value.
Decrements the current stencil buffer value. Wraps stencil buffer value to the maximum representable unsigned value when decrementing a stencil buffer value of zero.
Different options for front and back facing polygons.
When you need to draw an intercection of two entities, e.g. a reflection in the mirror, you can test against the stencil buffer, that has to be enabled with
stenciloption intoHtmlWith.Stencil test decides if the pixel should be drawn on the screen. Depending on the results, it performs one of the following operations on the stencil buffer:
fail—the operation to use when the stencil test fails;zfail—the operation to use when the stencil test passes, but the depth test fails;zpass—the operation to use when both the stencil test and the depth test pass, or when the stencil test passes and there is no depth buffer or depth testing is disabled.For example, draw the mirror
Entityon the screen and fill the stencil buffer with all 1's:Crop the reflection
Entityusing the values from the stencil buffer:You can see the complete example here.