View Full Version : Mapping screen coordinates to OpenGL scene coordin

01-19-2011, 08:36 AM

I tried replicating the article at:


in trying to map mouse coor. to scene coor.

However, when I run the code I get the exception:

javax.media.opengl.GLException: pack pixel_buffer_object must be enabled to call this method
and was wondering if anyone has experience with using glReadPixels() in JOGL.



public static Vector3D screenCoordinatesToOpenGLCoordinates(GL gl, int screenX, int screenY)
// viewport
int[] viewport = new int[4]; // Where The Viewport Values Will Be Stored
gl.glGetIntegerv(GL.GL_VIEWPORT,viewport,0); // Retrieves The Viewport Values (X, Y, Width, Height)
// modelview
double[] modelview = new double[16]; // Where The 16 Doubles Of The Modelview Matrix Are To Be Stored
gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelview,0); // Retrieve The Modelview Matrix
// projection
double[] projection = new double[16]; // Where The 16 Doubles Of The Projection Matrix Are To Be Stored
gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection,0); // Retrieve The Projection Matrix
// Subtract The Current Mouse Y Coordinate From The Screen Height since +ve y acts downwards
screenY = viewport[3] - screenY;
// screen z
long screenZ = 0;
gl.glReadPixels(screenX, screenY, 1, 1, GL.GL_DEPTH_COMPONENT, GL.GL_INT,screenZ);

GLU glu = new GLU();
double[] pos = new double[3];
glu.gluUnProject(screenX,screenY,screenZ, modelview,0, projection,0, viewport,0, pos,0);

Vector3D posVec = new Vector3D(pos[0],pos[1],pos[2]);
return posVec;