使用gluLookAt的OpenGL相机旋转 [英] OpenGL camera rotation using gluLookAt

查看:606
本文介绍了使用gluLookAt的OpenGL相机旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用gluLookAt在OpenGL固定功能管道中实现FPS样式的相机.鼠标应沿任何给定方向旋转相机.

I am trying to use gluLookAt to implement an FPS style camera in OpenGL fixed function pipeline. The mouse should rotate the camera in any given direction.

我存储相机的位置:

float xP;
float yP;
float zP;

我存储一下坐标:

float xL;
float yL;
float zL;

向上矢量始终设置为(0,1,0)

我按如下方式使用此相机:gluLookAt(xP,yP,zP, xL,yL,zL, 0,1,0);

I use this camera as follows: gluLookAt(xP,yP,zP, xL,yL,zL, 0,1,0);

我希望我的相机能够沿着偏航和俯仰方向移动,但不能滚动.

I want my camera to be able to be able to move along the yaw and pitch, but not roll.

每帧之后,我将鼠标的坐标重置为屏幕中间.由此,我可以同时改变x和y.

After every frame, I reset the coordinates of the mouse to the middle of the screen. From this I am able to get a change in both x and y.

如何在每一帧之后转换x和y的变化,以适当地更改监视坐标(xL,yL,zL)以旋转相机?

How can I convert the change in x and y after each frame to appropriately change the lookat coordinates (xL, yL, zL) to rotate the camera?

推荐答案

从一组向量开始:

fwd = (0, 0, -1);
rht = (1, 0, 0);
up = (0, 1, 0);

考虑到从您提到的鼠标位置获取的xy足够小,可以分别将它们直接作为偏航和俯仰旋转.使用偏航值时,将rhtfwd向量旋转到up向量上,然后将fwd vactor旋转到具有螺距值的rht上.这样,您将为相机(fwd真空吸尘器)提供一个新的前进方向,从中可以得出一个新的观察点(在您的情况下为L = P + fwd).

Given that Your x and y, taken from the mouse positions You mentioned, are small enough You can take them directly as yaw and pitch rotations respectively. With yaw value rotate the rht and fwd vectors over the up vector, than rotate fwd vactor over the rht with pitch value. This way You'll have a new forward direction for Your camera (fwd vactor) from which You can derive a new look-at point (L = P + fwd in Your case).

您必须记住限制音高旋转,以使fwdup向量在某些点上不平行.每次进行俯仰旋转时,都可以通过重新创建up向量来防止这种情况-只需在rhtfwd真空臂之间做一个叉积即可.不过,这里有一个旁注-这样up并不总是(0,1,0).

You have to remember to restrict pitch rotation not to have fwd and up vectors parallel at some point. You can prevent that by recreating the up vector every time You do pitch rotation - simply do a cross product between rht and fwd vactors. A side-note here though - this way up will not always be (0,1,0).

这篇关于使用gluLookAt的OpenGL相机旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆