Using Display Lists for Text Output

The OpenGL WGL library, has a couple of functions to help render fonts.

Both these functions use the Device Context's (DC) currently selected fonts. This must be a truetype font.

This functionallity works well with the glCallLists(), which can take an array of bytes and call each display list in the array. Actually, glCallLists() adds a fixed value to each byte to determine which display lists to call. This value is set using glListBase().

Remember that an string in C/C++ is simply an array of bytes. We will use this fact, in conjunction with these function to implement a simple text displaying framework.

2D Text

First thing to do is select a truetype font and use wglUseFontBitmaps to create a disply list for each character in the font. This is normally done in the initialisation stages of a program (Prog_init()).

//globally defined
int font_list_base_2d=2000; // set the start of the display lists for the 2d font
	
...
	
// somewhere in Prog_Init()....
	
HFONT font = CreateFont(40, 0, 0, 0,
                           FW_NORMAL, FALSE, FALSE, FALSE,
                           ANSI_CHARSET, 0,
							0,0,0,"Times New Roman"); // can only use true type fonts 
													// 	this one is 40 points in size

SelectObject(g_hdc, font);
	
wglUseFontBitmaps(g_hdc,0,127,font_list_base_2d); // create a display list for each character (0-127)
													// start numbering the display lists at font_list_base_2d

 

Note; due to the nature of the Bitmaps in OpenGL, the 2d fonts will not be effected by the transformation matrices.

The function glRasterPos*() sets the start position of bitmap drawing. The 3-D point set by glRasterPosition is effected by transformations. If this point is outside any of the clipping planes, the text is not printed.

glCallLists() is then used to call the display list for every character in the array (string).

Note that the display list for eaach character contains transformations to ensure that the next character's relative position is correct.

 

glColor3d(1.0,0.0,0.0);// red
	
glRasterPos3i(-1,0,-5); // set start position

glListBase(font_list_base_2d); //start of our font display list numbers 

glCallLists(12, GL_UNSIGNED_BYTE, "Hello World."); 


 

3D Text

The procedure for drawing 3d text is similar, except wglDrawFontOutlines() is used to create adisplay list which renders a polygon mesh for each font, .

//globally defined
int font_list_base_3d=2000; // set the start of the display lists for the 2d font
	
...
// somewhere in Prog_Init()....

HFONT font = CreateFont(40, 0, 0, 0,
                           FW_NORMAL, FALSE, FALSE, FALSE,
                           ANSI_CHARSET, 0,
							0,0,0,"Times New Roman"); // can only use true type fonts

SelectObject(g_hdc, font);


wglUseFontOutlines(g_hdc,0,127,font_list_base_3d, 0.0f, 0.1f,WGL_FONT_POLYGONS,NULL); 
		// generate a display list for every character (up to ascii 127)
		// generate as accurately as possible (0.0f deviation)
		// extrude by 0.1 units
		// generate polygons (WGL_FONT_POLYGONS) (not lines) to render the fonts
	

3D font Display lists (Font Outlines), are normal 3D objects, which can be transformed, lit etc.

 

glPushMatrix(); // save transforms

glListBase(font_list_base_3d); //start of our font lists
glCallLists(12, GL_UNSIGNED_BYTE, "Hello World."); // 12 characters to be displayed

glPopMatrix(); // restore transforms

This is a windows executable, showing 3d text. The full source code is here.