2

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?

Community
  • 1
  • 1
user986122
  • 365
  • 5
  • 16

2 Answers2

1

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.

SigTerm
  • 26,089
  • 6
  • 66
  • 115
  • I did that earlier in the code and it worked great with just the quad texture I'm drawing. Is there any reason I'd need to do it again? – user986122 Jan 06 '12 at 05:03
0

Give this a shot:

#include <GL/glut.h>
#include <string>

using namespace std;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3ub(255,0,0);
    glPushMatrix();
        glScalef(5,5,5);
        glBegin(GL_QUADS);
            glVertex2f(-1,-1);
            glVertex2f(1,-1);
            glVertex2f(1,1);
            glVertex2f(-1,1);
        glEnd();
    glPopMatrix();

    glColor3ub(0,255,0);    // A
    glRasterPos2i(0,0);     // B

    string tmp( "wha-hey!" );
    for( size_t i = 0; i < tmp.size(); ++i )
    {
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, tmp[i]);
    }

    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    double aspect_ratio = (double)w / (double)h;
    glOrtho(-10*aspect_ratio, 10*aspect_ratio, -10, 10, -1, 1);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(800,600);
    glutCreateWindow("Text");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return EXIT_SUCCESS;
}

Oddly enough swapping lines A and B causes the glColor3ub() call to not take effect. I think that's what you were running into with the code sequence you posted.

As an aside glColor4b() takes chars which max out at 127. You should switch to glColor4ub() if you want to persist in passing in 255.

Documented here ("The sequence of glRasterPos(), glColor(), glBitmap() doesn't result in the desired bitmap color"), but no explanation given :(

EDIT: Ah ha! The current raster position contains its own color state, which is only updated during a glRasterPos() call.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • I think you're on to something. I figured I'd start trying out those glGet functions to see what some of the raster values are. The GL_CURRENT_RASTER_COLOR value is correct (MAX_INT,MAX_INT,MAX_INT,MAX_INT), but the raster position (which I set to (1,1)) is set to (0,0,0,0), which is incorrect. I checked the GL_CURRENT_RASTER_POSITION_VALID value and it's NOT GL_TRUE. I cast it to an int and it was 0. I'm guessing this means there's something wrong with how I'm positioning the text. Perhaps it has to do with the original projection matrix I set up? I'll let you know what I find. – user986122 Jan 06 '12 at 05:12
  • So far the only position I can get to work is 0,0,0,0. It returns true, but all other positions fail. – user986122 Jan 06 '12 at 05:35
  • **SOLVED!** I decided I'd read that article you linked more completely and I ended up just using the window_pos function they wrote. It correctly set the position. Thanks for the help! – user986122 Jan 06 '12 at 16:48