【发布时间】:2017-01-05 21:19:37
【问题描述】:
所以我开始制作自己的引擎,并让我的 Z 轴指向远离我的相机。我不知道为什么会这样,我几乎关注了this tutorial。
我将发布我的相机persprojection和view计算,请评论我应该发布哪些其他代码,我会更新我的帖子。
PersProjection
void Camera::setPerspective(float width, float height, float nearZ, float farZ, float angleDeg) {
perspectiveTrans.toZero();
float ar = width / height;
angleDeg = Math3D::ToRadian(angleDeg);
perspectiveTrans(0, 0) = 1.0f / (ar * tanf(angleDeg / 2.0f));
perspectiveTrans(1, 1) = 1.0f / tanf(angleDeg / 2.0f);
perspectiveTrans(2, 2) = (-nearZ - farZ) / (nearZ - farZ);
perspectiveTrans(2, 3) = (2.0f * farZ * nearZ) / (nearZ - farZ);
perspectiveTrans(3, 2) = 1.0f;
}
查看和投影计算
Matrix4x4f Camera::getViewProjection() {
Matrix4x4f cameraRotationTrans(MatrixType::IDENTITY);
Vector3f N = target;
N.normalize();
Vector3f U = up;
U.normalize();
U = U.cross(target);
Vector3f V = N.cross(U);
cameraRotationTrans.setRow(0, Vector4f(U, 0));
cameraRotationTrans.setRow(1, Vector4f(V, 0));
cameraRotationTrans.setRow(2, Vector4f(N, 1));
Matrix4x4f cameraTranslationTrans(MatrixType::IDENTITY);
cameraTranslationTrans.setCol(3, Vector4f(-position, 1));
return perspectiveTrans * cameraRotationTrans * cameraTranslationTrans;
}
【问题讨论】:
-
通常你会将该操作放在模型视图矩阵中,作为 Z 和 -1 因子的缩放组件。
标签: c++ opengl 3d coordinate-systems perspectivecamera