PDA

View Full Version : Screen to world coordinates



merk
08-21-2001, 10:23 AM
I'm trying to translate an object using mouse input coordinates and I would like the object to track the mouse. I'm using a perspective viewing transformation. I've tried using gluUnproject() with glReadPixels to give me the correct depth as hinted at in the FAQ, but I can't seem to get it to work. The object motion simply does not track the mouse motion. Am I doing this all wrong? Does anyone have any pointers or code snippets?

Thanks!

Platinum
08-22-2001, 06:15 PM
I had a similar problem. I wanted to track a cube on the screen with mouse input. It didn't work, the object was jumping on random positions. The problem was, that I was using modelview matrix already set by translations and rotations. Solution: Load identity matrix.

some code:

case WM_MOUSEMOVE:
{
GLdouble modelmatrix[16];
GLdouble projmatrix[16];
GLint viewport[4];
int realY;
GLdouble getX, getY, getZ;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
realY = viewport[3] - (int)HIWORD(lParam) - 1;
gluUnProject(LOWORD(lParam), realY, -4.0f, modelmatrix, projmatrix, viewport, &getX, &getY, &getZ);

break;
}

p.s.: this code is a little bit changed code from the Red Book.

Don't know if this is what you want, just hope it helps.

Platinum
08-22-2001, 06:17 PM
P.S.:This is just for fixed depth.

john
08-22-2001, 10:09 PM
Ja, well, of course...?

<looks dumbfounded> if you calculate a world coordinate for a given screen cordinate, and then render that world coordinate, the current modelview matrix when the coordinate is rendered MUST be the same as the modelview matrix you pass to gluUnproject. If you just pass the identity matrix to unproject, then.. yes, you NEED to set the modelview matrix to I when its rendered...

cheers,
John