Unit1.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLBaseClasses"
  9. #pragma link "GLCoordinates"
  10. #pragma link "GLCrossPlatform"
  11. #pragma link "GLGraph"
  12. #pragma link "GLScene"
  13. #pragma link "GLWin32Viewer"
  14. #pragma resource "*.dfm"
  15. TForm1 *Form1;
  16. TColor grid[11][11];
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19. : TForm(Owner)
  20. {
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TForm1::FormCreate(TObject *Sender)
  24. {
  25. int ix, iy;
  26. // initialize grid color to white/gray (checked pattern)
  27. for (ix=-5; ix < 5; ix++)
  28. for (iy=-5; iy < 5; iy++)
  29. if (((ix ^ iy) && 1)==0)
  30. grid[ix][iy] = clWhite;
  31. else
  32. grid[ix][iy] = clSilver;
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TForm1::HeightFieldGetHeight(const float x, const float y, float &z,
  36. TVector4f &Color, TTexPoint &TexPoint)
  37. {
  38. int ix, iy;
  39. // Nothing fancy here, the color is directly taken from the grid,
  40. // and the z function is a basic cosinus. The '+0.01' are to take
  41. // rounding issues out of the equation.
  42. ix = Int(ClampValue(x+0.01, -5, 5));
  43. iy = Int(ClampValue(y+0.01, -5, 5));
  44. Color = ConvertWinColor(grid[ix][iy]);
  45. z = Cos(VectorLength(x, y)*1.5);
  46. }
  47. //---------------------------------------------------------------------------
  48. void __fastcall TForm1::GLSceneViewerMouseDown(TObject *Sender, TMouseButton Button,
  49. TShiftState Shift, int X, int Y)
  50. {
  51. TAffineVector v;
  52. int ix, iy;
  53. mx = X;
  54. my = Y;
  55. if (RBPaint->Checked)
  56. {
  57. // In Paint mode
  58. // get absolute 3D coordinates of the point below the mouse
  59. v = GLSceneViewer->Buffer->PixelRayToWorld(X, Y);
  60. // convert to heightfield local coordinates
  61. v = HeightField->AbsoluteToLocal(v);
  62. // convert that local coords to grid pos
  63. ix = Int(v.X);
  64. iy = Int(v.Y);
  65. // if we are in the grid...
  66. if ((ix>=-5) && (ix<=5) && (iy>=-5) && (iy<=5))
  67. {
  68. // show last coord in the caption bar
  69. Label2->Caption = Format("%d %d",
  70. ARRAYOFCONST ((ix, iy)));
  71. // and paint blue or red depending on the button
  72. if (Button==TMouseButton(mbLeft))
  73. grid[ix][iy] = clBlue;
  74. else
  75. grid[ix][iy] = clRed;
  76. // Height field changed, rebuild it!
  77. HeightField->StructureChanged();
  78. }
  79. }
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TForm1::GLSceneViewerMouseMove(TObject *Sender, TShiftState Shift,
  83. int X, int Y)
  84. {
  85. if (RBPaint->Checked)
  86. {
  87. // in paint mode, paint if a button is pressed
  88. if (Shift.Contains(ssLeft))
  89. GLSceneViewerMouseDown(Sender, TMouseButton(mbLeft), Shift, X, Y);
  90. else
  91. if (Shift.Contains(ssRight))
  92. GLSceneViewerMouseDown(Sender, TMouseButton(mbRight), Shift, X, Y);
  93. }
  94. else
  95. {
  96. // rotate mode
  97. if (Shift.Contains(ssAlt) || Shift.Contains(ssCtrl) || Shift.Contains(ssShift))
  98. {
  99. GLCamera1->MoveAroundTarget(my-Y, mx-X);
  100. mx = X;
  101. my = Y;
  102. }
  103. }
  104. }
  105. //---------------------------------------------------------------------------