#### Archived

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.

## 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 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 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.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 77
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632968
• Total Posts
3009583
• ### Who's Online (See full list)

There are no registered users currently online

×