GL_SELECT not working


My following code (in Windows XP) is to determine which objects are being drawn near the cursor. But I always get a return of zero hit. Can you spot any error? I disable GL_CULL_FACE since the documentation says “With polygons, no hit occurs if the polygon is culled”. Could somebody help me out? Thanks for any advise.


const int selectBuffSize = 1000;
GLuint selectBuff[selectBuffSize];
glSelectBuffer (selectBuffSize, selectBuff);

glMatrixMode (GL_PROJECTION);

double dDiameter = 50.;
int viewport[4];
double projM[16];
glGetDoublev(GL_PROJECTION_MATRIX, projM);
glGetIntegerv (GL_VIEWPORT, viewport);

// point stores the double click location
gluPickMatrix ((GLdouble)(point.x), (GLdouble)(viewport[3]-point.y),
dDiameter, dDiameter, viewport);

// Enter the Select mode.
glRenderMode (GL_SELECT);

glInitNames ();
glPushName (0);

for (…) // loop through aGraphObjects

GLint nNumHits = glRenderMode(GL_RENDER);

if (nNumHits != 0)
return (void*)selectBuff[3];
return NULL;

Looks ok on first sight.
Same debug recommendation as everytime when selection doesn’t seem to work: Debug visually. Do not enable glRenderMode(GL_SELECT) and look at what you really draw. Make the selection area bigger (well, 50 pixels is already pretty big for a precise selection) to see the surrounding objects.
While you draw the objects, does the code assume the GL_MODELVIEW matrix is active and changes it?
Is the viewport the whole window? (Then point.x is ok.)
Unrelated: You should use float for the get and mult matrix. They are floats inside most/all OpenGL implementations.

Hi Relic,

Thanks for your reply. Can you be more specific about your question “While you draw the objects, does the code assume the GL_MODELVIEW matrix is active and changes it”? I think that may be the problem but I don’t fully understand what you meant. I am only a beginner. Thanks again for your advise.


You know how to single step in a debugger?
In the call aGraphObjects[i]->Draw() is there any matrix manipulation done like glTranslate, glRotate glScale, glLoadMatrix, glMultMatrix, or glLoadIdentity?
If yes, was there a glMatrixMode(GL_MODELVIEW) before?
If not, the matrix manipulations are applied to the current matrix mode which you set to GL_PROJECTION in the posted code. The objects will not occur where you wanted them.
If you looked at what you render in the picking path, then it’s obvious if there is something drawn or not.