Archived

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

31337

Converting Mouse Clicks to 3D Coordinates

Recommended Posts

How do I convert mouse clicks to 3D coordinates with DirectX? The problem I''m facing is as follows: My camera is looking at a 2D grid being drawn (with line strips) and the world matrix is used to translate the grid up, down, left, and right, however the camera''s z position changes too. Yet it always looks at the origin. So, how do I do it? Thanks

Share this post


Link to post
Share on other sites
I could be wrong because I havn''t completely read through the picking example (its a pretty big "sample") but it seems almost like a hack. Isn''t there a directx command that will do it? I could have sworn there was something in GL for it...

Share this post


Link to post
Share on other sites
quote:
Original post by 31337
I could be wrong because I havn''t completely read through the picking example (its a pretty big "sample") but it seems almost like a hack. Isn''t there a directx command that will do it? I could have sworn there was something in GL for it...

If you understand how the sample framework is set up, you can quickly just go to the parts that are interesting about the particular sample. In the Pick sample, the important methods are CMyD3DApplication:ick(), and CMyD3DApplication::IntersectTriangle. The rest are just standard framework stuff that you don''t really need to worry about. Also note that D3DX has it''s own ray-triangle intersection routine so you don''t even have to use the one that comes with the sample. The comments in these methods are pretty well done and should help you understand the steps involved.

As far as OpenGL having some built-in functionality for picking, I don''t think it does. I could be wrong though. It''s been a while since I dealth with OpenGL in any depth.

Good luck
neneboricua

Share this post


Link to post
Share on other sites
OpenGL does have built in support for picking. I believe it relies on a stack based approach where you set up a bunch of parameters, including a really small viewing window around your cursor, then call through all of your geometry. Each logical item in your world then pushes a unique id onto this stack before it makes all of it''s render calls. When your all done with that, then you go through the stack (which also contains minZ/maxZ values between each push of a new id onto the stack), and figure out what the closest item is. I was confused at first as well coming from OpenGL, but once I figured out ray picking, it was fairly simple.

Share this post


Link to post
Share on other sites
Yeah I had this problem and am looking at ray picking to resolve it, in opengl you can read the depth of a pixel back from the depth buffer and use this in a call to gluUnProject to find the exact world space coordinate under the mouse.

there is an unproject method on Vector3 however as you cannot directly access a value in the z-buffer it is not as usefull as the OpenGL equivalent.

Some hardware does support lockable z-buffers so in the future I suspect things will be more like OpenGL.

[edit]
If your working on a flat plane then you could just do a single ray plane intersection with the plane eqn = (0,1,0,0)
[/edit]

Mark



[edited by - MButchers on October 8, 2003 6:50:11 AM]

Share this post


Link to post
Share on other sites