Ok, ignorant question time. I am trying to use gluUnproject to get coordinates of a mouse click in my scene but the x and y are always between -1.0 and 1.0.
I have drawn a grid on the XY plane and positioned a camera on the Z axis above looking down. I use gluLookAt to position, and allow zooming in and out (up and down) by then translating the viewport.
The Z coordinate returned from gluUnproject increases with the amount of Z translating I am doing to the camera (which don’t really want or care about). But X and Y ranges are the same.
I don’t understand why the modelview matrix is needed for the unproject. Doesn’t it change with each object transformation I do?
It seems I am missing something. Help, please. ???
Hi, here is a little method used to retrieve lat / long coordinate of a point on a sphere. Hope it can help.
private void unProjection( GL gl, GLU glu )
{
if( !clicked )
{
return;
}
gl.glGetDoublev( GL_MODELVIEW_MATRIX, model_view );
gl.glGetDoublev( GL_PROJECTION_MATRIX, projection );
gl.glGetIntegerv( GL_VIEWPORT, viewport );
double winX = clickX;
double winY = viewport[3] - clickY;
float[] winZ = new float[1];
gl.glReadPixels ( clickX, (int)winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, winZ );
double[] worldX = new double[1];
double[] worldY = new double[1];
double[] worldZ = new double[1];
glu.gluUnProject ( winX, winY, (double)winZ[0], model_view, projection, viewport, worldX, worldY, worldZ );
double r = sqrt ( worldX[0] * worldX[0] + worldY[0] * worldY[0] + worldZ[0] * worldZ[0] );
double lon = toDegrees ( atan2( worldX[0], worldZ[0] ) );
double lat = 90 - toDegrees ( acos( worldY[0] / r ) );
System.err.println("Radius = " + r );
System.err.println("Longitude = " + lon );
System.err.println("Latitude = " + lat );
System.err.println("");
System.err.println("");
nextLat = lat;
nextLon = lon;
clicked = false;
}
I was having problems similar to you when I noticed that I was doing gluUnproject out of the display method (outside my valid rending context). Maybe this is the problem you are having.