fHFPickC.cpp 3.6 KB

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