Some of my favorite YouTube videos belong to
the Will It Blend? series. The episode featuring the
pulverization of an iPhone is a perennial favorite, seconded only by the
Chuck Norris episode. Alas, this article deals with blending of a
different sort. OpenGL blending requires five ingredients:Ensure your color contains alpha. If it
comes from a texture, make sure the texture format contains alpha; if
it comes from a vertex attribute, make sure it has all four color
components.
Disable depth testing.
glDisable(GL_DEPTH_TEST);
Pay attention to the ordering of your draw
calls.
Enable blending.
glEnable(GL_BLENDING);
Set your blending function.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
For step 5, I’m giving a rather classic
blending equation as an example, but that’s not always what you’ll want!
(More on this later.) Specifically, the previous function call sets up the
following equation:

S is the source color,
D is the starting destination color, and
F is the final destination color. By default,
OpenGL’s blending equation is this:

Since the default blending function ignores
alpha, blending is effectively turned off even when you’ve enabled it with
glEnable. So, always remember to set your blending
function—this is a common pitfall in OpenGL programming.
Here’s the formal declaration of
glBlendFunc:
void glBlendFunc (GLenum sfactor, GLenum dfactor);
The blending equation is always an operation on
two scaled operands: the source color and the destination color. The
template to the equation is this:

The sfactor and
dfactor arguments can be any of the
following:
GL_ZERO
Multiplies the operand with zero.
GL_ONE
Multiplies the operand with one.
GL_SRC_ALPHA
Multiplies the operand by the alpha
component of the source color.
GL_ONE_MINUS_SRC_ALPHA
Multiplies the operand by the inverted
alpha component of the source color.
GL_DEST_ALPHA
Multiplies the operand by the alpha
component of the destination color.
GL_ONE_MINUS_DEST_ALPHA
Multiplies the operand by the inverted
alpha component of the destination color.
Additionally, the
sfactor parameter supports the following:
GL_DST_COLOR
Component-wise multiplication of the
operand with the destination color.
GL_ONE_MINUS_DST_COLOR
Component-wise multiplication of the
operand with the inverted destination color.
GL_SRC_ALPHA_SATURATE
Returns the minimum of source alpha and
inverted destination alpha. This exists mostly for historical
reasons, because it was required for an outmoded anti-aliasing technique.
And the dfactor
parameter also supports the following:
GL_SRC_COLOR
Component-wise multiplication of the
operand with the source color.
GL_ONE_MINUS_SRC_COLOR
Component-wise multiplication of the
operand with the inverted source color.
OpenGL ES 2.0 relaxes the blending constraints
by unifying the set of choices for sfactor and
dfactor, with the exception of
GL_SRC_ALPHA_SATURATE.
Note:
ES 2.0 also adds the concept of “constant
color,” specified via glBlendColor. For more
information, look up glBlendColor and
glBlendFunc at the Khronos website:
- http://www.khronos.org/opengles/sdk/docs/man/