GLM相机X,Y旋转引入Z旋转的问题 [英] Issue with GLM Camera X,Y Rotation introducing Z Rotation

查看:192
本文介绍了GLM相机X,Y旋转引入Z旋转的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在使用GLM库在OpenGL和C ++中实现的相机遇到了麻烦.我瞄准的相机类型是可绕相机旋转的相机,可以轻松探索3D世界.我设法使相机正常工作,它既美观又流畅,可以环顾四周,并且运动看起来也不错且正确.

So I've been having trouble with a camera I've implemented in OpenGL and C++ using the GLM library. The type of camera I'm aiming for is a fly around camera which will allow easy exploration of a 3D world. I have managed to get the camera pretty much working, it's nice and smooth, looks around and the movement seems to be nice and correct.

我似乎唯一的问题是,沿着相机的X和Y轴(向上和向下)旋转会导致围绕其Z轴的旋转.其结果是导致世界在旅行时略微滚动.

The only problem I seem to have is that the rotation along the camera's X and Y axis (looking up and down) introduces some rotation about it's Z axis. This has the result of causing the world to slightly roll whilst travelling about.

作为示例...如果我在镜头前有一个方形的四边形,然后以圆周运动方式移动照相机,就好像用头环视一圈,一旦运动完成,该四边形将具有略微滚动,好像您已经歪了歪头.

As an example... if I have a square quad in front of the camera and move the camera in a circular motion, so as if looking around in a circle with your head, once the motion is complete the quad will have rolled slightly as if you've tilted your head.

我的相机当前是一个组件,可以将其附加到场景中的对象/实体上.每个实体都有一个框架",基本上是该实体的模型矩阵.框架包含以下属性:

My camera is currently a component which I can attach to an object/entity in my scene. Each entity has a "Frame" which is basically the model matrix for that entity. The Frame contains the following attributes:

glm::mat4 m_Matrix;
glm::vec3 m_Position;
glm::vec3 m_Up;
glm::vec3 m_Forward;

然后相机将其用于创建适当的viewMatrix,如下所示:

These are then used by the camera to create the appropriate viewMatrix like this:

const glm::mat4& CameraComponent::GetViewMatrix()
{
    //Get the transform of the object
    const Frame& transform = GetOwnerGO()->GetTransform();

    //Update the viewMatrix
    m_ViewMatrix = glm::lookAt(transform.GetPosition(), //position of camera
                           transform.GetPosition() + transform.GetForward(), //position to look at
                           transform.GetUp()); //up vector

    //return reference to the view matrix
    return m_ViewMatrix; 
}

现在...这是我在Frame对象中的X和Y旋转方法,我猜这是问题所在:

And now... here are my rotate X and Y methods within the Frame object, which I'm guessing is the place of the problem:

void Frame::RotateX( float delta )
{
    glm::vec3 cross = glm::normalize(glm::cross(m_Up, m_Forward)); //calculate x axis

    glm::mat4 Rotation = glm::rotate(glm::mat4(1.0f), delta, cross);

    m_Forward = glm::normalize(glm::vec3(Rotation * glm::vec4(m_Forward, 0.0f))); //Rotate forward vector by new rotation

    m_Up = glm::normalize(glm::vec3(Rotation * glm::vec4(m_Up, 0.0f))); //Rotate up vector by new rotation
}


void Frame::RotateY( float delta )
{
    glm::mat4 Rotation = glm::rotate(glm::mat4(1.0f), delta,  m_Up);    

    //Rotate forward vector by new rotation
    m_Forward = glm::normalize(glm::vec3(Rotation * glm::vec4(m_Forward, 0.0f))); 
}

所以在那里的某个地方,我一直在寻找解决问题的方法.我已经将它弄乱了几天,尝试随机的东西,但是我得到了相同的结果,或者固定了z轴旋转,但是出现了其他错误,例如X,Y旋转和相机移动不正确.

So somewhere in there, there's a problem which I've been searching around trying to fix. I've been messing with it for a few days now, trying random things but I either get the same result, or the z axis rotation is fixed but other bugs appear such as incorrect X, Y rotation and camera movement.

我看了一下万向节锁,但据我了解,这个问题对我来说似乎并不像是万向节锁.但是我可能是错的.

I had a look at gimbal lock but from what I understood of it, this problem didn't seem quite like gimbal lock to me. But I may be wrong.

推荐答案

存储当前的俯仰/偏航角并即时生成相机矩阵,而不是尝试在中间矢量上累积小变化.

Store the current pitch/yaw angles and generate the camera matrix on-the-fly instead of trying to accumulate small changes on the intermediate vectors.

这篇关于GLM相机X,Y旋转引入Z旋转的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆