Jump to content
  • Advertisement
Sign in to follow this  

OpenGL screen coordinates to world coordinates

This topic is 3896 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
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'.

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!