Oh. I actually solved the problem, sort of.
I first, modify from the source code of glUnproject to have the following:
public static Vec3 unProject(
float winx, float winy, float winz,
Matrix44 resultantMatrix,
int width, int height){
float[] m = new float[16],
in = new float[4],
out = new float[4];
m = Matrix44.invert(resultantMatrix.get());
in[0] = (winx / (float)width) * 2 - 1;
in[1] = (winy / (float)height) * 2 - 1;
in[2] = 2 * winz - 1;
in[3] = 1;
Matrix.multiplyMV(out, 0, m, 0, in, 0);
if (out[3]==0)
return null;
out[3] = 1/out[3];
return new Vec3(out[0] * out[3], out[1] * out[3], out[2] * out[3]);
}
Input to the above would be the point in the Projected View Frustum Coordinates (i.e., screen input). For example:
unProject(30, 50, 0, mvpMatrix, 800, 480)
will translate the screen input (click) at (30,50) to the world coordinate where the object is sitting at. The third parameter, winz
is actually on which projected plane the click is occured, here, 0 means the nearZ of the projection plane.
The way I make picking functions, is by unprojecting two points, using the above function, on the far and near clipping plane, so:
Vec3 near = unProject(30, 50, 0, mvpMatrix, 800, 480);
Vec3 far = unProject(30, 50, 1, mvpMatrix, 800, 480); // 1 for winz means projected on the far plane
Vec3 pickingRay = Subtract(far, near); // Vector subtraction
Once we have the picking ray, what I am doing is simply testing the distance between the picking ray and the "center" of those "pickable" objects. (of course, you can have some more sophisticated testing algorithm).