PDA

View Full Version : Quick Selection Question



Richard145
04-24-2007, 11:56 AM
Please Help,

I am new to the OpenGL Selection and have a quick question that I hope someone can help me with. I get the selection to work fine, i.e., I can record "hits" when switching from GL_SELECT to GL_RENDER but the NAME is not stored.

The output I get (when I select the Object):
"--------------------------
HITS: 1
- - - - - - - - - - - - - -
Number of Names: 0
z1: -0.09484
z2: -0.3213
names:
- - - - - - - - - - - - - - -
------------------------------"

The following is the code from which the previous console output comes:

private 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++;
}// End of INNER FOR-LOOP
System.out.println("- - - - - - - - - - - -");
}// End of OUTER FOR-LOOP
System.out.println("--------------------------------- ");
}// End of processHits(int,IntBuffer)The naming of the Object is done in the following code:

int[] viewport = new int[4];
int hits = 0;

// allocate fast memory. Necessary since glSelectBuffer takes IntBuffer
//
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4*BUFFERSIZE);
byteBuffer.order(ByteOrder.nativeOrder());
this.selectionBuffer = byteBuffer.asIntBuffer();

// Get the current Viewport for this Class
//
this.gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);

// the hits will be stored in the selection buffer after.
//
// Note that it's called *before* we change the render mode
//
this.gl.glSelectBuffer(selectionBuffer.capacity(), this.selectionBuffer);

// Set the GL Rendering Mode to SELECT
//
this.gl.glRenderMode(GL.GL_SELECT);

// clear name stack
//
this.gl.glInitNames();

// Switch to the Projection Matrix
//
this.gl.glMatrixMode(GL.GL_PROJECTION);

// save current projection matrix
//
this.gl.glPushMatrix();

// reset the projection matrix
//
this.gl.glLoadIdentity();

// restrict drawing to around the cursor
//
this.glu.gluPickMatrix(this.mouseX, viewport[3] - this.mouseY, 5.0d, 5.0d, viewport, 0);
this.glu.gluPerspective(45, this.ratio, 1, 200);
this.gl.glMatrixMode(GL.GL_MODELVIEW);
if (mode == GL.GL_SELECT) {
gl.glPushName(20);
}
// Call draw() that is using geometric primatives to render Object
draw();

this.gl.glMatrixMode(GL.GL_PROJECTION);
this.gl.glPopMatrix();
hits = gl.glRenderMode(GL.GL_RENDER);
this.mode = GL.GL_RENDER;
processHits(hits, selectionBuffer);Thanks be to anyone that has any idea why this is happening.

Ido_Ilan
04-24-2007, 10:33 PM
Hi,

Where is the glPopName?

Ido

Richard145
04-25-2007, 04:08 AM
Oops. I think I got the call mixed up with glLoadName()?

Richard145
04-25-2007, 04:31 AM
Still no luck. I tried using both glLoadName(GLUint) and (when that didn't work) I instead did the glPushName(GLUint) (draw Object with primatives) followed by glPopName().

Perhaps it is my printing out function?

Richard145
04-25-2007, 06:15 AM
Never mind. I found it. I was not setting GL_SELECT when drawing the Model at all so the name stack was never loaded.

The result of a long day of coding with no breaks.