next up previous contents
Next: 12.3 Sorting Up: 12. Transparency Previous: 12.1 Screen-Door Transparency

   
12.2 Alpha Blending

To draw semi-transparent geometry, the most common technique is to use alpha blending. In this technique, the alpha value for each fragment drawn reflects the transparency of that object. (To be totally correct, the alpha value actually represents the opacity, since an alpha value of 1.0 represents a 100% opaque surface). Each fragment is combined with the values in the framebuffer using the blending equation:


Cout = Csrc * Asrc + (1 - Asrc) * Cdst (11)

Here, Cout is the output color which will be written to the frame buffer. Csrc and Asrc are the source color and alpha, which come from the fragment. Cdst is the destination color, which is the color value currently in the framebuffer at the location. This equation is specified using the OpenGL command glBlendFuncGL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA). Blending is then enabled with glEnableGL_BLEND(GL_BLEND).

Transparent primitives drawn using alpha blending should always be drawn after all opaque primitives are drawn. Unless the transparent objects are sorted in back to front order, depth buffer updates must be disabled using glDepthMask_FALSE(GL_FALSE), although depth buffer compares should remain enabled.

If the objects are not sorted and drawn in back to front order, the above blending equation produces order-dependent rendering artifacts that can be quite objectionable. If sorting of the scene is undesirable, order dependencies can be eliminated by using GL_ONE for the destination factor rather than GL_ONE_MINUS_SRC_ALPHA. This method does not look as natural, especially when transparent objects are drawn over light objects, but it requires no sorting.

A common mistake when implementing alpha blended transparency is to assume that it requires a framebuffer with an alpha channel. The alpha value used for blended transparency comes down the graphics pipeline with each fragment; the alpha values in the framebuffer (GL_DST_ALPHA) are not actually used, so no alpha buffer is required.

The alpha value of the fragment can be set in several ways. If lighting is not being used, the alpha value can be set using a 4- component color command such as glColor4f(). If lighting is enabled, the fourth color component of the diffuse reflectance coefficient of the material corresponds to the transparency of the object.

If texturing is enabled, the source of the alpha channel is controlled by the texture internal format, the texture environment function, and the texture environment constant color. The interaction is described in more detail in the glTexEnv() man page. Many intricate effects can be implemented using alpha values from textures.


next up previous contents
Next: 12.3 Sorting Up: 12. Transparency Previous: 12.1 Screen-Door Transparency
David Blythe
1999-08-06