View Full Version : 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?
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.
GLdouble getX, getY, getZ;
realY = viewport - (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.
08-22-2001, 06:17 PM
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...
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.