Hello all!
I’m trying gluUnproject for the first time to turn mouse coordinates into world space coordinates. I have it working like a charm for orthographic projections but am having trouble getting the world coordinates I expect when using a perspective projection. For example, with the code below, I get world coordinates of -19.0, -19.0, -19.0 when I click on what should be the world center. The good news is that these numbers change in the correct relative sense. For example, if I click on an area above the world center, the y coord will increase in value.
Here’s my sample code. I listed the glutMouseFunc() and glutDisplayFunc() functions I am calling. It’s set up to produce the perspective projection, but I also have the two lines necessary to produce the correctly-working ortho projection commented out. In order to post this I had to strip the parentheses out of the gluUnProject command. I kept getting errors about posting html. How are other people posting code fragments?
static void myDisplay(void)
{
glClearColor(0.6, 0.6, 0.6, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
//gluPerspective(45.0, 1.0, 1.0, 50.0);
glOrtho(-2.0, 2.0, -2.0, 2.0, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glutWireSphere(1.0, 10, 8);
glutSwapBuffers();
}
static void myMouse(int button, int state, int x, int y)
{
GLint viewport[4];
GLdouble model[16], projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;
glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
winX = (float)x;
winY = (float)viewport[3] - (float)y;
winZ = 1.0;
cout << "window coords are: " << winX << " " << winY << " " << winZ << endl;
gluUnProject winX, winY, winZ, model, projection, viewport, &posX, &posY, &posZ;
cout << posX << " " << posY << " " << posZ << endl;
}