17 Using Fonts
OpenGL doesn't provide direct font support, so the application must use any of OpenGL's other features for font rendering, such as drawing bitmaps or pixmaps, creating texture maps containing an entire character set, drawing character outlines, or creating 3D geometry for each character.
Use bitmaps or pixmaps
The most straightforward method for rendering simple fonts is to use a glBitmap() or glDrawPixels() call for each character. The result is simple 2D text, which is suitable for labeling GUI controls, annotating 3D parts, etc.
glBitmap() is the fastest and simplest of the two, and renders characters in the current color. You can also use glDrawPixels() if required. However, note that glDrawPixels() always draws a rectangle, so if you desire a transparent background, it must be removed with alpha test and/or blending.
Typically, each glBitmap() call, one for every glyph in the font, is stored in an individual display list, which is indexed by its ASCII character value. Thus, a single call to glCallLists() can render an entire string of characters.
In X Windows, the glXUseXFont() call is available to create these display lists painlessly from a given font.
If you're using Microsoft Windows, look at the MSDN documentation for wglUseFontBitmaps(). It's conceptually identical to glXUseXFonts().
For GLUT, you need to use the glutBitmapCharacter() routine, which generates a bitmap for the specified character from the specified GLUT bitmap font.
Use texture mapping
In many OpenGL implementations, rendering glBitmap() and glDrawPixels() primitives is inherently slower than rendering an equivalent texture mapped quad. Use texture mapped primitives to render fonts on such devices.
The basic idea is to create a single texture map that contains all characters in a font (or at least all the characters that need to be rendered). To render an individual character, draw a texture mapped quad with texture coordinates configured to select the desired individual character. If desired, you can use alpha test to discard background pixels.
Follow this link to info on texturemapped fonts, as well as other OpenGL tidbits.
A library for using texture mapped fonts can be found here. It comes with source code.
The GLUT source distribution comes with a texture mapped font demo.
The NeHe web page has a tutorial on using texture mapped fonts.
If you're using Microsoft Windows, look up the MSDN documentation on wglUseFontOutlines(). It contains example code for rendering stroked characters.
The glutStrokeCharacter() routine renders a single stroked character from a specified GLUT stroke font.
The NeHe web page has a tutorial for rendering geometric fonts. Look for the tutorial on outline fonts.
The NeHe web page has tutorials that show how to use TrueType fonts in a variety of ways.
See the NeHe web page for a tutorial on using geometric fonts. Look for the tutorial on outline fonts.
GLTT supports geometric TrueType fonts in OpenGL. It was formerly available from http://www.moonlight3d.org/gltt/, but fortunately is still available around the Web. Download GLTT v 2.4 (~125KB).
Glut 3.7 has an example called progs/contrib/text3d.c that may be informative.