Jump to content
  • Advertisement
Sign in to follow this  
Coasterxtreme

Mouse to 3D space

This topic is 4621 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 I read through this: http://nehe.gamedev.net/data/articles/article.asp?article=13 I added a function to my program and am passing in the values I get back from GetCursorPos. The X and Y values I get back seem to be correct but the Z value is not correct. When I set up my persepctive my far plane is at 1000 and the Z value i get back from gluUnProject is like 999.----. When i make my far plane 100 I get a z value of 100.----. When I make my far plane 50 I get a Z value of 49.----. For some reason the Z value is not correct and it never changes. I am working on a terrain editor and I need to know which point the mouse is closest to so I know which point to raise and lower. How can I figure out which point my mouse is closest to? I thought gluUnProject would work with this and then I am figuring out the distance from the mouse point to each of my points on the terrain to see which has the shortest distance but with that Z value not being correct it is not working correctly.

Share this post


Link to post
Share on other sites
Advertisement
I don't know the whole context, so the following may be off-base. But here are two thoughts:

1. The inaccuracies you describe in the z result seem like they could be within reasonable limits for floating-point computations, so that may not be the problem.

2. For selection, you usually don't work with a single point derived from the mouse position, but rather a ray starting at the camera position and passing through the point. In your case you might find the distance from each vertex to the line (we're not worrying about broad phase for now), and select the vertex that is closest to the line.

The above idea is a start, but what you really need to do is create a small frustum centered on the picking ray and check vertices against this frustum; that will make picking appear to work uniformly regardless of how far away the vertex is from the viewer.

Ok, that was all kind of general, so ask if you need clarification.

Share this post


Link to post
Share on other sites
Well someone in my class was also doing a terrain editor and he used gluUnProject and he can move his mouse around and raise and lower points anywhere on the terrain. I don't complete understand what you described to do.

Share this post


Link to post
Share on other sites
If you can, you might ask your friend how he did it. I can't really think how you'd use a single unprojected point to select objects in a 3d environment, so I'm guessing your friend is using rays or picking volumes as I described (could be wrong though...)

Share this post


Link to post
Share on other sites
Maybe it would be easier for your to implement color picking on your terrain to see where a user clicked?

You can read about that here: http://gpwiki.org/index.php/OpenGL_Selection_Using_Unique_Color_IDs

Share this post


Link to post
Share on other sites
you can use the picking facility in OpenGL . You may have to associate a id with every point , but that should not be a problem .. See the chapter Selection and Feedback in REDBOOK

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!