Jump to content
  • Advertisement
Sign in to follow this  
kaonashi

gluUnProject giving different coords each time

This topic is 4816 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 use gluUnProject to get my 3D mouse coords, and then do a ray with my camera to find out the coord i clicked on a plane. The problem is that everytime i click on the same point, gluUnProject gives me different values.. The weird things is that if i make an average of those values it gives me approximatively the right point (still a little wrong though..). Here's my gluUnptoject code : glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); glGetDoublev( GL_PROJECTION_MATRIX, projection ); glGetIntegerv( GL_VIEWPORT, viewport ); winX = (float)x; winY = (float)viewport[3] - (float)y; glReadPixels( x, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ ); gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ); Anyone has an idea what might cause that? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Ok I found out why i get different values when i click twice on the same point. I guess it because I move something from the old point that was clicked to the new one. I tried without moving anything and I got the same coords when I was clicking twice on a point. So I guess its because the matrix changes everytime i move something, so is there a way to fixed that so even when I move my object I get the same coords when I click twice the same point?
Thanks

Share this post


Link to post
Share on other sites
I don't know much about glReadPixels(), but it looks like you're using the z value of the pixel at the mouse position for your window z value. This will of course change if your camera or objects in your world move. The point will always be along the ray corresponding with that mouse position, but will in general not be the same point from frame to frame.

If you want consistent results, submit a constant value for winz rather than querying for it. Common values to use are 0 (corresponding to the near plane by default) or 1 (corresponding to the far plane by default).

Also, make sure the modelview matrix only contains the view transform when you query for it.

Share this post


Link to post
Share on other sites
thanks for the replies!
I found out what was causing the problem.. I just needed to reload the model view matrix everything i moved my object. Now its working fine. :)

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!