在 openGL 中实现左转功能
Implements turn left function in 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;
}
旋转函数采用度数和轴两个参数,并返回旋转矩阵,该矩阵为 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);
}
旋转功能是正确的。
相关文章:
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何使用lock_guard在c++11中实现scoped_lock功能
- 多次实现成员功能
- 如何在现代C++中实现没有宏的系统特定功能
- 模板功能实现中的错误C2975
- 代码仅在2个功能实现中的1个中溢出
- 多个精度基本功能实现
- C 将功能实现对象作为接口类型
- 功能实现:过程或线程部门
- 在接口的功能实现中
- 将功能实现为函子的优缺点
- 授权派生的功能实现,而不使其纯粹
- 将成员功能实现为外部功能
- opencv功能实现
- 纯虚拟功能实现
- 链表推回成员功能实现
- 使用信号中止功能实现
- 虚拟功能实现和多重继承
- 异常规范在声明和功能实现方面不兼容