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