XMVector3Unproject - Screen to world coordinate at specific Z

814 views Asked by At

I want to convert screen coordinate to world coordinate in Directx 11. I found that I can do it easily with function XMVector3Unproject. There is one catch, I want to pick my own Z coordinate to tell the function how far from the camera my point is (screen coords are 2d, world coords are 3d). From what I observed, I have to figure out what's the position of my point on the depth buffer.

My depth buffer ranges from 0.0 to 1.0 (usual case). My near plane is 0.1f and far plane 1000.0f. I know that values on depth buffer are NOT distributed uniformly. In fact, for my setting, 20.0f is somewhere around 0.995 on depth buffer (close to near plane but nearly all the way down in depth buffer). How do I figure out these values ?

1

There are 1 answers

0
Maciej Szpakowski On BEST ANSWER

This formula works:

z'=(far+near)/(2*(far-near))+1/z*(-far*near)/(far-near)+1/2

where z is distance from the camera (20 in my example above) near and far are near and far clipping planes (0.1 and 1000 respectively). Source: Z-buffering