我一直在使用 GLSceneViewer1.Buffer.GetPickedObject( x, y ) 根据挑选演示在 GLViewerMouseDown 事件中挑选 GLscene 对象。我需要选择一个对象,更改颜色,单击鼠标左键并再次单击鼠标左键取消选择,如果选择了另一个对象,则取消选择它。似乎 TGLSceneObject 需要一个属性 IsPicked : boolean 才能实现这一点。如果有人知道在不修改 GLScene 的情况下这样做会很酷。这是我编写的代码,它有点工作,但有点不行。SetSelected(Selected, SelectedColor) 只是改变选定对象的颜色。
procedure TForm32.GLSceneViewer1MouseDown( Sender : TObject; Button : TMouseButton; Shift : TShiftState; X, Y : Integer );
var
Selected : TGLSceneObject;
AButton : TGLMouseButton;
begin
AButton := TGLMouseButton( Button );
// if an object is picked...
Selected := ( GLSceneViewer1.Buffer.GetPickedObject( x, y ) as TGLSceneObject );
case AButton of
mbLeft:
begin
if( Selected <> UnSelected ) then
begin
if( Assigned( Selected ) ) then
begin
SetSelected( Selected, SelectedColor );
StatusBar1.Panels[0].Text := 'Selected';
UnSelected := Selected;
end
else
if( not Assigned( Selected ) ) then
begin
UnSelected.Material.FrontProperties.Emission.Color:= clrBlack;
UnSelected.Material.FrontProperties.Ambient.Color := clrGray20;
UnSelected.Material.FrontProperties.Diffuse.Color := clrGray80;
StatusBar1.Panels[0].Text := 'Unselected';
UnSelected := Selected;
end;
end;
end;
end;
end;
对我来说,这会更容易:
procedure TForm32.GLSceneViewer1MouseDown( Sender : TObject; Button : TMouseButton; Shift : TShiftState; X, Y : Integer );
var
Selected : TGLSceneObject;
begin
Selected := ( GLSceneViewer1.Buffer.GetPickedObject( x, y ) as TGLSceneObject );
if( not Selected.IsPicked ) then
SetSelected( Selected, SelectedColor )
else
SetSelected( Selected, UnSelectedColor );
end;