PDA

View Full Version : Screen coordinates to World coordinates?



malancha
04-28-2004, 01:16 AM
When the user clicks somewhere on the window, is there any way in which I can find the world coordinates of that point? I've been doing all sorts of things with gluUnProject and gluProject, but I haven't been able to make much progress.
Thanks in advance.

plasmonster
04-28-2004, 02:50 AM
malancha,

make sure your passing the gl what it is expecting. Pay particular attention to the screen space z value required in gluUnProject(...); if that's off, even a hair, it can cause big problems. It should be between 0 and 1.

malancha
04-28-2004, 03:22 AM
Thanks, Portal. I know the screen z value passed to gluUnProject should lie between 0 and 1, but how do I get that value? I tried reading from the depth buffer, but that's not giving me anything in that range:


GLdouble scrz;
glReadBuffer ( GL_FRONT );
glReadPixels ( mouse_x, mouse_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &scrz); Should this be giving me scrz between 0 and 1? If it shouldn't, can you tell me how to make it do so?
I know that all the other parameters passed to glReadPixels are OK because I can find the color under the mouse accurately.

plasmonster
04-28-2004, 03:30 AM
ReadPixels reads from the lower left, did you remap the mouse_y? What kind of values are you getting?

malancha
04-28-2004, 03:38 AM
Yes, I'm taking care of the the mouse y-coordinate in advance.
Played around a little with the code, and now I'm consistently getting values of either -60 or +505.
Very strange.

EDIT -- figured out where the -60 is coming from, but not why. My perspective projection is set up as:
gluPerspective( 20.0f, width/height, 60.0f, 1000.0f );
If I change the near plane to 50.0f, then glReadPixels starts spitting out -50!

plasmonster
04-28-2004, 03:43 AM
Check your type: you're asking for a float, but giving it a double.

malancha
04-28-2004, 03:53 AM
I changed it to GLfloat, but no luck.
It's simply taking the location of my near plane, negating it, and dumping it into scrz.

plasmonster
04-28-2004, 03:59 AM
I've never tried getting the z this way. Try setting the read buffer back to GL_BACK (the default).

edit:

Also, it might help to see the glUnProject() in its entirety, along with any other relevent code.

Scratch the read buffer suggestion, that's for color buffers only - I need some sleep.

I can't seem to reproduce your problem here. I ran a test with the limited information I have about your setup, and everything works as expected. I suggest you go over all your code carefully and look for hidden blunders.

malancha
05-04-2004, 12:03 AM
Update: I find that I'm getting "believable" values for my screen z coordinate only if there's a mouse click. Shouldn't all this work even on passive mouse motion?

Backing up a little, I'd like to make very sure I know what I'm trying to do here. Let's assume that I set up a perspective projection and then draw two red lines intersecting at (10,20,30). I then call a few rotates and translates in succession, and then use glReadPixels and gluUnProject when my mouse is located exactly at the intersection of the two lines. Should this give me (10,20,30)?

plasmonster
05-04-2004, 12:24 AM
Let's assume that I set up a perspective projection and then draw two red lines intersecting at (10,20,30). I then call a few rotates and translates in succession, and then use glReadPixels and gluUnProject when my mouse is located exactly at the intersection of the two lines. Should this give me (10,20,30)? If you supply the correct information, then glUnProject() sould return the world coordinates, yes.

Andrew Davey
05-04-2004, 12:30 AM
Let's assume that I set up a perspective projection and then draw two red lines intersecting at (10,20,30). I then call a few rotates and translates in succession, and then use glReadPixels and gluUnProject when my mouse is located exactly at the intersection of the two lines. Should this give me (10,20,30)?Sorry for the off topic, but was wondering if anyone knows a way to work out this interception if you know the direction the line is going the origin of each line and the stop point of each line. So sorry for off topic post.

malancha
05-04-2004, 08:50 PM
Just wondering: will gluUnProject work correctly if I've previously made a call to glScalef?

plasmonster
05-04-2004, 09:00 PM
Well, glUnProject(...) needs the current projection and modelview matrices to do its thing, so I'm not sure what you mean here.

malancha
05-04-2004, 09:12 PM
What I'm asking is whether or not gluUnProject can "understand" the Modelview matrix even after it's been modified by glScale.
Put another way, does gluUnProject work properly regardless of the manner in which the Modelview matrix has been transformed, assuming that those transformations are all legitimate?

plasmonster
05-04-2004, 09:23 PM
Put another way, does gluUnProject work properly regardless of the manner in which the Modelview matrix has been transformed, assuming that those transformations are all legitimate?
Yes. :)