绕摄影机旋转opengl c++/设置旋转中心

Rotate around camera opengl c++ / Set center for rotation

本文关键字:旋转 设置 c++ 摄影机 opengl      更新时间:2023-10-16

Opengl始终围绕(0,0,0)旋转 但我需要绕着相机旋转 我知道在GL中没有相机, 但如果我翻译回(0,0,-6.0) ,它会围绕(0,0,0)旋转,而不是围绕(0,0,6.0)旋转

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

我已经找到了这个帖子:OpenGL围绕一个点旋转相机

显示此解决方案的位置:

旋转函数通常绕原点旋转。旋转在另一点P附近,您必须:平移(-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.

非常感谢您的帮助!

所以,我实际上发现运动与旋转无关,这让一切都很容易实现。代码:

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