我一直在用 FreeGlut 和 Glew 做一个小项目。现在我正在编写一个相机系统,但有些事情很奇怪:
在全屏模式下,如果鼠标在屏幕下方区域移动,则相机移动速度比相机在上方区域移动时更快。
相机做出奇怪的运动,总是在同一个方向,一个小的 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);