在Open GL 2.0和glm中创建第一人称相机

Dar*_*ium 5 c++ opengl matrix glm-math

我对Open GL和c ++都很陌生,并且遇到了创建第一人称相机的问题.我不理解矩阵数学,所以这对我来说更加困难.到目前为止,为了计算相机的旋转,我已经为此做了:

void CameraFP::calculate_view()  {
    m_view = glm::rotate(m_view, this->get_rotation_x(), glm::vec3(1, 0, 0));
    m_view = glm::rotate(m_view, this->get_rotation_y(), glm::vec3(0, 1, 0));
}
Run Code Online (Sandbox Code Playgroud)

每次更新调用都会调用该函数.

为了通过mous处理相机的旋转,我做了以下工作:

void CameraFP::process_mouse(sf::Window *app)  {
    GLfloat mouse_x = app->GetInput().GetMouseX();
    GLfloat mouse_y = app->GetInput().GetMouseY();

    GLfloat mouse_x_delta = std::max(mouse_x, old_mouse_x) - std::min(mouse_x, old_mouse_x);
    GLfloat mouse_y_delta = std::max(mouse_y, old_mouse_y) - std::min(mouse_y, old_mouse_y);

    y_rot += mouse_x_delta / (float)app->GetWidth();
    x_rot += mouse_y_delta / (float)app->GetHeight();

    this->old_mouse_x = mouse_x;
    this->old_mouse_y = mouse_y;

    app->SetCursorPosition(app->GetWidth() / 2, app->GetHeight() / 2);
}
Run Code Online (Sandbox Code Playgroud)

为了处理运动,我做了以下事情:

void CameraFP::process_keyboard(sf::Window *app)  {
    const sf::Input *input = &app->GetInput();

    if (input->IsKeyDown(sf::Key::W))  {
        position.z += 1.0f / 100.0f;
    }
    if (input->IsKeyDown(sf::Key::S))  {
        position.z -= 1.0f / 100.0f;
    }
    if (input->IsKeyDown(sf::Key::A))  {
        position.x -= 1.0f / 100.0f;
    }
    if (input->IsKeyDown(sf::Key::D))  {
        position.x += 1.0f / 100.0f;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题在于我的相机不会朝着您所面对的方向移动,并且它永远不会停止旋转:/.另外,如果你能指点我的指南或Matrix数学上的东西会很棒:)

编辑2: '

我刚开始一个新的过程鼠标功能,它沿着屏幕的x轴移动,正确地为相机旋转.但是,如果我向左或向右移动鼠标并不重要,两个动作都会向右旋转.与3d空间中的x轴相同,但这是向下发生的.关于是什么导致这个的任何想法?

void CameraFP::process_mouse(sf::Window *app)  {
    GLfloat mouse_x = app->GetInput().GetMouseX();
    GLfloat mouse_y = app->GetInput().GetMouseY();

    GLfloat mouse_x_delta = old_mouse_x - mouse_x;
    GLfloat mouse_y_delta = old_mouse_y - mouse_y;

    if (mouse_x_delta > 0)  {
        y_rot += mouse_x_delta / (float)app->GetWidth() * 0.1f;
    } else if (mouse_x_delta < 0)  {
        y_rot -= mouse_x_delta / (float)app->GetWidth() * 0.1f;
    }
    if (mouse_y_delta > 0)  {
        x_rot += mouse_y_delta / (float)app->GetWidth() * 0.1f;
    } else if (mouse_y_delta < 0)  {
        x_rot -= mouse_y_delta / (float)app->GetWidth() * 0.1f;
    }


    if (mouse_x != old_mouse_x)  {
        m_view = glm::rotate(m_view, y_rot, glm::vec3(0, 1, 0));
    }
    if (mouse_y != old_mouse_y)  {
        m_view = glm::rotate(m_view, x_rot, glm::vec3(1, 0, 0));
    }

    this->old_mouse_x = mouse_x;
    this->old_mouse_y = mouse_y;

    app->SetCursorPosition(app->GetWidth() / 2, app->GetHeight() / 2);
}
Run Code Online (Sandbox Code Playgroud)

Tar*_*ara 0

我现在没有时间查找我的相机代码,但我现在可以告诉你的是,你的运动计算完全是错误的。
您没有考虑相机旋转。你必须做这样的事情:

if (input->IsKeyDown(sf::Key::W))  {
    position.z += sin(camera_rot_y);
}
else if (input->IsKeyDown(sf::Key::S))  {
    position.z -=  sin(camera_rot_y);
}

if (input->IsKeyDown(sf::Key::A))  {
    position.x -=  cos(camera_rot_y);
}
else if (input->IsKeyDown(sf::Key::D))  {
    position.x +=  cos(camera_rot_y);
}
Run Code Online (Sandbox Code Playgroud)

你也可以使用“else if”,因为你不能同时向前和向后移动。^^

另外,为什么在下面的代码中使用 std::min() 和 max() ?:

GLfloat mouse_x_delta = std::max(mouse_x, old_mouse_x) - std::min(mouse_x, old_mouse_x);
GLfloat mouse_y_delta = std::max(mouse_y, old_mouse_y) - std::min(mouse_y, old_mouse_y);
Run Code Online (Sandbox Code Playgroud)

如果您想沿相反方向旋转,则需要获得负增量,这对于您的代码来说是不可能的。摆脱 min() 和 max() 函数。

顺便说一句:我的相机代码不是最有效的(使用旋转而不是直接计算),但它可以工作。