176 CHAPTER 6: Will It Blend?^
Table 6-1 has all of the allowable OpenGL ES blending factors, although not all are
supported by both source and destination. As you can see, there is ample room for
tinkering, with no set rules of what creates the best-looking effect. This will be highly
reliant on your individual tastes and needs. It is a lot of fun to try different values,
though. Make sure to fill the background with a dim gray, because some of the
combinations will just yield black when written to a black background.
Table 6-1. The Source and Destination Blending Values; Note That Not All Are Available to Both Channels
Blend Factor Description
GL_ZERO Multiplies the operand by 0.
GL_ONE Multiplies the operand by 1.
GL_SRC_COLOR Multiplies the operand by the four components of the source
color (destination only).
GL_ONE_MINUS_SRC_COLOR Multiplies the operand by (1.0 – source colors) (destination only).
GL_DST_COLOR Multiplies the operand by the four components of the destination
color (source only).
GL_ONE_MINUS_DST_COLOR Multiplies the operand by the 1.0 – destination colors
(source only).
GL_SRC_ALPHA Multiplies the operand by the source alpha.
GL_ONE_MINUS_SRC_ALPHA Multiplies the operand by (1.0 – source alpha).
GL_DST_ALPHA Multiplies the operand by the destination alpha.
GL_ONE_MINUS_DST_ALPHA Multiplies the operand by (1.0 – destination alpha).
GL_SRC_ALPHA_SATURATE Special mode for older graphics implementations to help anti-
aliasing. You’ll likely never use it. (Source only.)
In Chapter 5, we took a look at the GL extensions that OpenGL ES on iOS devices
supported. Several of those are for more sophisticated blending solutions such as
GL_OES_blend_equation_separate, GL_OES_blend_func_separate,
GL_OES_blend_subtract, and GL_EXT_blend_minmax. These values are used with the
methods glBlendEquation() and glBlendEquationSeparate().
Look back at the default blending equation, where the final color is determined by a
source value+a dest value. But what if you wanted the source to subtract the destination
instead of add? Calling glBlendEquation(GL_FUNC_SUBTRACT) will do the job. Add that
line right below glBlendFunc(), ensure both squares have an alpha of .5, and reset the
colors back to the original with red in front, compile and run. The results may be slightly
nonobvious, as in Figure 6-5 (left). What is happening is that the operation really is
‘‘subtracting’’ blue from the red source, but there is no blue component in the red