Unit1.cpp 5.7 KB

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