Picking

Hello,
I try since few days to make the picking work on my program but it doesn’t. The results are weird:

Slot 0: - Value: 1
Slot 1: - Value: -103863766
Slot 2: - Value: -103863766
Slot 3: - Value: 1
Slot 4: - Value: 0
Slot 5: - Value: 0
Slot 6: - Value: 0
Slot 7: - Value: 0
Slot 8: - Value: 0
Slot 9: - Value: 0
Slot 10: - Value: 0
Slot 11: - Value: 0
Slot 12: - Value: 0
Slot 13: - Value: 0
Slot 14: - Value: 0
Slot 15: - Value: 0
Slot 16: - Value: 0
Slot 17: - Value: 0
Slot 18: - Value: 0
Slot 19: - Value: 0
Slot 20: - Value: 0
Slot 21: - Value: 0
Slot 22: - Value: 0
Slot 23: - Value: 0
Buff size: 1
click on x:248,y:236
1 hits:
Number: 1
Min Z: 42
Max Z: 42
Name on stack: 1

though my code is this :

double x=clavier.getEvent().button.x;
double y=480-clavier.getEvent().button.y;

printf("click on x:%g,y:%g
",x,y);

GLuint buff[64] = {0};
GLint hits, viewport[4];
glSelectBuffer(512, buff);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

gluPickMatrix(x, y, 1.0f, 1.0f, viewport);
gluPerspective(70, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);

    glLoadName(1);
//draw a cube



    glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

hits = glRenderMode(GL_RENDER);

list_hits(hits, buff);

gl_selall(hits, buff);

void Jeu::gl_selall(GLint hits, GLuint *buff)
{
GLuint *p;
int i;

terrain.dessinerPick();

p = buff;
for (i = 0; i < 6 * 4; i++)
{
	printf("Slot %d: - Value: %d

", i, p[i]);
}

printf("Buff size: %x

", (GLbyte)buff[0]);
}

void list_hits(GLint hits, GLuint *names)
{
int i;
printf("%d hits:
", hits);

for (i = 0; i < hits; i++)
	printf(	"Number: %d

"
"Min Z: %d
"
"Max Z: %d
"
"Name on stack: %d
",
(GLubyte)names[i * 4],
(GLubyte)names[i * 4 + 1],
(GLubyte)names[i * 4 + 2],
(GLubyte)names[i * 4 + 3]
);

printf("

");
}

Thankyou :slight_smile:

I think the problem comes from the position of the camera. How could I change it when I am picking?

do you guys have any idea?

Why do you think it does not work?
The weird code is this:


  (GLubyte)names[i * 4 + i]

Why are you casting it to byte? It is 32bit integer.

Yes but this part doesn’t actually cause much trouble because the probleme is indeed earlier, when I try the picking. I mean, when I click somewhere on the screen it doesn’t detect the object as well :frowning:

How do you set viewport array? I do not see any assignment.

You’re right but even if I add this :
glGetIntegerv(GL_VIEWPORT,viewport); before gluPickMatrix(x, y, 1.0f, 1.0f, viewport);
It is still the same :frowning:

No idea dude?