2013-03-23 4 views
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 시스템을 사용했습니다. 그 때 나는이 작품 이유를 모르겠어요

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); 
+0

왜 함수의 끝에'MousePreviousX/Y'를 선언합니까? 함수 호출간에 값을 유지하려면 정적 또는 전역 변수 여야합니다. – Nobody

+0

나의 나쁜, 나는 실제로 그들을 두 번 명백하게 선언하게한다. 나는 그것들을 제거했지만 이상한 것들은 사라지지 않았다. 사실, 카메라가 전혀 움직이지 않는 것처럼 보입니다. – Nyxeria

+0

그래, 움직이지만 이상하게. – Nyxeria

답변

0

모든 "cam_norm"변수를 전달하지만, 모든 문제 해결 :

: 다음

bool isCursorWarping = false; 

void MouseOps(int x, int y) 
{ 

    if(isCursorWarping == false){ 
     // Changes in mousepositions. Always same direction and in lower right corner of monitor faster, for some reason. 
     deltaX = x - MousePreviousX; 
     deltaY = y - MousePreviousY; 

     yaw = yaw + ((((deltaX/resx)) * deginrad) * 360); 
     pitch = pitch + ((((deltaY/resy)) * deginrad) * 360); 


     //Check clippings (eg. camera wont end upside down etc.) 

     if(x >= resx - 1 || y >= resy - 1 || x == 0 || y == 0) 
     { 
      warpCursor(); 
      MousePreviousX = resx/2; 
      MousePreviousY = resy/2; 
     }else{ 
      MousePreviousX = x; 
      MousePreviousY = y; 
     } 

     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(pitch) * cos(yaw); 
     cam_normY = sin(pitch) * sin(yaw); 
     cam_normZ = cos(pitch) * sin(yaw); 



    } 
     // Current x and y to previous and cleanup 



     isCursorWarping = false; 


} 

void warpCursor() 
{ 
    isCursorWarping = true; 
    glutWarpPointer(resx/2, resy/2); 


} 

난에 "cam_norm"값을 전달을

gluLookAt(0.0f, 1.0f, 2.0f, 0.0f + cam_normX, 1.0f + cam_normY, 2.0f+ cam_normZ, 0.0f, 0.1f, 0.0f);