简体   繁体   English

如何使用 gluLookAt 围绕 OpenGL 中的一个点旋转相机视图

[英]How to rotate the camera view around a point in OpenGL with gluLookAt

I'm trying to use the gluLookAt function to set up the correct view transformation for the camera's position and orientation.我正在尝试使用 gluLookAt function 为相机的 position 和方向设置正确的视图转换。 In my program there is a sphere centered in the origin and when the arrows key are pressed the camera moves according (left, right, up and down) around the sphere.在我的程序中,有一个以原点为中心的球体,当按下箭头键时,相机会根据(左、右、上、下)球体移动。

I tried to use this transformation: gluLookAt(xc, yc, zc, 0.0, 0.0, 0.0, 0.0, 1,0, 0,0);我尝试使用这种转换: gluLookAt(xc, yc, zc, 0.0, 0.0, 0.0, 0.0, 1,0, 0,0);

To verify its correctness I printed out the three axes.为了验证它的正确性,我打印了三个轴。 It doesn't seem to properly work.它似乎无法正常工作。 When I move up and down the y axis doesn't seem to move.当我上下移动时,y 轴似乎没有移动。

Can you help understand what is wrong with that transformation?你能帮助理解这种转变有什么问题吗?

Constant up vector is wrong !恒定up向量是错误的!

When your xc=0,yc=?,zc=0 (which is when you move up/down in y axis) then your up vector is (anti)parallel to your view direction which means gluLookAt can not create 3 perpendicular basis vectors using cross product which it needs for constructing the transform matrix .当您的xc=0,yc=?,zc=0 (即当您在 y 轴上向上/向下移动时)时,您的向上向量与您的视图方向(反)平行,这意味着gluLookAt无法使用创建 3 个垂直基向量构建变换矩阵所需的叉积。

In such case the up vector should be different... I usually use dot product在这种情况下,向上向量应该不同......我通常使用点积

if (dot(a,b)/(|a|.|b|)>0.9)

to detect near to (anti)parallel vectors and set different one in your case its simplied like this:检测接近(反)平行向量并在您的情况下设置不同的向量,其简化如下:

if (fabs(yc*yc)/(xc*xc+yc*yc+zc*zc)>(0.9*0.9)) use different up vector

You can use (+/-1,0,0) or (0,0,+/-1) which one and which sign depends on your coordinate system conventions (so your view does not twist/mirror/rotate on going through this singularity).您可以使用(+/-1,0,0)(0,0,+/-1)哪个和哪个符号取决于您的坐标系约定(因此您的视图不会扭曲/镜像/旋转通过这个奇异性)。

The 0.9 threshold means: 0.9阈值意味着:

cos(da) = 0.9
da = acos(0.9)
da = 25.84 deg

which is the max allowed angular difference between the two vectors...这是两个向量之间允许的最大 angular 差异......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM