Unit1.cpp 4.8 KB

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