其他分享
首页 > 其他分享> > An Interpretation of Depth Value

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:

image001

In this matrix:

image002 is width of the screen

image003 is height of the screen

image004 is near clipping plane

image005 is far clipping plane

All these values are measured in view space.

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

image006

image007

After perspective division, the actual depth value is:

image008

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 image009 as an expression of image010, we get:

image011

Something interesting happens! The depth value is linear interpolation weight for the reciprocals 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 image012 and image013. The projection matrix is also known, so you can calculate their screen space coordinates image014 and image015. You want to perform linear interpolation on image plane with image016 the interpolation weight. The xy coordinate of point C is easy:

image017

image018

image019

But how can I get image020 conveniently, given image016[1] ? I can calculate image020[1] given image021, it’s

image022

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.

image023

Let

image024,

image025.

Then we have:

image026

image027

Also, we know

image028

image029

and

image030

Put them together, we have

image031

image032

image033

Put it into image020[2] , we have:

image034

image035

image036

Finally, we get

image037

That is to say, instead of using image021[1] to linear interpolate image009[1] ,we can use image016[2] to linear interpolate image038! We’ve already use image016[3] to linear interpolate xy coordinate. If we consider image038[1] 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.

image039

image040

image041

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

image037[1]

Then we have

image042

image043

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

image044

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, using the z component in view space directly), 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 screen space. That is the idea behind the depth computation formula.

标签:Interpretation,linear,space,screen,depth,value,Depth,Value,view
来源: https://www.cnblogs.com/dydx/p/10875105.html