To illustrate the flexibility of OpenGL in performing different types of rendering tasks, we outline three methods for the particular task of displaying text. The three methods are: using bitmaps, using line segments to generate outlined text, and using a texture to generate antialiased text.
The first method defines a font as a series of display lists, each of which contains a single bitmap:
for i = start + 'a' to start + 'z' {
glBeginList(i);
glBitmap( ... );
glEndList();
}
glBitmap specifies both a pointer to an encoding
of the bitmap and offsets that indicate how the bitmap is positioned
relative to previous and subsequent bitmaps.
In GLX,
the effect of defining a number of display lists in this way
may also be achieved by calling glXUseXFont.
glXUseXFont generates a number of display lists,
each of which contains the bitmap (and associated offsets) of a single
character from the specified X font.
In either case,
the string ``Bitmapped Text''
whose origin is the projection of a location in 3D is produced
by
glRasterPos3i(x, y, z);
glListBase(start);
glCallLists("Bitmapped Text",14,GL_BYTE);
See Figure The second method is similar to the first, but uses line segments to outline each character. Each display list contains a series of line segments:
glTranslate(ox, oy, 0);
glBegin(GL_LINES);
glVertex(...);
...
glEnd();
glTranslate(dx-ox, dy-oy, 0);
The initial glTranslate updates the
transformation matrix to position the character with respect to a character
origin.
The final glTranslate updates that character origin in preparation
for the following character.
A string is displayed with this method just as in the
previous example, but since line segments have 3D position, the text
may be oriented as well as positioned in 3D (Figure Finally, a different approach may be taken by creating a texture image containing an array of characters. A certain range of texture coordinates thus corresponds to each character in the texture image. Each character may be drawn in any size and in any 3D orientation by drawing a rectangle with the appropriate texture coordinates at its vertices:
glTranslate(ox, oy, 0);
glBegin(GL_QUADS)
glTexCoord( ... );
glVertex( ... );
...
glEnd();
glTranslate(dx-ox, dy-oy, 0);
If each group of commands for each character is enclosed in
a display list, and the commands for describing the texture image itself
(along with the setting of the list base) are enclosed in another display
list called TEX,
then the string ``Texture mapped text!!'' may be displayed by:
glCallList(TEX);
glCallLists("Texture mapped text!!",21,
GL_BYTE);
One advantage of this method is that,
by simply using appropriate texture filtering,
the resulting characters are antialiased (Figure 