旋转相机opengl c ++ /设置旋转中心

时间:2017-01-22 13:07:49

标签: c++ opengl rotation

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)rotate translate(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.

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

所以,我实际上发现运动不是相对于旋转,是什么让一切都很容易实现。 代码:

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,他告诉我它的倒退。