Unproject Explained
Unproject Explained
The core issue is: given a mouse event (click, move, With our projection analysis, we can phrase this
question more specically now: given a set of coordinates on the 2D viewport, what are the corresponding coordinates in 3D space?
(click)
monitor/window
Observations
First off, note from the diagram that a 2D mouse click
does not translate into a point, but into a ray after all, we are adding an entire dimension 3D point; the best we can do is identify the line along which the mouse points 3D equivalent must lie line but what you do with it after that is up to you
gluUnProject() Double-Take
Given what we have said so far, some parts of
Why does the screen (win) point have a z-coordinate? Since the result of the function is a 3D point, the output arguments are passed as pointers; so what is that integer that the function returns directly?
We answer the second question rst: not all matrices Now back to that z-coordinate on the screen
are invertible thus, gluUnProject() might not succeed, in which case it will return GL_FALSE, with successful inversion returning GL_TRUE
Grab the current values for the three matrices: model Call gluUnProject() twice, once for (mx, my, 0.0) and
Once you have the two points, what you do next now
depends on how youre representing the objects in your model intersect that line, then choose one of them as the hit object, and act accordingly two endpoints, you can represent their line in terms of a single argument u, where u = 0 corresponds to the near point, and u = 1 corresponds to the far point L(u) = nearPoint + u(farPoint nearPoint)
In the sample program, were testing against a xed plane with a known z, so we solve for u using bilinear interpolation using the z coordinates, then use u to subsequently calculate x and y; the resulting (x, y, z) is the point on the plane that was clicked on by the mouse