Unit1.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.BaseClasses"
  9. #pragma link "GLS.Cadencer"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLS.GeomObjects"
  12. #pragma link "GLS.Objects"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma resource "*.dfm"
  16. TForm1 *Form1;
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19. : TForm(Owner)
  20. {
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TForm1::FormDestroy(TObject *Sender)
  24. {
  25. // Delete the queries
  26. TimerQuery->Free();
  27. OcclusionQuery->Free();
  28. bOcclusionQuery->Free();
  29. }
  30. //---------------------------------------------------------------------------
  31. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  32. const double newTime)
  33. {
  34. // Move some of the scene objects around
  35. GLDummyCube1->Position->X = Sin(newTime);
  36. dcTestObjects->Turn(deltaTime * 50);
  37. dcTestObjects->Position->Z = 2 * Sin(newTime);
  38. GLDummyCube2->Position->X = - Sin(newTime);
  39. }
  40. //---------------------------------------------------------------------------
  41. void __fastcall TForm1::GLSceneViewer1BeforeRender(TObject *Sender)
  42. {
  43. // Occlusion queries are supported by extensions with lower version of OpenGL.
  44. // To use them, you'd need to check if GL_NV_occlusion_query or GL_ARB_occlusion_query
  45. // extensions are present, and makes the appropriate calls to the functions/procedures
  46. // they provide.
  47. // in Delphi was: if (not TGLOcclusionQueryHandle.IsSupported) then
  48. if (!(GL_NV_occlusion_query || GL_ARB_occlusion_query))
  49. {
  50. MessageDlg("Requires hardware that supports occlusion queries to run",
  51. mtError, TMsgDlgButtons() << mbOK, 0);
  52. Close();
  53. }
  54. }
  55. //---------------------------------------------------------------------------
  56. void __fastcall TForm1::OGLBeginQueriesRender(TObject *Sender, TGLRenderContextInfo &rci)
  57. {
  58. // Generate the queries, if not already created
  59. if (!queriesCreated)
  60. {
  61. OcclusionQuery = new TGLOcclusionQueryHandle;
  62. bOcclusionQuery = new TGLBooleanOcclusionQueryHandle;
  63. TimerQuery = new TGLTimerQueryHandle;
  64. queriesCreated = true;
  65. }
  66. // Begin the timer + occlusion queries
  67. TimerQuery->BeginQuery();
  68. if (CheckBox1->Checked)
  69. bOcclusionQuery->BeginQuery();
  70. else
  71. OcclusionQuery->BeginQuery();
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TForm1::OGLEndQueriesRender(TObject *Sender, TGLRenderContextInfo &rci)
  75. {
  76. TGLQueryHandle *lQuery;
  77. // End the timer + occlusion queries
  78. if (CheckBox1->Checked)
  79. lQuery = bOcclusionQuery;
  80. else
  81. lQuery = OcclusionQuery;
  82. lQuery->EndQuery();
  83. TimerQuery->EndQuery();
  84. // Most of the frame rate is lost waiting for results to become available
  85. // + updating the captions every frame, but as this is a demo, we want to
  86. // see what is going on.
  87. while (! lQuery->IsResultAvailable()) ; // wait
  88. // would normally do something in this period before checking if
  89. // result is available
  90. samplesPassed = OcclusionQuery->PixelCount();
  91. while (! TimerQuery->IsResultAvailable()) ;// wait
  92. // would normally do something in this period before checking if
  93. // result is available
  94. timeTaken = TimerQuery->Time();
  95. // Use this line instead of the one above to use 64 bit timer, to allow
  96. // recording time periods more than a couple of seconds (requires Delphi 7+)
  97. // timeTaken := TimerQuery.QueryResultUInt64;
  98. switch (CheckBox1->Checked)
  99. {
  100. case true:
  101. Label3->Visible = ! lQuery->QueryResultBool(); break;
  102. case false:
  103. {
  104. Label3->Visible = (samplesPassed = 0);
  105. Label2->Caption = "Number of test pixels visible: " + IntToStr(samplesPassed);
  106. break;
  107. }
  108. default:
  109. ;
  110. }
  111. }
  112. //---------------------------------------------------------------------------
  113. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  114. {
  115. // Convert time taken from ns => ms & display
  116. if (timerQuerySupported)
  117. Label1->Caption = "Time taken: " + FloatToStr(timeTaken / 1000000) + " ms" ;
  118. else
  119. Label1->Caption = "Time query unavailable, requires hardware support";
  120. LabelFPS->Caption = GLSceneViewer1->FramesPerSecondText(0);
  121. GLSceneViewer1->ResetPerformanceMonitor();
  122. }
  123. //---------------------------------------------------------------------------