Game Development Reference
In-Depth Information
Figure 7-13. Texture mapping Bob onto our triangle
There's one last thing we haven't mentioned yet, and it's of great importance: All bitmaps we
load must have a width and height of a power of two. Stick to it or else things will explode.
So what does this actually mean? The image of Bob that we used in our example has a size of
128×128 pixels. The value 128 is 2 to the power of 7 (2×2×2×2×2×2×2). Other valid image sizes
would be 2×8, 32×16, 128×256, and so on. There's also a limit to how big our images can be.
Sadly, it varies depending on the hardware on which our application is running. The OpenGL ES
1.x standard doesn't specify a minimally supported texture size; however, from experience, it
seems that 512×512-pixel textures work on all current Android devices (and most likely will work
on all future devices as well). We'd even go so far to say that 1024×1024 is OK as well.
Another issue that we have pretty much ignored so far is the color depth of our textures.
Luckily, the method GLUtils.texImage2D() , which we used to upload our image data to the
GPU, handles this for us fairly well. OpenGL ES can cope with color depths like RGBA8888,
RGB565, and so on. We should always strive to use the lowest possible color depth to decrease
bandwidth. For this, we can employ the BitmapFactory.Options class, as in previous chapters,
to load an RGB888 Bitmap to an RGB565 Bitmap in memory, for example. Once we have loaded
our Bitmap instance with the color depth we want it to have, GLUtils.texImage2D() takes over
and makes sure that OpenGL ES gets the image data in the correct format. Of course, you
should always check whether the reduction in color depth has a negative impact on the visual
fidelity of your game.
A Texture Class
To reduce the code needed for subsequent examples, we wrote a little helper class called
Texture . It will load a bitmap from an asset and create a texture object from it. It also has a few
convenience methods to bind the texture and dispose of it. Listing 7-8 shows the code.
 
Search WWH ::




Custom Search