Unit1.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 "GLS.BaseClasses"
  9. #pragma link "GLS.Coordinates"
  10. #pragma link "GLS.GeomObjects"
  11. #pragma link "GLS.Objects"
  12. #pragma link "GLS.Scene"
  13. #pragma link "GLS.VectorFileObjects"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma link "GLS.File3DS"
  16. #pragma resource "*.dfm"
  17. TForm1 *Form1;
  18. //---------------------------------------------------------------------------
  19. __fastcall TForm1::TForm1(TComponent* Owner)
  20. : TForm(Owner)
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TForm1::FormCreate(TObject *Sender)
  25. {
  26. // Load mushroom mesh
  27. SetGLSceneMediaDir();
  28. FreeForm1->LoadFromFile("mushroom.3ds");
  29. }
  30. //---------------------------------------------------------------------------
  31. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  32. TShiftState Shift, int X, int Y)
  33. {
  34. Gls::Vectorgeometry::TVector rayStart, rayVector, iPoint, iNormal;
  35. // retrieve raycasting data:
  36. // rayStart is obtained for camera and screen position
  37. // rayVector is the camera direction (i.e direction to target since our camera is targeted)
  38. // (note that (0, 0) is lower left for the Screen function, whereas Delphi
  39. // uses top-left as origin, hence the Y inversion)
  40. SetVector(rayStart, GLSceneViewer1->Buffer->OrthoScreenToWorld(X, GLSceneViewer1->Height-Y));
  41. SetVector(rayVector, GLCamera1->AbsoluteVectorToTarget());
  42. NormalizeVector(rayVector);
  43. // Here we require RayCast intersection
  44. if (FreeForm1->RayCastIntersect(rayStart, rayVector, &iPoint, &iNormal))
  45. {
  46. // got one, move the sphere there and orient it appropriately
  47. Sphere1->Position->AsVector = iPoint;
  48. Sphere1->Direction->AsVector = VectorNormalize(iNormal);
  49. // make it visible
  50. Sphere1->Visible = True;
  51. }
  52. else
  53. // hide it if we did not hit
  54. Sphere1->Visible = False;
  55. }
  56. //---------------------------------------------------------------------------
  57. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  58. int X, int Y)
  59. {
  60. // when mouse moves, recompute intersection
  61. if (Shift.Contains(ssLeft)||Shift.Contains(ssRight))
  62. GLSceneViewer1MouseDown(Sender, TMouseButton(Vcl::Controls::mbLeft), Shift, X, Y);
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TForm1::GLSceneViewer2MouseDown(TObject *Sender, TMouseButton Button,
  66. TShiftState Shift, int X, int Y)
  67. {
  68. Gls::Vectorgeometry::TVector rayStart, rayVector, iPoint, iNormal;
  69. // retrieve raycasting data:
  70. // rayStart is the eye (camera) position
  71. // rayVector is computed from screen position
  72. // (note that (0, 0) is lower left for the Screen function, whereas Delphi
  73. // uses top-left as origin, hence the Y inversion)
  74. SetVector(rayStart, GLCamera2->AbsolutePosition);
  75. SetVector(rayVector, GLSceneViewer2->Buffer->ScreenToVector(AffineVectorMake(X, GLSceneViewer2->Height-Y, 0)));
  76. NormalizeVector(rayVector);
  77. // Here we request RayCast intersection
  78. if (FreeForm1->RayCastIntersect(rayStart, rayVector, &iPoint, &iNormal))
  79. {
  80. // got one, move the sphere there and orient it appropriately
  81. Sphere1->Position->AsVector = iPoint;
  82. Sphere1->Direction->AsVector = VectorNormalize(iNormal);
  83. // make it visible
  84. Sphere1->Visible = True;
  85. }
  86. else
  87. // hide it if we did not hit
  88. Sphere1->Visible = False;
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TForm1::GLSceneViewer2MouseMove(TObject *Sender, TShiftState Shift,
  92. int X, int Y)
  93. {
  94. // when mouse moves, recompute intersection
  95. if (Shift.Contains(ssLeft)||Shift.Contains(ssRight))
  96. GLSceneViewer2MouseDown(Sender, TMouseButton(Vcl::Controls::mbLeft), Shift, X, Y);
  97. }
  98. //---------------------------------------------------------------------------