Helpful Snippet

For reference, here's the complete snippet to create a texture object, load image data, and set the filters on Android:

Bitmap bitmap = BitmapFactory.decodeStream(game.getFileIO().readAsset("bobrgb888.png"));

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();

Not so bad after all. The most important part of all this is to actually recycle the Bitmap once we are done. Otherwise we'd waste memory. Our image data is safely stored in video RAM in the texture object (until the context is lost and we need to reload it again).

0 0

Post a comment