Well, if you have ready-made matrices, you can do this:
float[] modelView = float[16]; float[] projection = float[16]; float[] view = {0, 0, 640, 480}; // viewport float x = mouseX, y = mouseY, z = -1; // those are the inputs float[] pos = new float[4]; GLU.gluUnProject(x, y, z, modelView, 0, projection, 0, world.view().get_size(), 0, pos, 0); System.out.println("position of mouse in 3D is (" + pos[0] + ", " + pos[1] + ", " + pos[2] + ")");
If you want to select objects, you call gluUnProject () twice, once with z = -1 and once with z = 1. This gives you mouse positions in the near and far planes. Subtract them to get the viewing direction, use the first one as the source, and you have a good trace task (selecting an object).
source share