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