Screen to world coordinates

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?


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:

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

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


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.

P.S.:This is just for fixed depth.

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…