Game Development Reference
In-Depth Information
int textureIds[] = new int [1];
gl.glGenTextures(1, textureIds, 0);
int textureId = textureIds[0];
gl.glBindTexture(GL10. GL_TEXTURE_2D , textureId);
GLUtils. texImage2D (GL10. GL_TEXTURE_2D , 0, bitmap, 0);
gl.glTexParameterf(GL10. GL_TEXTURE_2D , GL10. GL_TEXTURE_MIN_FILTER , GL10. GL_NEAREST );
gl.glTexParameterf(GL10. GL_TEXTURE_2D , GL10. GL_TEXTURE_MAG_FILTER , GL10. GL_NEAREST );
gl.glBindTexture(GL10. GL_TEXTURE_2D , 0);
bitmap.recycle();
return textureId;
} catch (IOException e) {
Log. d ("TexturedTriangleTest", "couldn't load asset 'bobrgb888.png'!");
throw new RuntimeException("couldn't load asset '" + fileName + "'");
}
}
@Override
public void present( float deltaTime) {
GL10 gl = glGraphics.getGL();
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glClear(GL10. GL_COLOR_BUFFER_BIT );
gl.glMatrixMode(GL10. GL_PROJECTION );
gl.glLoadIdentity();
gl.glOrthof(0, 320, 0, 480, 1, -1);
gl.glEnable(GL10. GL_TEXTURE_2D );
gl.glBindTexture(GL10. GL_TEXTURE_2D , textureId);
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);
gl.glDrawArrays(GL10. GL_TRIANGLES , 0, 3);
}
We took the freedom to put the texture loading into a method called loadTexture() , which
simply takes the filename of a bitmap to be loaded. The method returns the texture object ID
generated by OpenGL ES, which we'll use in the present() method to bind the texture.
The definition of our triangle shouldn't be a big surprise; we just added texture coordinates to
each vertex.
The present() method does what it always does: it clears the screen and sets the projection
matrix. Next, we enable texture mapping via a call to glEnable() and bind our texture object.
The rest is just what we did before: enable the vertex attributes we want to use; tell OpenGL
ES where it can find them and what strides to use; and finally, draw the triangle with a call to
glDrawArrays() . Figure 7-13 shows the output of the preceding code.
Search WWH ::




Custom Search