Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Mapping 2D coordinates to 3D

This topic is 5441 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

Hi all. I''m trying to implement object selection, just like in Maya/Max, and I was wondering how I convert 2D mouse coordinates into 3D coordinates? I''m guessing that I could take the mouse.x and mouse.y and create a vector... ie. v = [ mouse.x, mouse.y, 0, 1 ] and then multiply this vector by the modelview matrix and then by the projection matrix. My code doesn''t work though, and I was wondering anyone else has implemented this? I''m also trying to get it so that I can translate/rotate/scale the object on one axis at a time. Like in Maya, there is a green/blue/red vector that comes out of the pivot point of the object. That''s essentially what I''m trying to implement. Help please!!!

Share this post

Link to post
Share on other sites
I''ve only ever implemented this for a raytracing engine, and that''s quite simple (since all I had to do was generate a ray through the given pixel and do a set of intersection tests) - so I''m not sure what help I can be, but here''s a thought for you anyways. If, when you render the scene, you can access some kind of buffer storing IDs for the faces visible at each pixel, you can just look into that buffer to get the ID, and use a lookup to tie that back to the "owning" object. However, this will only work if you''ve got access to the rendering logic.

If all you need is a vector from the viewpoint into world space, here''s a quick and dirty algorithm (assumes you know the world coordinates of the corners of your viewing plane, which should be easy to obtain). I usually work in a +x = left, +y = up, +z = away from camera world, so if you use a different coordinate system, change letters/signs as appropriate.

Convert x in pixels to a percentage of the scene width
Convert y in pixels to a percentage of the scene height
World x = viewing plane top left x + (percent * viewing plane width in world coordinates)
Same for world y
World z = viewing plane distance (usually == near-clipping distance)

Now you have a world coordinate which you can transform by your viewing matrix to obtain a vector from the camera through the pixel. Depending on how your pipeline is set up, you may need to use the inverse of the viewing matrix to get the correct vector.

Share this post

Link to post
Share on other sites
Thanks for the info ApochPiQ. I don''t have access to my code just now, but when I get a chance I''ll give what you suggest a try. It sounds like it might work.

Thanks for the reply.

Share this post

Link to post
Share on other sites

  • 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!