Question

I'm writing a simple OpenGL application that uses GLUT. I don't want to roll my own font rendering code, instead I want to use the simple bitmap fonts that ship with GLUT. What are the steps to get them working?

Was it helpful?

Solution

Simple text display is easy to do in OpenGL using GLUT bitmap fonts. These are simple 2D fonts and are not suitable for display inside your 3D environment. However, they're perfect for text that needs to be overlayed on the display window.

Here are the sample steps to display Eric Cartman's favorite quote colored in green on a GLUT window:

We'll be setting the raster position in screen coordinates. So, setup the projection and modelview matrices for 2D rendering:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0.0, WIN_WIDTH, 0.0, WIN_HEIGHT);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

Set the font color. (Set this now, not later.)

glColor3f(0.0, 1.0, 0.0); // Green

Set the window location where the text should be displayed. This is done by setting the raster position in screen coordinates. Lower left corner of the window is (0, 0).

glRasterPos2i(10, 10);

Set the font and display the string characters using glutBitmapCharacter.

string s = "Respect mah authoritah!";
void * font = GLUT_BITMAP_9_BY_15;
for (string::iterator i = s.begin(); i != s.end(); ++i)
{
    char c = *i;
    glutBitmapCharacter(font, c);
}

Restore back the matrices.

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top