views:

1978

answers:

3

I'm trying to use gluOrtho2D with glutBitmapCharacter so that I can render text on the screen as well as my 3D objects. However, when I use glOrtho2D, my 3D objects dissapear; I assume this is because I'm not setting the projection back to the OpenGL/GLUT default, but I'm not really sure what that is.

Anyway, this is the function I'm using to render text:

void GlutApplication::RenderString(Point2f point, void* font, string s)
{
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0.0, WindowWidth, 0.0, WindowHeight);

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    glDisable(GL_TEXTURE);
    glDisable(GL_TEXTURE_2D);

    glRasterPos2f(point.X, point.Y);
    for (string::iterator i = s.begin(); i != s.end(); ++i)
    {
     glutBitmapCharacter(font, *i);
    }

    glEnable(GL_TEXTURE);
    glEnable(GL_TEXTURE_2D);

    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
}

And, the render function is similar to this:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glLoadIdentity();

// Do some translation here.

// Draw some 3D objects.

glPopMatrix();

// For some reason, this stops the above from being rendered,
// where the camera is facing (I assume they are still being rendered).
Point2f statusPoint(10, 10);
RenderString(statusPoint, GLUT_BITMAP_9_BY_15, "Loading...");
+1  A: 

When I needed something similar I didn't try to push and pop the matrix states, I just set everything back from scratch each time:

void set2DMode()
{
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, w, h, 0, -1, 1);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}

void set3DMode()
{
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(50.0, (float) w / h, 1, 1024);
        gluLookAt(0, 0, 400, 0, 0, 0, 0.0, 1.0, 0.0);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}

void cb_display(void)
{
        set3DMode();
        // draw some stuff

        set2DMode();
        // draw some text
        // and some more
 }
Alnitak
+1  A: 

Your code looks okay. Most likely you've just messed up the matrix stack somewhere.

I suggest that you check if you forgot a glPopMatrix somewhere. To do so you can get the stack depth via glGet (GL_MODELVIEW_STACK_DEPTH). Getters for the other matrix-stacks are available as well.

Also you can take a look at the current matrix. Call glGetFloatv (GL_MODELVIEW_MATRIX, Pointer_To_Some_Floats) to get it. You can print out the floats each time you've set up a modelview or projection matrix. That way you should be able to to find out which matrix erratically ends up as the current matrix.

That should give you enough clues to find the bug.

Nils Pipenbrinck
A: 

You're in projection mode after calling the RenderString() function. Not sure if that would break anything, but it stood out to me.

Andrei Krotkov