我需要从相机中提取变换矩阵以将其分配给网格。
我在学校的计算图形项目中工作,目的是以第一人称的角度模拟角色的手臂。
我的相机实现包括摄像机位置的vector3,所以我可以将其分配给我的网格,问题是我无法从视图矩阵中提取相机的旋转。
我用旋转函数计算我的最终俯仰和偏航,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);
我可以成功分配摄像机位置,但不能旋转。
我期望的是将完整的相机变换分配给我的网格,或者单独提取旋转并将其分配给网格。
设置模型视图投影矩阵的步骤,我一直遵循(这并不意味着它是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轴旋转了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));