TUTORIAL 1 - OpenGL Fundamentals

1  2  3  4  5  6  7  8  9  10  11  12  13

Text Output with OpenGL

There are a couple of options to print some text on the screen with OpenGL. The simplest (but unfortunately windows specific and therefore not suitable for Linux) is to use the windows support for truetype fonts by using the wglUseFontOutlines function. The basic idea behind using these fonts is to first define all relevant parameters of the font (like font size, name, style,…), create the font, make display lists for each character (these display lists are what OpenGL actually displays when we print text on the screen), delete the font (but keep the display lists) and finally we can print the text by using the glCallList OpenGL command. Lets take a closer look at the structure with the font parameters:

	LOGFONT logfont;

	logfont.lfHeight = -12;        // setup font characteristics
	logfont.lfWidth = 0;
	logfont.lfEscapement = 0;
	logfont.lfOrientation = 0;
	logfont.lfWeight = FW_NORMAL;
	logfont.lfItalic = FALSE;
	logfont.lfUnderline = TRUE;
	logfont.lfStrikeOut = FALSE;
	logfont.lfCharSet = ANSI_CHARSET;
	logfont.lfOutPrecision = OUT_DEFAULT_PRECIS;
	logfont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	logfont.lfQuality = PROOF_QUALITY;
	logfont.lfPitchAndFamily = DEFAULT_PITCH || FF_ROMAN;
	strcpy(logfont.lfFaceName,"Verdana");

The parameters in this structure are pretty self explanatory. Now that we have done all the preparatory work for creating the font, we can finally create it and use it for the creation of the display list:

	hFont = CreateFontIndirect(&logfont);
	SelectObject (hdc, hFont);
	nFontList = glGenLists(128);
	wglUseFontOutlines(hdc, 0, 128, nFontList, 0.0f, 0.5f, WGL_FONT_POLYGONS, agmf);
	DeleteObject(hFont);

With the display list in place, we can use it to print the text to the screen:

      glListBase(nFontList);
      glCallLists (11, GL_UNSIGNED_BYTE, "Text Output");

In glCallLists we first define the number of display lists to be executed (one for each character we want to print on the screen), the next parameter is the type of values in the list, which is in our case GL_UNSIGNED_BYTE, and finally comes the text we want to put on the screen). Since this text is basically a normal OpenGL object, we can use all the OpenGL commands to manipulate it (for example set the color with glColor3f, set the Size with glScalef etc.). Now we have a rotating 3D object with text output.

previous   next