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