Picking problems

Hi guys, I’m implementing a picking simple program…
I’ve got two solid cubes (with glutSolidCube). When I pick the cube0 works fine, when I pick the cube1 works fine, but when I pick around the scene (where is nothing), the selection tell me that I pick the cube1, what does happend?.. Anybody has an idea???..

later
shark_pilot

if not picking cube0 or cube1 then picking nothing.

that must you say to the (stupid) computer.

ps: please don’t talk with your pc, yust coding ;.

Originally posted by shark_pilot:
[b]Hi guys, I’m implementing a picking simple program…
I’ve got two solid cubes (with glutSolidCube). When I pick the cube0 works fine, when I pick the cube1 works fine, but when I pick around the scene (where is nothing), the selection tell me that I pick the cube1, what does happend?.. Anybody has an idea???..

later
shark_pilot[/b]

have you pushed a NULL name to your selection stack before selection-rendering?

e.g: glPushName( 0 );
when doing so, none of your object should have the id ‘0’…

This what I did in the code:

if(hits>0)
{
m_selection=selectBuff[3];
if(m_selection>=0)
{
switch(m_selection)
{
case CUBE0:
AfxMessageBox(“Ha seleccionado el cube 0”);
break;
case CUBE1:
AfxMessageBox(“Ha seleccionado el cube 1”);
break;
default:
break;
}



This is my drawobjects…

void COpenGLViewAlex: rawObjects()
{
glColor3ub(200,150,50);
glInitNames();
glPushName(0);

	if (TypePolygon == 1) 
		{ 
			glPushMatrix();				
			glTranslatef(0,0,-50);
			glRotatef(70,1,1,1);
			glutSolidCube(xp); 
			glLoadName(1);
			glPushName(1);
			glPopName();
			glPopMatrix();	
			
			glPushMatrix();
			glTranslatef(30,0,-50);
			glRotatef(20,0,0,1);
			glutSolidCube(10);
			glLoadName(2);
			glPushName(2);
			glPopName();
			glPopMatrix();

		}
	if (TypePolygon == 2) 
		glutSolidCube(xp);

}

Is this the correct way to draw objects with stack names for picking selection??..

Originally posted by shark_pilot:
[b]This is my drawobjects…

void COpenGLViewAlex: rawObjects()
{
glColor3ub(200,150,50);
glInitNames();
glPushName(0);

  if (TypePolygon == 1) 
  	{ 
  		glPushMatrix();				
  		glTranslatef(0,0,-50);
  		glRotatef(70,1,1,1);
  		glutSolidCube(xp); 
  		glLoadName(1);
  		glPushName(1);
  		glPopName();
  		glPopMatrix();	
  		
  		glPushMatrix();
  		glTranslatef(30,0,-50);
  		glRotatef(20,0,0,1);
  		glutSolidCube(10);
  		glLoadName(2);
  		glPushName(2);
  		glPopName();
  		glPopMatrix();
  	}
  if (TypePolygon == 2) 
  	glutSolidCube(xp);

}

Is this the correct way to draw objects with stack names for picking selection??..[/b]

don’t pop your names!

and call pushName( ) before your glutSolidCube( ), so that it also receives a name

I did what you said, but it still doesn’t work…

  1. Use glPushName() only once (immediately after initialising the name stack with glInitNames()). Remove all the calls to glPopName().

  2. Make your calls to glLoadName() before you draw the object, not after. Otherwise your names are going to come out wrong.

You may need to set selectBuff[3] to 0. In other words, you do:
case CUBE1:
// do stuff
selectBuff[3] = 0;
break;
otherwise, selectBuff[3] will still have the last entrance down.

Guys, I did all the things that you’ve said but still doesn’t work…
I don’t understand…

I think it might be better to set selectBuff[3] to 0 at the start of your picking code before any picking is done.

Originally posted by Unregistered:
You may need to set selectBuff[3] to 0. In other words, you do:
case CUBE1:
// do stuff
selectBuff[3] = 0;
break;
otherwise, selectBuff[3] will still have the last entrance down.

Post all your code called directly or indirectly when you do a mouse click.

This is my drawobjects…

void COpenGLViewAlex: rawObjects()
{
glColor3ub(200,150,50);
glInitNames();
glPushName(0);

	if (TypePolygon == 1) 
		{ 
			glPushMatrix();				
			glTranslatef(0,0,-50);
			glRotatef(70,1,1,1);
                            glLoadName(1); //name defined BEFORE drawing the object.
			glutSolidCube(xp); 
							glPopMatrix();	
			
			glPushMatrix();
			glTranslatef(30,0,-50);
			glRotatef(20,0,0,1);

glloadname(2); //name defined BEFORE drawing the object.
glutSolidCube(10);
glPopMatrix();

		}
	if (TypePolygon == 2) 
                    glloadname(3); //which name have to be this if typepolygon==2?
		glutSolidCube(xp);

}

The initname and pushname(0) have to be defined just once before start drawing somthing.

Also note that when opengl give you the objects picked, MORE THAT ONE OBJECT COULD BE RETURNED, so if hit=3 mean that there are 3 object selected whit your last click.

This is becouse opengl will give all objects draw behind the object that you clicked.

tp.