I'm not a good mathematician, but I'm trying to unproject 2D screen coordinates to a plane in a 3D space with perspective.
I first do an uniform scaling on the 3D scene. Then I rotate around X axis, the plane defined by X axis and Z axis. Then I translate the scene on Z axis, with a value of -1.0
.
So I have a ProjectionView matrix, computed by the following way, using the library linmath.h, in language C:
mat4x4 ProjectionView, matrix;
mat4x4 projection;
mat4x4_identity(projection);
mat4x4_perspective(projection, fova, width/height, zNear, zFar);
mat4x4 view;
mat4x4_identity(view);
mat4x4_translate(view, 0.0, 0.0, -1.0);
mat4x4_rotate(matrix, view, 1.0, 0.0, 0.0, theta);
mat4x4_scale_aniso(view, matrix, scale, scale, scale);
mat4x4_mul(ProjectionView, projection, view);
Here is the graphic I use to try to unproject:
The distance Oz
is what I can compute using trigonometry, it is on the plane where are the unprojected points, but it doesn't take in consideration the effects of perspective.
So I would like to know how to correct this Oz
distance, using the perspective parameters, in order to have the unprojected z
coordinate ?
And then how to compute x
coordinate, from the unprojected z
and from the perspective parameters ?
After some readings on internet, I tried using the inverse ProjectionView matrix, or with the inverse Projection matrix, without a good result, maybe due to the fact I don't know the z
value to give to these matrices. So I wonder if there is a way to solve this problem without using an inverse matrix ?
The computed Oz
distance is close to the true result, I think it just need a correction due to the perspective.