0

我尝试编写两个独立移动的对象,这意味着它们都在移动两个不同的方向,但它不能连续移动。当我放在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();

}

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

4

1 回答 1

2

请注意,一般来说,我建议使用像OpenGL 数学这样的数学库来进行矩阵计算并将glLoadMatrix()类型的矩阵加载glm::mat4到当前矩阵。

无论如何,像这样的操作glTranslatef()会创建一个新矩阵并将当前矩阵乘以新矩阵。这就是为什么连续呼吁glTranslatef引起进步的“运动”。

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, ...)并通过 重新加载它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应该在视图矩阵初始化之后加载,并且当视图矩阵发生变化时,矩阵不会考虑这一点。

于 2019-06-16T09:05:18.667 回答