Problem with selecction

Hi Im new on the world of opengl, jogl

I did some objects. now I want to interact with them. I have this when a click happens

  {
            context.makeCurrent();
	int buff[]=new int[4 * CubeScene.maxSel];
	BufferSeleccion= ByteBuffer.allocateDirect(5 * CubeScene.maxSel).order(ByteOrder.nativeOrder()).asIntBuffer(); 
	IntBuffer viewport = IntBuffer.allocate(4);
	int[] viewp=new int[4];
	GL gl=this.context.getGL();
	gl.glSelectBuffer((4 * CubeScene.maxSel ),BufferSeleccion);
	gl.glGetIntegerv(GL.GL_VIEWPORT, viewp,IntBuffer.wrap(viewp).arrayOffset());
	gl.glMatrixMode(GL.GL_PROJECTION);
	gl.glPushMatrix();	
	gl.glLoadIdentity();
	glu.gluPickMatrix(e.x, viewp[3]-e.y, 5, 5, IntBuffer.wrap(viewp));
	System.err.println("COORDENADAS: "+e.x+" "+e.y);
	//glu.gluPerspective(50.0f, (float)rect.right/(float)rect.bottom, 0.1f, 50.0f);
	gl.glMatrixMode(GL.GL_MODELVIEW);
	gl.glPushMatrix();
    	CubeScene.drawAll(gl,2);  // this function is the one who draws
    }

here a part of the function who draws

void drawAll(GL gl, int render)
{
if(render==1)
{

		int NumSel= gl.glRenderMode(GL.GL_RENDER);
	
	// NumSel really has the valor 1 when the click happens 
		if(NumSel==-1) NumSel=maxSel;
		
		if(NumSel > 0) 
		{
	
			int z1;
			seleccionado=Scenegrip.BufferSeleccion.get(3);
			...

// it is supossed to have 3 elemets in my buffer each one should use 4 spaces. and the third is supposed to have the id of the object I selected
// but it throws in debug mode, java.lang.IndexOutOfBoundsException.

else //2
{
System.err.println(“MODO SELECCION”);
gl.glRenderMode(GL.GL_SELECT);

	///////////////// SOLO FUNCA SI TA EN MODO SELECCION////
	// Inicializa, limpia, la pila de nombres
	// de seleccion.
	gl.glInitNames();	
	// Guarda un primer numero en la pila de
	// nombres.
	gl.glPushName(0);	
	
	GLU glu = new GLU();
	id=1;

// here start drawing

	gl.glLoadName(id);
	float color2[]={1f,0f,0f};
	gl.glColor3fv(color,0); 
	
	gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
	glu.gluSphere(MosFet.QUADRIC, RADIUS,2,CubeScene.stacks()*3);
	gl.glTranslatef(g/4,0.0f,0.0f);

	gl.glTranslatef(g/4,0.0f,0.0f);

	gl.glTranslatef(g/4,0.0f,0.0f);

	gl.glTranslatef(g/4,0.0f,0.0f);

	gl.glTranslatef(g/4,0.0f,0.0f);

	gl.glTranslatef(g/4,0.0f,0.0f);
	//gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
	glu.gluSphere(MosFet.QUADRIC, RADIUS,2,CubeScene.stacks());
               id++;

[quote] seleccionado=Scenegrip.BufferSeleccion.get(3);

// it is supossed to have 3 elemets in my buffer each one should use 4 spaces. and the third is supposed to have the id of the object I selected
[/quote]
I got a bit lost with your code I’m affraid. It’s a good idea to use the Java coding standards, ie variable names start with lower case letter, class names uppercase. :slight_smile:

Anyway, If you are accessing the third item you need to remeber that arrays in Java are zero based. So you probably want to get the integer at location 2 in your buffer array thingy. (is it an int buffer view onto a bytbuffer? I wasn’t sure)

Thanks for answering i took the example from a c++ code and the arrays are Zero based too, and the second location of the array throws a number that is not the id a gave my objects( 1,2,3…) sometimes it returns -11242114 or something like that (garbage maybe in c but java initialize with 0 the values).

the function gl.glSelectBuffer() ask me for an IntBuffer direct, and the only way to create one direct I found was with the code:
bufferSeleccion= ByteBuffer.allocateDirect(5 * CubeScene.maxSel).order(ByteOrder.nativeOrder()).asIntBuffer();

the first method is on my class Scenegrip that handles the events with the mouse.

I corrected the name of bufferSeleccion, i little mistake the B instead of b
public class Scenegrip extends MouseAdapter implements MouseMoveListener, Listener, KeyListener
{
private float xrot;
private float yrot;
private float zoff;
private float yoff;
private float xoff;
private float xcpy;
private float ycpy;
private boolean move;
private int xdown;
private int ydown;
private int mouseDown;
GLU glu;
static IntBuffer bufferSeleccion;
private GLContext context;
public void mouseDoubleClick(MouseEvent e)
{
context.makeCurrent();
int buff[]=new int[4 * CubeScene.maxSel];
bufferSeleccion= ByteBuffer.allocateDirect(5 * CubeScene.maxSel).order(ByteOrder.nativeOrder()).asIntBuffer();
IntBuffer viewport = IntBuffer.allocate(4);
int[] viewp=new int[4];
GL gl=this.context.getGL();
gl.glSelectBuffer((4 * ActualScene.maxSel ),bufferSeleccion);
gl.glGetIntegerv(GL.GL_VIEWPORT, viewp,IntBuffer.wrap(viewp).arrayOffset());
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
glu.gluPickMatrix(e.x, viewp[3]-e.y, 5, 5, IntBuffer.wrap(viewp));
System.err.println("COORDENADAS: “+e.x+” "+e.y);
//glu.gluPerspective(50.0f, (float)rect.right/(float)rect.bottom, 0.1f, 50.0f);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
ActualScene.drawAll(gl,2); // this function is the one who draws
}


}

the other function is on another class that is the who draws

public class ActualScene extends GLScene
{

void drawAll(GL gl, int render)
{
if(render==1)
{

     int NumSel= gl.glRenderMode(GL.GL_RENDER);
  
  // NumSel really has the valor 1 when the click happens
     if(NumSel==-1) NumSel=maxSel;
     
     if(NumSel > 0)
     {
  
        int z1;
        seleccionado=Scenegrip.bufferSeleccion.get(3);
        ...
   // it is supossed to have 3 elemets in my buffer each one should use 4 spaces. and the third is supposed to have the id of the object I 
  // selected but it throws in debug  mode, java.lang.IndexOutOfBoundsException.


  else //2
  {
     System.err.println("MODO SELECCION");
     gl.glRenderMode(GL.GL_SELECT);

  
  ///////////////// SOLO FUNCA SI TA EN MODO SELECCION////
  // Inicializa, limpia, la pila de nombres
  // de seleccion.
  gl.glInitNames();   
  // Guarda un primer numero en la pila de
  // nombres.
  gl.glPushName(0);   
  
  GLU glu = new GLU();
  id=1;
  
  // here start drawing

         
  gl.glLoadName(id);
  float color2[]={1f,0f,0f};
  gl.glColor3fv(color,0);
  
  gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
  glu.gluSphere(MosFet.QUADRIC, RADIUS,2,ActualScene.stacks()*3);
  gl.glTranslatef(g/4,0.0f,0.0f);

  gl.glTranslatef(g/4,0.0f,0.0f);

  gl.glTranslatef(g/4,0.0f,0.0f);

  gl.glTranslatef(g/4,0.0f,0.0f);

  gl.glTranslatef(g/4,0.0f,0.0f);

  gl.glTranslatef(g/4,0.0f,0.0f);
  //gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
  glu.gluSphere(MosFet.QUADRIC, RADIUS,2,ActualScene.stacks());
               id++;
 ...
 }

}

hmm, have you got a matching gl.glPopName(); somewhere in there?

I was just looking at my code, (long time since i did that stuff). I have something like the following:

   public SelectionRecord(final IntBuffer selectBuffer) {
        if(DEBUG_SELECTION) Utils.logDebug("------");
        nameCount = selectBuffer.get();
        if(DEBUG_SELECTION) Utils.logDebug("nameCount=" + nameCount);
        minZ = selectBuffer.get();
        if(DEBUG_SELECTION) Utils.logDebug("minZ=" + minZ);
        maxZ = selectBuffer.get();
        if(DEBUG_SELECTION) Utils.logDebug("maxZ=" + maxZ);
...

which gives something similar to what you have when I click background.

[Debug]------
[Debug]nameCount=0
[Debug]minZ=-109747201
[Debug]maxZ=-109743361

But when I have clicked a named object

[Debug]------
[Debug]nameCount=1
[Debug]minZ=-109429249
[Debug]maxZ=-109425665
[Debug]selectedItem=145

Maybe you want to run your code through a debugger/println and see how many name records you are actually getting?

without a matching PopName you will get all your objects hit. PS try switching to color coded selection which is faster and not deprecated