17 Using Fonts

17.010 How can I add fonts to my OpenGL scene?

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.

You can find instructions on using texture mapped fonts here. This site also contains image files of fonts that you can download and read into your program to use as font texture maps. Additional useful information on other aspects of using OpenGL can be found at this site as well.

A library for using texture mapped fonts can be found here. It comes with source code.

Additional extensive information on texture mapped text and example code, can be found here.

The NeHe web page has a tutorial on using texture mapped fonts.

Stroked 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.

Geometric fonts

The NeHe web page has a tutorial for rendering geometric fonts. Look for the tutorial on outline fonts.

17.020 How can I use TrueType fonts in my OpenGL scene?

The NeHe web page has tutorials that show how to use TrueType fonts in a variety of ways.

See the Free Type library.

17.030 How can I make 3D letters, which I can light, shade, and rotate?

See the NeHe web page for a tutorial on using geometric fonts. Look for the tutorial on outline fonts.

See the Free Type library.

GLTT, formerly available from http://www.moonlight3d.org/gltt/, supports geometric TrueType fonts. Click here to download GLTT (~125KB).

Glut 3.7 has an example called progs/contrib/text3d.c that may be informative.