fFormulaC.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fFormulaC.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLScene.BaseClasses"
  8. #pragma link "GLScene.Coordinates"
  9. #pragma link "GLS.Mesh"
  10. #pragma link "GLS.Objects"
  11. #pragma link "GLS.Scene"
  12. #pragma link "GLS.SceneViewer"
  13. #pragma resource "*.dfm"
  14. TForm1* Form1;
  15. const int
  16. // half-grid resolution, grid width is actually cResolution*2 of "quads"
  17. cResolution = 50;
  18. //---------------------------------------------------------------------------
  19. __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {}
  20. //---------------------------------------------------------------------------
  21. TAffineVector __fastcall TForm1::MakeVect(const float aX, const float aY)
  22. {
  23. TAffineVector Result;
  24. SetVector(
  25. Result, aX * invRes1, sin((aX * aX + aY * aY) * invRes2), aY * invRes1);
  26. return Result;
  27. }
  28. //---------------------------------------------------------------------------
  29. void __fastcall TForm1::AddTriangle(const TAffineVector p1,
  30. const TAffineVector p2, const TAffineVector p3, const TGColorVector color)
  31. {
  32. Mesh1->Vertices->AddVertex(p1, NullVector, color);
  33. Mesh1->Vertices->AddVertex(p2, NullVector, color);
  34. Mesh1->Vertices->AddVertex(p3, NullVector, color);
  35. }
  36. //---------------------------------------------------------------------------
  37. void __fastcall TForm1::FormCreate(TObject* Sender)
  38. {
  39. int x, y;
  40. TAffineVector pTopLeft;
  41. TAffineVector pTopRight;
  42. TAffineVector pBottomRight;
  43. TAffineVector pBottomLeft;
  44. // scaling precalcs for our math func
  45. invRes1 = (float)10 / cResolution;
  46. invRes2 = 0.1 * sqrt(invRes1);
  47. //
  48. // Triangles
  49. //
  50. // this one is basic : we calculate the corner points for each grid quad and
  51. // add the two triangles that make it
  52. Mesh1->Mode = mmTriangles;
  53. Mesh1->Vertices->Clear();
  54. for (y = -cResolution; y < cResolution; y++)
  55. for (x = -cResolution; x < cResolution; x++) {
  56. pTopLeft = MakeVect(x, y + 1);
  57. pTopRight = MakeVect(x + 1, y + 1);
  58. pBottomRight = MakeVect(x + 1, y);
  59. pBottomLeft = MakeVect(x, y);
  60. // top left triangle
  61. AddTriangle(pBottomLeft, pTopLeft, pTopRight, clrBlue);
  62. // bottom right triangle
  63. AddTriangle(pTopRight, pBottomRight, pBottomLeft, clrBlue);
  64. }
  65. Mesh1->CalcNormals(fwCounterClockWise);
  66. // Vertices->Locked = True;
  67. //
  68. // TriangleStrip
  69. //
  70. // Same as triangle, however trianglestrips are continuous, and to cover
  71. // the grid, "null" segments are used at both ends of a strip (to avoid a
  72. // visible triangle that would stretch for the full width of the grid).
  73. // Note : this can be avoided by reversing grid traversing direction (one line
  74. // from left to right, one from right to left, etc.)
  75. Mesh2->Mode = mmTriangleStrip;
  76. Mesh2->Vertices->Clear();
  77. for (y = -cResolution; y < cResolution; y++) {
  78. pTopLeft = MakeVect(-cResolution, y + 1);
  79. Mesh2->Vertices->AddVertex(pTopLeft, NullVector, clrBlue);
  80. Mesh2->Vertices->AddVertex(pTopLeft, NullVector, clrBlue);
  81. for (x = -cResolution; x < cResolution; x++) {
  82. pTopRight = MakeVect(x + 1, y + 1);
  83. pBottomLeft = MakeVect(x, y);
  84. Mesh2->Vertices->AddVertex(pBottomLeft, NullVector, clrBlue);
  85. Mesh2->Vertices->AddVertex(pTopRight, NullVector, clrBlue);
  86. }
  87. pBottomRight = MakeVect(cResolution + 1, y);
  88. Mesh2->Vertices->AddVertex(pBottomRight, NullVector, clrBlue);
  89. Mesh2->Vertices->AddVertex(pBottomRight, NullVector, clrBlue);
  90. Mesh2->CalcNormals(fwClockWise);
  91. // Vertices->Locked = True;
  92. }
  93. }
  94. //---------------------------------------------------------------------------
  95. void __fastcall TForm1::Timer1Timer(TObject* Sender)
  96. {
  97. // nb of triangles in scene
  98. Caption = "Formula " +
  99. Format("%d Triangles",
  100. ARRAYOFCONST((2 * (cResolution * 2) * (cResolution * 2))));
  101. // calculate & display triangles framerate
  102. // we render twice to get a fair FPS rating
  103. GLSceneViewer1->ResetPerformanceMonitor();
  104. GLSceneViewer1->Buffer->Render();
  105. GLSceneViewer1->Buffer->Render();
  106. Label1->Caption = Format("%.2f FPS (mmTriangles)",
  107. ARRAYOFCONST((GLSceneViewer1->FramesPerSecond())));
  108. // calculate & display trianglestrip framerate
  109. // we render twice to get a fair FPS rating
  110. GLSceneViewer2->ResetPerformanceMonitor();
  111. GLSceneViewer2->Buffer->Render();
  112. GLSceneViewer2->Buffer->Render();
  113. Label2->Caption = Format("%.2f FPS (mmTriangleStrip)",
  114. ARRAYOFCONST((GLSceneViewer2->FramesPerSecond())));
  115. }
  116. //---------------------------------------------------------------------------
  117. void __fastcall TForm1::GLSceneViewer1MouseDown(
  118. TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
  119. {
  120. mx = X;
  121. my = Y;
  122. }
  123. //---------------------------------------------------------------------------
  124. void __fastcall TForm1::GLSceneViewer1MouseMove(
  125. TObject* Sender, TShiftState Shift, int X, int Y)
  126. {
  127. if (Shift.Contains(ssLeft)) {
  128. GLSceneViewer1->Camera->MoveAroundTarget(my - Y, mx - X);
  129. my = Y;
  130. mx = X;
  131. }
  132. }
  133. //---------------------------------------------------------------------------
  134. void __fastcall TForm1::GLSceneViewer2MouseDown(
  135. TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
  136. {
  137. mx = X;
  138. my = Y;
  139. }
  140. //---------------------------------------------------------------------------
  141. void __fastcall TForm1::GLSceneViewer2MouseMove(
  142. TObject* Sender, TShiftState Shift, int X, int Y)
  143. {
  144. if (Shift.Contains(ssLeft)) {
  145. GLSceneViewer2->Camera->MoveAroundTarget(my - Y, mx - X);
  146. my = Y;
  147. mx = X;
  148. }
  149. }
  150. //---------------------------------------------------------------------------