Unit1.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.Cadencer"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLS.Material"
  12. #pragma link "GLS.Objects"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma resource "*.dfm"
  16. TForm1 *Form1;
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19. : TForm(Owner)
  20. {
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TForm1::FormCreate(TObject *Sender)
  24. {
  25. Randomize;
  26. RayStart = AffineVectorMake(Random()*2 -1, Random()*2 -1, Random()*2 -1);
  27. }
  28. //---------------------------------------------------------------------------
  29. void __fastcall TForm1::Button1Click(TObject *Sender)
  30. {
  31. TAffineVector iPnt, afScale ;
  32. // Change pos.
  33. if (CheckBox2->Checked)
  34. {
  35. BoxPos = AffineVectorMake(Random()*2 -1, Random()*2 -1, Random()*2 -1);
  36. DCCamTarg->Position->SetPoint(BoxPos);
  37. BoxScale = AffineVectorMake(Random()*1 +0.5, Random()*1 +0.5, Random()*1 +0.5);
  38. DCCube1->Scale->SetVector(BoxScale);
  39. afScale = VectorScale(BoxScale, 0.5);
  40. RayStart = AffineVectorMake(Random()*3 -1.5, Random() *3 -1.5, Random() *3 -1.5);
  41. }
  42. RayDir = AffineVectorMake(Random()*2 -1, Random() *2 -1, Random()*2 -1);
  43. NormalizeVector(RayDir);
  44. GLLines1->Nodes->Clear();
  45. GLLines1->Nodes->AddNode(RayStart);
  46. GLLines1->Nodes->AddNode(VectorAdd(RayStart, VectorScale(RayDir, 8)) );
  47. GLPoints1->Positions->Clear();
  48. GLPoints1->Positions->Add(RayStart);
  49. GLPoints1->Positions->Add(BoxPos);
  50. GLPoints1->Positions->Add(VectorSubtract(BoxPos, afScale));
  51. GLPoints1->Positions->Add(VectorAdd(BoxPos, afScale));
  52. if (RayCastBoxIntersect(RayStart, RayDir, VectorSubtract(
  53. BoxPos, afScale), VectorAdd(BoxPos, afScale), &iPnt))
  54. {
  55. Label1->Caption = Format("Intersect point: %.3f %.3f %.3f",
  56. ARRAYOFCONST ((iPnt.X, iPnt.Y, iPnt.Z)));
  57. GLPoints1->Positions->Add(iPnt);
  58. Beep();
  59. }
  60. else
  61. Label1->Caption = "no intersection";
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TForm1::CheckBox1Click(TObject *Sender)
  65. {
  66. GLCube1->Visible = CheckBox1->Checked;
  67. }
  68. //---------------------------------------------------------------------------
  69. void __fastcall TForm1::GLCadencerProgress(TObject *Sender, const double deltaTime,
  70. const double newTime)
  71. {
  72. if (Form1->Active)
  73. Viewer->Invalidate();
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  77. {
  78. LabelFPS->Caption = Format("%.1f FPS",
  79. ARRAYOFCONST ((Viewer->FramesPerSecond())));
  80. Viewer->ResetPerformanceMonitor();
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TForm1::ViewerMouseMove(TObject *Sender, TShiftState Shift, int X,
  84. int Y)
  85. {
  86. if (Shift.Contains(ssLeft))
  87. GLCamera1->MoveAroundTarget(mdy -Y, mdx - X);
  88. mdx = X;
  89. mdy = Y;
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  93. TPoint &MousePos, bool &Handled)
  94. {
  95. GLCamera1->AdjustDistanceToTarget(Power(1.02, WheelDelta/120));
  96. }
  97. //---------------------------------------------------------------------------
  98. void __fastcall TForm1::ViewerMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
  99. int X, int Y)
  100. {
  101. Viewer->SetFocus();
  102. }
  103. //---------------------------------------------------------------------------
  104. void __fastcall TForm1::FormResize(TObject *Sender)
  105. {
  106. GLCamera1->FocalLength = MinInteger(Height, Width) / 10;
  107. }
  108. //---------------------------------------------------------------------------
  109. void __fastcall TForm1::FormKeyPress(TObject *Sender, System::WideChar &Key)
  110. {
  111. if (Key = 0x27)
  112. Close();
  113. }
  114. //---------------------------------------------------------------------------