在 openGL 中实现左转功能

Implements turn left function in openGL

本文关键字:功能 实现 openGL      更新时间:2023-10-16

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

// 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;
}

旋转函数采用度数和轴两个参数,并返回旋转矩阵,该矩阵为 3 x 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;
}  

尝试这样的事情是否可以修复它:

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);
}

我用谷歌搜索并找到了一个解决方案:

// 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);
}

旋转功能是正确的。