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?
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();

- 76,204
- 83
- 211
- 292
-
I have a few objects in 3D world co-ordinates and I need to display overlay text at a fixed position on the viewport. I tried this approach, but it doesn't give me fixed overlay text. On the contrary, it gives me text which rotates as the camera moves. How do you get this to work? – Papouh Sep 17 '13 at 09:17
-
@Papouh: That is strange. If you notice, the calls I'm using are 2D, so for the window. They should not move around in 3D. – Ashwin Nanjappa Sep 19 '13 at 07:41
-
@Ashwin: I was doing some things wrong, when I wrote the previous comment. But the main problem was that I was using `glOrtho2D` as in your example instead of `glOrtho`. That resolved the problem. – Papouh Sep 19 '13 at 17:10
-
On occasion this can require some modification to work. See this post: http://stackoverflow.com/a/21923064/1767377 – SyntaxRules Feb 21 '14 at 01:48