GLKit与金属透视矩阵的区别 [英] GLKit vs. Metal perspective matrix difference

查看:127
本文介绍了GLKit与金属透视矩阵的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读金属教程在raywenderlich.com上,其中引入了纯Swift Swift 4x4 助手类. 99%的内容只是GLKit函数的包装,除了一个确实令我困惑的函数:

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
  }

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

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?

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

Is this a particular choice this tutorial made? If not, are there any other incompatibilities between GLKit and Metal mathematics?

更新:我在WWDC 2016会议:采用Metal I中找到了有关Metal的剪辑空间坐标系的漂亮插图.

Update: I found a nice illustration about Metal's clip space coordinate system from the WWDC 2016 Session: Adopting Metal I.

推荐答案

OpenGL使用的剪辑空间坐标与Metal不同(在GL中,z 从-1到1,而在Metal z中从0到1),因此使用 GLKMatrix4MakePerspective没有为您提供适当的矩阵 从眼睛空间转换为剪辑空间.相反,它转化了一半 眼后的观看量的变化,有时会引起微妙的变化 裁剪和剔除问题.您可以修复返回的矩阵 通过设置与深度相关的矩阵元素 将以下代码添加到makePerspectiveViewAngle:

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)

let zs = farZ / (nearZ - farZ)

q [2] [2] = zs

q[2][2] = zs

q [3] [2] = zs * nearZ

q[3][2] = zs * nearZ

这篇关于GLKit与金属透视矩阵的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆