//have to get processing to dump all it's matricies into GL, so the functions work.
int viewport[] = new int[4];
//For the viewport matrix... not sure what all the values are,
//I think the first two are width and height, and all Matricies in GL seem to be 4 or 16...
double[] proj=new double[16];
//For the Projection Matrix, 4x4
double[] model=new double[16];
//For the Modelview Matrix, 4x4
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
//fill the viewport matrix
gl.glGetDoublev(GLMatrixFunc.GL_PROJECTION_MATRIX, proj, 0);
//projection matrix
gl.glGetDoublev(GLMatrixFunc.GL_MODELVIEW_MATRIX, model, 0);
//modelview matrix
FloatBuffer floatBuffer=ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asFloatBuffer();
//set up a floatbuffer to get the depth buffer value of the mouse position
gl.glReadPixels(mouseX, settings.getDisplayHeigth() - mouseY, 1, 1, GL2.GL_DEPTH_COMPONENT, GL.GL_FLOAT, floatBuffer);
//Get the depth buffer value at the mouse position.
//have to do height-mouseY, as GL puts 0,0 in the bottom left, not top left.
floatBuffer.rewind();
//finish setting up this.
double[] mousePosArr=new double[4];
//the result x,y,z will be put in this.. 4th value will be 1, but I think it's "scale" in GL terms,
//but I think it'll always be 1.
glu.gluUnProject((double)mouseX, settings.getDisplayHeigth() - (double)mouseY, (double)floatBuffer.get(0),
model, 0, proj,
0, viewport, 0, mousePosArr, 0);
//the magic function. You put all the values in, and magically the x,y,z values come out :)
gl.glEnd();
//return new float[]{(float)mousePosArr[0],(float)mousePosArr[1],(float)mousePosArr[2]};
//The values are all doubles, so throw them into floats to make life easier.}
Vector3D vector = new Vector3D(mousePosArr[0], mousePosArr[1], mousePosArr[2]);
System.out.println("mouse x "+ mousePosArr[0] );
System.out.println("mouse y "+ mousePosArr[1] );
System.out.println();
System.out.println("worldx "+ x);
System.out.println("worldy "+ y);
System.out.println("worldz "+ z);
System.out.println();