Creating Real Time 3D Graphics With OpenGL
Pages: 1, 2, 3

Texture mapping

Several years ago some high-IQ types realized that if you could wrap two-dimensional images around three-dimensional objects, you could make them look much more realistic. Rather than trying to represent all the detail of a scene with individual geometry, it also turns out to be much faster to texture map fine details onto a surface than add many more triangles.

Texture maps are so important that many consumer-level 3D-accelerated cards now include two independent texture rendering units in silicon. This means two different texture maps can be applied to an object in one pass. So, for example, one texture might be the usual surface texture of a brick wall, and the other texture might be a light-map from a nearby lightbulb.

Texture mapping is extremely computationally intensive, and it becomes even more so as additional filtering is turned on. The filtering is desired in order to make the textures look reasonable when they're displayed at sharp angles or at a partial magnification. The use of mipmaps can also aid in making textures look better when they are viewed from far away. Mipmaps don't take more CPU time (except at creation), but they do require more memory.

Figure 3. Texture mapping is computationally intensive.

Figure 3. Texture mapping is computationally intensive.

Textures can be applied to an object in a variety of ways for different effects and can include an alpha or transparency channel. Depending on the mode chosen, a texture can be applied as a decal to an object, it can simply replace the object's color, or it can be blended or modulated with the object's existing color. Using textures with an alpha channel, it's possible to "punch holes" in an object or make the edge of an object appear irregular.

Texture mapping is an art form, with the need for creative tricks with patterns and an artist's hand to create the textures themselves. Any graphics program can be used to create RGB (color) textures, and advanced programs like Photoshop and the Gimp can be used to create RGBA (color with alpha channel) textures. Textures can also be created dynamically by way of the creative use of code.

It's important to note that most OpenGL applications are limited in the size of the textures that are allowed, some as small as 256 pixels square. Textures' sizes must also be a power-of-2, although the width and height need not be the same. For example, a texture sized 128 by 4 pixels is valid, while one 60 by 30 is not.

OpenGL solutions for Linux

As mentioned above, both the OpenGL SI from SGI and the GPLed Mesa are available for Linux. Mesa is often included with Linux distributions, at least in the form of client libraries. Versions intended for developers, with the required header files, aren't always included, however, and might need to be downloaded and installed. A quick search on for OpenGL will also find many available miniGLs targeted at game developers.

Later versions of MesaGL are also able to leverage on installed 3dfx Voodoo2/3, Matrox G200/400, ATI Rage Pro, or Intel i810 video cards to provide hardware acceleration, to some degree or another. However, because of technical limitations in XFree86 3.x, accelerated 3D-in-a-window is almost impossible to do, and Mesa can only provide acceleration for full-screen mode.

Thanks to SGI's contribution to the open source community of GLX, a special high-speed, direct-to-hardware-within-X rendering infrastructure they developed for their own IRIX environment, the full-screen-only problem under XFree86 has been solved. First SGI, then RedHat, then several other video card vendors paid Precision Insight to port GLX and Mesa to work with XFree86 4.0 and a variety of accelerated 3D cards in a fully integrated solution called the Direct Rendering Infrastructure (DRI). The DRI allows windows to be rendering using available hardware and can even handle accelerating more than one window at a time.

Although it will take a few months for XFree86 4.0 to become available as an off-the-CD install option for common Linux distributions, when it does there will be cheering from a great many 3D video card owners. For those who can't wait, the complete DRI codebase (including XFree 86 4.0) is available from CVS on Details are available at (, but be warned: This is not for end-users yet.

For those who need 3D acceleration-in-a-window today, and don't want to deal with ".0" code, a few commercial solutions do exist. For example, Xi Graphics offers their 3D Accelerated-X product, with several drivers available for specific cards. For most people, though, simply waiting until XFree86 4.0 becomes available will be the easiest and cheapest route.

OpenGL: the cross-platform choice

Figure 4. With OpenGL you can throw together some code which results in clients' and friends' mouths dropping open.

Figure 4. With OpenGL you can throw together some code which results in clients' and friends' mouths dropping open.

OpenGL is the only truly cross-platform 3D API available that is designed for hardware acceleration. Write an application using OpenGL to run on one system, and it should port to another platform with little more than running a build in the new environment. Linux to IRIX to Windows; there's even a MiniGL for the Palm.

OpenGL's ability to port to Windows is probably what prompted Microsoft to develop Direct3D, a competing API that many Windows game developers are using. Direct3D is now up to version 7, and it's beginning to look promising. The problem is, of course, that it's limited to a single platform.

OpenGL continues to be the only choice for most high-end, real-time applications, particularly in the simulation, scientific, and technical fields. Many 3D environment systems are also built on top of OpenGL to do the actual rendering. For example, SGI's IRIX Performer and id Software's Quake SDK both use OpenGL, but abstract the developer from much of the low-level detail.

Of course, the low-level details are exactly what some developers are after, and getting down and dirty may be the only way to achieve some desired effect or another. Knowing the right tool for a particular problem is of course a large part of a programmer's skill-set.

Having a working knowledge of OpenGL can be a very empowering ability. You can quickly throw together a bit of code which results in clients', friends', and coworkers' mouths dropping open. It can also help to understand what an application that uses OpenGL is actually doing to produce its images.

And, last but not least, programming 3D applications is simply fun! With ever more powerful hardware coming into the hands of more and more users, and SGI actively encouraging OpenGL's deployment on Linux, the possibilities are only going to become more exciting over time.

Chris Halsall is the Managing Director of Ideas 4 Lease (Barbados). Chris is a specialist... at automating information gathering and presentation systems.

Discuss this article in the O'Reilly Network Forum.

Return to the O'Reilly Network Hub.