2
0

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