首页 > 解决方案 > 如何使用左手坐标系进行渲染?

问题描述

我正在尝试设置视图和投影矩阵以使用我想要的世界坐标和惯用手。我要使用左手坐标系,+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 个单元)。

任何帮助表示赞赏!

标签: c++mathmatrixvulkan

解决方案


Vulkan 与非旧版 OpenGL 和 DX 11+ 一样,都独立于任何选择的“惯用手”,这是您正在使用的数学库(如果有的话)的产物。

至于您的实际问题,您正在构建的矩阵是右手的,因为您分配-1matrix[3][2]. 左手版本是相同的,只是它具有1该位置。


推荐阅读