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

发布于 2024-09-28 18:30:54 字数 290 浏览 4 评论 0原文

我有与标题相同的问题:/ 我做了类似的事情:

  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 :/

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

I have the same qustion as in the title :/
I make something like:

  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 :/

And in my app camera is rotating not model :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似狗非友 2024-10-05 18:30:54

想必您认为是相机在移动而不是模型在移动的原因是因为场景中的所有对象都一起移动?

在渲染模型之后和渲染其他对象之前,您是否重置 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 :/

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

通常,为了绕某个点 (x,y,z) 旋转,您需要执行以下操作:

  1. 通过向量 (-x,-y) 平移,将 (x,y,z) 平移到原点 (0,0,0) ,-z)
  2. 执行旋转
  3. 通过向量 (x,y,z) 平移将原点平移回 (x,y,z)
  4. 绘图

如果您尝试绕点 (0,0,zoom) 旋转,则您将缺少步骤 3。
因此,请尝试在渲染模型之前添加此内容:

glTranslatef(0.0f, 0, zoom); // translate back from origin

另一方面,如果您尝试围绕原点 (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

Presumably the reason you believe it's your camera moving and not your model is because all the objects in the scene are moving together?

After rendering your model and before rendering other objects, are you resetting the MODELVIEW matrix? In other words, are you doing another glLoadIdentity() or glPopMatrix() after you render the model you're talking about and before rendering other objects?

Because if not, whatever transformations you applied to that model will also apply to other objects rendered, and it will be as if you rotated the whole world (or the camera).

I think there may be another problem with your code though:

  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 :/

Are you trying to rotate your model around the point (0, 0, -zoom)?

Normally, in order to rotate around a certain point (x,y,z), you do:

  1. Translate (x,y,z) to the origin (0,0,0) by translating by the vector (-x,-y,-z)
  2. Perform rotations
  3. Translate the origin back to (x,y,z) by translating by the vector (x,y,z)
  4. Draw

If you are trying to rotate around the point (0,0,zoom), you are missing step 3.
So try adding this before you render your model:

glTranslatef(0.0f, 0, zoom); // translate back from origin

On the other hand if you are trying to rotate the model around the origin (0,0,0), and also move it along the z-axis, then you will want your translation to come after your rotation, as @nonVirtual said.

And don't forget to reset the MODELVIEW matrix before you draw other objects. So the whole sequence would be something like:

  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
何以笙箫默 2024-10-05 18:30:54

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

You use GL_MODELVIEW when defining the transformation for your objects and GL_PROJECTION to transform your viewpoint.

美人迟暮 2024-10-05 18:30:54

我相信这与您应用转换的顺序有关。尝试将调用顺序切换为 glRotateglTranslate。现在,您将其从原点向外移动,然后绕原点旋转,这将给出物体围绕相机旋转的外观。如果您在其仍位于原点时旋转它,然后移动它,它应该会给出您想要的结果。希望有帮助。

I believe it has to do with the order in which you are applying your transformations. Try switching the order of the call to glRotate and glTranslate. As it is now you are moving it outward from the origin, then rotating it about the origin, which will give the appearance of the object orbiting around the camera. If you instead rotate it while it is still at the origin, then move it, it should give you the desired result. Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文