【发布时间】:2021-07-14 23:05:20
【问题描述】:
我正在尝试设置视图和投影矩阵以使用我想要的世界坐标和惯用手。我要使用左手坐标系,+X 在您的右侧,+Y 在您上方,+Z 在您之前。
Y 坐标工作正常,但放置在相机前面的物体 (+Z) 显示在它后面,所以我必须将相机旋转 180 度才能看到它们,这是一个简单的修复,因为翻转视图矩阵' Z 做到了,但现在对象被 X 方向翻转(文本被视为在镜子中)。我尝试对每个对象 Z 的模型矩阵求反,效果很好,但我觉得应该有另一种更清洁的解决方案。
我的问题与此类似:Inverted X axis in OpenGL,但我找不到合适的解决方案。
这是投影矩阵代码。
Matrix4 BuildPerspectiveMatrix(const float32 fov, const float32 aspectRatio, const float32 nearPlane, const float32 farPlane)
{
Matrix4 matrix;
//Tangent of half the vertical view angle.
const auto yScale = 1.0f / Tangent(fov * 0.5f);
const auto far_m_near = farPlane - nearPlane;
matrix[0][0] = yScale / aspectRatio; //xScale
matrix[1][1] = -yScale;
matrix[2][2] = farPlane / (nearPlane - farPlane);
matrix[2][3] = (farPlane * nearPlane) / (nearPlane - farPlane);
matrix[3][2] = -1.0f;
matrix[3][3] = 0.0f;
return matrix;
}
场景设置如下:
相机位于 (0, 0, 0)(世界中心),对象 1 位于 (0, 0, 2)(相机前方 2 个单位),对象 2 位于 (1, 0) , 2) (右侧 1 个单元,摄像机前方 2 个单元)。
感谢任何帮助!
【问题讨论】: