GLKit vs. Metal perspective matrix difference

坚强是说给别人听的谎言 提交于 2019-12-12 08:55:56

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/48311452/glkit-vs-metal-perspective-matrix-difference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!