fPickD.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. unit fPickD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.Classes,
  6. Vcl.Controls,
  7. Vcl.Dialogs,
  8. Vcl.Forms,
  9. GLScene.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. GLSceneViewer1: 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 GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  32. X, Y: Integer);
  33. procedure GLSceneViewer1MouseDown(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. procedure TForm1.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  69. X, Y: Integer);
  70. var
  71. pickedObject: TGLCustomSceneObject;
  72. begin
  73. // find what's under the mouse
  74. pickedObject := (GLSceneViewer1.Buffer.GetPickedObject(X, Y) as TGLCustomSceneObject);
  75. // if it has changed since last MouseMove...
  76. if (pickedObject <> oldPick) then
  77. begin
  78. // ...turn to black previous "hot" object...
  79. if Assigned(oldPick) then
  80. oldPick.Material.FrontProperties.Emission.Color := clrBlack;
  81. // ...and heat up the new selection...
  82. if Assigned(pickedObject) then
  83. pickedObject.Material.FrontProperties.Emission.Color := clrRed;
  84. // ...and don't forget it !
  85. oldPick := pickedObject;
  86. end;
  87. end;
  88. procedure TForm1.GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  89. Shift: TShiftState; X, Y: Integer);
  90. var
  91. pickedObject: TGLCustomSceneObject;
  92. begin
  93. // if an object is picked...
  94. pickedObject := (GLSceneViewer1.Buffer.GetPickedObject(X, Y) as TGLCustomSceneObject);
  95. if Assigned(pickedObject) then
  96. begin
  97. // ...turn it to yellow and show its name
  98. pickedObject.Material.FrontProperties.Emission.Color := clrYellow;
  99. if (pickedObject is TGLPoints) then
  100. begin
  101. Points.Colors.Clear;
  102. Points.Colors.Add(1,1,0,1);
  103. end;
  104. ShowMessage('You clicked the ' + pickedObject.Name);
  105. end;
  106. end;
  107. end.