2
0

fdPick.pas 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. unit fdPick;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.Classes,
  6. Vcl.Controls,
  7. Vcl.Dialogs,
  8. Vcl.Forms,
  9. Stage.VectorTypes,
  10. GLS.Scene,
  11. GLS.Objects,
  12. GLS.Texture,
  13. GLS.SceneViewer,
  14. GLS.GeomObjects,
  15. GLS.Color,
  16. GLS.Coordinates,
  17. GLS.BaseClasses,
  18. GLS.SimpleNavigation;
  19. type
  20. TForm1 = class(TForm)
  21. GLScene1: TGLScene;
  22. glsViewer: TGLSceneViewer;
  23. GLCamera1: TGLCamera;
  24. GLDummyCube1: TGLDummyCube;
  25. GLLightSource1: TGLLightSource;
  26. Sphere: TGLSphere;
  27. Cylinder: TGLCylinder;
  28. Torus: TGLTorus;
  29. Cone: TGLCone;
  30. Points: TGLPoints;
  31. procedure glsViewerMouseMove(Sender: TObject; Shift: TShiftState;
  32. X, Y: Integer);
  33. procedure glsViewerMouseDown(Sender: TObject; Button: TMouseButton;
  34. Shift: TShiftState; X, Y: Integer);
  35. procedure FormCreate(Sender: TObject);
  36. private
  37. Color: TVector3f;
  38. oldPick: TGLCustomSceneObject;
  39. public
  40. end;
  41. var
  42. Form1: TForm1;
  43. implementation //==============================================================
  44. {$R *.DFM}
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. var
  47. I: Integer;
  48. NumPoints: Integer;
  49. X, Y, Z: Single;
  50. begin
  51. NumPoints := 10000;
  52. Points.Size := 5.0;
  53. Points.Style := psSmooth;
  54. for I := 0 to NumPoints - 1 do
  55. begin
  56. // add positions of Points
  57. X := Random(20) - 10;
  58. Y := Random(20) - 10;
  59. Z := Random(20) - 10;
  60. Points.Positions.Add(X * 0.05, Y * 0.05, Z * 0.05);
  61. // add colors of Points
  62. Color.X := Random();
  63. Color.Y := Random();
  64. Color.Z := Random();
  65. Points.Colors.AddPoint(Color);
  66. end;
  67. end;
  68. //----------------------------------------------------------------------------
  69. procedure TForm1.glsViewerMouseMove(Sender: TObject; Shift: TShiftState;
  70. X, Y: Integer);
  71. var
  72. pickedObject: TGLCustomSceneObject;
  73. begin
  74. // find what's under the mouse
  75. pickedObject := (glsViewer.Buffer.GetPickedObject(X, Y) as TGLCustomSceneObject);
  76. // if it has changed since last MouseMove...
  77. if (pickedObject <> oldPick) then
  78. begin
  79. // ...turn to black previous "hot" object...
  80. if Assigned(oldPick) then
  81. oldPick.Material.FrontProperties.Emission.Color := clrBlack;
  82. // ...and heat up the new selection...
  83. if Assigned(pickedObject) then
  84. begin
  85. pickedObject.Material.FrontProperties.Emission.Color := clrRed;
  86. if (pickedObject is TGLPoints) then
  87. begin
  88. Points.Colors.Clear;
  89. Points.Colors.Add(1,0,0,1); // red
  90. end;
  91. end;
  92. // ...and don't forget it !
  93. oldPick := pickedObject;
  94. end;
  95. end;
  96. //----------------------------------------------------------------------------
  97. procedure TForm1.glsViewerMouseDown(Sender: TObject; Button: TMouseButton;
  98. Shift: TShiftState; X, Y: Integer);
  99. var
  100. pickedObject: TGLCustomSceneObject;
  101. begin
  102. // if an object is picked...
  103. pickedObject := (glsViewer.Buffer.GetPickedObject(X, Y) as TGLCustomSceneObject);
  104. if Assigned(pickedObject) then
  105. begin
  106. // ...turn it to yellow and show its name
  107. pickedObject.Material.FrontProperties.Emission.Color := clrYellow;
  108. if (pickedObject is TGLPoints) then
  109. begin
  110. Points.Colors.Clear;
  111. Points.Colors.Add(1,1,0,1); // yellow
  112. end;
  113. ShowMessage('You clicked the ' + pickedObject.Name);
  114. end;
  115. end;
  116. end.