gpt4 book ai didi

c++ - 绕轴旋转相机?

转载 作者:太空狗 更新时间:2023-10-29 22:59:22 25 4
gpt4 key购买 nike

如何沿轴旋转相机?我必须乘以什么矩阵?

我正在使用 glm::lookAt 构建 viewMatrix,但我试图将它乘以一个旋转矩阵,但没有任何反应。

glm::mat4 GetViewMatrix()
{
return glm::lookAt(this->Position, this->Position + this->Front, glm::vec3(0.0f, 5.0f, 0.0f));
}

glm::mat4 ProjectionMatrix = glm::perspective(actual_camera->Zoom, (float)g_nWidth / (float)g_nHeight, 0.1f, 1000.0f);
glm::mat4 ViewMatrix = actual_camera->GetViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;

最佳答案

使用 glm::rotate 旋转相机的正面和向上 vector :

glm::mat4 GetViewMatrix()
{
auto front = glm::rotate(this->Front, angle, axis);
auto up = glm::rotate(glm::vec3(0, 1, 0), angle, axis);
return glm::lookAt(this->Position, this->Position + front, up);
}

或者,您可以在 MVP 构造中添加与旋转矩阵的乘法:

glm::mat4 MVP = ProjectionMatrix * glm::transpose(Rotation) * ViewMatrix * ModelMatrix;

重要的是旋转发生在 View 矩阵之后,因此所有对象都将相对于相机的位置旋转。此外,您必须使用 transpose(Rotation)(旋转矩阵的逆是它的转置),因为顺时针旋转相机等同于逆时针旋转所有对象。

关于c++ - 绕轴旋转相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37092569/

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