Converting Mouse Clicks to 3D Coordinates

Started by
5 comments, last by 31337 20 years, 6 months ago
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
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/
Advertisement
Look at the Pick sample that comes with the SDK. That sample illustrates how to convert mouse coordinates into 3D coordinates and then determine what object in your scene you actually clicked on.

neneboricua
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...
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/
no i dont believe there are any built in functions for it...what you want to do is "ray picking" here is a site to start with
http://www.mvps.org/directx/articles/rayproj.htm

if that doesnt help at all..google for "ray picking"
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
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.
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]

This topic is closed to new replies.

Advertisement