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