2
0

fHeightfieldC.cpp 5.8 KB

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