View Full Version : finding a point on a square

04-24-2001, 04:49 PM
I'm looking to find where on a square the mouse is pointing.
The projection matrix is gluPerspective(60,ratio,0.1,1000);
The modelview matrix is gluLookAt(0, 10, 10, 0, 0, 0, 0, 1, 0);
The viewport is (0, 0, 800, 600).
The corners of the square are (-5, 0, -5) and (5, 0, 5).

I tried using gluUnProject but I realized that I don't know the window z coordinate. So I tried making a vector by calling gluUnProject twice using window coords of 0 and 1 then interpolating for y=0 but that didn't seem to work. Should it have? Is there a better way? Any help is appreciated.

04-24-2001, 11:53 PM
You can use gluUnProject. The window Z-coordinate is the value you read from the depthbuffer. Use GL_DEPTH_COMPONENT (I think) with glReadPixels to extract the depthvalue for gluUnProject.

04-25-2001, 10:28 AM
Thanks Bob.

04-25-2001, 01:01 PM
It seems like this is almost works.


GLdouble modelmat[16], projmat[16];
GLint viewmat[4];
glGetDoublev(GL_MODELVIEW_MATRIX, modelmat);
glGetDoublev(GL_PROJECTION_MATRIX, projmat);
glGetIntegerv(GL_VIEWPORT, viewmat);
GLfloat zdepth;
x, y, // window location
1, 1, // box size
GL_DEPTH_COMPONENT, // from depth buffer
GL_FLOAT, // return type
gluUnProject(x, y, zdepth, modelmat, projmat, viewmat, &objx, &objy, &objz);

The objy is usually very close to 0. I changed my near and far viewing planes to 4 and 32 because I thought it might be losing precision in the depth buffer but it's still off by some. I'm pretty sure I'm sending the right matrices. Is there normally error or am I doing this wrong?