Fonts

From OpenGL.org
Jump to: navigation, search

OpenGL doesn't support font rendering since OpenGL is a basic 3D API. Fonts require some OS interaction and font files to be installed, therefore core GL doesn't support fonts at all.

On Windows, Microsoft offers certain wgl functions that do font rendering :

wglUseFontBitmaps : this does rendering with glBitmap under the hood.

wglUseFontOutlines : despite the name, this generates geometry. In other words, this does 3D text. It probably use glBegin, glVertex, glEnd under the hood.

Both of those functions were great in 1995 and use display lists.

Here is an example from the MSDN from 1995.

Notice that depends on Fixed Function Pipeline.

The example doesn't show the call to CreateFont.

 HDC    hdc;  // A TrueType font has already been selected 
 HGLRC  hglrc; 
 GLYPHMETRICSFLOAT agmf[256]; 
 //create display lists for glyphs 0 through 255 with 0.1 extrusion 
 //and default deviation. The display list numbering starts at 1000 
 //(it could be any number) 
 wglUseFontOutlines(hdc, 0, 255, 1000, 0.0f, 0.1f, WGL_FONT_POLYGONS, &agmf); 
 //Set up transformation to draw the string 
 glLoadIdentity(); 
 glTranslate(0.0f, 0.0f, -5.0f) 
 glScalef(2.0f, 2.0f, 2.0f);
 //Display a string 
 glListBase(1000); // Indicates the start of display lists for the glyphs 
 //Draw the characters in a string 
 glCallLists(24, GL_UNSIGNED_BYTE, "Hello Win32 OpenGL World."); 

These days, it seems mostly newcomers use those while learning with old tutorials.

The Wiki recommends using something crossplatform, such as FTGL and there are many other libraries as well. Another option is to use whatever 2D graphics API is available on your platform (e.g. Cairo), as that usually includes high-quality text rendering, as well as rendering to an offscreen bitmap/pixmap. There will then usually be some way to transfer that bitmap/pixmap to OpenGL to use as a texture on a plane or some other object.