0

I am using the GL_SELECT method to achieve mouse selection in OpenGL using JOGL Java Library. I know the method is deprecated and such, but it is a simple school assignment and this should do it.

However I am having some trouble: even though something is rendered in GL_SELECT mode, glRenderMode(GL_RENDER) returns zero hits. The problem is deterministic, but I don't see a kind of pattern; for example, if I have a sphere in the center, it works if I click on its upper part, but not on its lower part. For a cube, it only won't work on one specific face. For a rectangle it works alright.

I have tested commenting out the glRenderMode(GL_SELECT) to check if something was indeed being rendered and yes, I could see the shape, but even so glRenderMode(GL_RENDER) gave me zero.

EDIT: I have also tested removing the call to gluPickMatrix() and the glRenderMode(GL_SELECT), which gave me exactly the same as the normal (non-picking) render, so the projection and model view matrixes are set up correctly I think.

So, I don't think I am rendering incorrectly in select mode. What can be going on?

EDIT: maybe this could be a hardware problem, as the method is deprecated. Is that possible?

Thanks in advance.

// Get required information
point.y = getHeight() - point.y;
gl.glGetIntegerv(GL2.GL_VIEWPORT, view, 0);

// Setup OpenGL for selection
gl.glSelectBuffer(64, buffer);
gl.glRenderMode(GL2.GL_SELECT);
gl.glInitNames();

// Setup projection matrix
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();

Util.glu.gluPickMatrix( point.x, point.y, 5.0, 5.0, view, 0 );
Util.glu.gluPerspective(camera.getFieldOfView(), getWidth() * 1.0 / getHeight(),
                    camera.getCloseDistance(), camera.getFarDistance() );

// Setup model view matrix for rendering
gl.glMatrixMode(GL2.GL_MODELVIEW);
camera.setView(gl); // Set to model view and use glLookAt
gl.glClear( GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );

// Render objects
for(int i = 0; i < shapeList.size(); i++) {
    gl.glPushName(i);
    // Execute transformations for translation/rotation/scale and render shape
    shapeList.get(i).display(gl, false);
    gl.glPopName();
}
// Process hits
hits = gl.glRenderMode(GL2.GL_RENDER);
System.out.println("Hits = " + hits);
// ... Process hits here ...
// Reset matrixes
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL2.GL_MODELVIEW);
gl.glLoadIdentity();

camera.setView function:

public void setView( GL2 gl ) {
    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
    Util.glu.gluLookAt( eye[Axis.X], eye[Axis.Y], eye[Axis.Z],
               target[Axis.X], target[Axis.Y], target[Axis.Z],
               up[Axis.X], up[Axis.Y], up[Axis.Z] );
}
Hossomi
  • 133
  • 1
  • 6
  • Have you considered `glLoadName` instead of `glPushName` and `glPopName`? I don't see the point of using the name stack here since each object gets its own name and doesn't care about the name of the previous. I doubt it will change anything, it just doesn't make sense to me to go to all the trouble of saving and restoring the stack's state. You would need to push a name (any arbitrary integer would work) onto the stack after initializing it though, but after that just call `glLoadName` repeatedly to overwrite the top of the stack. – Andon M. Coleman Dec 24 '14 at 18:48
  • Yes I did, I should go back to `glLoadName`. As you said, nothing changed. Thanks for advice. – Hossomi Dec 24 '14 at 19:09
  • One thing that is missing in the code shown here is a `glPopMatrix()` call to match the `glPushMatrix()` of the projection matrix. Do you have that later? Is the `gluPerspective()` call the same as the one used for rendering? Does `camera.setView()` call `glLoadIdentity()` first? You shouldn't really have to change the modelview matrix here. – Reto Koradi Dec 24 '14 at 22:46
  • Yes, I hve `glPopMatrix()` in the end; sorry, I should have included them (now done). And yes, `gluPerspective()` is exactly the same. In fact, if I keep `GL_RENDER` mode and remove the call to `gluPickMatrix()` I get exactly the same view I have as in normal render. – Hossomi Dec 25 '14 at 02:49

0 Answers0