GLKit vs. Metal 透视矩阵差异 [英] GLKit vs. Metal perspective matrix difference
问题描述
我正在阅读 Metal
引用自 这个论坛问题
<块引用>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
I'm reading a Metal tutorial on raywenderlich.com, where it introduces a pure Swift float4x4 helper class. 99% it's just wrapper around GLKit functions, except one function which really puzzles me:
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
}
Why does it need to change q[2][2]
and q[3][2]
. Is this some incompatibility between Metal's and GLKit's coordinate system?
Is this a particular choice this tutorial made? If not, are there any other incompatibilities between GLKit and Metal mathematics?
Update: I found a nice illustration about Metal's clip space coordinate system from the WWDC 2016 Session: Adopting Metal I.
Quoting from 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
这篇关于GLKit vs. Metal 透视矩阵差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!