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