OpenGL / GLUT:glTranslatef和glRotatef在绘制立方体之前,还是之后?

时间:2014-12-02 19:31:06

标签: c opengl graphics rotation glut

我目前正在使用OpenGL和GLUT框架来玩粒子。

但是,我似乎可以让我的逻辑正确地用于旋转/翻译。

我当前情况的伪代码:

void display() {
     drawEnvironment();

     for each particle 'part' in the array {
          glPushMatrix(); // pushes the matrix for the current transformations? i.e. this particle?

          glRotatef(part.angles); // rotate this matrix based of it's own angles (constantly changing)

          drawParticle(part); // draws at origin

          glTranslatef(part.positon); // translate to the position

          glPopMatrix();
     }
}

我认为我在这里做的事情如下:

  1. 从堆栈中推送我需要的转换矩阵(对于当前粒子)
  2. 旋转所述矩阵(glRotatef围绕原点(0,0,0)旋转)
  3. 在原点绘制粒子,使其在现场旋转
  4. 将粒子转换为现在旋转的位置
  5. 将转换矩阵弹回堆栈
  6. 我也试过翻译 - >旋转 - >绘画,以及其他一些混音。

    如果没有快速视频,很难解释出现了什么问题: https://www.youtube.com/watch?v=G0ouhCKKcIM

    在被翻译之后看起来它正在旋转,因此它在原点周围旋转时会跟随那个更大的圆圈,而不是在它自己的轴上旋转。

1 个答案:

答案 0 :(得分:2)

应按此顺序应用转换:

  1. 从堆栈推送矩阵
  2. 转换为职位
  3. 旋转它
  4. 画出来
  5. 弹出矩阵
  6. 这是因为OpenGL通过预乘而工作。第一个操作是最后执行的。