实现在openGL中左转功能

时间:2013-10-05 09:21:20

标签: c++ opengl

基本上,当按左键(向右转动视图)时,我需要正确地改变眼睛和向上矢量。我的实现如下,但它似乎没有通过测试。有人可以帮忙吗?

// Transforms the camera left around the "crystal ball" interface
void Transform::left(float degrees, vec3& eye, vec3& up) {
    // YOUR CODE FOR HW1 HERE
    eye = rotate(degrees, vec3(0, 1, 0)) * eye;
    up = rotate(degrees, vec3(0, 1, 0)) * up;
}

旋转函数采用两个参数degree和axis,并返回一个3乘3矩阵的旋转矩阵:

mat3 Transform::rotate(const float degrees, const vec3& axis) {
    // YOUR CODE FOR HW1 HERE

    mat3 rot, I(1.0);
    mat3 a_x;
    a_x[0][0] = 0;
    a_x[0][1] = -axis[2];
    a_x[0][2] = axis[1];
    a_x[1][0] = axis[2];
    a_x[1][1] = 0;
    a_x[1][2] = -axis[0];
    a_x[2][0] = -axis[1];
    a_x[2][1] = axis[0];
    a_x[2][2] = 0;
    float theta = degrees / 180 * pi;
    rot = I * cos(theta) + glm::outerProduct(axis, axis) *(1-cos(theta)) + a_x*sin(theta);
    return rot;
}  

2 个答案:

答案 0 :(得分:0)

尝试这样的东西修复它:

glm::mat3 Transform::rotate(float angle, const glm::vec3& axis) {
    glm::mat3 a_x(   0.0f,  axis.z, -axis.y,
                  -axis.z,    0.0f,  axis.x,
                   axis.y, -axis.x,    0.0f);
    angle = glm::radians(angle);
    return glm::mat3() * cos(angle) + sin(angle) * a_x
        + (1.0f - cos(angle)) * glm::outerProduct(axis, axis);
}

答案 1 :(得分:0)

我用Google搜索并找到解决方案:

// Transforms the camera left around the "crystal ball" interface
void Transform::left(float degrees, vec3& eye, vec3& up) {
    // YOUR CODE FOR HW1 HERE
    eye = eye * rotate(degrees, up);
}

旋转功能正确。