fdFxy.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. unit fdFxy;
  2. interface
  3. uses
  4. System.SysUtils,
  5. System.Classes,
  6. System.Math,
  7. System.Types,
  8. Vcl.Graphics,
  9. Vcl.Controls,
  10. Vcl.Forms,
  11. Vcl.StdCtrls,
  12. Vcl.ComCtrls,
  13. Vcl.ExtCtrls,
  14. Stage.VectorGeometry,
  15. Stage.VectorTypes,
  16. GLS.Objects,
  17. GLS.Graph,
  18. GLS.Color,
  19. GLS.Scene,
  20. GLS.State,
  21. GLS.SceneViewer,
  22. GLS.Coordinates,
  23. GLS.BaseClasses,
  24. GLS.AsyncTimer;
  25. type
  26. TFormFxy = class(TForm)
  27. GLScene1: TGLScene;
  28. GLSceneViewer1: TGLSceneViewer;
  29. GLCamera1: TGLCamera;
  30. GLLightSource1: TGLLightSource;
  31. HeightField: TGLHeightField;
  32. YZGrid: TGLXYZGrid;
  33. XZGrid: TGLXYZGrid;
  34. XYGrid: TGLXYZGrid;
  35. Panel1: TPanel;
  36. TBXYPosition: TTrackBar;
  37. TBYZPosition: TTrackBar;
  38. TBXZPosition: TTrackBar;
  39. Label2: TLabel;
  40. Label3: TLabel;
  41. Label4: TLabel;
  42. GLAsyncTimer1: TGLAsyncTimer;
  43. rgFormula: TRadioGroup;
  44. rgPolygonMode: TRadioGroup;
  45. CBCentered: TCheckBox;
  46. procedure GLSceneViewer1MouseDown(Sender: TObject; Button: TMouseButton;
  47. Shift: TShiftState; X, Y: Integer);
  48. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  49. X, Y: Integer);
  50. procedure CBCenteredClick(Sender: TObject);
  51. procedure TBXYPositionChange(Sender: TObject);
  52. procedure TBYZPositionChange(Sender: TObject);
  53. procedure TBXZPositionChange(Sender: TObject);
  54. procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
  55. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  56. procedure HeightField1GetHeight(const X, Y: Single; var z: Single;
  57. var Color: TVector4f; var TexPoint: TTexPoint);
  58. procedure GLAsyncTimer1Timer(Sender: TObject);
  59. procedure rgFormulaClick(Sender: TObject);
  60. procedure rgPolygonModeClick(Sender: TObject);
  61. public
  62. mx, my: Integer;
  63. procedure Formula0(const x: Single; const y: Single; var z: Single;
  64. var Color: TVector4f; var TexPoint: TTexPoint);
  65. procedure Formula1(const x: Single; const y: Single; var z: Single;
  66. var Color: TVector4f; var TexPoint: TTexPoint);
  67. procedure Formula2(const x: Single; const y: Single; var z: Single;
  68. var Color: TVector4f; var TexPoint: TTexPoint);
  69. procedure Formula3(const x: Single; const y: Single; var z: Single;
  70. var Color: TVector4f; var TexPoint: TTexPoint);
  71. end;
  72. var
  73. FormFxy: TFormFxy;
  74. implementation //=============================================================
  75. {$R *.DFM}
  76. //------------------------- Formula ------------------------------------------
  77. procedure TFormFxy.Formula0(const x, y: Single; var z: Single;
  78. var Color: TVector4f; var TexPoint: TTexPoint);
  79. begin
  80. // 0ro formula
  81. z := VectorNorm(x, y);
  82. z := x * y;
  83. VectorLerp(clrBlue, clrRed, (z + 1) / 2, color);
  84. end;
  85. procedure TFormFxy.Formula1(const x, y: Single; var z: Single;
  86. var Color: TVector4f; var TexPoint: TTexPoint);
  87. begin
  88. // 1st formula
  89. z := VectorNorm(x, y);
  90. z := x * y * z; // or z = (x*x)*(y*y);
  91. VectorLerp(clrBlue, clrRed, (z + 1) / 2, color);
  92. end;
  93. procedure TFormFxy.Formula2(const x, y: Single; var z: Single;
  94. var Color: TVector4f; var TexPoint: TTexPoint);
  95. begin
  96. // 2nd formula
  97. z := VectorNorm(x, y);
  98. z := sin(z * 12) / (2 * (z * 6.28 + 1));
  99. VectorLerp(clrBlue, clrRed, (z + 1) / 2, color);
  100. end;
  101. procedure TFormFxy.Formula3(const x, y: Single; var z: Single;
  102. var Color: TVector4f; var TexPoint: TTexPoint);
  103. begin
  104. // 3rd formula
  105. z := VectorNorm(x, y);
  106. z := (Power(x, 2) + Power(y, 2)) * sin(8 * ArcTan2(x, y));
  107. VectorLerp(clrBlue, clrRed, (z + 1) / 2, color);
  108. end;
  109. //------------------------ HeightField1GetHeight -----------------------------
  110. procedure TFormFxy.HeightField1GetHeight(const X, Y: Single; var z: Single;
  111. var Color: TVector4f; var TexPoint: TTexPoint);
  112. begin
  113. z := VectorNorm(X, Y);
  114. z := cos(z * 12) / (2 * (z * 6.28 + 1));
  115. end;
  116. //----------------------------------------------------------------------------
  117. procedure TFormFxy.CBCenteredClick(Sender: TObject);
  118. begin
  119. if CBCentered.Checked then
  120. begin
  121. XZGrid.YSamplingScale.Origin := 0;
  122. YZGrid.XSamplingScale.Origin := 0;
  123. XYGrid.ZSamplingScale.Origin := 0;
  124. end
  125. else
  126. begin
  127. XZGrid.YSamplingScale.Origin := -1;
  128. YZGrid.XSamplingScale.Origin := -1;
  129. XYGrid.ZSamplingScale.Origin := -1;
  130. end;
  131. end;
  132. procedure TFormFxy.TBXYPositionChange(Sender: TObject);
  133. begin
  134. XYGrid.ZSamplingScale.Origin := -(TBXYPosition.Position / 10);
  135. end;
  136. procedure TFormFxy.TBYZPositionChange(Sender: TObject);
  137. begin
  138. YZGrid.XSamplingScale.Origin := -(TBYZPosition.Position / 10);
  139. end;
  140. procedure TFormFxy.TBXZPositionChange(Sender: TObject);
  141. begin
  142. XZGrid.YSamplingScale.Origin := -(TBXZPosition.Position / 10);
  143. end;
  144. //---------------------------------------------------------------------------
  145. procedure TFormFxy.rgFormulaClick(Sender: TObject);
  146. begin
  147. case (rgFormula.ItemIndex) of
  148. 0:
  149. HeightField.OnGetHeight := Formula0;
  150. 1:
  151. HeightField.OnGetHeight := Formula1;
  152. 2:
  153. HeightField.OnGetHeight := Formula2;
  154. 3:
  155. HeightField.OnGetHeight := Formula3;
  156. else
  157. HeightField.OnGetHeight := Formula0;
  158. end;
  159. end;
  160. //---------------------------- rgPolygonMode ---------------------------------
  161. procedure TFormFxy.rgPolygonModeClick(Sender: TObject);
  162. begin
  163. case (rgPolygonMode.ItemIndex) of
  164. 0:
  165. HeightField.Material.PolygonMode := pmFill;
  166. 1:
  167. HeightField.Material.PolygonMode := pmLines;
  168. 2:
  169. HeightField.Material.PolygonMode := pmPoints;
  170. end;
  171. HeightField.StructureChanged();
  172. end;
  173. //----------------------------------------------------------------------------
  174. // following code takes care of camera movement, see camera & movement demos
  175. // for explanations and more samples
  176. procedure TFormFxy.GLAsyncTimer1Timer(Sender: TObject);
  177. begin
  178. HeightField.StructureChanged;
  179. end;
  180. procedure TFormFxy.GLSceneViewer1MouseDown(Sender: TObject;
  181. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  182. begin
  183. mx := X;
  184. my := Y;
  185. end;
  186. procedure TFormFxy.GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  187. X, Y: Integer);
  188. begin
  189. if Shift <> [] then
  190. begin
  191. GLCamera1.MoveAroundTarget(my - Y, mx - X);
  192. mx := X;
  193. my := Y;
  194. end;
  195. end;
  196. procedure TFormFxy.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  197. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  198. begin
  199. GLCamera1 := GLSceneViewer1.Camera;
  200. // Note that 1 wheel-step induces a WheelDelta of 120,
  201. // this code adjusts the distance to target with a 10% per wheel-step ratio
  202. GLCamera1.AdjustDistanceToTarget(Power(1.1, WheelDelta / 120));
  203. end;
  204. end.