纯opengl锁定鼠标到屏幕中心

时间:2017-03-29 17:54:54

标签: c++ opengl

当我使用SetCursorPos

将光标锁定到屏幕中心时出现问题

我的问题是它阻止我的相机旋转,当我不尝试锁定时我的相机工作正常。但它允许鼠标在不全屏时拖离屏幕。

void SetPosition(HWND hWnd, int x, int y)
{
    POINT pt;
    pt.x = x;
    pt.y = y;
    ClientToScreen(hWnd, &pt);
    SetCursorPos(pt.x, pt.y);
}

void COpenGLRenderer::OnMouseMove(int X, int Y)
{
    //This works fine if i dont set the mouse and i look around
    Camera.OnMouseMove(LastX - X, LastY - Y);

    if (LastX != X && LastY != Y)
        SetPosition(hWnd, this->Width / 2, this->Height / 2);

    LastX = X;
    LastY = Y;
}

void CCamera::OnMouseMove(int dx, int dy)
{
    float Sensitivity = 0.25f;
    Position -= Reference;

    if(dx != 0)
    {
        float DeltaX = (float)dx * Sensitivity;

        X = rotate(X, DeltaX, vec3(0.0f, 1.0f, 0.0f));
        Y = rotate(Y, DeltaX, vec3(0.0f, 1.0f, 0.0f));
        Z = rotate(Z, DeltaX, vec3(0.0f, 1.0f, 0.0f));
    }

    if(dy != 0)
    {
        float DeltaY = (float)dy * Sensitivity;

        Y = rotate(Y, DeltaY, X);
        Z = rotate(Z, DeltaY, X);

        if(Y.y < 0.0f)
        {
            Z = vec3(0.0f, Z.y > 0.0f ? 1.0f : -1.0f, 0.0f);
            Y = cross(Z, X);
        }
    }

    Position = Reference + Z * length(Position);

    CalculateViewMatrix();
}

void CCamera::CalculateViewMatrix()
{
    ViewMatrix = mat4x4(X.x, Y.x, Z.x, 0.0f, X.y, Y.y, Z.y, 0.0f, X.z, Y.z, Z.z, 0.0f, -dot(X, Position), -dot(Y, Position), -dot(Z, Position), 1.0f);
    ViewMatrixInverse = inverse(ViewMatrix);
    ViewProjectionMatrix = ProjectionMatrix * ViewMatrix;
    ViewProjectionMatrixInverse = ViewMatrixInverse * ProjectionMatrixInverse;
}

0 个答案:

没有答案