In RGBA mode, the alpha test allows you to accept or reject a fragment based on its alpha value. If enabled, the test compares the incoming alpha value with a reference value. The fragment is accepted or rejected depending on the result of the comparison. Both the reference value and the comparison function are set with glAlphaFunc(). To determine whether the alpha test is enabled and to obtain the reference value and the comparison function, use GL_ALPHA_TEST with glIsEnabled() and GL_ALPHA_TEST_FUNC or GL_ALPHA_TEST_REF with glGetIntegerv(). void glAlphaFunc(GLenum func, GLclampf ref);
Sets the reference value and comparison function for the alpha test. The reference value ref is clamped to be between 0 and 1. The possible values for func and their meaning are listed in Table 10-2 .
| Parameter | Meaning |
|---|---|
GL_NEVER | Never accept the fragment |
GL_ALWAYS | Always accept the fragment |
GL_LESS | Accept fragment if fragment alpha < reference alpha |
GL_LEQUAL | Accept fragment if fragment alpha ≤ reference alpha |
GL_EQUAL | Accept fragment if fragment alpha = reference alpha |
GL_GEQUAL | Accept fragment if fragment alpha ≥ reference alpha |
GL_GREATER | Accept fragment if fragment alpha > reference alpha |
GL_NOTEQUAL | Accept fragment if fragment alpha ≠ reference alpha |
The alpha test is enabled and disabled by passing GL_ALPHA_TEST to glEnable() and glDisable(). By default, the reference value is zero, the comparison function is GL_ALWAYS, and the alpha test is disabled.
One application for the alpha test is to implement a transparency algorithm. Render your entire scene twice, the first time accepting only fragments with alpha values of one, and the second time accepting fragments with alpha values that aren't equal to one. Turn the depth buffer on during both passes, but disable depth buffer writing during the second pass.
Another use might be to make decals with texture maps where you can see through certain parts of the decals. Set the alphas in the decals to 0.0 where you want to see through, set them to 1.0 otherwise, set the reference value to 0.5 (or anything between 0.0 and 1.0), and set the comparison function to GL_GREATER. The decal has see-through parts, and the values in the depth buffer aren't affected. This technique, called billboarding, is described in "Sample Uses of Blending."
OpenGL Programming Guide