Hi
I coded up the examples at:
http://nehe.gamedev.net/article/using_gluunproject/16013/
http://www.java-tips.org/other-api-tips/jogl/how-to-use-gluunproject-in-jogl.html
to find the world coordinates of a mouse point; see code below which I simply called in display() to display the results each refresh.
The NeHe example uses glReadPixels() to determine the z-coordinate. However, it always a value of 1 for me.
The Java Tips example explicitly sets z to either 0 or 1.
But I’d like to obtain the complete window (x,y,z) to then pass to gluUnProject() to obtain the world (x,z,y).
What am I doing wrong?
Thanks
Graham
===
public Point3D worldCoordinates(GLAutoDrawable drawable)
{
// [http://www.java-tips.org/other-api-tips/jogl/how-to-use-gluunproject-in-jogl.html](http://www.java-tips.org/other-api-tips/jogl/how-to-use-gluunproject-in-jogl.html)
GL gl = drawable.getGL();
int viewport[] = new int[4];
float mvmatrix[] = new float[16];
float projmatrix[] = new float[16];
int realy = 0;// GL y coord pos
float wcoord[] = new float[4];// wx, wy, wz;// returned xyz coords
int x = xCoord;
int y = yCoord;
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX, mvmatrix, 0);
gl.glGetFloatv(GL2.GL_PROJECTION_MATRIX, projmatrix, 0);
realy = viewport[3] - (int)y; /* note viewport[3] is height of window in pixels */
FloatBuffer floatBuffer = FloatBuffer.allocate(1);
gl.glReadPixels( x, (int)realy, 1, 1, GL2.GL_DEPTH_COMPONENT, GL2.GL_FLOAT,floatBuffer);
float z = floatBuffer.get(0);
System.out.println("z: " + z);
System.out.println("Coordinates at cursor are (" + x + ", " + realy);
boolean ok = glu.gluUnProject((float) x, (float)realy, 0.0f, //
mvmatrix, 0,
projmatrix, 0,
viewport, 0,
wcoord, 0);
System.out.println("World coords at z=0.0 are ( " //
+ wcoord[0] + ", " + wcoord[1] + ", " + wcoord[2]
+ "); ok: " + ok);
ok = glu.gluUnProject((float) x, (float) realy, 1.0f, //
mvmatrix, 0,
projmatrix, 0,
viewport, 0,
wcoord, 0);
System.out.println("World coords at z=1.0 are (" //
+ wcoord[0] + ", " + wcoord[1] + ", " + wcoord[2]
+ "); ok: " + ok);
return new Point3D(wcoord[0],wcoord[1],wcoord[2]);
}
===