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