Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using GLUT bitmap fonts

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?

like image 304
Ashwin Nanjappa Avatar asked Aug 18 '08 08:08

Ashwin Nanjappa


People also ask

Where are bitmap fonts used?

Bitmap fonts are used in the Linux console, the Windows recovery console, and embedded systems. Older dot matrix printers used bitmap fonts; often stored in the memory of the printer and addressed by the computer's print driver. Bitmap fonts may be used in cross-stitch.

What is glutBitmapCharacter OpenGL?

glutBitmapCharacter automatically sets the OpenGL unpack pixel storage modes it needs appropriately and saves and restores the previous modes before returning. The generated call to glBitmap will adjust the current raster position based on the width of the character.


1 Answers

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();
like image 159
Ashwin Nanjappa Avatar answered Oct 21 '22 09:10

Ashwin Nanjappa