0

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?

RushSykes
  • 41
  • 1
  • 9
  • I think you should stop thinking in terms of "2D vs. 3D". That distinction makes no sense, truth to be told. What you're switching there is the projection and of course you can use an orthographic projection also for 3D scenes. – datenwolf Dec 03 '16 at 10:17

1 Answers1

0

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.

ltjax
  • 15,837
  • 3
  • 39
  • 62
  • Thanks for the answer. I tried this but now I can't see the 2d scene anymore, and the cube in the 3d scene still gets smaller. – RushSykes Dec 03 '16 at 10:42
  • Well, depending on how you handle rotations, you might want to reset the model view matrix too, like you did before. It's not a good idea, but that's what changed when you swapped the call order – ltjax Dec 03 '16 at 11:01
  • oh yes I got it, I use projection mode matrix for both scenes, so I should not set the matrix to model view again....Thanks again! – RushSykes Dec 03 '16 at 11:06