tags:

views:

535

answers:

3

Hi everyone,

I'm currently trying to work out how to get world coordinates from JOGL - at the moment it only returns x = 0.0, y= 0.0 and z = 0.0 no matter where I click. What am I doing wrong?

 public double[] getMousePosition(int x, int y){

 int viewport[] = new int[4];
 double modelview[] = new double[16];
 double projection[] = new double[16];
 float winX, winY, winZ;
 float posX, posY, posZ;
 double wcoord[] = new double[4];

 gl.glGetDoublev( GL2.GL_MODELVIEW_MATRIX, modelview, 0 );
 gl.glGetDoublev( GL2.GL_PROJECTION_MATRIX, projection, 0 );
 gl.glGetIntegerv( GL2.GL_VIEWPORT, viewport, 0 );

 winX = (float)x;
 winY = (float)viewport[3] - (float)y;

 float[] depth = new float[1];
 // gl.glReadPixels(winX, winY, 1, 1, gl.GL_DEPTH_COMPONENT, GL2.GL_FLOAT, depth);

 boolean test =  glu.gluUnProject( winX, winY, 0.0, modelview, 0, projection, 0, viewport, 0, wcoord, 0);

 System.out.println("x: " + wcoord[0] +"y: "+wcoord[1]+" worked? "+test);
 System.out.println(modelview[0]);
 return wcoord;
}

EDIT :: Forgot to mention that I noticed that glu.gluUnproject returns a boolean so I assigned it to a boolean called test which returns false.

EDIT2 :: I've added another debug statement - System.out.println(modelview[0]); and is also returning 0.0

Thank you for your help

James

A: 

One possible reason: you're grabbing the model-view matrix and the projection-matrix at the same time?..

gluUnProject() is working as intended on my side, and that's what I'm doing:

1) Only when the view-port size and/or the "camera" are updated: grab the viewport and the projection-matrix.
I.E. You should be after a gl.glMatrixMode(GL.GL_PROJECTION)

2) Each "frame": grab the model-view matrix.
I.E. You should be after a gl.glMatrixMode(GL.GL_MODELVIEW)

3) Then only: pass the 3 array to gluUnProject()...

Ariel Malka
A: 

gluUnproject() will always return TRUE and (0,0,0) for the window coordinates if the 3D point you are trying to find is far behind the near clipping plane (typically also behind the camara position). You may have to do the calculations manually after querying for the matrices and viewport info.

JRS
A: 

I've finally found the reason why this was happening, it turns out that I was pulling gl.glGetIntegerv( GL2.GL_VIEWPORT, viewport, 0 ); in a method before this one. It seems that if you do this the next method to call it will be returned nothing but a set of zeros!

Thanks for your suggestions

James Raybould