gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:51:47 25 4
gpt4 key购买 nike

我对 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));
}

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

为了通过鼠标处理相机的旋转,我做了以下操作:

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);
}

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

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;
}
}

我的问题在于我的相机没有朝你面对的方向移动,而且它永远不会停止旋转:/。另外,如果你能给我指点一个指南或一些关于矩阵数学的东西,那就太棒了:)

编辑 2:'

我刚刚启动了一个新的过程鼠标功能,它沿着屏幕的 x 轴移 Action 为相机的 y 旋转正确。但是,无论我向左还是向右移动鼠标都没有关系,这两个 Action 都会使我向右旋转。与 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);
}

最佳答案

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

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);
}

你也可以使用“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);

如果您想朝相反的方向旋转,您需要获得一个负增量,这对您的代码来说是不可能的。去掉 min() 和 max() 函数。

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

关于c++ - 在 Open GL 2.0 和 glm 中创建第一人称相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12126882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com