I have a few questions as I’m stilling trying to get picking working correctly.
Q1) If the objects I’m drawing on screen are drawn as a mesh (I use gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE)), will I get a hit if I’m doing picking and I select the center of the polygon? (i.e. I’m guessing not cause there’s nothing there as my polygon is not filled in. It’s just a wire frame) I’m supposed to get a hit if I drag
Q2) In my mouseDragged(MouseEvent me) method, I’m setting a select mode so that I can process the Hits in my display() method when I drag my mouse like a sweep selection through the mesh of polygons. Of cours I don’t have this working now, but my problem is that I don’t even see any hits being processed. Hits most of the time turns up as 0. Do I have the code below correct to do this?
Note: I’m not trying to select the entire object as I only have one geometry displayed, rather, I’m trying to select the individual polygon of the mesh of my object which is displayed as a wire frame. So not sure I’m doing the code correct so far to do this.
public void display(GLDrawable drawable) {
GL gl = drawable.getGL();
GLU glu = drawable.getGLU();
this.gldrawable = drawable;
switch (cmd) {
case UPDATE:
...
break;
case SELECT:
int hits = 0;
int buffSize = 512;
int[] viewPort = new int[4];
IntBuffer selectBuffer = BufferUtils.newIntBuffer(buffSize);
double x = (double) prevMouseX;
double y = (double) prevMouseY;
gl.glGetIntegerv(GL.GL_VIEWPORT, viewPort);
gl.glSelectBuffer(buffSize, selectBuffer);
gl.glRenderMode(GL.GL_SELECT);
gl.glInitNames();
gl.glPushName(0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
glu.gluPickMatrix(x, (double) viewPort[3] - y,
7.0d, 7.0d, viewPort);
// glu.gluOrtho2D(0.0d, 1.0d, 0.0d, 1.0d);
// gl.glOrtho(0.0, 400.0, 0.0, 300.0, -1000, 1000);
if (selectedFile) {
model.draw();
}
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();
gl.glFlush();
hits = gl.glRenderMode(GL.GL_RENDER);
processHits(hits, selectBuffer);
cmd = UPDATE;
break;
}
}
public void processHits(int hits, IntBuffer buffer) {
System.out.println("---------------------------------");
System.out.println(" HITS: " + hits);
int offset = 0;
int names;
float z1, z2;
for (int i = 0; i < hits; i++) {
System.out.println("- - - - - - - - - - - -");
System.out.println(" hit: " + (i + 1));
names = buffer.get(offset);
offset++;
z1 = (float) buffer.get(offset) / 0x7fffffff;
offset++;
z2 = (float) buffer.get(offset) / 0x7fffffff;
offset++;
System.out.println(" number of names: " + names);
System.out.println(" z1: " + z1);
System.out.println(" z2: " + z2);
System.out.println(" names: ");
for (int j = 0; j < names; j++) {
System.out.print(" " + buffer.get(offset));
if (j == (names - 1))
System.out.println("<-");
else
System.out.println();
offset++;
}
System.out.println("- - - - - - - - - - - -");
}
}
// mouse down w/ left ctrl means I want to select
// the polygon I'm in and any that I drag across.
public void mouseDragged(MouseEvent me) {
int x = me.getX();
int y = me.getY();
if (SwingUtilities.isLeftMouseButton(me)) {
if (me.isControlDown()) {
cmd = SELECT;
prevMouseX = x;
prevMouseY = y;
}
...
this.gldrawable.display();
}
…