Pro OpenGL ES for iOS

(singke) #1

CHAPTER 6: Will It Blend? (^179)
Multicolor Blending
Now we can spend a few minutes looking at the effect of blending functions when the
squares are defined with individual colors for each vertex. Add Listing 6-2 to the
venerable drawInRect(). The first color set defines yellow, magenta, and cyan (the three
complementary colors to the standard red-green-blue specified in the second set).
Listing 6-2. Vertex Colors for the Two Squares
static const GLfloat squareColorsYMCA[] =
{
1.0, 1.0, 0, 1.0,
0, 1.0, 1.0, 1.0,
0, 0, 0, 1.0,
1.0, 0, 1.0, 1.0,
};
static const GLfloat squareColorsRGBA[] =
{
1.0, 0, 0, 1.0,
0, 1.0, 0, 1.0,
0, 0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
};
Assign the first color array to the first square (which has been the blue one up until now),
and assign the second to the former red square. And don’t forget to enable the use of
the color array. You should be familiar enough now to know what to do. Also, notice that
the arrays are now normalized as a bunch of GLfloats as opposed to the previously
used unsigned bytes, so you’ll have to tweak the calls to glColorPointer(). The solution
is left up to the reader (I’ve always wanted to say that). With the blending disabled, you
should see Figure 6-7 (left), and when enabled using the traditional function for
transparency, Figure 6-7 (center) should be the result. What? It isn’t? You say it still
looks like the first figure? Why would that be?
Look back at the color arrays. Notice how the last value in each row, alpha, is at its
maximum of 1.0. Remember that with this blending mode, any of the destination values
are multiplied by: (1.0 ---- source alpha), or rather, 0.0, so that the source color reigns
supreme as you saw in a previous example. One solution to seeing some real
transparency would be to use the following:
glBlendFunc(GL_ONE, GL_ONE);
This works because it ditches the alpha channel altogether. If you want alpha with the
‘‘standard’’ function, merely change the 1.0 values to something else, such as .5. And
the result is Figure 6-7 (right).

Free download pdf