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


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:


\begin{displaymath}
C_{out} = C_{src} * A_{src} + (1 - A_{src}) * C_{dst}
\end{displaymath} (11)

Here, $C_{out}$ is the output color which will be written to the frame buffer. $C_{src}$ and $A_{src}$ are the source color and alpha, which come from the fragment. $C_{dst}$ 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 glDepthMaskGL_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   Contents
2001-01-10