fOcclusionQueryC.cpp 4.5 KB

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