Unit1.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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.Collision"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLS.Objects"
  12. #pragma link "GLS.Scene"
  13. #pragma link "GLS.SpaceText"
  14. #pragma link "GLS.VectorFileObjects"
  15. #pragma link "GLS.SceneViewer"
  16. #pragma link "GLS.File3DS"
  17. #pragma resource "*.dfm"
  18. TForm1 *Form1;
  19. // const int cbmFaces = 5;
  20. const String
  21. StringNames[] = {"Point","Sphere","Ellipsoid","Cube","Faces"};
  22. //---------------------------------------------------------------------------
  23. __fastcall TForm1::TForm1(TComponent* Owner)
  24. : TForm(Owner)
  25. {
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TForm1::FormCreate(TObject *Sender)
  29. {
  30. int i;
  31. SetGLSceneMediaDir();
  32. TeaPot1->LoadFromFile("TeaPot.3ds");
  33. TeaPot1->BuildOctree();
  34. TeaPot2->LoadFromFile("TeaPot.3ds");
  35. TeaPot2->BuildOctree();
  36. // rgObjectsClick(nil);
  37. //Fill StringGrid1 with current state of collisions
  38. for (i=0; i < cbmFaces; i++)
  39. {
  40. StringGrid1->Cells[0][i+1] = StringNames[i];
  41. StringGrid1->Cells[i+1][0] = StringNames[i];
  42. }
  43. //point
  44. StringGrid1->Cells[1][1] = "complete"; //Point-Point
  45. StringGrid1->Cells[1][2] = "complete"; //Sphere-Point
  46. StringGrid1->Cells[1][3] = "complete"; //Ellipsoid-Point
  47. StringGrid1->Cells[1][4] = "complete"; //Cube-Point
  48. StringGrid1->Cells[1][5] = "Cube-Point"; //Faces-Point
  49. //sphere
  50. StringGrid1->Cells[2][1] = "complete"; //Point-Sphere
  51. StringGrid1->Cells[2][2] = "complete"; //Sphere-Sphere
  52. StringGrid1->Cells[2][3] = "complete"; //Ellipsoid-Sphere
  53. StringGrid1->Cells[2][4] = "complete"; //Cube-Sphere
  54. StringGrid1->Cells[2][5] = "Cube-Sphere"; //Faces-Sphere
  55. //ellipsoid
  56. StringGrid1->Cells[3][1] ="complete"; //Point-Ellipsoid
  57. StringGrid1->Cells[3][2] ="complete"; //Sphere-Ellipsoid
  58. StringGrid1->Cells[3][3] ="incorrect"; //Ellipsoid-Ellipsoid
  59. StringGrid1->Cells[3][4] ="Cube-Sphere"; //Cube-Ellipsoid
  60. StringGrid1->Cells[3][5] ="Cube-Ellipsoid";//Faces-Ellipsoid
  61. //cube
  62. StringGrid1->Cells[4][1] ="complete"; //Point-Cube
  63. StringGrid1->Cells[4][2] ="complete"; //Sphere-Cube
  64. StringGrid1->Cells[4][3] ="Sphere-Cube"; //Ellipsoid-Cube
  65. StringGrid1->Cells[4][4] ="complete"; //Cube-Cube
  66. StringGrid1->Cells[4][5] ="experimental"; //Faces-Cube
  67. //Faces
  68. StringGrid1->Cells[5][1] ="Point-Cube"; //Point-Faces
  69. StringGrid1->Cells[5][2] ="Sphere-Cube"; //Sphere-Faces
  70. StringGrid1->Cells[5][3] ="Ellipsoid-Cube";//Ellipsoid-Faces
  71. StringGrid1->Cells[5][4] ="experimental"; //Cube-Faces
  72. StringGrid1->Cells[5][5] ="complete"; //Faces-Faces
  73. }
  74. //---------------------------------------------------------------------------
  75. void __fastcall TForm1::cbCollisionModeClick(TObject *Sender)
  76. {
  77. ((TGLBCollision *)(TeaPot1->Behaviours->Items[0]))->BoundingMode =
  78. ((TCollisionBoundingMode)(cbCollisionMode->ItemIndex));
  79. ((TGLBCollision *)(TeaPot2->Behaviours->Items[0]))->BoundingMode =
  80. ((TCollisionBoundingMode)(cbCollisionMode->ItemIndex));
  81. ((TGLBCollision *)(Bar->Behaviours->Items[0]))->BoundingMode = cbmCube;
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TForm1::FormShow(TObject *Sender)
  85. {
  86. //initialize
  87. CurrSO = TeaPot1;
  88. cbCollisionModeClick(NULL);
  89. }
  90. //---------------------------------------------------------------------------
  91. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  92. {
  93. const TColor
  94. cColor[false][true] = {clLime, clRed};
  95. __int64 t;
  96. Timer1->Enabled = false;
  97. CollisionDetected = false;
  98. t = StartPrecisionTimer();
  99. Memo1->Lines->Clear();
  100. Memo1->Lines->BeginUpdate();
  101. CollisionManager1->CheckCollisions();
  102. Memo1->Lines->EndUpdate();
  103. LATime->Caption = Format("%.1f ms", ARRAYOFCONST ((StopPrecisionTimer(t)*1000)));
  104. Shape1->Brush->Color = *cColor[CollisionDetected];
  105. Timer1->Enabled = true;
  106. }
  107. //---------------------------------------------------------------------------
  108. void __fastcall TForm1::CollisionManager1Collision(TObject *Sender, TGLBaseSceneObject *object1,
  109. TGLBaseSceneObject *object2)
  110. {
  111. if (Sender=CollisionManager1)
  112. {
  113. CollisionDetected = true;
  114. Memo1->Lines->Add(object1->Name+
  115. /*"("+StringNames[((TGLBCollision*)(object1->Behaviours->GetByClass(TGLBCollision))->BoundingMode)]+")"*/ +
  116. " - "+object2->Name
  117. /*+"("+StringNames[((TGLBCollision*)(object2->Behaviours->GetByClass(TGLBCollision))->BoundingMode)]+")"*/);
  118. }
  119. else
  120. Memo1->Lines->Add(object1->Name+
  121. /*"("+StringNames[((TGLBCollision*)(object1->Behaviours->GetByClass(TGLBCollision))->BoundingMode)]+")"*/ +
  122. " - "+object2->Name
  123. /*+"("+StringNames[((TGLBCollision*)(object2->Behaviours->GetByClass(TGLBCollision))->BoundingMode)]+") ** BB collision **"*/);
  124. ;
  125. }
  126. //---------------------------------------------------------------------------
  127. void __fastcall TForm1::GLSceneViewer1MouseDown(TObject *Sender, TMouseButton Button,
  128. TShiftState Shift, int X, int Y)
  129. {
  130. TGLCustomSceneObject *pick;
  131. pick = (TGLCustomSceneObject *) GLSceneViewer1->Buffer->GetPickedObject(X, Y) ;
  132. if (pick)
  133. CurrSO = pick;
  134. // store mouse coordinates when a button went down
  135. mdx = X;
  136. mdy = Y;
  137. }
  138. //---------------------------------------------------------------------------
  139. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  140. int X, int Y)
  141. {
  142. int dx, dy;
  143. Gls::Vectorgeometry::TVector VX, VY;
  144. TGLCamera *Camera;
  145. Camera = GLSceneViewer1->Camera;
  146. // calculate delta since last move or last mousedown
  147. dx = mdx - X;
  148. dy = mdy - Y;
  149. mdx = X;
  150. mdy = Y;
  151. if (Shift.Contains(ssLeft))
  152. {
  153. if (Shift.Contains(ssShift))
  154. // left button with shift rotates the object
  155. // (rotation happens around camera's axis)
  156. Camera->RotateObject(CurrSO, dy, dx);
  157. else
  158. // left button without shift changes camera angle
  159. // (we're moving around the parent and target dummycube)
  160. Camera->MoveAroundTarget(dy, dx);
  161. }
  162. else
  163. if (Shift.Contains(ssRight))
  164. {
  165. //Moving the objects
  166. //Description:
  167. //1. via VectorPerpendicular we create a vector that is 90° to camera view and points to Y (Up)
  168. // this is Y-direction of moving
  169. //2. now using VectorCrossProduct we create the vector that is 90° to camera view and to the other
  170. // vector (VY), this is X-direction of moving
  171. VY = VectorMake(VectorPerpendicular(YVector, VectorNormalize(GLCamera2->Position->AsAffineVector)));
  172. VX = VectorCrossProduct(VY, VectorNormalize(GLCamera2->Position->AsVector));
  173. NormalizeVector(VY);
  174. NormalizeVector(VX);
  175. CurrSO->Position->Translate(VectorCombine(VX, VY,
  176. -dx * 0.132 * Camera->DistanceToTarget() / Camera->FocalLength,
  177. dy * 0.132 * Camera->DistanceToTarget() / Camera->FocalLength));
  178. }
  179. }
  180. //---------------------------------------------------------------------------