OpenGL glMatrixMode(GL_PROJECTION)vs glMatrixMode(GL_MODELVIEW)

时间:2015-04-22 16:50:37

标签: opengl rotation transform matrix-multiplication

glMatrixMode(GL_PROJECTION)之后放置 glRotatef()之间有什么区别;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glRotatef(red, green, blue);

并在 glMatrixMode(GL_MODELVIEW)之后放置 glRotatef();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(red, green, blue);

2 个答案:

答案 0 :(得分:2)

来自documentation

glMatrixMode()指定哪个矩阵是当前矩阵。

GL_MODELVIEW - Applies subsequent matrix operations to the modelview matrix stack.

GL_PROJECTION - Applies subsequent matrix operations to the projection matrix stack.

它们意味着什么?

如果将当前矩阵模式设置为投影(例如glMatrixMode(GL_PROJECTION)),则需要更改投影矩阵。当然,其中一个预计是下一行:

对于正投影:

  • glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);
  • gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);

透视投影:

  • void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far);
  • void gluPerspective(GLdouble fov, GLdouble aspect, GLdouble near, GLdouble far);

如果将当前矩阵模式设置为modelView(例如glMatrixMode(GL_MODELVIEW)),则表示我在模型视图矩阵中,我可以应用基本操作来转换对象,如:

  • glRotatef();
  • glTranslatef();
  • glScalef();

在你的问题中,如果你在gl_projection之后使用rotatef而不是gl_modelview,你可以旋转投影矩阵,这会破坏你的投影矩阵。

`

答案 1 :(得分:0)

OpenGL分别存储Projection和ModelView矩阵,通过调用glMatrixMode(),您可以指定要使用以下调用操作的矩阵。 因此,在第一个示例中,您将旋转应用于投影矩阵,然后在第二个示例中将其应用于ModelView矩阵(这将更常见)

另请查看此答案以澄清Difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);