0
我一直在与FreeGlut和Glew一起开发一个小项目。现在我编码一个摄像系统,但也有一些事情,只是奇怪:OpenGL相机旋转怪异
在全屏模式下,如果在屏幕的下方区域移动鼠标,摄像头的动作比如果上镜头移动速度更快区域。
该相机使奇怪的运动,总是在同一个方向,一个小8移动移动。
代码:
void MouseOps(int x, int y)
{
// Changes in mousepositions. Always same direction and
// in lower right corner of monitor faster, for some reason.
deltaX = x - MousePreviousX;
deltaY = y - MousePreviousY;
// Also I didn't bother to put * 360 in next equations,
// because it would make the camera jump for crazy.
// resx and resy are screen resolutions.
// Endresult should be that camera can
// rotate once when mouse moves over screen
yaw = yaw + (((deltaX/resx)) * deginrad);
pitch = pitch + (((deltaY/resy)) * deginrad);
//Check clippings (eg. camera wont end upside down etc.)
if(yaw >= (2 * pi) || yaw <= (-2 * pi) )
yaw = 0;
if(pitch >= (pi/2))
pitch = pi/2;
if(pitch <= (pi/-2))
pitch = pi/-2;
//Calculate x, y, and z coordinates of unit sphere to look at (r = 1)
cam_normX = cos(yaw) * sin(pitch);
cam_normY = sin(yaw) * sin(pitch);
cam_normZ = cos(yaw);
// Current x and y to previous
int MousePreviousX = x;
int MousePreviousY = y;
}
我试图用这个 http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates 系统计算的角度去看待。然后,我通过了所有“cam_norm”变量
gluLookAt(cam_posX, cam_posY, cam_posZ,
cam_posX+cam_normX, cam_posY+cam_normY, cam_posZ + cam_normZ,
cam_upX, cam_upY, cam_upZ);
为什么在函数的结尾声明'MousePreviousX/Y'?它们需要是静态或全局变量才能在函数调用之间保留它们的值。 – Nobody 2013-03-23 20:14:52
我的不好,我实际上让他们明显地宣布了两次。我删除了这些,但奇怪的东西没有消失。实际上,相机看起来根本不动。 – Nyxeria 2013-03-23 20:26:13
好吧,它移动,但又奇怪了。 – Nyxeria 2013-03-23 20:46:16