Game Development Reference
Using textures with OpenGL
The last major part in the armoury of our 3D engine must be texture
mapping. When low polygon scenes use bitmaps on the surface of the
polygons they are transformed from ridiculously simplistic to at least
vaguely plausible, and as the polygon count goes up they can become
ever more realistic. Texture mapping is no black art and OpenGL makes
it quite easy to achieve. The main stages to placing textures on your
1 Loading the bitmaps into memory.
2 Informing OpenGL how the bitmaps are stored in memory.
3 Creating a texture ID number.
4 Binding the texture ID and setting up wrapping and filtering
5 Generating the texture object.
6 Calculating texture coordinates for each vertex of each polygon that
uses a texture.
7 Choosing a texture rendering mode.
8 Enabling textures.
Let's look at each of these in turn.
Loading the bitmaps into memory
In Chapter 6 we will explore the techniques necessary to load windows
bitmap files (bmp), Truevision Targa files (tga) and JPEG compliant files
(jpg). In all the loaders the result is converted to an uncompressed 24-bit
windows bitmap. Windows bitmaps are aligned in such a way that each
raster line is exactly divisible by 4 (DWORD aligned). But another aspect
of the loader is to ensure that the image stored in memory has sides that
are an exact power of 2. If the image was saved as (150
62), then it will
be loaded then resized to 128
32. Here the next power of 2 down is
used. The resizing method is simply to duplicate or delete pixels and does
not contain any intelligent filtering technique. If you do use the loading
source code provided, then I recommend resizing your bitmaps using a
good bitmap editor, such as Paint Shop Pro or Photoshop; the result will
be far better since good filtering algorithms are used in the resize. Space
prohibits going into great detail regarding bitmap loaders and file formats.
If you intend to write your own loader then get hold of a good topic on
bitmap file formats. For most purposes the loaders that come with this