Transform the depth buffer Z-value into the camera coordinate system

Source: Internet
Author: User

A projection matrix is required to convert the Z-value in depth buffer to the camera coordinate system

Opengl:

The projection matrix of OpenGL is

Set the camera coordinate point to (vx,vy,vz,1.0)

The point after transformation by the above projection matrix is (PX,PY,PZ,-VZ)

PX and py We do not care, pz=az+b,a=-(f+n)/(F-n), b=-2fn/(F-n),

Then divide the items by W,pz=-a-b/vz.

Now that PZ is between [ -1,1], OpenGL will transform it into [0,1] to get DZ

The above is the camera coordinate system to the projection coordinate system process

When you get the DZ in depth buffer, follow the procedure below to get the camera coordinate system vz

1. Transform DZ from [0,1] to [ -1,1] to get dz ', ie dz ' =2*dz-1

2. By the foregoing, Dz ' =-a-b/vz,vz=-b/(A+dz ')

In the case of known F and N, vz=2fn/(Dz ' (f-n)-(f+n))

Since OpenGL is the right-hand coordinate system, the camera is oriented to the-Z direction, so the above vz is multiplied by-1.0

namely Vz=-1.0*vz

vz=b/(A+dz ')

vz=2fn/((f+n)-dz ' (f-n))

The above is the process of OpenGL transformation

The transformation process of D3D

The projection matrix of the D3D is

Set the camera coordinate point to (vx,vy,vz,1.0)

The point after transformation by the above projection matrix is (PX,PY,PZ,VZ)

PX and py We don't care, pz=az+b,a=f/(f-n), b=-fn/(F-n),

Then divide the items by W,pz=a+b/vz.

The resulting pz is between [0,1] and PZ is the DZ in depth buffer.

Known DZ we use the following process to get VZ

Dz=a+b/vz

vz=b/(DZ-A)

vz=fn/(F-dz (f-n))

Transform the depth buffer Z-value into the camera coordinate system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.