gpt4 book ai didi

swift - GLKit 与 Metal 透视矩阵的区别

转载 作者:搜寻专家 更新时间:2023-10-31 08:28:59 24 4
gpt4 key购买 nike

我正在阅读 Metal tutorial在 raywenderlich.com 上,它介绍了纯 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 mathematics 之间还有其他不兼容的地方吗?


更新:我在 WWDC 2016 session :采用 Metal I 中找到了关于 Metal 的剪辑空间坐标系的很好的说明。

clip-space

最佳答案

引自this forum question

OpenGL uses different clip-space coordinates than Metal (in GL, z goes from -1 to 1, while in Metal z goes from 0 to 1), so using GLKMatrix4MakePerspective doesn't give you a matrix that properly transforms from eye space to clip space. Instead, it transforms half of the viewing volume behind the eye, causing sometimes-subtle clipping and culling issues. You can fix up the matrix you get back from GLK by setting the matrix elements that are relevant to depth by adding the following code to makePerspectiveViewAngle:

let zs = farZ / (nearZ - farZ)

q[2][2] = zs

q[3][2] = zs * nearZ

关于swift - GLKit 与 Metal 透视矩阵的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48311452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com