3
\$\begingroup\$

I'm using JOGL to access OpenGL-methods on an old OpenGL version, because of school.

I have written a method which is called before any objects are drawn. This method shows which object is picked.

Before I call my "picking" method, I draw a camera. This is where the problems begin: I can't click precisely on the objects, because they don't know they are transformed by the camera. I have tried to leave the projection matrix unchanged, but this leads to other problems...

I first draw the camera, then do picking, then draw the objects to screen.

What am I missing?


My camera:

public void draw(GL2 gl) {
    GLU glu = GLU.createGLU(gl);
    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();

    glu.gluPerspective(viewingAngle, whRatio, 1, 1000);

    gl.glMatrixMode(GL2.GL_MODELVIEW);
    glu.gluLookAt(xPos, yPos, distance, 0, 0, 0, 0, 1, 0);
}

My picking method:

private void picking(GL2 gl) {
    if (currentClickPoint != null) {
        int buffSize = 512;
        int[] select = new int[buffSize];
        int viewport[] = new int[4];
        IntBuffer selectBuffer = newDirectIntBuffer(buffSize);

        gl.glGetIntegerv(GL2.GL_VIEWPORT, viewport, 0);

        gl.glSelectBuffer(buffSize, selectBuffer);
        //gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glRenderMode(GL2.GL_SELECT);
        gl.glInitNames();
        //gl.glPushName(-1);

        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glPushMatrix();
        gl.glLoadIdentity();

        glut.gluPickMatrix((double) currentClickPoint[0],
                (double) (viewport[3] - currentClickPoint[1]),
                10.0, 10.0, viewport, 0);

        //gl.glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);

        gl.glMatrixMode(GL2.GL_MODELVIEW);
        drawObjects(gl, GL2.GL_SELECT);

        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glPopMatrix();
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glFlush();


        selectBuffer.get(select);

        interpretClicks(select, gl.glRenderMode(GL2.GL_RENDER));

        resetClickPoint();
    }
}
\$\endgroup\$
1
\$\begingroup\$

According to the gluPickMatrix docs, this function is designed to be used in tandem with your normal projection matrix. In other words, it shouldn't replace the normal projection matrix as your code does, but be combined with it.

The docs suggest using the following sequence of calls to set up the picking matrix:

gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
glut.gluPickMatrix(...);
glu.gluPerspective(...);

The gluPerspective call would have the same arguments as in your normal camera setup. The modelview matrix should also remain the same as in the normal camera setup.

\$\endgroup\$
  • \$\begingroup\$ Thank you! This works for me. gl.glMatrixMode(GL2.GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity(); glut.gluPickMatrix(..) glut.gluPerspective(/*cam setup / gl.glMatrixMode(GL2.GL_MODELVIEW); //draw my objects gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glFlush(); / get buffer and interpret clicks */ gl.glMatrixMode(GL2.GL_PROJECTION); gl.glPopMatrix(); gl.glMatrixMode(GL2.GL_MODELVIEW); \$\endgroup\$ – Fruro Feb 15 '14 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.