Hi all,

Im having a little trouble with GL Picking.. it works fine when there are 2 or more objects to select, how ever if i click in a space where there is no objects it always select the last object created.. heres a screenshot:



I can select the left and the right faces without a problem, but if for example i click in the very bottom right cornour, the left face is always selected.

below is the code im using in the OnClick event of my mouse class:

Code:
procedure GLOnClick(const X,Y: Integer; const Button: TXGUIMouseButton);
var
  selectBuff : Array Of Cardinal;
  vp: TVector4i;
  hits,SelectedName: Cardinal;
  Obj: TXObject;
begin
  SetLength(selectBuff, _XObjects.Count);

  glGetIntegerv(GL_VIEWPORT, @vp);  
  glSelectBuffer(_XObjects.Count, @selectBuff);

  glMatrixMode(GL_PROJECTION);
  glPushMatrix;
    glRenderMode(GL_SELECT);
    _XEngine.Selection := True;

    glInitNames;
    glPushName(0);
    glLoadIdentity;

    gluPickMatrix(X, Y, 1, 1, vp);
    gluPerspective(45.0, vp[2]/vp[3], 0.1, 100.0);  
    glMatrixMode(GL_MODELVIEW);

    _XEngine.DoRender;

    hits := glRenderMode(GL_RENDER);

    Obj := Nil;
    If (hits > 0) Then
    Begin
      SelectedName := selectBuff[3];
      Obj := _XO_Find(SelectedName);
    End;
    _XEngine.Selected := Obj;

    glMatrixMode(GL_PROJECTION);
  glPopMatrix;
  _XEngine.Selection := False;
  glMatrixMode(GL_MODELVIEW);
end;
and in the objects rendering code i have:
Code:
  If Not Visible Then Exit;
  If _XEngine.Selection Then glLoadName(fID);
  glPushMatrix;
    PreRender(Options);
    DoRender(Options);
    PostRender(Options);
  glPopMatrix;
.. fID is a unique ID.

I have double checked it with several tutorials and examples but i cant seem to find anything i have missed :?

Can anyone see if i have missed something, or is this something with SDL/OpenGL?