The advantage of glXUseXFont() is that bitmaps for X glyphs in the font match exactly what OpenGL draws. This solves the problem of font matching between X and OpenGL display areas in your application.
To use display lists to display X bitmap fonts, your code should do the following:
The glXUseXFont() function automatically generates display lists (one per glyph) for a contiguous range of glyphs in a font.
Each glyph display list contains a glBitmap() call to render the glyph and update the current raster position based on the glyph's width.
Note: You can also use the glc library, which sits atop of OpenGL, for fonts and strings. The library is not specific to GLX and lets you do more than glXUseXFont().
Example 3-4 : Font and Text Handling
#include <GL/gl.h> #include <GL/glu.h> #include <GL/glx.h> #include <X11/Xlib.h> #include <X11/Xutil.h> GLuint base; void makeRasterFont(Display *dpy) { XFontStruct *fontInfo; Font id; unsigned int first, last; fontInfo = XLoadQueryFont(dpy, "-adobe-times-medium-r-normal--17-120-100-100-p-88-iso8859-1"); if (fontInfo == NULL) { printf ("no font found\n"); exit (0); } id = fontInfo->fid; first = fontInfo->min_char_or_byte2; last = fontInfo->max_char_or_byte2; base = glGenLists(last+1); if (base == 0) { printf ("out of display lists\n"); exit (0); } glXUseXFont(id, first, last-first+1, base+first); } void printString(char *s) { glListBase(base); glCallLists(strlen(s), GL_UNSIGNED_BYTE, (unsigned char *)s); } void display(void) { GLfloat white[3] = { 1.0, 1.0, 1.0 }; long i, j; char teststring[33]; glClear(GL_COLOR_BUFFER_BIT); glColor3fv(white); for (i = 32; i < 127; i += 32) { glRasterPos2i(20, 200 - 18*i/32); for (j = 0; j < 32; j++) teststring[j] = i+j; teststring[32] = 0; printString(teststring); } glRasterPos2i(20, 100); printString("The quick brown fox jumps"); glRasterPos2i(20, 82); printString("over a lazy dog."); glFlush (); }