fRayBoxD.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. unit fRayBoxD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. Winapi.Windows,
  6. System.SysUtils,
  7. System.Classes,
  8. System.Math,
  9. System.Types,
  10. Vcl.Graphics,
  11. Vcl.Controls,
  12. Vcl.Forms,
  13. Vcl.Dialogs,
  14. Vcl.ExtCtrls,
  15. Vcl.StdCtrls,
  16. GLS.Scene,
  17. GLS.VectorTypes,
  18. GLS.Objects,
  19. GLS.Cadencer,
  20. GLS.VectorFileObjects,
  21. GLS.SceneViewer,
  22. GLS.Texture,
  23. GLS.VectorGeometry,
  24. GLS.Material,
  25. GLS.Coordinates,
  26. GLS.BaseClasses;
  27. type
  28. TFormRayBox = class(TForm)
  29. Viewer: TGLSceneViewer;
  30. GLScene: TGLScene;
  31. GLCadencer: TGLCadencer;
  32. GLCamera1: TGLCamera;
  33. GLLightSource1: TGLLightSource;
  34. DCCamTarg: TGLDummyCube;
  35. Timer1: TTimer;
  36. GLMaterialLibrary: TGLMaterialLibrary;
  37. GLLightSource2: TGLLightSource;
  38. Panel1: TPanel;
  39. Button1: TButton;
  40. GLLines1: TGLLines;
  41. GLPoints1: TGLPoints;
  42. Label1: TLabel;
  43. CheckBox1: TCheckBox;
  44. GLCube1: TGLCube;
  45. CheckBox2: TCheckBox;
  46. GLDummyCube1: TGLDummyCube;
  47. DCCube1: TGLDummyCube;
  48. LabelFPS: TLabel;
  49. procedure GLCadencerProgress(Sender: TObject; const deltaTime, newTime: Double);
  50. procedure Timer1Timer(Sender: TObject);
  51. procedure FormCreate(Sender: TObject);
  52. procedure ViewerMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  53. procedure FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer;
  54. MousePos: TPoint; var Handled: Boolean);
  55. procedure ViewerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState;
  56. X, Y: Integer);
  57. procedure FormResize(Sender: TObject);
  58. procedure FormKeyPress(Sender: TObject; var Key: Char);
  59. procedure Button1Click(Sender: TObject);
  60. procedure CheckBox1Click(Sender: TObject);
  61. private
  62. mdx, mdy: Integer;
  63. public
  64. end;
  65. var
  66. FormRayBox: TFormRayBox;
  67. BoxPos, BoxScale, RayStart, RayDir: TAffineVector;
  68. implementation
  69. {$R *.DFM}
  70. procedure TFormRayBox.FormCreate(Sender: TObject);
  71. begin
  72. Randomize;
  73. RayStart := AffineVectorMake(Random * 2 - 1, Random * 2 - 1, Random * 2 - 1);
  74. end;
  75. procedure TFormRayBox.Button1Click(Sender: TObject);
  76. var
  77. iPnt, afScale: TAffineVector;
  78. begin
  79. // Change pos.
  80. if CheckBox2.Checked then
  81. begin
  82. BoxPos := AffineVectorMake(Random * 2 - 1, Random * 2 - 1, Random * 2 - 1);
  83. DCCamTarg.Position.SetPoint(BoxPos);
  84. BoxScale := AffineVectorMake(Random * 1 + 0.5, Random * 1 + 0.5, Random * 1 + 0.5);
  85. DCCube1.Scale.SetVector(BoxScale);
  86. afScale := VectorScale(BoxScale, 0.5);
  87. RayStart := AffineVectorMake(Random * 3 - 1.5, Random * 3 - 1.5, Random * 3 - 1.5);
  88. end;
  89. RayDir := AffineVectorMake(Random * 2 - 1, Random * 2 - 1, Random * 2 - 1);
  90. NormalizeVector(RayDir);
  91. GLLines1.Nodes.Clear;
  92. GLLines1.Nodes.AddNode(RayStart);
  93. GLLines1.Nodes.AddNode(VectorAdd(RayStart, VectorScale(RayDir, 8)));
  94. GLPoints1.Positions.Clear;
  95. GLPoints1.Positions.Add(RayStart);
  96. GLPoints1.Positions.Add(BoxPos);
  97. GLPoints1.Positions.Add(VectorSubtract(BoxPos, afScale));
  98. GLPoints1.Positions.Add(VectorAdd(BoxPos, afScale));
  99. if RayCastBoxIntersect(RayStart, RayDir, VectorSubtract(BoxPos, afScale),
  100. VectorAdd(BoxPos, afScale), @iPnt) then
  101. begin
  102. Label1.Caption := Format('Intersect point: %.3f %.3f %.3f', [iPnt.X, iPnt.Y, iPnt.Z]);
  103. GLPoints1.Positions.Add(iPnt);
  104. beep;
  105. end
  106. else
  107. Label1.Caption := 'no intersection';
  108. end;
  109. procedure TFormRayBox.CheckBox1Click(Sender: TObject);
  110. begin
  111. GLCube1.Visible := CheckBox1.Checked;
  112. end;
  113. procedure TFormRayBox.GLCadencerProgress(Sender: TObject; const deltaTime, newTime: Double);
  114. begin
  115. if FormRayBox.Active then
  116. Viewer.Invalidate
  117. end;
  118. procedure TFormRayBox.Timer1Timer(Sender: TObject);
  119. begin
  120. LabelFPS.Caption := Format('%.1f FPS', [Viewer.FramesPerSecond]);
  121. Viewer.ResetPerformanceMonitor;
  122. end;
  123. procedure TFormRayBox.ViewerMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  124. begin
  125. if Shift = [ssLeft] then
  126. GLCamera1.MoveAroundTarget(mdy - Y, mdx - X);
  127. mdx := X;
  128. mdy := Y;
  129. end;
  130. procedure TFormRayBox.FormMouseWheel(Sender: TObject; Shift: TShiftState; WheelDelta: Integer;
  131. MousePos: TPoint; var Handled: Boolean);
  132. begin
  133. GLCamera1.AdjustDistanceToTarget(Power(1.02, WheelDelta / 120));
  134. end;
  135. procedure TFormRayBox.ViewerMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState;
  136. X, Y: Integer);
  137. begin
  138. Viewer.SetFocus;
  139. end;
  140. procedure TFormRayBox.FormResize(Sender: TObject);
  141. begin
  142. GLCamera1.FocalLength := MinInteger(Height, Width) / 10;
  143. end;
  144. procedure TFormRayBox.FormKeyPress(Sender: TObject; var Key: Char);
  145. begin
  146. if Key = #27 then
  147. close;
  148. end;
  149. end.