Hi to everyone. I’m creating an orthographic projection with:
gl.glOrtho(-750, 750, -500, 500, -10, 10);
Later on I try to proccess some mouse clicks. I want to get the mouse world coordinates and I do it through this function:
public void makeTransform(int x, int y, double[] worldXY) {
GL gl = glDrawable.getGL();
GLU glu = glDrawable.getGLU();
int[] viewport = new int[4];
double[] mvmatrix = new double[16];
double[] projmatrix = new double[16];
int realy;
double[] worldCoords = new double[4];
/*double[] wy = new double[1];
double[] wz = new double[1];*/
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport);
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, mvmatrix);
gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projmatrix);
realy = viewport[3] - y - 1;
System.out.println("Coordinates at cursor: (" + x + ", " + realy + ")");
glu.gluUnProject4((double)x, (double)realy, 0.0, 1.0,
mvmatrix, projmatrix, viewport, -10, 10,
worldCoords);
worldXY[0] = worldCoords[0];
worldXY[1] = worldCoords[1];
System.out.println("Coordinates at world: (" + worldCoords[0] + ", " + worldCoords[1] + ")");
}
Now the problem is I don’t get proper world coordinates. For example, if I click on the four corners of the window, these are the window coordinates (which are correct)
0, 600 900, 600
0, 0 900, 0
and these are the world coordinates (approximately):
-1240, 740 245, 740
-1240, -230 245, -230
There is some logic in them but they require some biasing to get correct results. But how come this is happening? Is gluUnProject4 not supposed to work with orthographic projections? By the way I wrote this in JOGL(java bindings for opengl).