Re: Find position in 3D model from position on 2D screen
Posted by nabo on
URL: https://forum.jogamp.org/Find-position-in-3D-model-from-position-on-2D-screen-tp4025324p4025347.html
i tried your advice, but it does always return 0 hits. Can you help me with my code? I cannot find the mistake:
private static final int BUFSIZE = 512;
private IntBuffer selectBuffer;
private void pickModels(GL2 gl2, int x, int y)
{
startPicking(gl2, x, y);
for (Objekt3d objekt3d : objekt3dListe)
{
gl2.glPushName(1 + objekt3dListe.indexOf(objekt3d));
objekt3d.draw(gl2);
gl2.glPopName();
}
selectionMode = false;
endPicking(gl2);
}
private void startPicking(GL2 gl2, int x, int y)
{
selectBuffer = GLBuffers.newDirectIntBuffer(BUFSIZE);
gl2.glSelectBuffer(BUFSIZE, selectBuffer);
gl2.glRenderMode(GL2.GL_SELECT);
gl2.glInitNames();
gl2.glMatrixMode(GL2.GL_PROJECTION);
gl2.glPushMatrix();
gl2.glLoadIdentity();
int viewport[] = new int[4];
gl2.glGetIntegerv(GL2.GL_VIEWPORT, viewport, 0);
glu.gluPickMatrix((double) x, (double) (viewport[3] - y), 5, 5, viewport, 0);
System.out.println(x + " " + ((viewport[3] - y)));
float widthHeightRatio = (float) drawable.getWidth() / (float) drawable.getHeight();
glu.gluPerspective(30.0, widthHeightRatio, 1.0, 1000.0);
gl2.glMatrixMode(GL2.GL_MODELVIEW);
}
private void endPicking(GL2 gl2)
{
gl2.glMatrixMode(GL2.GL_PROJECTION);
gl2.glPopMatrix();
gl2.glMatrixMode(GL2.GL_MODELVIEW);
gl2.glFlush();
int numHits = gl2.glRenderMode(GL2.GL_RENDER);
System.out.println("endpicking, hits: " + numHits);
processHits(numHits);
selectionMode = false;
}