I am stuck on a math problem at the moment. I want to project 2D screen coordinates to a plane in 3D view space which is parallel to the view plane and has got a particular distance.

Let's suppose I have got a 2D point P

_{2}(x, y) on my view plane. My resolution is 800x600, so my x is ranging from 0 to 800 and my y from 0 to 600. I define a distance d

_{z}which the plane in 3D view space and the view plane should have. Now I want to estimate the the 3D point P

_{3}(x', y', z') in view space on that specific plane with distance d

_{z}to my view plane.

A projection matrix is given:

XMMATRIX projMatrix = XMMatrixPerspectiveFovLH(XM_PIDIV4, (float)(screenWidth) / (float)(screenHeight), 0.1f, 1000.0f);

The camera position vector camPos, the lookTo-vector and the up-vector are also given.

How do I estimate P

_{3}now? I tried to calculate the differen coordinates like that:

z' = camPos + lookTo*d

_{z}

x' = z' + cross(lookTo, up)*x

y = z' + up*y

The problem is, that due to perspective projection the plane in view space is bigger than the view plane itself and it gets bigger the bigger the distance d

_{z}is between them. Thus when I got a big distance between those planes the projected plane on the on the view plane is very small and when I move my cursor to the right top corner to draw something for example it gets drawn somewhere in the middle of the screen.

I have to scale the x and y values somehow but I don't know how. Is there maybe another approach to solve this?