Screen to World

I’m working on a small app where I’ve positioned myself in “space” with gluLookAt(). On a mouse-click…I’m using gluUnProject() to convert the mouse “XY” “back” into “world” space (at the near clipping plane). If I click at a projection of a world-space location known to be at (0,0,0)…I’d like to “get” (0,0,0)… I’m not! Anyone know of an “easy” way to get there? Thanks!

Mind to post the code?

Sure!..Hope this is “clear enough”
(and that I “attached” the “code” right)

// code lifted from a variety of methods…

// the code “basically” does the following:
// Sets the viewport
// sets the projection matrix…(orthographic)
// displays the GL “scene”
// sets model matrix
// “look at” to/from the desired location
// displayList processing
// as the mouse moves…attempts to convert screen XY
// back to “world”…

//------------------------------------------------

…at window resize, load, etc…()
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
SetPerspectiveOrOrtho();

// repaint…which ultimately hits RengerGLScene()
}

//------------------------------------------------

…SetPerspectiveOrOrtho()
{
// misc calcs

glOrtho(…);
}

//------------------------------------------------

…ModelPrep()
{
// some glLookAt() calcs

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLookAt(…);
}

//------------------------------------------------

…RenderGLScene()
{
ModelPrep();

//display various displayList models…
}

//------------------------------------------------

…on a MouseMove(…x, y, *worldX, *worldY, *worldZ, etc)
{
GLint viewport[4];
GLdouble modelViewMatrix[16];
GLdouble projectionMatrix[16];

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, modelViewMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);

gluUnproject(x, y, 0 /* near clipping plane */,
modelViewMatrix, projectionMatrix, viewport,
worldX, worldY, worldZ);
}

In your case
What is being returned in worldX, worldY, worldZ is the point where the ray which is projected into the screen (perpendicular) from the location of your mouse click (x,y)intersects with the near clipping plane.

If you were to make a second call to gluUnproject like so…

gluUnproject(x,y,1.0 /*far clipping plane */,
modelViewMatrix, projectionMatrix, viewport,
worldX2,worldY2,worldZ2);

…then the line joining
the point (worldX,worldY,worldZ)
and point (worldX2,worldY2,worldZ2)

should pass through (0,0,0) in world space if that’s where you’ve clicked on the screen.

It is important to note that only very specific cases, such as when the camera is at the origin in world space, will the plane defined by z=0 (your near clipping plane) in screen space pass through the origin in world space.