【问题标题】:GLKit vs. Metal perspective matrix differenceGLKit vs. Metal 透视矩阵的区别
【发布时间】:2018-01-17 23:08:40
【问题描述】:

我正在 raywenderlich.com 上阅读 Metal tutorial,其中介绍了纯 Swift float4x4 helper class。 99% 它只是 GLKit 函数的包装,除了一个让我很困惑的函数:

  static func makePerspectiveViewAngle(_ fovyRadians: Float, aspectRatio: Float, nearZ: Float, farZ: Float) -> float4x4 {
    var q = unsafeBitCast(GLKMatrix4MakePerspective(fovyRadians, aspectRatio, nearZ, farZ), to: float4x4.self)
    let zs = farZ / (nearZ - farZ)
    q[2][2] = zs
    q[3][2] = zs * nearZ
    return q
  }

为什么需要更改q[2][2]q[3][2]。这是 Metal 和 GLKit 坐标系之间的一些不兼容吗?

这是本教程做出的特殊选择吗?如果没有,GLKit 和 Metal 数学之间是否还有其他不兼容之处?


更新:我从 WWDC 2016 Session: Adopting Metal I 中找到了一个关于 Metal 剪辑空间坐标系的很好的插图。

【问题讨论】:

    标签: swift opengl 3d metal glkit


    【解决方案1】:

    引用this forum question

    OpenGL 使用与 Metal 不同的剪辑空间坐标(在 GL 中,z 从 -1 到 1,而在 Metal z 中从 0 到 1),所以使用 GLKMatrix4MakePerspective 没有给你一个正确的矩阵 从眼睛空间转换到剪辑空间。相反,它改变了一半 眼睛后面的观看量,有时会导致微妙的 裁剪和剔除问题。你可以修复你得到的矩阵 从 GLK 通过设置与深度相关的矩阵元素 将以下代码添加到 makePerspectiveViewAngle:

    让 zs = farZ / (nearZ - farZ)

    q[2][2] = zs

    q[3][2] = zs * nearZ

    【讨论】:

    • 还有来自 WWDC 视频的很好的视觉解释,将其添加到我的问题中。
    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2011-03-09
    • 2015-08-06
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2012-11-27
    相关资源
    最近更新 更多