gluLookAt矢量和FPS风格的相机

时间:2012-10-27 22:25:25

标签: opengl graphics vector-graphics frame-rate glulookat

我试图通过更新三个向量来实现FPS风格的摄像头:EYE,DIR,UP。这些向量与gluLookAt使用的相同(因为gluLookAt由摄像机的位置,它所看到的方向和向上矢量指定)。

我已经实施了左右和上下扫射动作,但是我在理解保持静止时使相机环顾四周的数学方面遇到了很多麻烦。在这种情况下,EYE向量保持不变,而我必须更新DIR和UP。

以下是我尝试过的代码,但它似乎无法正常工作。有什么建议吗?

void Transform::left(float degrees, vec3& dir, vec3& up) {
    vec3 axis;
    axis = glm::normalize(up);
    mat3 R = rotate(-degrees, axis);
    dir = R*dir;
    up = R*up;
};

void Transform::up(float degrees, vec3& dir, vec3& up) {
    vec3 axis;
    axis=glm::normalize(glm::cross(dir,up));
    mat3 R = rotate(-degrees, axis);
    dir = R*dir;
    up = R*up;
};

rotate方法创建一个旋转矩阵,围绕轴旋转一定量。

-

编辑:我编辑了它(将“dir”切换为“center”,但它仍然无效。当我尝试向左/向右旋转时,没有任何反应。当我尝试向上/向下旋转时,对象消失。

void Transform::left(float degrees, vec3& center, vec3& up) {
    center = center*rotate(-degrees,glm::normalize(up));
}
void Transform::up(float degrees, vec3& center, vec3& up) {
    vec3 axis = glm::normalize(glm::cross(center,up));
    center = center*rotate(-degrees, axis);
}

1 个答案:

答案 0 :(得分:0)

没有定义rotate()方法,似乎你忘了实现它。并且它似乎是围绕任意轴的旋转,您可以从等式中实现它 http://upload.wikimedia.org/math/f/b/a/fbaee547c3c65ad3d48112502363378a.png
(来自轴和角度的旋转矩阵)形成Wiki http://en.wikipedia.org/wiki/Rotation_matrix

代码:

mat3 Transform::rotate(const float degrees, const vec3& axis) { 
glm::mat3 m3;
glm::mat3 I(1.0f,0.0f,0.0f,0.0f,1.0f,0.0f,0.0f,0.0f,1.0f);
glm::mat3 T(axis.x*axis.x, axis.x*axis.y, axis.x*axis.z,axis.y*axis.x, axis.y*axis.y, axis.y*axis.z,axis.z*axis.x, axis.z*axis.y, axis.z*axis.z);
glm::mat3 A(0.0f,-axis.z, axis.y, axis.z, 0.0f, -axis.x,-axis.y,axis.x,0.0f);
m3 = glm::cos(degrees)*I + (1.0f-glm::cos(degrees))*T + glm::sin(degrees)*A;  return m3;}
相关问题