fBoxC.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #include <stdlib.h>
  5. #include <GLS.Keyboard.hpp>
  6. #pragma hdrstop
  7. #include "fBoxC.h"
  8. #include "GLS.FPSMovement.hpp"
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. #pragma link "GLS.GeomObjects"
  12. #pragma link "GLS.Navigator"
  13. #pragma link "GLS.Cadencer"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma link "GLS.Objects"
  16. #pragma link "GLS.VectorFileObjects"
  17. #pragma link "GLS.Scene"
  18. #pragma link "GLS.File3DS"
  19. #pragma link "GLS.VectorGeometry"
  20. #pragma link "GLS.Keyboard"
  21. #pragma link "GLS.Coordinates"
  22. #pragma link "GLS.BaseClasses"
  23. #pragma link "GLS.Coordinates"
  24. #pragma link "GLS.Cadencer"
  25. #pragma link "GLS.SceneViewer"
  26. #pragma resource "*.dfm"
  27. TForm1 *Form1;
  28. float random()
  29. {
  30. return (float)(rand() & 0x1FFF) / (float)0x1FFF;
  31. }
  32. //---------------------------------------------------------------------------
  33. __fastcall TForm1::TForm1(TComponent * Owner):TForm(Owner)
  34. {
  35. TFileName Path = GetCurrentAssetPath();
  36. SetCurrentDir(Path + "\\model");
  37. FreeForm1->LoadFromFile("BoxedIn.3ds");
  38. FreeForm1->BuildOctree(3);
  39. Label1->Caption =
  40. "Octree Nodes : " + IntToStr(FreeForm1->Octree->NodeCount);
  41. Label2->Caption =
  42. "Tri Count Octree: " + IntToStr(FreeForm1->Octree->TriCountOctree);
  43. Label3->Caption =
  44. "Tri Count Mesh : " + IntToStr(FreeForm1->Octree->TriCountMesh);
  45. Lines1->AddNode(0, 0, 0);
  46. Lines1->ObjectStyle = Lines1->ObjectStyle << osDirectDraw;
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall TForm1::GLCadencer1Progress(TObject * Sender,
  50. const double deltaTime,
  51. const double newTime)
  52. {
  53. TGLVector rayStart, rayVector;
  54. float velocity;
  55. TGLVector pPoint;
  56. TGLVector pNormal;
  57. __int64 t;
  58. if(IsKeyDown(VK_ESCAPE))
  59. Close();
  60. velocity = TrackBar1->Position * deltaTime * 50;
  61. t = StartPrecisionTimer();
  62. SetVector(rayStart, Sphere2->AbsolutePosition);
  63. SetVector(rayVector, Sphere2->AbsoluteDirection);
  64. NormalizeVector(rayVector);
  65. //Note: since collision may be performed on multiple meshes, we might need to know which hit
  66. // is closest (ie: d=raystart - pPoint)->
  67. if(FreeForm1->
  68. OctreeSphereSweepIntersect(rayStart, rayVector, velocity, Sphere2->Radius,
  69. &pPoint, &pNormal))
  70. {
  71. // Show the polygon intersection point
  72. NormalizeVector(pNormal);
  73. Sphere1->Position->AsVector = pPoint;
  74. Sphere1->Direction->AsVector = pNormal;
  75. // Make it rebound->->->
  76. Sphere2->Direction->AsAffineVector =
  77. VectorReflect(Sphere2->Direction->AsAffineVector,
  78. AffineVectorMake(pNormal));
  79. // Add some "english"->->->
  80. Sphere2->Direction->X = Sphere2->Direction->X + random() / 10;
  81. Sphere2->Direction->Y = Sphere2->Direction->Y + random() / 10;
  82. Sphere2->Direction->Z = Sphere2->Direction->Z + random() / 10;
  83. // Add intersect point to trail
  84. AddToTrail(pPoint);
  85. }
  86. else
  87. {
  88. Sphere2->Move(velocity); //No collision, so just move the ball->
  89. }
  90. // Last trail point is always the sphere's current position
  91. Lines1->Nodes->Last()->AsVector = Sphere2->Position->AsVector;
  92. colTotalTime = colTotalTime + StopPrecisionTimer(t);
  93. colCount++;
  94. }
  95. //---------------------------------------------------------------------------
  96. void TForm1::AddToTrail(const TGLVector & p)
  97. {
  98. int i, k;
  99. Lines1->Nodes->Last()->AsVector = p;
  100. Lines1->AddNode(0, 0, 0);
  101. if(Lines1->Nodes->Count > 20) // limit trail to 20 points
  102. Lines1->Nodes->Delete(0);
  103. for(i = 0; i <= 19; i++)
  104. {
  105. k = Lines1->Nodes->Count - i - 1;
  106. if(k >= 0)
  107. ((TGLLinesNode *) Lines1->Nodes->Items[k])->Color->Alpha =
  108. 0.95 - i * 0.05;
  109. }
  110. }
  111. void __fastcall TForm1::Timer1Timer(TObject * Sender)
  112. {
  113. float t;
  114. String s;
  115. if(colCount > 0)
  116. t = colTotalTime * 1000 / colCount;
  117. else
  118. t = 0;
  119. LabelFPS->Caption = Format("%.2f FPS for collisions/frame",
  120. ARRAYOFCONST((GLSceneViewer2->FramesPerSecond())));
  121. GLSceneViewer2->ResetPerformanceMonitor();
  122. colTotalTime = 0;
  123. colCount = 0;
  124. }
  125. //---------------------------------------------------------------------------
  126. void __fastcall TForm1::Button1Click(TObject * Sender)
  127. {
  128. //if(the ball gets stuck in a pattern, hit the reset button->
  129. Sphere2->Position->X = random();
  130. Sphere2->Position->Y = random();
  131. Sphere2->Position->Z = random();
  132. Sphere2->Direction->X = random();
  133. if(random() > 0.5)
  134. Sphere2->Direction->X = -Sphere2->Direction->X;
  135. Sphere2->Direction->Y = random();
  136. if(random() > 0.5)
  137. Sphere2->Direction->Y = -Sphere2->Direction->Y;
  138. Sphere2->Direction->Z = random();
  139. if(random() > 0.5)
  140. Sphere2->Direction->Z = -Sphere2->Direction->Z;
  141. }
  142. //---------------------------------------------------------------------------