有没有办法从glm中的视图矩阵中提取变换矩阵?
Is there any way to extract a transform matrix from a view matrix in glm?
我需要从相机中提取变换矩阵以将其分配给网格。
我在学校做一个计算图形项目,objective是模拟第一人称视角的角色手臂。
我的相机实现包括一个用于相机位置的 vector3,因此我可以将其分配给我的网格,问题是我还不能从我的视图矩阵中提取相机的旋转。
我在rotation函数中的最终pitch和yaw是这样计算的,x和y是当前鼠标在屏幕中的位置
m_yaw += (x - m_mouseLastPosition.x) * m_rotateSpeed;
m_pitch -= (y - m_mouseLastPosition.y) * m_rotateSpeed;
这就是我在更改时更新视图矩阵的方式
glm::vec3 newFront;
newFront.x = -cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
newFront.y = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
newFront.z = sin(glm::radians(m_pitch));
m_front = glm::normalize(newFront);
m_right = glm::normalize(glm::cross(m_front, m_worldUp));
m_up = glm::normalize(glm::cross(m_right, m_front));
m_viewMatrix = glm::lookAt(m_position, (m_position + m_front), m_up);
现在我可以将相机的位置分配给我的网格,就像这样
m_mesh.m_transform = glm::translate(glm::mat4(1.0f), m_camera.m_position);
我可以成功分配相机位置,但不能旋转。
我期望的是将整个相机 transform 分配给我的网格,或者独立提取 rotation 并将其分配给网格之后。
我一直遵循的设置模型视图投影矩阵的步骤(这并不意味着它 100% 正确),这似乎是您遇到的问题:
// Eye position is in world coordinate system, as is scene_center. up_vector is normalized.
glm::dmat4 view = glm::lookat(eye_position, scene_center, up_vector);
glm::dmat4 proj = glm::perspective(field_of_view, aspect, near_x, far_x);
// This converts the model from it's units, to the units of the world coordinate system
glm::dmat4 model = glm::scale(glm::dmat4(1.0), glm::dvec3(1.0, 1.0, 1.0));
// Add model level rotations here utilizing glm::rotate
// offset is where the objects 0,0 should be mapped to in the world coordinate system
model = glm::translate(model, offset);
// Order of course matters here.
glm::dvec3 mvp = proj * view * model;
希望对您有所帮助。
非常感谢您的回答,他们帮助了很多。
我设法以一种非常简单的方式解决了我的问题,我只需要使用相机的单独属性直接将最终变换分配给我的网格。
glm 对我来说太陌生了,我不熟悉它处理矩阵乘法的方式。
最终代码采用带有相机位置的平移矩阵,然后我用我的俯仰在 Y 轴上旋转生成的矩阵,最后用我的偏航在 X 轴上旋转。
m_mesh.m_transform = glm::rotate(glm::rotate(glm::translate(glm::mat4(1.0f), camera.m_position), glm::radians(-camera.m_yaw), glm::vec3(0.0f, 1.0f, 0.0f)), glm::radians(-camera.m_pitch), glm::vec3(1.0f, 0.0f, 0.0f));
我需要从相机中提取变换矩阵以将其分配给网格。
我在学校做一个计算图形项目,objective是模拟第一人称视角的角色手臂。
我的相机实现包括一个用于相机位置的 vector3,因此我可以将其分配给我的网格,问题是我还不能从我的视图矩阵中提取相机的旋转。
我在rotation函数中的最终pitch和yaw是这样计算的,x和y是当前鼠标在屏幕中的位置
m_yaw += (x - m_mouseLastPosition.x) * m_rotateSpeed;
m_pitch -= (y - m_mouseLastPosition.y) * m_rotateSpeed;
这就是我在更改时更新视图矩阵的方式
glm::vec3 newFront;
newFront.x = -cos(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
newFront.y = sin(glm::radians(m_yaw)) * cos(glm::radians(m_pitch));
newFront.z = sin(glm::radians(m_pitch));
m_front = glm::normalize(newFront);
m_right = glm::normalize(glm::cross(m_front, m_worldUp));
m_up = glm::normalize(glm::cross(m_right, m_front));
m_viewMatrix = glm::lookAt(m_position, (m_position + m_front), m_up);
现在我可以将相机的位置分配给我的网格,就像这样
m_mesh.m_transform = glm::translate(glm::mat4(1.0f), m_camera.m_position);
我可以成功分配相机位置,但不能旋转。
我期望的是将整个相机 transform 分配给我的网格,或者独立提取 rotation 并将其分配给网格之后。
我一直遵循的设置模型视图投影矩阵的步骤(这并不意味着它 100% 正确),这似乎是您遇到的问题:
// Eye position is in world coordinate system, as is scene_center. up_vector is normalized.
glm::dmat4 view = glm::lookat(eye_position, scene_center, up_vector);
glm::dmat4 proj = glm::perspective(field_of_view, aspect, near_x, far_x);
// This converts the model from it's units, to the units of the world coordinate system
glm::dmat4 model = glm::scale(glm::dmat4(1.0), glm::dvec3(1.0, 1.0, 1.0));
// Add model level rotations here utilizing glm::rotate
// offset is where the objects 0,0 should be mapped to in the world coordinate system
model = glm::translate(model, offset);
// Order of course matters here.
glm::dvec3 mvp = proj * view * model;
希望对您有所帮助。
非常感谢您的回答,他们帮助了很多。
我设法以一种非常简单的方式解决了我的问题,我只需要使用相机的单独属性直接将最终变换分配给我的网格。
glm 对我来说太陌生了,我不熟悉它处理矩阵乘法的方式。
最终代码采用带有相机位置的平移矩阵,然后我用我的俯仰在 Y 轴上旋转生成的矩阵,最后用我的偏航在 X 轴上旋转。
m_mesh.m_transform = glm::rotate(glm::rotate(glm::translate(glm::mat4(1.0f), camera.m_position), glm::radians(-camera.m_yaw), glm::vec3(0.0f, 1.0f, 0.0f)), glm::radians(-camera.m_pitch), glm::vec3(1.0f, 0.0f, 0.0f));