An interpretation of depth value

Posted dydx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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/planar in view space, will also appear straight/planar in clip space. That is the idea behind the depth computation formula.

 

 





附件列表

     

    以上是关于An interpretation of depth value的主要内容,如果未能解决你的问题,请参考以下文章

    TypeError: 'list' object cannot be interpreted as an integer

    用random.randint函数时 报错 'str' object cannot be interpreted as an integer问题

    Bash is an sh-compatible command language interpreter that executes commands read from the standard

    每日一读On Interpretation of Network Embedding via Taxonomy Induction

    每日一读On Interpretation of Network Embedding via Taxonomy Induction

    Python3.4.1异常: 'float' object cannot be interpreted as an integer