简体   繁体   中英

GLKit vs. Metal perspective matrix difference

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM