Next: 6 Conclusion Up: The Design of the Previous: 4.12 API not Protocol

5 Example: Three Kinds of Text

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 5a. glListBase sets the display list base so that the subsequent glCallLists references the characters just defined. glCallLists invokes a series of display lists specified in an array; each value in the array is added to the display list base to obtain the number of the display list to use. In this case the array is an array of bytes representing a string. The second argument to glCallLists indicates the length of the string; the third argument indicates that the string is an array of 8-bit bytes (16- and 32-bit integers may be used to access fonts with more than 256 characters).

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 5b). More generally, the display lists could contain both polygons and line segments, and these could be antialiased.

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 5c).



Next: 6 Conclusion Up: The Design of the Previous: 4.12 API not Protocol


segal@asd.sgi.com
Fri Sep 23 17:28:42 PDT 1994