JOGL - glu.gluUnProject always returns 0.0

I'm currently trying to figure out how to get world coordinates from JOGL - at the moment it only returns x = 0.0, y = 0.0 and z = 0.0, regardless of 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 value, so I assigned it to the logical call test, which returns false.

EDIT2 :: I added another debugging statement - System.out.println (modelview [0]); and also returns 0.0

thanks for the help

James

+4
source share
3 answers

Finally, I found the reason why this happens, it turned out that I was pulling gl.glGetIntegerv (GL2.GL_VIEWPORT, viewport, 0); in the method before. It seems that if you do this, the next method to call will only be returned as a set of zeros!

Thanks for your suggestions.

0
source

One possible reason: are you simultaneously capturing the model matrix and projection matrix? ..

gluUnProject () works both on my side and what I do:

1) Only when updating the size of the viewing port and / or โ€œcameraโ€: capture the viewing area and the projection matrix.
I.E. You should after gl.glMatrixMode(GL.GL_PROJECTION)

2) Each "frame": capture the matrix of the model.
I.E. You should after gl.glMatrixMode(GL.GL_MODELVIEW)

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

0
source

gluUnproject () will always return TRUE and (0,0,0) for window coordinates if the 3D point you are trying to find is far behind the clipping plane (usually also behind the camera position). You may need to do the calculations manually after the query to get information about the matrices and viewport.

0
source

All Articles