Search code examples
openglglutfreeglut

Calling glRasterPos2i and glutBitmapString in the presence of ModelView transforms


I'm trying to display a text-overlay (basically a help screen which shows my keyboard shortcuts) on top of a 3D Texture I'm rendering. The texture works great and I've got some east-to-use rotations and translations for the user.

My thought was to use

const unsigned char tmp[100] = "text to render";

glRasterPos2i(x, y);

glColor4b(255, 255, 255, 255);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, tmp);

As recommended in How do I use glutBitmapString() in C++ to draw text to the screen? .

This works great except that the text now rotates with the object instead of remaining in a static location on the screen. I read some documentation and found that the glRasterPos functions are manipulated when you manipulate the model view matrix:

The object coordinates presented by glRasterPos are treated just like those of a glVertex command: They are transformed by the current modelview and projection matrices and passed to the clipping stage.

-Source

I then found via another post that you could push and pop the current matrix with glPushMatrix and glPopMatrix.

-Source

When I do this, the text disappears all together. At first I thought I might have had the coordinates wrong for the text, but I tried x=y=0 through x=y=25 in intervals of .01 and never saw the text. It's still possible I'm misunderstanding where this should be drawn, but I'm not sure what to try next.

My drawing function is calling something akin to:

glLoadIdentity();

glPushMatrix();

glTranslatef(0,0,-sdepth);

glRotatef(-stheta, 1.0, 0.0, 0.0);
glRotatef(sphi, 0.0, 0.0, 1.0);

glRotatef(rotateX,0,1,1);
glRotatef(rotateY,1,0,0);

glTranslatef(-0.5,-0.5,-0.5);

glPopMatrix();

glRasterPos2i(2, 2);

glColor4b(255, 255, 255, 255);
glutBitmapString(GLUT_BITMAP_HELVETICA_18, tmp);

Anyone have any recommendations for debug/troubleshooting steps to try to get this text to display in a single, static location on the screen?


Solution

  • Well, if glRasterPos is treated the same way as glVertex, then you need to set up proper projection (GL_PROJECTION) matrix (using gluOrtho2D) before calling glRasterPos.