Opengl glLoadIdentity和glPushMatrix/glPopMatrix,为什么不使用前者呢?

Opengl glLoadIdentity和glPushMatrix/glPopMatrix,为什么不使用前者呢?,opengl,graphics,Opengl,Graphics,将变换应用于对象时,我们使用glPushMatrix/GLPOPMARRIX。但是为什么我们不只使用glLoadIdentity呢 因此 这就是应该怎么做的对吗 可以成为 glLoadIdentity() ..apply tranformations ...draw object glLoadIdentity() ..apply tranformations ...draw object 因为你不能这样做: glPushMatrix() ..ap

将变换应用于对象时,我们使用glPushMatrix/GLPOPMARRIX。但是为什么我们不只使用glLoadIdentity呢

因此

这就是应该怎么做的对吗

可以成为

    glLoadIdentity()
    ..apply tranformations
    ...draw object
    glLoadIdentity()
    ..apply tranformations
    ...draw object

因为你不能这样做:

glPushMatrix()
..apply tranformations
glPushMatrix()
..apply ADDITIONAL transformations
..draw object with combined transforms
glPopMatrix()
...draw object without the additional transforms.
glPopMatrix()

我找到了一个很好的解释:

它们没有相同的效果

glLoadIdentity将当前矩阵替换为标识矩阵

glPushMatrix将当前矩阵推送到堆栈上。然后你可以拉 将顶层矩阵从堆栈中移除,并在以后使用 GLPOP矩阵

只有在以下情况下,它们才是相同的:

  • 您的相机始终固定在(0,0,0)沿z方向观看

  • 您的所有模型都是单一的刚性形状

  • 您首先加载了带有标识的矩阵堆栈

或者如果你自己在CPU上做很多不必要的运算, 使您的代码更加复杂,应用程序速度降低

glPushMatrix()
..apply tranformations
glPushMatrix()
..apply ADDITIONAL transformations
..draw object with combined transforms
glPopMatrix()
...draw object without the additional transforms.
glPopMatrix()