2
votes

I'm having troubles with rotation. I can get the cube to rotate just fine but I only want that cube to rotate. I know I have to use glPushMatrix() and glPopMatrix() but everytime I wrap those two around my drawing, it stops rotating.

Here is how I am setting up OpenGL:

void initOpenGL()
{
    //these are the current version of OpenGL
    string versions[14] =
    {
        "GL_VERSION_1_1",
        "GL_VERSION_1_2",
        "GL_VERSION_1_3",
        "GL_VERSION_1_4",
        "GL_VERSION_1_5",
        "GL_VERSION_2_0",
        "GL_VERSION_2_1",
        "GL_VERSION_3_0",
        "GL_VERSION_3_1",
        "GL_VERSION_3_2",
        "GL_VERSION_3_3",
        "GL_VERSION_4_0",
        "GL_VERSION_4_1",
        "GL_VERSION_4_2"
    };

    //Determine which versions are safe to use
    cerr << "OpenGL+GLEW Info: " << endl;
    for (int i = 0; i < 14; ++i)
    {
        if (glewIsSupported(versions[i].c_str()))
            cerr << versions[i] << " is supported" << endl;
        else
            cerr << "ERROR: " << versions[i] << " is not supported" << endl;
    }
    cerr << endl << "Open GL " << glGetString (GL_VERSION) << " is the current OpenGL version" << endl << endl;

    glClearColor(0, 0, 0, 0);
    //By setting this option, we can explicityly control how the program terminates to ensure proper cleanup
    glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);

    //setup the camera
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(MIN_X, MAX_X, MIN_Y, MAX_Y, MIN_Z, MAX_Z);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //enable access to the alpha channel
    glEnable (GL_BLEND);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

... some more code to load textures...
}

And here is my display function:

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

    glPushMatrix();
        glColor3f(1, 0, 0);
        glRotatef(angle, 1, 1, 0);
        glutSolidCube(30);
    glPopMatrix();

    glutSwapBuffers();
}
1
I just played around some more, and the glPopMatrix(); is the line in display that stops the rotation from happening. Any idea what might be going on?Steven

1 Answers

4
votes

You basically seem to rotate always to same angle, because popping the matrix returns it to the state in which it was when push was called. You might want to try increasing angle variable a bit on every display call (or somewhere else in your code so that it continuously changes).

The reason why it works without push and pop is that all matrix operations are cumulative and as you don't load identity matrix on each round you end up rotating by value of variable angle each time display function is called. By the way this is probably something you want to change going forward as it probably makes sense to separate state of your world from displaying it.