Game Development Reference
In-Depth Information
For vertex colors, we always have to specify an alpha component, either by using
glColor4f()
,
where the last argument is the alpha value, or by specifying the four components per vertex,
where again, the last component is the alpha value.
Let's put this into practice with a brief example. We want to draw Bob twice: once by using
the image
bobrgb888.png
, which does not have an alpha channel per pixel, and a second
time by using the image
bobargb8888.png
, which has alpha information. Note that the PNG
image actually stores the pixels in ARGB8888 format instead of RGBA8888. Luckily, the
GLUtils.texImage2D()
method we use to upload the image data for a texture will do the
conversion for us automatically. Listing 7-11 shows the code of our little experiment using the
Texture
and
Vertices
classes.
Listing 7-11. Excerpt from BlendingTest.java; Blending in Action
class
BlendingScreen
extends
Screen {
GLGraphics glGraphics;
Vertices vertices;
Texture textureRgb;
Texture textureRgba;
public
BlendingScreen(Game game) {
super
(game);
glGraphics = ((GLGame)game).getGLGraphics();
textureRgb =
new
Texture((GLGame)game, "bobrgb888.png");
textureRgba =
new
Texture((GLGame)game, "bobargb8888.png");
vertices =
new
Vertices(glGraphics, 8, 12,
true
,
true
);
float
[] rects =
new float
[] {
100, 100, 1, 1, 1, 0.5f, 0, 1,
228, 100, 1, 1, 1, 0.5f, 1, 1,
228, 228, 1, 1, 1, 0.5f, 1, 0,
100, 228, 1, 1, 1, 0.5f, 0, 0,
100, 300, 1, 1, 1, 1, 0, 1,
228, 300, 1, 1, 1, 1, 1, 1,
228, 428, 1, 1, 1, 1, 1, 0,
100, 428, 1, 1, 1, 1, 0, 0
};
vertices.setVertices(rects, 0, rects.length);
vertices.setIndices(
new short
[] {0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4 }, 0, 12);
}
Our little
BlendingScreen
implementation holds a single
Vertices
instance where we'll store the two
rectangles, as well as two
Texture
instances—one holding the RGBA8888 image of Bob and the
other one storing the RGB888 version of Bob. In the constructor, we load both textures from the
files
bobrgb888.png
and
bobargb8888.png
and rely on the
Texture
class and
GLUtils.texImag2D()
to convert the ARGB8888 PNG to RGBA8888, as needed by OpenGL ES. Next up, we define our
vertices and indices. The first rectangle, consisting of four vertices, maps to the RGB888 texture
of Bob. The second rectangle maps to the RGBA8888 version of Bob and is rendered 200 units