What you’ll learn in this chapter:
This chapter introduces the color blending and fog functions provided by OpenGL, both of which can be used to add that last bit of realism you need.
The color blending functions support effects such as transparency that can be used to simulate windows, drink glasses, and other transparent objects. The fog functions add a variable amount of color to the polygons you draw, producing a scene that looks “hazy” or just downright dreary!
Something to remember when using these special effects is that they don’t look good on an 8-bit display. Make sure your programs contain the option of disabling these effects when running on 8-bit displays.
Blending in OpenGL provides pixel-level control of RGBA color storage in the color buffer. Blending operations cannot be used in color index mode and are disabled in color index windows.
To enable blending in RGBA windows, you must first call glEnable(GL_BLEND). After this, you call glBlendFunc with two arguments: the source and the destination colors’ blending functions (see Tables 16-1 and 16-2). By default, these arguments are GL_ONE and GL_ZERO, respectively, which is equivalent to glDisable(GL_BLEND).
Transparency is perhaps the most typical use of blending, often used for windows, bottles, and other 3D objects that you can see through. Transparency can also be used to combine multiple images, or for “soft” brushes in a paint program.
Following are the blending functions for all of these applications:
glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
This combination takes the source color and scales it based on the alpha component, and then adds the destination pixel color scaled by 1 minus the alpha value. Stated more simply, this blending function takes a fraction of the current drawing color and overlays it on the pixel on the screen. The alpha component of the color can be from 0 (completely transparent) to 1 (completely opaque), as follows:
Rd = Rs * As + Rd * (1 - As) Gd = Gs * As + Gd * (1 - As) Bd = Bs * As + Bd * (1 - As)
Because only the source alpha component is used, you do not need a graphics board that supports alpha color planes in the color buffer. This is important because the standard Microsoft OpenGL implementation does not support alpha color planes.
Something to remember with alpha-blended transparency is that the normal depth-buffer test can interfere with the effect you’re trying to achieve. To make sure that your transparent polygons and lines are drawn properly, always draw them from back to front.
Listing 16-1 shows the code that was used to draw the transparent teapot in Figure 16-1. In the draw_scene function, we draw the two teapots from back to front to ensure that the rear teapot can be seen through the front one. You’ll notice some artifacts remain visible in the front teapot where the surface polygons intersect. You can’t eliminate these completely, but you can reduce them by sorting the polygons by depth first and enabling back-face culling with glEnable(GL_CULL_FACE).
The first thing draw_scene does is set the blending function to do transparency based on the drawing (source) color’s alpha component:
Next, the opaque teapot is drawn with blending disabled so that we can always see the teapot through the transparent one:
glDisable(GL_BLEND); glColor3f(1.0, 1.0, 0.0); auxSolidTeapot(1.0);
Finally, blending is enabled and the transparent teapot is drawn with an alpha (transparency) value of 0.25:
glEnable(GL_BLEND); glColor4f(1.0, 1.0, 1.0, 0.25); auxSolidTeapot(1.0);