Getting 2D to 3D

Something like this:

OnClick(int x, int y) {

GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realY //OpenGL coordinate, not window coordinate.
GLdouble wx, wy, wz //world space.

//test for which button, etc…

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
//viewport[3] is height of window in pixels
realY = viewport[3]-(GLint)y;

//print out mouse point if you wish.

gluUnProject((GLdouble)x, (GLdouble)realY, 0.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

//Print world space at Z=0.0f

gluUnProject(GLdouble)x, (GLdouble)realY, 1.0, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

//Print world space at Z=1.0f;

}

You may have to adjust your realY depending on what platform you’re on. Windows vs. Unix have different measures of coordinate systems. Windows is different from OpenGL in that windows measures positive Y as down, and Opengl measures positive Y as up (I THINK, I COULD BE WRONG ON THAT)