【问题标题】:Rotate around camera opengl c++ / Set center for rotation围绕相机旋转opengl c ++ /设置旋转中心
【发布时间】:2017-06-07 01:43:49
【问题描述】:

Opengl 总是围绕 (0,0,0) 旋转。 但我需要围绕相机旋转。 我知道在 GL 中没有摄像头, 但是如果我转换回 (0,0,-6.0) ,它会围绕 (0,0 ,0),而不是 (0,0,-6.0)

所以我的问题是:如何在opengl和c++中设置旋转中心

我已经找到了这篇文章:OpenGL rotating a camera around a point

这个解决方案的展示位置:

旋转函数通常绕原点旋转。旋转 在另一个点 P 附近,您必须: translate(-P) 旋转平移(P)

我现在不知道如何实现

翻译(P)

因为在旋转之后,平移是相对于旋转的

示例: 我想围绕(1,1,1)旋转。我翻译(-1,-1,-1)。现在我将它绕 x 轴旋转 90 度。这是正确的。但现在我必须把它翻译回来。如果我平移 (1,1,1),它将不起作用,因为它现在已旋转,并且移动是相对于旋转后的矩阵。

PS : 我不想使用 gluLookAt()

所以我的代码是:

//set rotation to zero
glRotatef(-yr,0.0f,1.0f,0.0f);  
glRotatef(-xr,1.0f,0.0f,0.0f);
//set points relative to camera
glTranslatef(cam_pos[0],cam_pos[1],cam_pos[2]);
//rotate back
glRotatef(yr,0.0f,1.0f,0.0f);   
glRotatef(xr,1.0f,0.0f,0.0f);
//rotate
glRotatef(angley,0.0f,1.0f,0.0f);   
glRotatef(anglex,1.0f,0.0f,0.0f);
//and now, how to translate back,
//movement is relative to the cam ?
//I would simply use :  
//glTranslatef(-cam_pos[0],-cam_pos[1],-cam_pos[2]);
//which wont work because its relative to the new rotation.

非常感谢任何帮助!

【问题讨论】:

  • 你说的不是真的,即使是用粗体字写的,第二次问了。您的数学倒退了,并且对 GL 的矩阵堆栈的工作方式有一些误解。我建议你拿一本关于线性代数的好书。而且,我建议你也放弃你在这里尝试使用的这个可怕的过时 GL。
  • 我可以让它工作,如果我想知道如何搬回来。这是我唯一的问题。
  • 已经将它“向后”移动(有点——只是方向不对,中间有奇怪的“重置旋转”)。您实际上并没有在 旋转之前移动它。正如我所说,你得到了数学向后
  • 好的,谢谢您的建议

标签: c++ opengl rotation


【解决方案1】:

所以,我实际上发现运动与旋转无关,是什么让一切变得容易实现。 代码:

glTranslatef(point_to_rotate_around_x,point_to_rotate_around_y,point_to_rotate_around_z);
glRotatef(anglex,1,0,0);
glRotatef(angley,0,1,0);
glTranslatef(-point_to_rotate_around_x,-point_to_rotate_around_y,-point_to_rotate_around_z);

感谢@derhass,他告诉我它倒退了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 1970-01-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多