Here is my code, most from red book…Thx.
void render(GLenum mode)
{
glPushMatrix();
glTranslated(0.0, 0.0, (GLdouble) translate);
glPushMatrix();
if (mode == GL_SELECT)
glLoadName(1);
if (selected == 1)
glRotated((GLdouble) rot_angle1, 1.0, 0.0, 0.0);
glCallList(CHAIR01);
glPopMatrix();
glTranslated(100.0 , 0.0, 0.0);
glPushMatrix();
if (mode == GL_SELECT)
glLoadName(2);
if (selected == 2)
glRotated((GLdouble) rot_angle2, 1.0, 0.0, 0.0);
glCallList(TABLE01);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render(GL_RENDER);
glFlush();
}
void proces****s (GLint hits, GLuint buffer[])
{
unsigned int i, j;
GLuint ii, jj, names, *ptr;
printf ("hits = %d
", hits);
if (hits == 0)
selected = 0;
ptr = (GLuint ) buffer;
for (i = 0; i < hits; i++) { / for each hit */
names = *ptr;
printf (" number of names for this hit = %d
“, names);
ptr++;
printf(” z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
printf(" z2 is %g
“, (float) ptr/0x7fffffff); ptr++;
printf (" names are ");
for (j = 0; j < names; j++) { / for each name */
printf (”%d “, *ptr);
selected = *ptr;
ptr++;
}
printf (”
");
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case ‘m’:
if (selected == 1)
translate1 = (translate1 + 10) % 360;
if (selected == 2)
translate2 = (translate2 + 10) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
void mouse(int button, int state, int x, int y)
{
GLuint selectBuf[512];
GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON | | state != GLUT_DOWN)
return;
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(512, selectBuf);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble) x, (GLdouble) viewport[3] - y, 5.0, 5.0, viewport);
gluPerspective(90.0, (GLfloat) 500/ (GLfloat) 500, 10.0, 1500.0);
render(GL_SELECT);
glPopMatrix();
hits = glRenderMode (GL_RENDER);
proces****s (hits, selectBuf);
glFlush();
glutPostRedisplay();
}