Search code examples
openglperspectiveprojection-matrix

OpenGL when switch fron 2D to 3D


Here's what I want to achieve, I have a flag called switch_2D_3D in the code below, and when it's true I switch to 2D mode, otherwise 3D.

void reshape(GLsizei width, GLsizei height)
{
    if (switch_2D_3D)
    {
        // GLsizei for non-negative integer
        // Compute aspect ratio of the new window
        if (height == 0)
            height = 1;                // To prevent divide by 0

        GLfloat aspect = (GLfloat)width / (GLfloat)height;

        // Reset transformations
        glLoadIdentity();

        // Set the aspect ratio of the clipping area to match the viewport
        glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix

        // Set the viewport to cover the new window
        glViewport(0, 0, width, height);

        if (width >= height)
        {
            // aspect >= 1, set the height from -1 to 1, with larger width
            gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
        }
        else
        {
            // aspect < 1, set the width to -1 to 1, with larger height
            gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
        }

        winWidth = width;
        winHeight = height;
    } // 2D mode
    else
    {
        // Prevent a divide by zero, when window is too short
        // (you cant make a window of zero width).
        if (height == 0)
            height = 1;

        float ratio = width * 1.0 / height;

        // Use the Projection Matrix
        glMatrixMode(GL_PROJECTION);

        // Reset Matrix
        glLoadIdentity();

        // Set the viewport to be the entire window
        glViewport(0, 0, width, height);

        // Set the correct perspective.
        gluPerspective(45.0f, ratio, 0.1f, 100.0f);

        // Get Back to the Modelview
        glMatrixMode(GL_MODELVIEW);

        winWidth = width;
        winHeight = height;
    }// 3D mode
}

Everything works perfectly when drawing only in 2d mode, but when I change the flag to switch to the 3d mode, here comes the problem

Every time I resize the window, the things I draw in the 3d scene(for example a cube) would be come smallerand smaller, eventually disappeared, why is this happening

And if I switch back to 2D mode, everything in 2d mode still works fine, the problem is with the 3d mode

Also, if I start the program with the flag set to false, I would see a cube and it still gets smaller as I resize the window each time

Why is this happening?


Solution

  • You should look at your glLoadIdentity() / glMatrixMode() interactions.

    Right now, you have two different behaviors:

    In 2D: you're resetting your matrix for whatever is active when you enter the function, presumably GL_MODELVIEW, which causes the gluOrtho2D calls to "stack up".

    In 3D: you're always resetting the projection matrix, which seems more correct.

    Try swapping the order of the glLoadIdentity and glMatrixMode calls in your first path (2D) only.

    It's a wise idea to always explicitly set the matrix you want to modify before actually modifying it.