With OpenGL, how can I use gluOrtho2D with the default projection correctly?

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

Anyway, this is the function that I use 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 rendering function is like 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...");
+3
source share
4 answers

Your code looks fine. Most likely, you just mixed up the matrix stack.

, glPopMatrix. glGet(GL_MODELVIEW_STACK_DEPTH). Getters .

. glGetFloatv(GL_MODELVIEW_MATRIX, Pointer_To_Some_Floats), . , . , , .

, .

+1

- , , :

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
 }
+3

RenderString(). , -, .

0

I agree with Andrey. Basically what you do: on the second and all subsequent frames immediately after working with glClear with the projection matrix, since the last operation was glMatrixMode (GL_PROJECTION); at the end of a RenderString. (at least looking at the code you posted).

Try putting glmatrixMode (GL_MODELVIEW) immediately after glClear.

0
source

Source: https://habr.com/ru/post/1706180/


All Articles