如何在OpenGL中旋转模型而不是相机?

ope*_*bie 5 opengl rotation

我和标题中的qustion相同:/我做了类似的事情:

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glTranslatef(0.0f, 0, -zoom);
  glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
  glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);

  //Here model render :/
Run Code Online (Sandbox Code Playgroud)

在我的应用程序相机旋转不是模型:/

Lar*_*rsH 5

大概是你认为它是你的相机移动而不是你的模型的原因是因为场景中的所有物体都在一起移动?

在渲染模型之后和渲染其他对象之前,您是否重置了MODELVIEW矩阵?换句话说,在渲染您正在讨论的模型之后以及渲染其他对象之前,您是在做另一个glLoadIdentity()还是glPopMatrix()?

因为如果没有,您应用于该模型的任何变换也将应用于渲染的其他对象,并且就像您旋转整个世界(或相机)一样.

我认为您的代码可能存在另一个问题:

  glTranslatef(0.0f, 0, -zoom);
  glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
  glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
  //Here model render :/
Run Code Online (Sandbox Code Playgroud)

您是否尝试围绕该点(0,0,-zoom)旋转模型?

通常,为了围绕某个点(x,y,z)旋转,您可以:

  1. 通过向量(-x,-y,-z)进行平移,将(x,y,z)转换为原点(0,0,0)
  2. 进行旋转
  3. 通过向量(x,y,z)平移将原点转换回(x,y,z)

如果您尝试围绕该点旋转(0,0,缩放),则缺少步骤3.因此,在渲染模型之前尝试添加此项:

glTranslatef(0.0f, 0, zoom); // translate back from origin
Run Code Online (Sandbox Code Playgroud)

另一方面,如果您尝试围绕原点(0,0,0)旋转模型,并沿z轴移动模型,那么您需要在旋转后进行翻译,如@nonVirtual所述.

在绘制其他对象之前,不要忘记重置MODELVIEW矩阵.所以整个序列将是这样的:

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

#if you want to rotate around (0, 0, -zoom):
  glTranslatef(0.0f, 0, -zoom);
  glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
  glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
  glTranslatef(0.0f, 0, zoom); // translate back from origin
#else if you want to rotate around (0, 0, 0):
  glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
  glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
  glTranslatef(0.0f, 0, -zoom);
#endif    
  //Here model render :/

  glLoadIdentity();
  // translate/rotate for other objects if necessary
  // draw other objects
Run Code Online (Sandbox Code Playgroud)


and*_*wmu 0

您在定义对象的转换时使用 GL_MODELVIEW,并使用 GL_PROJECTION 来转换您的视点。