在OpenGL相机类中计算侧倾

uidvcgyl  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(156)

我尝试向OpenGL相机添加滚动,但当我使用previous Stackoverflow question添加滚动时,相机滚动,但滚动会影响偏航和俯仰方向。
请问我应该在什么时候对胶卷进行矩阵旋转?
变量和类型:

glm::vec3 m_position;
    glm::vec3 m_front;
    glm::vec3 m_up;
    glm::vec3 m_right;
    glm::vec3 m_worldUp;

字符串
主要的相机更新功能

void Camera::UpdateCamera ()
{
    glm::vec3 front;
    front.x = cos (glm::radians (m_yaw)) * cos (glm::radians (m_pitch));
    front.y = sin (glm::radians (m_pitch));
    front.z = sin (glm::radians (m_yaw)) * cos (glm::radians (m_pitch));
    m_front = glm::normalize (front);

    m_right = glm::normalize (glm::cross (m_front, m_worldUp));
    m_up = glm::normalize (glm::cross (m_right, m_front));

    glm::mat4 roll_mat = glm::rotate(glm::mat4(1.0f), glm::radians(m_roll), m_front);
    m_up = glm::mat3(roll_mat) * m_up;
}


查看矩阵计算:

lm::mat4 Camera::GetViewMatrix ()
{
    return glm::lookAt (m_position, m_position + m_front, m_up);;
}


这就是偏航和俯仰计算的调用方式:

void KeyCtrl::CursorPositionChange(double xPosIn, double yPosIn)
{
    float xpos = static_cast<float>(xPosIn);
    float ypos = static_cast<float>(yPosIn);

    if (m_firstMouse)
    {
        m_lastX = xpos;
        m_lastY = ypos;
        m_firstMouse = false;
    }

    float xoffset = xpos - m_lastX;

    // reversed since y-coordinates go from bottom to top
    float yoffset = m_lastY - ypos;

    m_lastX = xpos;
    m_lastY = ypos;

    xoffset *= MouseSensitivity;
    yoffset *= MouseSensitivity;

    m_camera->SetYaw (xoffset);
    m_camera->SetPitch (yoffset);

    m_camera->UpdateCamera ();
}


不知何故,我把滚动数学错了,但是,我知道在哪里。

解决方案Camera::UpdateCamera中应删除以下两行:

glm::mat4 roll_mat = glm::rotate(glm::mat4(1.0f), glm::radians(m_roll), m_front);
    m_up = glm::mat3(roll_mat) * m_up;


创建模型视图时,将以下内容添加为最后一行:

model = glm::rotate (model, glm::radians (m_camera->GetRoll()), { 0.0f, 0.0f, 1.0f });

ff29svar

ff29svar1#

解决方案

下面两行应该从Camera::UpdateCamera中删除:

glm::mat4 roll_mat = glm::rotate(glm::mat4(1.0f), glm::radians(m_roll), m_front);
m_up = glm::mat3(roll_mat) * m_up;

字符串
创建模型视图时,将以下内容添加为最后一行:

model = glm::rotate (model, glm::radians (m_camera->GetRoll()), { 0.0f, 0.0f, 1.0f });

相关问题