fMainC.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fMainC.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.GeomObjects"
  8. #pragma link "GLS.Cadencer"
  9. #pragma link "GLS.SceneViewer"
  10. #pragma link "GLS.Objects"
  11. #pragma link "GLS.VectorFileObjects"
  12. #pragma link "GLS.Scene"
  13. #pragma link "GLS.File3DS"
  14. #pragma link "GLS.BaseClasses"
  15. #pragma link "GLS.Coordinates"
  16. #pragma link "GLS.Scene"
  17. #pragma resource "*.dfm"
  18. TForm1 *Form1;
  19. //---------------------------------------------------------------------------
  20. __fastcall TForm1::TForm1(TComponent * Owner):TForm(Owner)
  21. {
  22. __int64 t;
  23. TFileName Path = GetCurrentAssetPath();
  24. // Load high poly mesh (10 000 triangles).
  25. SetCurrentDir(Path + "\\model");
  26. FreeForm1->LoadFromFile("HighPolyObject.3ds");
  27. t = StartPrecisionTimer();
  28. FreeForm1->BuildOctree(3);
  29. LABuild->Caption =
  30. Format("Build time: %.3f ms", ARRAYOFCONST((StopPrecisionTimer(t) * 1000)));
  31. Label1->Caption = "Octree Nodes: " + IntToStr(FreeForm1->Octree->NodeCount);
  32. Label2->Caption =
  33. "Tri Count Octree: " + IntToStr(FreeForm1->Octree->TriCountOctree);
  34. Label3->Caption =
  35. "Tri Count Mesh: " + IntToStr(FreeForm1->Octree->TriCountMesh);
  36. mousex = -1;
  37. mousey = -1;
  38. }
  39. //---------------------------------------------------------------------------
  40. void __fastcall TForm1::GLSceneViewer2MouseDown(TObject * Sender,
  41. TMouseButton Button,
  42. TShiftState Shift, int X, int Y)
  43. {
  44. TGLVector rayStart, rayVector, iPoint, iNormal;
  45. __int64 t;
  46. SetVector(rayStart, GLCamera2->AbsolutePosition);
  47. SetVector(rayVector,
  48. GLSceneViewer2->Buffer->
  49. ScreenToVector(AffineVectorMake(X, GLSceneViewer2->Height - Y, 0)),0);
  50. NormalizeVector(rayVector);
  51. t = StartPrecisionTimer();
  52. if(CBOctree->Checked)
  53. {
  54. // Octree method (fast)
  55. if(FreeForm1->
  56. OctreeRayCastIntersect(rayStart, rayVector, &iPoint, &iNormal))
  57. {
  58. Sphere1->Visible = True;
  59. Sphere1->Position->AsVector = iPoint;
  60. Sphere1->Direction->AsVector = VectorNormalize(iNormal);
  61. }
  62. else
  63. Sphere1->Visible = False;
  64. Label4->Hint =
  65. "# Nodes hit with raycast: " +
  66. IntToStr(FreeForm1->Octree->ResultArray.Length);
  67. }
  68. else
  69. {
  70. // Brute-Force method (slow)
  71. if(FreeForm1->RayCastIntersect(rayStart, rayVector, &iPoint, &iNormal))
  72. {
  73. Sphere1->Visible = True;
  74. Sphere1->Position->AsVector = iPoint;
  75. Sphere1->Direction->AsVector = VectorNormalize(iNormal);
  76. }
  77. else
  78. Sphere1->Visible = False;
  79. }
  80. Label5->Hint =
  81. Format("Intersect Time: %.3f ms",
  82. ARRAYOFCONST((StopPrecisionTimer(t) * 1000)));
  83. }
  84. //---------------------------------------------------------------------------
  85. void __fastcall TForm1::GLSceneViewer2MouseMove(TObject * Sender,
  86. TShiftState Shift, int X, int Y)
  87. {
  88. mousex = X;
  89. mousey = Y;
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall TForm1::GLCadencer1Progress(TObject * Sender,
  93. const double deltaTime,
  94. const double newTime)
  95. {
  96. TShiftState ss;
  97. ss << ssShift;
  98. if(CheckBox1->Checked)
  99. GLSceneViewer2MouseDown(Sender, Controls::mbLeft, ss, mousex, mousey);
  100. FreeForm1->RollAngle = 5.0 * newTime; // 45° per second
  101. }
  102. //---------------------------------------------------------------------------
  103. void __fastcall TForm1::Timer1Timer(TObject * Sender)
  104. {
  105. // Show FPS Rating
  106. LabelFPS->Caption =
  107. Format("%.2f FPS", ARRAYOFCONST((GLSceneViewer2->FramesPerSecond())));
  108. GLSceneViewer2->ResetPerformanceMonitor();
  109. // Not doing so causes ugly flickering and a significant decrease in FPS...
  110. Label4->Caption = Label4->Hint;
  111. Label5->Caption = Label5->Hint;
  112. }
  113. //---------------------------------------------------------------------------