fFaceC.cpp 7.2 KB

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