Screen coordinates to World coordinates?

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.

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.

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.

ReadPixels reads from the lower left, did you remap the mouse_y? What kind of values are you getting?

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!

Check your type: you’re asking for a float, but giving it a double.

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.

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.

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)?

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.

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.

Just wondering: will gluUnProject work correctly if I’ve previously made a call to glScalef?

Well, glUnProject(…) needs the current projection and modelview matrices to do its thing, so I’m not sure what you mean here.

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?

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. :slight_smile: