Unit1.cpp 4.9 KB

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