hittest / glupickmatrix / selectionbuffer

Hello

  1. I have a scene with a lot of polygons.
  2. I am sucessfully getting a selectionbuffer.
  3. The selection buffer is working correctly if I select all the polygons that are “hit”.
  4. The problem is to pick/select only the nearest polygon.

I am playing with the z1 and z2 values that are in the selectionbuffer but the problem is that sometimes the nearest polygon has the greatest max(z1,z2) and sometimes it is the opposite being the nearest polygon the one with min(z1,z2).

What am i missing here?

I think the problem is in the picking part and not in the processing hits part as i mentioned before.

Const BUFSIZE As Integer = 512

    Dim selectBuf(BUFSIZE) As Integer
    Dim hits As Integer
    Dim viewport(3) As Integer
    Dim Projection(16) As Double

    glGetIntegerv(GL_VIEWPORT, viewport)
    glGetDoublev(GL_PROJECTION_MATRIX, Projection)

    glSelectBuffer(BUFSIZE, selectBuf)
    glRenderMode(GL_SELECT)

    glInitNames()
    glPushName(0)

    glMatrixMode(GL_PROJECTION)

    glPushMatrix()


    glLoadIdentity()
    ' create 5x5 pixel picking region near cursor location

        gluPickMatrix(e.X, viewport(3) - e.Y, 5 , 5, viewport)

    glMultMatrixd(Projection)
    glMatrixMode(GL_MODELVIEW)

'render the scene in gl_select mode:
drawScene(GL_SELECT)

    glPopMatrix()
    glFlush()

    hits = glRenderMode(GL_RENDER)
    processHits(hits, selectBuf)

    glMatrixMode(GL_PROJECTION)
    glPopMatrix() '                         /* Restore projection matrix           */
    glMatrixMode(GL_MODELVIEW) '

Any idea of what is wrong??