线性化深度 [英] Linearize depth

查看:326
本文介绍了线性化深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OpenGL中,您可以像这样线性化深度值:

In OpenGL you can linearize a depth value like so:

float linearize_depth(float d,float zNear,float zFar)
{
    float z_n = 2.0 * d - 1.0;
    return 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear));
}

(来源: https://stackoverflow.com/a/6657284/10011415 )

但是,Vulkan处理深度值的方式有所不同( https://matthewwellings .com/blog/the-new-vulkan-coordinate-system/).我不太了解其背后的数学原理,我需要对函数进行哪些更改以使用Vulkan线性化深度值?

However, Vulkan handles depth values somewhat differently (https://matthewwellings.com/blog/the-new-vulkan-coordinate-system/). I don't quite understand the math behind it, what changes would I have to make to the function to linearize a depth value with Vulkan?

推荐答案

此处OpenGL和Vulkan之间的重要区别在于,规范化设备坐标(NDC)的z范围(深度)不同.在OpenGL中是-1比1,在Vulkan中是0比1.

The important difference between OpenGL and Vulkan here is that the normalized device coordinates (NDC) have a different range for z (the depth). In OpenGL it's -1 to 1 and in Vulkan it's 0 to 1.

但是,在OpenGL中,当将深度存储到深度纹理中并从中读取时,该值会进一步标准化为0到1.在您的示例中似乎是这种情况,因为函数的第一行映射回到-1到1.

However, in OpenGL when the depth is stored into a depth texture and you read from it, the value is further normalized to 0 to 1. This seems to be the case in your example, since the first line of your function maps it back to -1 to 1.

在Vulkan中,您的深度始终在0到1之间,因此上述函数在Vulkan中也适用.您可以通过以下方式简化它:

In Vulkan, your depth is always between 0 and 1, so the above function works in Vulkan as well. You can simplify it a bit though:

float linearize_depth(float d,float zNear,float zFar)
{
    return zNear * zFar / (zFar + d * (zNear - zFar));
}

这篇关于线性化深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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