fHFPickC.cpp 3.3 KB

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