An interpretation of depth value

An Interpretation of Depth Value

Recently when I am working on Screen Space
Reflection, I noticed there are some subtleties in the computation of depth
value.

In this article I will explain the deeper
meaning in the computation of depth value.

Perspective Projection Matrix

First, it is well known that the DirectX
perspective projection matrix is:

In this matrix:

 is width of the
screen

 is height of
the screen

 is near
clipping plane

 is far clipping
plane

All these values are measured in camera
space.

Since DirectX uses row vector, the z and w
component are:

After perspective division, the actual
depth value is:

But what does it mean? Is there any deeper
meaning in it? Why not use a simpler one, like linear interpolation?

The answer to the second question is yes.

To dive into the deeper meaning, first we
express  as an expression
of ,
we get:

Something interesting happens! The depth
value is linear interpolation weight for the reciprocal of near and far value!

Sounds great, but who cares? Yes, in most
cases, you don’t need to care about that. However, in some specific case, it
will bring you a lot of convenience. Screen space reflection is an example.

Ray Marching in Screen Space

In screen space reflection, one need to
perform ray marching and find the intersection of a ray and depth buffer. Of
course, ray marching can be performed in view space, but it’s difficult to
choose a proper step size because the same step size in view space appear
smaller and smaller when the ray is moving away from the camera. Alternatively,
performing ray marching in screen space can avoid this problem because you can
choose the step size based on the pixel size. However, as the depth value is a
non-linear function of z value in view space, the step size for depth value is
not constant. If you use the projection matrix to calculate the depth value
based on the x and y values in every step, that will cause a lot of
computation. Is there a fast way to calculate the depth value? Yes! And the
linear interpolation nature of the reciprocal of depth value is the heart of
this method.

To explain this, I would like to formulate
the problem first. Suppose you know the view space coordinates of the both ends
of a line segment AB, denoted as  and
.
The projection matrix is also known, so you can calculate their screen space
coordinates  and
.
You want to perform linear interpolation on image plane with  the
interpolation weight. The xy coordinate of point C is easy:

But how
can I get  conveniently,
given  ? I can
calculate  given
, it’s

So, the
next step is to know the relationship between s and t.

Now
let’s look at the picture below, it’s the equivalent version of the last
picture.

Let

,

.

Then we
have:

Also we
know

and

Put them
together, we have

Put it
into  ,
we have:

Finally,
we get

That is
to say, instead of using  to
linear interpolate  ,we can use  to
linear interpolate !
We’ve already use  to linear
interpolate xy coordinate. Furthermore, we can consider  as
a special coordinate axis. So, in the (x’, y’, 1/z) coordinate space, every
axis can be linear interpolated!

Why is the depth value like that?

From the
first section, we know the depth value is the linear interpolation weight for
the reciprocal of near and far value.

From the
second section, we know (x’, y’, 1/z) can be linear interpolated.

Then we
have

That
means  linearly
interpolates depth value, and in the (x’, y’, depth) coordinate space, every
axis can be linear interpolated.

To show
the benefit of this, let’s see the picture above, suppose there is a line
segment in view space, if we choose a na?ve depth (for example, linear
interpolating near and far value), the line segment will become a curve in (x’,
y’, depth) space. However, by using DirectX depth (OpenGL depth is similar),
the line segment will be still a line segment!

Things
that appear straight/planarin
view space, will also appear straight/planar in clip space.
That is the idea behind the depth computation formula.

来自为知笔记(Wiz)

附件列表

原文地址:https://www.cnblogs.com/dydx/p/10854565.html

时间: 2024-08-10 17:02:53

An interpretation of depth value的相关文章

Maximum Depth of Binary Tree

这道题为简单题 题目: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 思路: 我是用递归做的,当然也可以用深搜和广搜,递归的话就是比较左右子树的深度然后返回 代码: 1 # Definition for a binary tre

测试不同格式下depth buffer的精度

这篇文章主要是参考MJP的"Attack of The Depth Buffer",测试不同格式下depth buffer的精度. 测试的depth buffer包含两类: 一是非线性的depth buffer,存储着perspective z(也就是最常用的,透视投影后归一化的z/w的buffer),二是线性的depth buffer,存储着view space z(这里的线性指的是在view space 中是否线性).测试的格式包括16位浮点数,32位浮点数,16位定点数,还有最常

[leetcode] 104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

leetcode笔记:Minimum Depth of Binary Tree

一. 题目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 二. 题目分析 这道题属于二叉树的深度优先搜索,然后返回深度最小的值,可以递归(当然,也可以使用迭代)来实现.递归退出的条件是到达叶子节点或者到达空子树,使用空子树

leetcode笔记:Maximum Depth of Binary Tree

一. 题目描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 二. 题目分析 这道题和Minimum Depth of Binary Tree一题相似,这个是求最大深度的,就是对二叉树进行递归,然后将子树的最大深度进行返回,最

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the

[LeetCode] 111. Minimum Depth of Binary Tree Java

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题意及分析:求一棵二叉树的最低高度,即根节点到叶子节点的最短路径.遍历二叉树,然后用一个变量保存遍历到当前节点的最小路径即可,然后每次遇到叶子节点,就判断当前叶节点高度和先

[LeetCode] 104. Maximum Depth of Binary Tree Java

题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题意及分析:找出一棵树的高度,即最深子节点.使用深度遍历的方法即可,用一个变量记录遍历到当前点的最大高度,然后当前点若有子节点,遍历到子节点,那么该点的高度+1和当前的最大

手势跟踪论文学习:Realtime and Robust Hand Tracking from Depth(三)Cost Function

iker原创.转载请标明出处:http://blog.csdn.net/ikerpeng/article/details/39050619 Realtime and Robust Hand Tracking from Depth中的Cost Function 学习 首先,我们应该知道,输入的数据是什么:3D 点云数据. 3D点云给我的感觉应该是这种 输出的是:拟合好的手模型(48球体模型). 而这里的的3D 点云数据用p表示,每个球体用Sx 表示. Ci 第i个球体的中心:D表示深度图( 区分还