0

私は 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); 
4

1 に答える 1

0

これが機能する理由はわかりませんが、すべての問題が修正されました。

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);
于 2013-03-24T10:19:56.080 に答える