Game Development Reference
In-Depth Information
gl.glBindTexture(GL10.
GL_TEXTURE_2D
, 0);
bitmap.recycle();
}
The
createMipmaps()
method is fairly straightforward. We start off by binding the texture so that
we can manipulate its attributes. The first thing we do is keep track of the bitmap's width and
height and set the filters. Note that we use
GL_LINEAR_MIPMAP_NEAREST
for the minification filter.
If we don't use that filter, mipmapping will not work and OpenGL ES will fall back to normal
filtering, only using the base image.
The
while
loop is straightforward. We upload the current bitmap as the image for the current
level. We start at level 0, the base level with the original image. Once the image for the current
level is uploaded, we create a smaller version of it, dividing its width and height by 2. If the new
width is less than or equal to 0, we can break out of the infinite loop, since we have uploaded an
image for each mipmap level (the last image has a size of 1×1 pixels). We use the
Canvas
class to
resize the image and to store the result in
newBitmap
. We then recycle the old bitmap so that we
clean up any of the memory it used and set the
newBitmap
as the current bitmap. We repeat this
process until the image is smaller than 1×1 pixels.
Finally, we unbind the texture and recycle the last bitmap that got created in the loop.
public void
reload() {
load();
bind();
setFilters(minFilter, magFilter);
glGraphics.getGL().glBindTexture(GL10.
GL_TEXTURE_2D
, 0);
}
public void
setFilters(
int
minFilter,
int
magFilter) {
this
.minFilter = minFilter;
this
.magFilter = magFilter;
GL10 gl = glGraphics.getGL();
gl.glTexParameterf(GL10.
GL_TEXTURE_2D
, GL10.
GL_TEXTURE_MIN_FILTER
,
minFilter);
gl.glTexParameterf(GL10.
GL_TEXTURE_2D
, GL10.
GL_TEXTURE_MAG_FILTER
,
magFilter);
}
public void
bind() {
GL10 gl = glGraphics.getGL();
gl.glBindTexture(GL10.
GL_TEXTURE_2D
, textureId);
}
public void
dispose() {
GL10 gl = glGraphics.getGL();
gl.glBindTexture(GL10.
GL_TEXTURE_2D
, textureId);
int
[] textureIds = { textureId };
gl.glDeleteTextures(1, textureIds, 0);
}
}