I am trying to create a 3D robot that has to perform certain actions when clicking certain parts of the body. I have successfully (sort of) implemented the choice, because if you click on any part of the x-plane, it will register a hit, but not elsewhere. That is, it does not register the depth, and if you click on it with a square head, you can only register a hit by clicking on the front of the head (facing you). Obviously, I donβt quite understand the choice and the choice, and I carelessly try to decipher what I know about 2D sampling in 3D (my teacher is as useful as stone), but I left something or did not change something related to depth. Can someone help me? Here are the related functions.
void processHits (GLint hits, GLuint buffer[])
{
unsigned int i, j;
GLint n, *ptr;
printf ("hits = %d\n", hits);
ptr = (GLint *) buffer;
for (i = 0; i < hits; i++)
{
n = *ptr;
ptr+=3;
printf ("hit %d has %d name(s)\n", i, n);
for (j = 0; j < n; j++)
{
if(*ptr==1) printf ("Body hit.\n");
else if(*ptr==2) printf ("Right shoulder hit.\n");
else if(*ptr==3) printf ("Left shoulder hit.\n");
else if(*ptr==4) printf ("Left arm hit.\n");
else if(*ptr==5) printf ("Right arm hit.\n");
else if(*ptr==6) printf ("Left leg hit.\n");
else if(*ptr==7) printf ("Right leg hit.\n");
else if(*ptr==8) printf ("Right foot hit.\n");
else if(*ptr==9) printf ("Left foot hit.\n");
else if(*ptr==10) printf ("Neck hit.\n");
else if(*ptr==11) printf ("Head hit.\n");
else printf ("Nothing hit.\n");
ptr++;
}
printf ("\n");
}
}
void selection(int mouse_x, int mouse_y)
{
GLuint buffer[512];
GLint hits;
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(512, buffer);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y), 0.01, 0.01, viewport);
gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
drawObjects(GL_SELECT);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
hits = glRenderMode(GL_RENDER);
processHits (hits, buffer);
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
selection(x, y);
}
}