Unit1.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.Graph"
  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. BoxScale = XYZVector;
  27. SphereRadius = 1;
  28. BoxMatrix = IdentityHmgMatrix;
  29. }
  30. //---------------------------------------------------------------------------
  31. void __fastcall TForm1::CheckBox04Click(TObject *Sender)
  32. {
  33. GLCube1->Visible = CheckBox04->Checked;
  34. GLLines1->Visible = CheckBox05->Checked;
  35. GLXYZGrid1->Visible = CheckBox06->Checked;
  36. GLSphere1->Visible = CheckBox07->Checked;
  37. }
  38. //---------------------------------------------------------------------------
  39. // Generates random rotation for matrix. It remains a scale.
  40. Gls::Vectorgeometry::TMatrix RandomRotation(Gls::Vectorgeometry::TMatrix const &aMatrix)
  41. {
  42. TAffineVector aScale;
  43. Gls::Vectorgeometry::TMatrix mat;
  44. int I;
  45. // Save scale.
  46. for (I = 0; I < 2; I++)
  47. aScale.V[I] = VectorLength(aMatrix.V[I]);
  48. mat.W = aMatrix.W;
  49. // Generate two not equal random vectors.
  50. while (VectorNorm(VectorSubtract(mat.X, mat.Y)) < 10e-6)
  51. {
  52. while (VectorNorm(mat.X) < 10e-6)
  53. mat.X = VectorMake(Random() * 2 - 1, Random() * 2 - 1, Random() * 2 - 1);
  54. while (VectorNorm(mat.Y) < 10e-6)
  55. mat.Y = VectorMake(Random() * 2 - 1, Random() * 2 - 1, Random() * 2 - 1);
  56. }
  57. // Calculate two perpendicular vectors.
  58. mat.Z = VectorCrossProduct(mat.X, mat.Y);
  59. mat.Y = VectorCrossProduct(mat.X, mat.Z);
  60. // Restore scale.
  61. for (I = 0; I < 2; I++)
  62. {
  63. NormalizeVector(mat.V[I]);
  64. ScaleVector(mat.V[I], aScale.V[I]);
  65. }
  66. return mat;
  67. }
  68. void __fastcall TForm1::Button4Click(TObject *Sender)
  69. {
  70. BoxMatrix = RandomRotation(BoxMatrix);
  71. Edit1Change(Sender);
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TForm1::Edit1Change(TObject *Sender)
  75. {
  76. const float EditorsScale = 0.1;
  77. bool Res1;
  78. if (!Form1->Visible)
  79. exit;
  80. GLLines3->Nodes->Clear();
  81. // Calc data.
  82. BoxMatrix.W.X = UpDown1->Position * EditorsScale;
  83. BoxMatrix.W.Y = UpDown2->Position * EditorsScale;
  84. BoxMatrix.W.Z = UpDown3->Position * EditorsScale;
  85. BoxMatrix.W.W = 1;
  86. BoxScale.X = UpDown4->Position * EditorsScale;
  87. BoxScale.Y = UpDown5->Position * EditorsScale;
  88. BoxScale.Z = UpDown6->Position * EditorsScale;
  89. SpherePos.X = UpDown7->Position * EditorsScale;
  90. SpherePos.Y = UpDown8->Position * EditorsScale;
  91. SpherePos.Z = UpDown9->Position * EditorsScale;
  92. SphereRadius = UpDown10->Position * EditorsScale;
  93. // dCollideSphereBox function !
  94. Res1 = IntersectSphereBox(VectorMake(SpherePos, 1), SphereRadius, BoxMatrix,
  95. BoxScale, &intersPoint, &ResNormal);
  96. if (Res1)
  97. {
  98. // Intersected.
  99. Label1->Caption = "Intersected";
  100. DCCamTarget->Position->SetPoint(intersPoint);
  101. // Draw normal
  102. GLLines3->Nodes->AddNode(intersPoint);
  103. GLLines3->Nodes->AddNode(VectorAdd(intersPoint, VectorScale(
  104. ResNormal, SphereRadius * 3)));
  105. }
  106. else
  107. {
  108. // Not intersected.
  109. Beep();
  110. Label1->Caption = "";
  111. }
  112. DCCamTarget->Visible = Res1;
  113. // Draw GLCube1 and GLSphere1.
  114. *GLCube1->Matrix = BoxMatrix;
  115. GLCube1->CubeWidth = BoxScale.X;
  116. GLCube1->CubeHeight = BoxScale.Y;
  117. GLCube1->CubeDepth = BoxScale.Z;
  118. *DCCube1->Matrix = *GLCube1->Matrix;
  119. DCCube1->Scale->SetVector(BoxScale);
  120. GLSphere1->Position->SetPoint(SpherePos);
  121. GLSphere1->Radius = SphereRadius;
  122. GLSphere2->Position->SetPoint(SpherePos);
  123. GLSphere2->Radius = SphereRadius;
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TForm1::Button3Click(TObject *Sender)
  127. {
  128. Edit1Change(Sender);
  129. }
  130. //---------------------------------------------------------------------------
  131. void __fastcall TForm1::ViewerMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
  132. int X, int Y)
  133. {
  134. Viewer->SetFocus();
  135. }
  136. //---------------------------------------------------------------------------
  137. void __fastcall TForm1::GLCadencerProgress(TObject *Sender, const double deltaTime,
  138. const double newTime)
  139. {
  140. if (Form1->Active)
  141. Viewer->Invalidate();
  142. }
  143. //---------------------------------------------------------------------------
  144. void __fastcall TForm1::ViewerMouseMove(TObject *Sender, TShiftState Shift, int X,
  145. int Y)
  146. {
  147. if (Shift.Contains(ssLeft))
  148. GLCamera1->MoveAroundTarget(mdy - Y, mdx - X);
  149. mdx = X;
  150. mdy = Y;
  151. }
  152. //---------------------------------------------------------------------------
  153. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  154. TPoint &MousePos, bool &Handled)
  155. {
  156. if (Viewer->Focused())
  157. GLCamera1->AdjustDistanceToTarget(Power(1.02, WheelDelta / 120));
  158. }
  159. //---------------------------------------------------------------------------