Game Development Reference
In-Depth Information
Together with our Texture class, we now have a pretty nice basis for all of our 2D OpenGL ES
rendering. In order to reproduce all our Canvas rendering abilities completely, however, we are
still missing blending. Let's have a look at that.
Alpha Blending: I Can See Through You
Alpha blending in OpenGL ES is pretty easy to enable. We only need two method calls:
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
The first method call should be familiar: it just tells OpenGL ES that it should apply alpha
blending to all triangles we render from this point on. The second method is a little bit more
involved. It specifies how the source color and destination color should be combined. Recall
from Chapter 3 that the way a source color and a destination color are combined is governed
by a simple blending equation. The method glBlendFunc() just tells OpenGL ES which kind of
equation to use. The preceding parameters specify that we want the source color to be mixed
with the destination color exactly as specified in the blending equation in Chapter 3. This is
equal to how the Canvas blended Bitmap s for us.
Blending in OpenGL ES is pretty powerful and complex, and there's a lot more to it. For our
purposes, we can ignore all those details, though, and just use the preceding blending function
whenever we want to blend our triangles with the framebuffer—the same way we blended
Bitmap s with the Canvas .
The second question is where the source and destination colors come from. The latter is easy to
explain: it's the color of the pixel in the framebuffer we are going to overwrite with the triangle we
draw. The source color is actually a combination of two colors.
The vertex color : This is the color we either specify via glColor4f() for all
vertices or specify on a per-vertex basis by adding a color attribute to
each vertex.
The texel color : As mentioned before, a texel is a pixel from a texture. When our
triangle is rendered with a texture mapped to it, OpenGL ES will mix the texel
colors with the vertex colors for each pixel of a triangle.
So, if our triangle is not texture mapped, the source color for blending is equal to the vertex
color. If the triangle is texture mapped, the source color for each of the triangle's pixels is a
mixture of the vertex color and the texel color. We could specify how the vertex and texel
colors are combined by using the glTexEnv() method. The default is to modulate the vertex
color by the texel color, which basically means that the two colors are multiplied with each
other component-wise (vertex r × texel r, and so on). For all our use cases in this topic, this is
exactly what we want, so we won't go into glTexEnv() . There are also some very specialized
cases where you might want to change how the vertex and texel colors are combined. As with
glBlendFunc() , we'll ignore the details and just use the default.
When we load a texture image that doesn't have an alpha channel, OpenGL ES will automatically
assume an alpha value of 1 for each pixel. If we load an image in RGBA8888 format, OpenGL ES
will happily use the supplied alpha values for blending.
 
Search WWH ::




Custom Search