fdHFPick.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. unit fdHFPick;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.SysUtils,
  6. System.Classes,
  7. Vcl.Graphics,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.Dialogs,
  11. Vcl.StdCtrls,
  12. Vcl.ExtCtrls,
  13. GLS.Scene,
  14. GLS.Graph,
  15. GLS.SceneViewer,
  16. Stage.VectorGeometry,
  17. Stage.VectorTypes,
  18. GLS.Texture,
  19. GLS.Objects,
  20. GLS.Color,
  21. GLS.Coordinates,
  22. GLS.BaseClasses;
  23. type
  24. TFormHFPick = class(TForm)
  25. GLScene1: TGLScene;
  26. glViewer: TGLSceneViewer;
  27. GLCamera1: TGLCamera;
  28. HeightField: TGLHeightField;
  29. GLLightSource1: TGLLightSource;
  30. Panel1: TPanel;
  31. RBPaint: TRadioButton;
  32. RBRotate: TRadioButton;
  33. Label1: TLabel;
  34. Label2: TLabel;
  35. procedure glViewerMouseDown(Sender: TObject; Button: TMouseButton;
  36. Shift: TShiftState; x, y: Integer);
  37. procedure glViewerMouseMove(Sender: TObject; Shift: TShiftState;
  38. x, y: Integer);
  39. procedure FormCreate(Sender: TObject);
  40. procedure HeightFieldGetHeight2(Sender: TObject; const x, y: Single;
  41. var z: Single; var Color: TGLColorVector; var TexPoint: TTexPoint);
  42. private
  43. public
  44. mx, my: Integer;
  45. grid: array [-5 .. 5, -5 .. 5] of TColor;
  46. end;
  47. var
  48. FormHFPick: TFormHFPick;
  49. implementation //============================================================
  50. {$R *.dfm}
  51. //----------------------------------------------------------------------------
  52. procedure TFormHFPick.FormCreate(Sender: TObject);
  53. var
  54. ix, iy: Integer;
  55. begin
  56. // initialize grid color to white/gray (checked pattern)
  57. for ix := -5 to 5 do
  58. for iy := -5 to 5 do
  59. if ((ix xor iy) and 1) = 0 then
  60. grid[ix, iy] := clWhite
  61. else
  62. grid[ix, iy] := clSilver;
  63. end;
  64. //----------------------------------------------------------------------------
  65. procedure TFormHFPick.HeightFieldGetHeight2(Sender: TObject; const x, y: Single;
  66. var z: Single; var Color: TGLColorVector; var TexPoint: TTexPoint);
  67. var
  68. ix, iy: Integer;
  69. begin
  70. // Nothing fancy here, the color is directly taken from the grid,
  71. // and the z function is a basic cosinus. The '+0.01' are to take
  72. // rounding issues out of the equation.
  73. ix := Round(ClampValue(x + 0.01, -5, 5));
  74. iy := Round(ClampValue(y + 0.01, -5, 5));
  75. color := ConvertWinColor(grid[ix, iy]);
  76. z := Cos(VectorLength(x, y) * 1.5);
  77. end;
  78. //----------------------------------------------------------------------------
  79. procedure TFormHFPick.glViewerMouseDown(Sender: TObject; Button: TMouseButton;
  80. Shift: TShiftState; x, y: Integer);
  81. var
  82. v: TAffineVector;
  83. ix, iy: Integer;
  84. begin
  85. mx := x;
  86. my := y;
  87. if RBPaint.Checked then
  88. begin
  89. // In Paint mode
  90. // get absolute 3D coordinates of the point below the mouse
  91. v := glViewer.Buffer.PixelRayToWorld(x, y);
  92. // convert to heightfield local coordinates
  93. v := HeightField.AbsoluteToLocal(v);
  94. // convert that local coords to grid pos
  95. ix := Round(v.x);
  96. iy := Round(v.y);
  97. // if we are in the grid...
  98. if (ix >= -5) and (ix <= 5) and (iy >= -5) and (iy <= 5) then
  99. begin
  100. // show last coord in the caption bar
  101. Label2.Caption := Format('%d %d', [ix, iy]);
  102. // and paint blue or red depending on the button
  103. if Button = TMouseButton(mbLeft) then
  104. grid[ix, iy] := clBlue
  105. else
  106. grid[ix, iy] := clRed;
  107. // Height field changed, rebuild it!
  108. HeightField.StructureChanged;
  109. end;
  110. end;
  111. end;
  112. //----------------------------------------------------------------------------
  113. procedure TFormHFPick.glViewerMouseMove(Sender: TObject; Shift: TShiftState;
  114. x, y: Integer);
  115. begin
  116. if RBPaint.Checked then
  117. begin
  118. // in paint mode, paint if a button is pressed
  119. if ssLeft in Shift then
  120. glViewerMouseDown(Sender, TMouseButton(mbLeft), Shift, x, y)
  121. else if ssRight in Shift then
  122. glViewerMouseDown(Sender, TMouseButton(mbRight), Shift, x, y);
  123. end
  124. else
  125. begin
  126. // rotate mode
  127. if Shift <> [] then
  128. begin
  129. GLCamera1.MoveAroundTarget(my - y, mx - x);
  130. mx := x;
  131. my := y;
  132. end;
  133. end;
  134. end;
  135. end.