simple way to draw TrueType Fonts in OpenGL
#include <windows.h>;
#include <gl.h>;
#include <glu.h>;
#define LISTBASE 10000
GLYPHMETRICSFLOAT fontlist[256];
HFONT Font;
BOOL glMakeFont(HDC ghDC, PCHAR FontName, LONG FontWeight, BOOL FontItalic)
{
Font=CreateFont(
1, // Height (does'nt matter here ...)
0, // Width
0, // Angle
0, // baseline escapement
FontWeight, // Weight
FontItalic, // italic?
FALSE, // underline?
FALSE, // strikeout?
DEFAULT_CHARSET, // character set
OUT_DEFAULT_PRECIS, // output precision
CLIP_DEFAULT_PRECIS, // clipping precision
DEFAULT_QUALITY, // default output quality
DEFAULT_PITCH | // fontpitch
FF_DONTCARE, // font family
FontName // font name
);
// select the just created font into current device context
SelectObject(ghDC, Font);
return wglUseFontOutlines(
ghDC, // the DC
0, // first letter
255, // last letter
LISTBASE, // start of ListBase
0.0f, // deviation from original outlines
0.0f, // `depth` of the letters
WGL_FONT_POLYGONS, // make Polygons
fontlist // where to write all the data
);
}
// print (nearly) aligned to center
void glPrint(PCHAR text)
{
// take the width of letter n since it seems to be a good avarage
glTranslatef(-fontlist[110].gmfCellIncX*strlen(text)*0.5f, 0.0f, 0.0f);
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);
}
So after creating the fontlist (the WM_CREATE event of your OpenGL window seems to be a good choice) with bold italic “Arial”
glListBase(LISTBASE);
if ( !glMakeFont(ghDC, "Arial", FW_BOLD, TRUE) )
{
// your error message
}
you are able to draw a text at the current position in your OpenGL window with
glPrint("This is a test");
and you can now make all the funny stuff with this text like rotating or moving … Watch it in action with the Bärenzwinger screen saver.