Unit1.cpp 4.0 KB

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