Reputation: 253
I was wondering about the problem of getting 3D position from a 2D one. In my application, I got everything set up (mvp matrix, screen position of mouse).
We go from 3D to 2D by the following transform.
gl_Position = projection* view * model * vertex;
Then these clip coordinates are divided by w to NDC, and then converted to screen coordinates. I want to do the reverse (mouse click to 3D). Screen to NDC is easy for x and y. But there is a loss of z data.
As far as I can tell, the ONLY method to recover the 3D data is using ray-casting with a tree based spatial data structure and maybe CUDA/OpenCL. But I am not sure. Is there any other way? Is there a method to recover 3D position given that we have the MVP matrix and screen coordinates?
Upvotes: 1
Views: 409
Reputation: 162164
Read the deph value at the mouse pointer position (glReadPixels(…, GL_DEPTH,…)) and use that for recovering Z. Or do the raycasting into a scene structure. Depending on your applications either one may be the better solution.
Upvotes: 1