fFormulaC.cpp 5.6 KB

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