next up previous contents
Next: 10.3 Sorting Up: 10 Transparency Previous: Screen-Door Transparency

10.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. Each fragment is combined with the values in the frame buffer using the blending equation
equation2212
Here, tex2html_wrap_inline7779 is the output color which will be written to the frame buffer. tex2html_wrap_inline7781 and tex2html_wrap_inline7783 are the source color and alpha, which come from the fragment. tex2html_wrap_inline7785 is the destination color, which is the color value currently in the frame buffer 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).

A common mistake when implementing alpha blending is to assume that it requires a frame buffer with an alpha channel. Note that the alpha values in the frame buffer (GL_DST_ALPHA) are not actually used, so no alpha buffer is required.

For the alpha blending technique to work correctly, the transparent primitives must be drawn in back to front order and must not intersect. To convince ourselves of this, we can consider two objects tex2html_wrap_inline7787 and tex2html_wrap_inline7789 with colors tex2html_wrap_inline7791 and tex2html_wrap_inline7793 and alphas tex2html_wrap_inline7795 and tex2html_wrap_inline7797. Assume that tex2html_wrap_inline7789 is in front of tex2html_wrap_inline7787 and that the frame buffer has been cleared to black. If tex2html_wrap_inline7789 is drawn first, tex2html_wrap_inline7787 will not be drawn at all unless depth buffering is disabled. Turning off depth buffering generally is a bad idea, but even if we could turn it off, the results would still be incorrect. After tex2html_wrap_inline7789 had been drawn, the frame buffer color would be tex2html_wrap_inline7809. After tex2html_wrap_inline7787 had been drawn, the color would be tex2html_wrap_inline7813. If tex2html_wrap_inline7787 had been drawn first, the value would be tex2html_wrap_inline7817. Sorting will be discussed in detail in Section  10.3.

The alpha channel of the fragment can be set in several ways. If lighting is not being used, then the alpha value can be set using a 4 component color command such as glColor4fv(). If lighting is enabled, then the ambient and diffuse reflectance coefficients of the material should correspond to the translucency 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: 10.3 Sorting Up: 10 Transparency Previous: Screen-Door Transparency

David Blythe
Thu Jul 17 21:24:28 PDT 1997