Game Development Reference
In-Depth Information
as it is in the texture coordinate system. The coordinate systems are completely decoupled. So,
let's see how we can add those texture coordinates to our vertices:
int VERTEX_SIZE = (2 + 2) * 4;
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * VERTEX_SIZE);
byteBuffer.order(ByteOrder.nativeOrder());
vertices = byteBuffer.asFloatBuffer();
vertices.put( new float [] { 0.0f, 0.0f, 0.0f, 1.0f,
319.0f, 0.0f, 1.0f, 1.0f,
160.0f, 479.0f, 0.5f, 0.0f});
vertices.flip();
That was easy. All we have to do is make sure that we have enough room in our buffer and
then append the texture coordinates to each vertex. The preceding code corresponds to the
rightmost mapping in Figure 7-10 . Note that our vertex positions are still given in the usual
coordinate system we defined via our projection. If we wanted to, we could also add the color
attributes to each vertex, as in the previous example. OpenGL ES would then, on the fly, mix
the interpolated vertex colors with the colors from the pixels of the texture to which the triangle
maps. Of course, we'd need to adjust the size of our buffer as well as the VERTEX_SIZE constant
accordingly; for example, (2 + 4 + 2) × 4. To tell OpenGL ES that our vertices have texture
coordinates, we again use glEnableClientState() together with the glTexCoordPointer()
method, which behaves exactly the same as glVertexPointer() and glColorPointer() (can you
see a pattern here?):
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
vertices.position(0);
gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
vertices.position(2);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, vertices);
Nice—that looks very familiar. So, the remaining question is, how can we upload the texture to
OpenGL ES and tell it to map it to our triangle? Naturally, that's a little bit more involved. But fear
not, it's still pretty easy.
Uploading Bitmaps
First, we have to load our bitmap. We already know how to do that on Android:
Bitmap bitmap = BitmapFactory.decodeStream(game.getFileIO().readAsset("bobrgb888.png"));
Here we load Bob in an RGB888 configuration. The next thing we need to do is tell OpenGL
ES that we want to create a new texture. OpenGL ES has the notion of objects for a couple of
things, such as textures. To create a texture object, we can call the following method:
GL10.glGenTextures( int numTextures, int [] ids, int offset)
 
Search WWH ::




Custom Search