glPushMatrix() glPopMatrix() 不起作用

glPushMatrix() glPopMatrix() doesn't work

我尝试编写两个对象独立移动的代码,这意味着它们都在两个不同的方向上移动它有效但不能连续移动。当我把 glTranslatef() 放在 glPushMatrix() ... glPopMatrix() 外面时,它工作正常。

void display()
{
    glClearColor(0.356, 0.701, 0.0, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

        // if put gltransate() at here the object will moving continuous

    glPushMatrix();
      glTranslatef(.5, 0, 0);
        glBegin(GL_QUADS);
        glColor3f(1.0, 1.0, 1.0);
        glVertex2f(-0.8, 0.5);
        glVertex2f(-0.8, 0.8);
        glVertex2f(-0.2, 0.8);
        glVertex2f(-0.2, 0.5);
      glEnd();
    glPopMatrix();

    glPushMatrix();
          glTranslatef(-.5, 0, 0);
      glBegin(GL_QUADS);
        glColor3f(.0, .0, .0);
        glVertex2f(-0.8, 0.2);
        glVertex2f(-0.8, 0.5);
        glVertex2f(-0.2, 0.5);
        glVertex2f(-0.2, 0.2);
        glEnd();
    glPopMatrix();

}

我希望第一个方形对象会不断向右移动,但似乎只是平移一次然后停在该位置。

注意,通常我推荐使用像OpenGL Mathematics to do the matrix calculations and glLoadMatrix()这样的数学库来加载glm::mat4类型的矩阵到当前矩阵。

无论如何,像glTranslatef()这样的操作会创建一个新矩阵并将当前矩阵乘以新矩阵。这就是为什么连续调用 glTranslatef 导致渐进 "movement"。

glPushMatrix / glPopMatrix 在矩阵栈上存储和恢复矩阵。所以连续移动是行不通的,因为当前矩阵在每一帧的开始都保持不变。


一个解决方案是将翻译存储到一个变量并递增该变量:

GLfloat trans_a = 0.0f;
GLflaot trans_b = 0.0f;

void display()
{
    glClearColor(0.356, 0.701, 0.0, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

      trans_a += 0.5f;
      glTranslatef(trans_a, 0, 0);

      glBegin(GL_QUADS);
        glColor3f(1.0, 1.0, 1.0);
        glVertex2f(-0.8, 0.5);
        glVertex2f(-0.8, 0.8);
        glVertex2f(-0.2, 0.8);
        glVertex2f(-0.2, 0.5);
      glEnd();
    glPopMatrix();

    glPushMatrix();

      trans_b += 0.5f;
      glTranslatef(trans_b, 0, 0);

      glBegin(GL_QUADS);
        glColor3f(.0, .0, .0);
        glVertex2f(-0.8, 0.2);
        glVertex2f(-0.8, 0.5);
        glVertex2f(-0.2, 0.5);
        glVertex2f(-0.2, 0.2);
      glEnd();
    glPopMatrix();
}

一个更通用的解决方案是通过 glGetFloatv(GL_MODELVIEW_MATRIX, ...) and to reload it by glLoadMatrix():

获取和存储当前矩阵(转换后)
// init identity matrices
GLfloat mat_a[16] = {1, 0, 0, 0,    0, 1, 0, 0,    0, 0, 1, 0,    0, 0, 0, 1 };
GLfloat mat_b[16] = {1, 0, 0, 0,    0, 1, 0, 0,    0, 0, 1, 0,    0, 0, 0, 1 };

void display()
{
    glClearColor(0.356, 0.701, 0.0, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glPushMatrix();

      glLoadMatrixf(mat_a)
      glTranslatef(0.5f, 0, 0);
      glGetFloatv(GL_MODELVIEW_MATRIX, mat_a);

      glBegin(GL_QUADS);
        glColor3f(1.0, 1.0, 1.0);
        glVertex2f(-0.8, 0.5);
        glVertex2f(-0.8, 0.8);
        glVertex2f(-0.2, 0.8);
        glVertex2f(-0.2, 0.5);
      glEnd();
    glPopMatrix();

    glPushMatrix();

      glLoadMatrixf(mat_b)
      glTranslatef(0.5f, 0, 0);
      glGetFloatv(GL_MODELVIEW_MATRIX, mat_b);

      glBegin(GL_QUADS);
        glColor3f(.0, .0, .0);
        glVertex2f(-0.8, 0.2);
        glVertex2f(-0.8, 0.5);
        glVertex2f(-0.2, 0.5);
        glVertex2f(-0.2, 0.2);
      glEnd();
    glPopMatrix();
}

注意,在这种情况下mat_amat_b应该在视图矩阵初始化之后加载,当视图矩阵发生变化时,矩阵不会考虑。