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

标签 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

显示此解决方案的位置:

rotation functions normally rotate about the origin. To rotate around another point P you have to: translate(-P) rotate translate(P)

我现在不知道如何实现

translate(P)

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

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

附言:我不想使用 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,他告诉我这是倒退的。

关于c++ - 围绕相机旋转 opengl c++/设置旋转中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41791259/

相关文章:

java - 图像旋转Java

jquery - 循环遍历 Div

c++ - XSLT 中的 MSXML 有多好?

c# - c++ 强制转换重载等效

c++ - 折线仅在调整窗口大小后完全渲染

c++ - 如何跟踪光位的变化?

rotation - Three.js - 两点,一个圆柱体,对齐问题

c++ - 如何在类成员函数内部的printf中获取编译警告

c# - PostMessage 参数从 32 位 C# 到 64 位 C++

c++ - 土壤,NPOT 纹理问题