screen coordinates to world coordinates

Started by
1 comment, last by Jan-Lieuwe 16 years, 5 months ago
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
Advertisement
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'.
Ring3 Circus - Diary of a programmer, journal of a hacker.
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:

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