fFormula.pas 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. unit fFormula;
  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.ExtCtrls,
  12. Vcl.StdCtrls,
  13. GLS.Scene,
  14. GLS.Objects,
  15. GLS.VectorTypes,
  16. GLS.VectorGeometry,
  17. GLS.Texture,
  18. GLS.Cadencer,
  19. GLS.Mesh,
  20. GLS.SceneViewer,
  21. GLS.State,
  22. GLS.Color,
  23. GLS.Coordinates,
  24. GLS.BaseClasses;
  25. type
  26. TFormFormula = class(TForm)
  27. GLSceneViewer1: TGLSceneViewer;
  28. GLScene1: TGLScene;
  29. Mesh1: TGLMesh;
  30. DummyCube1: TGLDummyCube;
  31. GLCamera1: TGLCamera;
  32. GLLightSource1: TGLLightSource;
  33. Timer1: TTimer;
  34. GLSceneViewer2: TGLSceneViewer;
  35. Panel1: TPanel;
  36. Label1: TLabel;
  37. GLScene2: TGLScene;
  38. DummyCube2: TGLDummyCube;
  39. Mesh2: TGLMesh;
  40. GLLightSource2: TGLLightSource;
  41. GLCamera2: TGLCamera;
  42. Label2: TLabel;
  43. procedure FormCreate(Sender: TObject);
  44. procedure Timer1Timer(Sender: TObject);
  45. procedure GLSceneViewer1MouseDown(Sender: TObject;
  46. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  47. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  48. X, Y: Integer);
  49. private
  50. mx, my : Integer;
  51. invRes1, invRes2 : Single;
  52. function MakeVect(const aX, aY : Single) : TAffineVector;
  53. procedure AddTriangle(const p1, p2, p3 : TAffineVector;
  54. const color : TColorVector);
  55. public
  56. end;
  57. var
  58. FormFormula: TFormFormula;
  59. implementation
  60. {$R *.DFM}
  61. const
  62. // half-grid resolution, grid width is actually cResolution*2 of "quads"
  63. cResolution = 50;
  64. function TFormFormula.MakeVect(const aX, aY : Single) : TAffineVector;
  65. begin
  66. SetVector(Result, aX*invRes1, sin((aX*aX+aY*aY)*invRes2), aY*invRes1);
  67. end;
  68. procedure TFormFormula.AddTriangle(const p1, p2, p3 : TAffineVector;
  69. const color : TColorVector);
  70. begin
  71. with Mesh1.Vertices do begin
  72. AddVertex(p1, NullVector, color);
  73. AddVertex(p2, NullVector, color);
  74. AddVertex(p3, NullVector, color);
  75. end;
  76. end;
  77. procedure TFormFormula.FormCreate(Sender: TObject);
  78. var
  79. x, y : Integer;
  80. pTopLeft, pTopRight, pBottomRight, pBottomLeft : TAffineVector;
  81. begin
  82. // scaling precalcs for our math func
  83. invRes1:=10/cResolution;
  84. invRes2:=0.1*Sqr(invRes1);
  85. //
  86. // Triangles
  87. //
  88. // this one is basic : we calculate the corner points for each grid quad and
  89. // add the two triangles that make it
  90. with Mesh1 do begin
  91. Mode:=mmTriangles;
  92. Vertices.Clear;
  93. for y:=-cResolution to cResolution do begin
  94. for x:=-cResolution to cResolution do begin
  95. pTopLeft:=MakeVect(x, y+1);
  96. pTopRight:=MakeVect(x+1, y+1);
  97. pBottomRight:=MakeVect(x+1, y);
  98. pBottomLeft:=MakeVect(x, y);
  99. // top left triangle
  100. AddTriangle(pBottomLeft, pTopLeft, pTopRight, clrBlue);
  101. // bottom right triangle
  102. AddTriangle(pTopRight, pBottomRight, pBottomLeft, clrBlue);
  103. end;
  104. end;
  105. CalcNormals(fwCounterClockWise);
  106. // Vertices.Locked:=True;
  107. end;
  108. //
  109. // TriangleStrip
  110. //
  111. // Same as triangle, however trianglestrips are continuous, and to cover
  112. // the grid, "null" segments are used at both ends of a strip (to avoid a
  113. // visible triangle that would stretch for the full width of the grid).
  114. // Note : this can be avoided by reversing grid traversing direction (one line
  115. // from left to right, one from right to left, etc.)
  116. with Mesh2 do begin
  117. Mode:=mmTriangleStrip;
  118. Vertices.Clear;
  119. for y:=-cResolution to cResolution do begin
  120. pTopLeft:=MakeVect(-cResolution, y+1);
  121. Vertices.AddVertex(pTopLeft, NullVector, clrBlue);
  122. Vertices.AddVertex(pTopLeft, NullVector, clrBlue);
  123. for x:=-cResolution to cResolution do begin
  124. pTopRight:=MakeVect(x+1, y+1);
  125. pBottomLeft:=MakeVect(x, y);
  126. with Vertices do begin
  127. AddVertex(pBottomLeft, NullVector, clrBlue);
  128. AddVertex(pTopRight, NullVector, clrBlue);
  129. end;
  130. end;
  131. pBottomRight:=MakeVect(cResolution+1, y);
  132. Vertices.AddVertex(pBottomRight, NullVector, clrBlue);
  133. Vertices.AddVertex(pBottomRight, NullVector, clrBlue);
  134. end;
  135. CalcNormals(fwClockWise);
  136. // Vertices.Locked:=True;
  137. end;
  138. end;
  139. procedure TFormFormula.Timer1Timer(Sender: TObject);
  140. begin
  141. // nb of triangles in scene
  142. Caption:= 'Formula ' + Format('%d Triangles', [2*(cResolution*2)*(cResolution*2)]);
  143. // calculate & display triangles framerate
  144. with GLSceneViewer1 do begin
  145. // we render twice to get a fair FPS rating
  146. ResetPerformanceMonitor;
  147. Buffer.Render;
  148. Buffer.Render;
  149. Label1.Caption:=Format('%.2f FPS (mmTriangles)', [FramesPerSecond]);
  150. end;
  151. // calculate & display trianglestrip framerate
  152. with GLSceneViewer2 do begin
  153. // we render twice to get a fair FPS rating
  154. ResetPerformanceMonitor;
  155. Buffer.Render;
  156. Buffer.Render;
  157. Label2.Caption:=Format('%.2f FPS (mmTriangleStrip)', [FramesPerSecond]);
  158. end;
  159. end;
  160. procedure TFormFormula.GLSceneViewer1MouseDown(Sender: TObject;
  161. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  162. begin
  163. mx:=X; my:=Y;
  164. end;
  165. procedure TFormFormula.GLSceneViewer1MouseMove(Sender: TObject;
  166. Shift: TShiftState; X, Y: Integer);
  167. begin
  168. if ssLeft in Shift then begin
  169. TGLSceneViewer(Sender).Camera.MoveAroundTarget(my-Y, mx-X);
  170. my:=Y; mx:=X;
  171. end;
  172. end;
  173. end.