2D text in a 3D world

I’ve tried to do 2D text in my 3D world, but I with no success so far.
I’ve tried going to gluOrtho2D and then using glRasterPos2D to place the text, and then glutBitmapCharacter to actually draw the text.
The problem is, I don’t exactly know how to set up gluOrtho2D, and then what coordinates to put in glRasterPos2D (I’ve tried 0,1,0,1 in gluOrtho2D and then something like 0.4,0.4 in glRasterPos2D but that didn’t work).
If someone can please point me in the right direction, including what matrix operations I need to do (like glPushMatrix or glMatrixMode to go to the projection matrix maybe?).

Thanks.

The glRasterPos2D function looks like this:

glRasterPos2D(int x, int y);

You are not allowed to put float-digits as parameters.

Hi Tidhar,

if you switch from 3d to 2d you can use:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho (0.0, g_width, g_height,0, -1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

This will give you a rectangle with range (0,0) to (g_width,g_height). You may set g_width and g_height to the size of your window.

Then you can do all your 2d drawing and text output (BTW void glRasterPos2f(GLfloat x, GLfloat y); is perfectly legal) and then reset your matrices:

glEnable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Please note that this restores your projection matrix but not your modelview matrix. But I guess that you’ll update it each frame…

Hope that helps,
Stefan

Hi,

I’m trying to do the same, raster a 2d text. It works fine, but after restoring the projection and modelview matrixs something doesn’t work properly with selection. I always get a hit wherever I put the mouse pointer. It seems like something is wrong with the modelview restoration. I don’t know what I’m missing.

Thats’s a code snippet of what I’m doing:

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, (GLfloat)(viewport[2] - viewport[0]), 0.0, (GLfloat)(viewport[3] - viewport[1]), -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glPushAttrib(GL_LIST_BIT);
glListBase(IDBASELISTAFUENTETEXTO);

glRasterPos2d(…);
glCallLists(…); //to draw the text

glPopAttrib();

glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();

I’ve even tried to use glPushAttrib(GL_ALL_ATTRIB_BITS) and glPopAttrib()…

Thanks in advance,

Ignasi