screen coordinates to world coordinates
When I click the mouse on the window, I am x,y points,
which I understand are the screen coordinates or window coordinates.
Now How to find the world coordinate of this window coordinates.
OpenGL/DirectX has built in Functions. But I am not suppose to use
these functions.
What is the algorithm and please explain the theory behind it.
Thanks in advance
It is called unprojection. Transform the screen coordinates by the inverse world-view-projection-viewport matrix.
Since this matrix is singular (due to the projection), a degree of freedom remains and so we typically unproject (x, y, 0) and (x, y, 1) to produce the terminal points (on the near & far clipping-planes) of the corresponding world-space line-segment.
It has been discussed to death on this forum, and all over the web. Key words would be 'picking, object, ray, unproject'.
Since this matrix is singular (due to the projection), a degree of freedom remains and so we typically unproject (x, y, 0) and (x, y, 1) to produce the terminal points (on the near & far clipping-planes) of the corresponding world-space line-segment.
It has been discussed to death on this forum, and all over the web. Key words would be 'picking, object, ray, unproject'.
The basics:
You first convert a screen position to a 'world line'. Then you create a ray from that 'world line' and shoot it against an arbitrary placed plane in the world. The intersection point is your 'world position'.
Pseudo code:
Edit: code tags
You first convert a screen position to a 'world line'. Then you create a ray from that 'world line' and shoot it against an arbitrary placed plane in the world. The intersection point is your 'world position'.
Pseudo code:
Vector3 screenPosToWorldPos (Vector2 screenPos, // screen position Plane plane) // projection plane{ // convert screen pos to a line in the world Vector3 near, far; // this function requires an inverse projection * inverse view matrix screenPosToWorldLine(screenPos, near, far); // create a ray and shoot it against the plane Ray ray(near, far); float distance = ray.getDistanceBetween(near, plane); return near + (ray.getDirection() * distance);}
Edit: code tags
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement