我正在开发一个2D / 3D游戏引擎,可在那里找到:https://github.com/crakouille/ngengine
图书馆3D相机类在include / video / D3 / camera / camera.h中定义
我存储了gluLookAt中所需的向量:
glm::vec3 _position;
glm::vec3 _target;
glm::vec3 _normal;
在examples / 05_3D中编写FreeFly Camera实现(使用Library 3D Camera),使用球坐标。效果很好。
在examples / 06_CameraQuat中,使用Quaternions编写了另一个FreeFly Camera实现。我的问题就在那里。
当我在Axis上旋转时,它运行良好,但是当我在两个Axis上旋转时(通过用鼠标做圆圈),相机会在目标轴上旋转,这是一个问题。
没有错误(05_3D示例):
目标轴旋转的错误(06_CameraQuat)。
以下是相机的代码:
FreeFly.h:
class FreeFly : public nge::video::D3::Camera {
public:
...
void rotateForward(double angle); // rotate around on the forward axis
void rotateLeft(double angle); // rotate around on the left axis
void rotateNormal(double angle); // rotate around on the normal axis
private:
void updateVectRel(); // update _targetRel, _leftRel and _normalRel
glm::quat _quat; // the quaternion which contains the rotations
glm::vec3 _targetRel; // the target vector, normalized, relative to _position
glm::vec3 _leftRel; // the left vector, normalized
glm::vec3 _normalRel; // the normal vector (up), normalized
};
FreeFly.cpp:
#include <06_CameraQuat/FreeFly.h>
#include <glm/gtx/quaternion.hpp>
......
void FreeFly::rotateForward(double angle)
{
glm::quat q2 = glm::angleAxis((float) -angle, _targetRel);
q2 = glm::normalize(q2);
// q2 represents the rotation around the forward axis
_quat = _quat * q2; // adding the rotation to _quat
this->updateVectRel(); // updating the vectors
}
void FreeFly::rotateLeft(double angle)
{
// idem with left axis
glm::quat q2 = glm::angleAxis((float) -angle, _leftRel);
q2 = glm::normalize(q2);
_quat = _quat * q2;//glm::cross(_quat, q2);
this->updateVectRel();
}
void FreeFly::rotateNormal(double angle)
{
// idem with normal axis
glm::quat q2 = glm::angleAxis((float) -angle, _normalRel);
_quat = _quat * q2; //glm::cross(_quat, q2);
this->updateVectRel();
}
void FreeFly::updateVectRel()
{
_targetRel = glm::dvec3(1., 0., 0.);
_leftRel = glm::dvec3(0., -1., 0.);
_normalRel = glm::dvec3(0., 0., 1.);
_quat = glm::normalize(_quat);
_targetRel = _targetRel * _quat;
_leftRel = _leftRel * _quat;
_normalRel = _normalRel * _quat;
_target = _position + _targetRel;
_normal = _normalRel;
}
编辑:
为什么我的相机会在目标轴上旋转?
注意: 当我沿着顺时针/逆时针方向用鼠标做圆圈时,围绕目标轴的旋转也分别是顺时针/逆时针方向。