GLScene - Select an object

disclaimer

Sometimes an object has to be selected for subsequent processing. There are several ways :

selecting with the mouse

provided the object is visible and the contours are simple, picking with the mouse is a fast way.
 attach a mousedown event to the SceneViewer

 have a global 

 SelPtr:TGLSceneObject;  // holds a selected object
 SelPtr:=nil;		 // init

 procedure GLSceneViewer1Mousedown(..x,y:integer)
 var pick:TGLSceneObject;
 begin
  pick:=(GLSceneViewer1.GetPickedObject(x,y) as TGLSceneObject);
  if (pick<>SelPtr) then begin
   if assigned(SelPtr) then   { ..unselect(SelPtr).. }
   if assigned(pick) then     { .. select(pick).. }
    SelPtr:=pick;
  end;
 end;

{ ..unselect(APtr).. } may be APtr.material.frontproperties.emission.color:=clrBlack;
{  ..select(APtr)..  } may be APtr.material.frontproperties.emission.color:=clrYellow;

selecting with a combobox

Another way is a combobox. It lets object being selected that are not necessarily visible at the moment.
 The combobox must somehow be populated. I'll use a buttom here for now.
 
 Combobox.name:='SelObjects';
 
 SelObjects.Clear;
 for i:=0 to GLScene1.Objects.Count-1 do begin
  SelObjects.Items.Add(GLScene1.Object[i].name);     // get the name
  SelObjects.Items.Objects[i]:=GLScene1.Objects[i]; // get the pointer
 end;

selecting with the OnChangeEvent :

 procedure TForm1.SelObjectsChange(..)
 begin
  if assigned(SelPtr) then { ..unselect(SelPtr).. }
  if (SelObjects.Itemindex>=0) then begin
   SelPtr:=TGLSceneObject(SelObjects.items.objects[SelObjects.Itemindex]);
   { ..select(SelPtr).. }
  end;
 end;

selecting with a combobox and a mouse

when the two are combined, the mousedown has to be enhanced to reflect a selection in the combobox.
 procedure GLSceneViewer1Mousedown(..x,y:integer)
 var pick:TGLSceneObject;
 j:integer;
 begin
  pick:=(GLSceneViewer1.GetPickedObject(x, y) as TGLSceneObject);
  if (pick<>SelPtr) then begin
   if assigned(SelPtr) then   { ..unselect(SelPtr).. }
   if assigned(pick) then begin
     { .. select(pick)..}
     j:=SelObjects.Items.IndexOfObject(pick);
     SelObjects.itemindex:=j;
    end
   else SelObjects.Itemindex:=-1;
   SelPtr:=pick;
  end;
 end;


OpenGL
home

last updated: 12.aug.00


Copyright (99,2000) Ing.Büro R.Tschaggelar