View Full Version : What exactly do gluProject and gluUnProject do?

DalTXColtsFan

07-15-2003, 05:32 PM

Greetings all,

I was reading up on the two functions in the subject line and slapped together a little MFC test program that's on the site in my sig.

What exactly is gluUnProject "unprojecting"? I know my mouse arithmetic isn't perfect yet, but the numbers it's returning don't make any sense - they're not affected by whether or not the mouse is actually pointing at the cube.

So what exactly do these functions do and when might you want to use them? Pointers appreciated!

Thanks

Joe

http://daltxcoltsfan.tripod.com/OpenGL/OpenGL.htm (please pardon the popups)

Salutations

So what exactly do these functions do and when might you want to use them? Pointers appreciated!

The mathematical meaning for 'projection' is to to map an n-dimensional space onto a m-dimensional space where m < n. With that in mind: gluProject takes a point in three-dimensions and produces the mapped, windows coordinate point in two-dimensions (except its not really two dimensions--more later). gluUnProject undoes this mapping; it takes the two-dimensional window coordinate and some extra information and gets back the three-dimensional point. Consequently, if P is the matrix that represents the projection process, then P^-1 is the process which "unprojects" a vertex.

Now, about that extra information that you need for gluUnproject, and why I said gluProject is not really mapping to two dimensions. glProject will return the projected two-d windows coordinate and a magic z-coordinate. the magic z is the value is used to stash in the z-buffer for depth testing. gluUnproject needs a similar z value to undo the projection process. The reason is that projection is not a one-to-one mapping: lots of three-dimensional points can be mapped to the SAME two-d point... so without that information then the best you can do (well, the best you can theoretically do; gluUnProject doesn't do this) is unproject a point into a *RAY* in space. What gluUnproject actually does is computes the point along the ray that will uniquely map to the window coordinate AND the magic z component, so you get back a single 3D point.

make sense?

cheers

John

DalTXColtsFan

07-16-2003, 08:22 AM

Yeah that all makes perfect sense - thank you.

In the code sample on my site, the values returned by gluUnProject seem to be independent of whether or not the mouse is over the rectangular prism I drew - is that intentional as well or did I do something wrong in my math?

I totally agree with what you're saying about a ray - it doesn't make sense to me to say that a point in 2D space corresponds to only one in 3D space unless you do like what you said and do some sort of intersection with the depth value.

I guess I need to experiment a little more.

Let me ask one more question if you wouldn't mind. Suppose you wanted to allow the user to drag-draw with the mouse on the OpenGL context. Would you pretty much have to restrict them to, say, a 2D plane at least for the starting point? I mean, if you don't, if they click on a certain point and draw a sphere, how would you know where on the ray the sphere was? Same for dragging, I mean you still really have to do a lot of your own math don't you?

I apologize if I'm not making sense.

DalTXColtsFan

07-16-2003, 10:45 AM

I got it!!! http://www.opengl.org/discussion_boards/ubb/smile.gif

I was doing two things wrong - 1. I wasn't using the ScreenToClient and ClientToScreen functions correctly, so my mouse coordinates were all over the place, 2. I forgot that while Windows understands mouse coordinates relative to the *upper* left corner, OpenGL understands them relative to the *lower* left corner.

I have a 10x10 rectangular prism, and now when you move the mouse over the screen you can see instantly the difference when the mouse is pointing at the prism vs. when it isn't.

If anyone else is wondering how this function works, go ahead and check my page. MFC is required.

Cool stuff!

Thanks

Joe

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.