If what your’e trying to get is to convert the mouse screen coordinates to the world coordinates then try the codes below. You will need a 3D math library for the Vector3f stuff. You can find one at http://www.objectclub.jp/download/files/vecmath/vecmath1.2-1.14.tar.gz
Let me know if it works for you.
/**
- Transform screen pixel coordinates to 3D world coordinates
*/
public float[] screenToWorld(GL gl, GLU glu, int sx, int sy)
{
StringBuffer result = new StringBuffer(32);
double[] p0 = new double[]{0f, 0f, 0f};
double[] p1 = new double[]{0f, 0f, 0f};
float[] p = new float[]{0f, 0f, 0f};
double[] objX = new double[]{-1};
double[] objY = new double[]{-1};
double[] objZ = new double[]{-1};
int realy = viewport[3] - sy;
/* Get world coordinates of mouse position at near plane */
glu.gluUnProject(sx, realy, 0, model, proj, viewport, objX, objY, objZ);
p0[0] = objX[0];
p0[1] = objY[0];
p0[2] = objZ[0];
/* Get world coordinates of mouse position at far plane */
glu.gluUnProject(sx, realy, 1, model, proj, viewport, objX, objY, objZ);
p1[0] = objX[0];
p1[1] = objY[0];
p1[2] = objZ[0];
/* Intersect a ray with this plane, outputing the 3D intersection point */
Vector3f rayStart = new Vector3f((float)p0[0], (float)p0[1], (float)p0[2]);
Vector3f rayDirection = new Vector3f((float)(p0[0] - p1[0]),
(float)(p0[1] - p1[1]),
(float)(p0[2] - p1[2]));
Vector3f normal = NORMAL(new Vector3f((float)west, 1f, south),
new Vector3f((float)west, 1f, (float)north),
new Vector3f((float)east, 1f, (float)north));
Vector3f origin = new Vector3f();
float denom = rayDirection.dot(normal);
if (denom != 0.0f)
{
Vector3f tmpDir = new Vector3f();
tmpDir.sub(origin, rayStart);
float t = tmpDir.dot(normal) / denom;
/* Find intersection point */
Vector3f tmpPt = new Vector3f();
tmpPt.set(rayDirection);
tmpPt.scale(t);
tmpPt.add(rayStart);
tmpPt.get(p);
/* As the northings are negative, trip the sign before using. */
p[2] = Math.abs(p[2]);
}
//System.out.println(“SCREEN TO WORLD - WORLD COORDINATES(x,y,z)=”+p[0]+","+p[1]+","+p[2]);
return p;
}
/**
*
*/
private Vector3f NORMAL(Vector3f p0, Vector3f p1, Vector3f p2)
{
Vector3f n = new Vector3f();
n.x = (p1.y - p0.y) * (p2.z - p0.z) - (p1.z - p0.z) * (p2.y - p0.y);
n.y = (p1.z - p0.z) * (p2.x - p0.x) - (p1.x - p0.x) * (p2.z - p0.z);
n.z = (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);
return n;
}