Hi,
I’m trying to use picking to tell me when a certain object in my window is clicked. I’ve tried using the RedBook code but as soon as I click the screen blanks and no hits are ever registered. I think it’s to do with me using gluLookAt() in my display routene - if someone can point out where I’m going wrong then that’d be great.
Here’s a simplification of my code:
void drawScene(GLenum mode) {
if (mode != GL_SELECT) {
glLoadIdentity();
}
gluLookAt( -5.0, 5.0, 0.0, // Eye pos.
0.0, 0.0, 0.0, // Centre pos.
0.0, 1.0, 0.0 ); // Up Vector
glPushMatrix();
if (mode == GL_SELECT)
glPushName(1);
glCallList(doorList);
if (mode == GL_SELECT)
glPopName();
glTranslatef(0.0, 0.0, -3.0);
glCallList(doorList);
glPopMatrix();
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
drawScene(GL_RENDER);
glFlush();
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON | | state != GLUT_DOWN)
return;
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf);
glRenderMode (GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
/* create 5x5 pixel picking region near cursor location */
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
5.0, 5.0, viewport);
gluPerspective( 90, 1.0, 6.0, 80.0);
drawScene (GL_SELECT);
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glFlush ();
hits = glRenderMode (GL_RENDER);
// Will do the processing here, but for now just print number of hits to
// find out if it's working
printf("no of hits %d
", hits);
glutPostRedisplay();
}
void myinit(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
/* Allocate display lists */
doorList = glGenLists (1);
/* Define normals so we have correct lighting */
makeNormals( doorVerts, doorFaces, doorNoFaces, doorNorms );
/* Construct the display lists */
buildDisplayLists();
}
void main(int argc, char **argv) {
GLenum light[] = { GL_LIGHT0, GL_LIGHT1 };
GLfloat lightAmbient[][4] = {
{ 0.3, 0.3, 0.3, 1.0 },
{ 0.1, 0.3, 0.5, 1.0 }
};
GLfloat lightDiffuse[][4] = {
{ 0.9, 0.9, 0.9, 1.0 },
{ 0.0, 0.0, 0.0, 1.0 }
};
GLfloat lightSpecular[][4] = {
{ 0.0, 0.0, 0.1, 1.0 },
{ 1.0, 1.0, 1.0, 1.0 }
};
GLfloat lightPosition[][4] = {
{ -10.0, 5.0, 6.0, 0.0 },
{ 12.0, 4.0, 0.0, 0.0 }
};
int l;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("OpenGL");
myinit();
glutMouseFunc (mouse);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutKeyboardFunc( keyboard );
glEnable( GL_LIGHTING );
for (l = 0; l < 2; l++) {
glLightfv( light[l], GL_POSITION, lightPosition[l] );
glLightfv( light[l], GL_AMBIENT, lightAmbient[l] );
glLightfv( light[l], GL_DIFFUSE, lightDiffuse[l] );
glLightfv( light[l], GL_SPECULAR, lightSpecular[l] );
glEnable( light[l] );
}
glEnable( GL_DEPTH_TEST );
glShadeModel( GL_SMOOTH );
glEnable( GL_NORMALIZE );
glutMainLoop();
}