Login  Register

Re: 2D Picking with Jogl

Posted by Wade Walker on Mar 01, 2011; 1:57am
URL: https://forum.jogamp.org/2D-Picking-with-Jogl-tp2594712p2600140.html

Hi Daniel,

In 2D, all you have to do is convert your mouse pointer coordinates to object space, then search your list of objects to see which one the point is inside. With matrices are set up as you showed, you'd do something like this:

int [] aiViewport = new int [4];
gl.glGetIntegerv( GL.GL_VIEWPORT, aiViewport, 0 );

double [] adModelView = new double [16];
gl.glGetDoublev( GL.GL_MODELVIEW_MATRIX, adModelView, 0 );

double [] adProjection = new double [16];
gl.glGetDoublev( GL.GL_PROJECTION_MATRIX, adProjection, 0 );

double [] adObjectPos = new double [3];

GLU glu = new GLU();
if( !glu.gluUnProject( iX, glcanvas.getSize().y - iY, iZ,
                                adModelView, 0,
                                adProjection, 0,
                                aiViewport, 0,
                                adObjectPos, 0 ) )
            log( "Couldn't map window coordinates to object coordinates." );

Where iX, iY are your mouse coordinates on screen, and iZ is just zero. Then just find the object that contains the point (adObjectPos[0], adObjectPos[1]).