fсHeightfield.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <System.Math.hpp>
  4. #pragma hdrstop
  5. #include "fñHeightfield.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.BaseClasses"
  9. #pragma link "GLS.Cadencer"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLS.Graph"
  12. #pragma link "GLS.Objects"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma resource "*.dfm"
  16. TFormHeightField* FormHeightField;
  17. //---------------------------------------------------------------------------
  18. __fastcall TFormHeightField::TFormHeightField(TComponent* Owner) : TForm(Owner)
  19. {
  20. }
  21. //---------------------------------------------------------------------------
  22. void __fastcall TFormHeightField::FormCreate(TObject* Sender)
  23. {
  24. // start with first formula
  25. HeightField1->OnGetHeight = Formula1;
  26. // no per-vertex coloring
  27. ComboBox1->ItemIndex = 1;
  28. ComboBox1Change(Sender);
  29. }
  30. //---------------------------------------------------------------------------
  31. void __fastcall TFormHeightField::Formula1(const float x, const float y,
  32. float &z, TVector4f &color, TTexPoint &texPoint)
  33. { // first formula
  34. z = VectorNorm(x, y);
  35. z = cos(z * 12) / (2 * (z * 6.28 + 1));
  36. VectorLerp(clrBlue, clrRed, (z + 1) / 2, color);
  37. }
  38. //---------------------------------------------------------------------------
  39. void __fastcall TFormHeightField::Formula2(const float x, const float y,
  40. float &z, TVector4f &color, TTexPoint &texPoint)
  41. {
  42. // 2nd formula
  43. z = (x * x) * (y * y);
  44. VectorLerp(clrBlue, clrRed, (z + 1) / 2, color);
  45. }
  46. //---------------------------------------------------------------------------
  47. void __fastcall TFormHeightField::Formula3(const float x, const float y,
  48. float &z, TVector4f &color, TTexPoint &texPoint)
  49. {
  50. // 3rd formula, dynamic
  51. z = 1 /
  52. (1 + VectorNorm(Sphere1->Position->X - x, Sphere1->Position->Y - y));
  53. if (((ceil(x * 4) + ceil(y * 4)) && 1) == 1)
  54. color = clrBlue;
  55. else
  56. color = clrYellow;
  57. }
  58. //---------------------------------------------------------------------------
  59. void __fastcall TFormHeightField::Sphere1Progress(
  60. TObject* Sender, const double deltaTime, const double newTime)
  61. {
  62. // move our little sphere around
  63. if (Sphere1->Visible) {
  64. Sphere1->Position->SetPoint(cos(newTime * 2.3), sin(newTime), 1.5);
  65. HeightField1->StructureChanged();
  66. }
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TFormHeightField::ComboBox1Change(TObject* Sender)
  70. {
  71. // change per vertex color mode
  72. switch (ComboBox1->ItemIndex) {
  73. case 0:
  74. HeightField1->ColorMode = hfcmNone;
  75. break;
  76. case 1:
  77. HeightField1->ColorMode = hfcmEmission;
  78. break;
  79. case 2:
  80. HeightField1->ColorMode = hfcmDiffuse;
  81. break;
  82. default:;
  83. }
  84. }
  85. //---------------------------------------------------------------------------
  86. void __fastcall TFormHeightField::CheckBox1Click(TObject* Sender)
  87. {
  88. // enable two sided surface
  89. if (CheckBox1->Checked)
  90. HeightField1->Options = HeightField1->Options << hfoTextureCoordinates,
  91. hfoTwoSided;
  92. else
  93. HeightField1->Options = HeightField1->Options << hfoTextureCoordinates;
  94. }
  95. //---------------------------------------------------------------------------
  96. void __fastcall TFormHeightField::TrackBar1Change(TObject* Sender)
  97. {
  98. // adjust X extents
  99. HeightField1->XSamplingScale->Min = -TrackBar1->Position / 10;
  100. HeightField1->XSamplingScale->Max = TrackBar1->Position / 10;
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TFormHeightField::TrackBar2Change(TObject* Sender)
  104. {
  105. // adjust Y extents
  106. HeightField1->YSamplingScale->Min = -TrackBar2->Position / 10;
  107. HeightField1->YSamplingScale->Max = TrackBar2->Position / 10;
  108. }
  109. //---------------------------------------------------------------------------
  110. void __fastcall TFormHeightField::TrackBar3Change(TObject* Sender)
  111. {
  112. // adjust grid steps (resolution)
  113. HeightField1->XSamplingScale->Step = (float)TrackBar3->Position / 1000;
  114. HeightField1->YSamplingScale->Step = (float)TrackBar3->Position / 1000;
  115. }
  116. //---------------------------------------------------------------------------
  117. void __fastcall TFormHeightField::GLSceneViewerMouseMove(
  118. TObject* Sender, TShiftState Shift, int X, int Y)
  119. {
  120. if (Shift.Contains(ssLeft))
  121. GLCamera1->MoveAroundTarget(my - Y, mx - X);
  122. mx = X;
  123. my = Y;
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TFormHeightField::GLSceneViewerMouseDown(
  127. TObject* Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
  128. {
  129. mx = X;
  130. my = Y;
  131. }
  132. //---------------------------------------------------------------------------
  133. void __fastcall TFormHeightField::RadioGroup1Click(TObject* Sender)
  134. {
  135. Sphere1->Visible = false;
  136. // switch between formulas
  137. switch (RadioGroup1->ItemIndex) {
  138. case 0:
  139. HeightField1->OnGetHeight = Formula1;
  140. break;
  141. case 1:
  142. HeightField1->OnGetHeight = Formula2;
  143. break;
  144. case 2: {
  145. HeightField1->OnGetHeight = Formula3;
  146. Sphere1->Visible = true;
  147. break;
  148. };
  149. default:;
  150. }
  151. }
  152. //---------------------------------------------------------------------------
  153. void __fastcall TFormHeightField::CheckBox2Click(TObject* Sender)
  154. {
  155. GLLightSource1->Shining = CheckBox2->Checked;
  156. }
  157. //---------------------------------------------------------------------------
  158. void __fastcall TFormHeightField::Timer1Timer(TObject* Sender)
  159. {
  160. // Display number of triangles used in the mesh
  161. // You will note that this number quickly gets out of hand if you are
  162. // using large high-resolution grids
  163. /// LabelFPS->Caption = Format("%d Triangles - %.2f FPS",
  164. /// ARRAYOFCONST((HeightField1->TriangleCount, GLSceneViewer1->FramesPerSecond())));
  165. LabelFPS->Caption = GLSceneViewer->FramesPerSecond();
  166. GLSceneViewer->ResetPerformanceMonitor();
  167. }
  168. //---------------------------------------------------------------------------
  169. void __fastcall TFormHeightField::FormMouseWheel(TObject* Sender,
  170. TShiftState Shift, int WheelDelta, TPoint &MousePos, bool &Handled)
  171. {
  172. GLCamera1->AdjustDistanceToTarget(Power(1.1, (WheelDelta / 120.0)));
  173. }
  174. //---------------------------------------------------------------------------