CmGLHardwareOcclusionQuery.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGLHardwareOcclusionQuery.h"
  25. #include "OgreException.h"
  26. namespace CamelotEngine {
  27. /**
  28. * This is a class that is the base class of the query class for
  29. * hardware occlusion testing.
  30. *
  31. * @author Lee Sandberg email: [email protected]
  32. *
  33. * Updated on 12/7/2004 by Chris McGuirk
  34. * - Implemented ARB_occlusion_query
  35. * Updated on 13/9/2005 by Tuan Kuranes email: [email protected]
  36. */
  37. //------------------------------------------------------------------
  38. /**
  39. * Default object constructor
  40. *
  41. */
  42. GLHardwareOcclusionQuery::GLHardwareOcclusionQuery()
  43. {
  44. // Check for hardware occlusion support
  45. if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
  46. {
  47. glGenQueriesARB(1, &mQueryID );
  48. }
  49. else if (GLEW_NV_occlusion_query)
  50. {
  51. glGenOcclusionQueriesNV(1, &mQueryID);
  52. }
  53. else
  54. {
  55. OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR,
  56. "Cannot allocate a Hardware query. This video card doesn't supports it, sorry.",
  57. "GLHardwareOcclusionQuery::GLHardwareOcclusionQuery" );
  58. }
  59. }
  60. //------------------------------------------------------------------
  61. /**
  62. * Object destructor
  63. */
  64. GLHardwareOcclusionQuery::~GLHardwareOcclusionQuery()
  65. {
  66. if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
  67. {
  68. glDeleteQueriesARB(1, &mQueryID);
  69. }
  70. else if (GLEW_NV_occlusion_query)
  71. {
  72. glDeleteOcclusionQueriesNV(1, &mQueryID);
  73. }
  74. }
  75. //------------------------------------------------------------------
  76. void GLHardwareOcclusionQuery::beginOcclusionQuery()
  77. {
  78. if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
  79. {
  80. glBeginQueryARB(GL_SAMPLES_PASSED_ARB, mQueryID);
  81. }
  82. else if (GLEW_NV_occlusion_query)
  83. {
  84. glBeginOcclusionQueryNV(mQueryID);
  85. }
  86. }
  87. //------------------------------------------------------------------
  88. void GLHardwareOcclusionQuery::endOcclusionQuery()
  89. {
  90. if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
  91. {
  92. glEndQueryARB(GL_SAMPLES_PASSED_ARB);
  93. }
  94. else if (GLEW_NV_occlusion_query)
  95. {
  96. glEndOcclusionQueryNV();
  97. }
  98. }
  99. //------------------------------------------------------------------
  100. bool GLHardwareOcclusionQuery::pullOcclusionQuery( unsigned int* NumOfFragments )
  101. {
  102. if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
  103. {
  104. glGetQueryObjectuivARB(mQueryID, GL_QUERY_RESULT_ARB, (GLuint*)NumOfFragments);
  105. mPixelCount = *NumOfFragments;
  106. return true;
  107. }
  108. else if (GLEW_NV_occlusion_query)
  109. {
  110. glGetOcclusionQueryuivNV(mQueryID, GL_PIXEL_COUNT_NV, (GLuint*)NumOfFragments);
  111. mPixelCount = *NumOfFragments;
  112. return true;
  113. }
  114. return false;
  115. }
  116. //------------------------------------------------------------------
  117. bool GLHardwareOcclusionQuery::isStillOutstanding(void)
  118. {
  119. GLuint available;
  120. if(GLEW_VERSION_1_5 || GLEW_ARB_occlusion_query)
  121. {
  122. glGetQueryObjectuivARB(mQueryID, GL_QUERY_RESULT_AVAILABLE_ARB, &available);
  123. }
  124. else if (GLEW_NV_occlusion_query)
  125. {
  126. glGetOcclusionQueryuivNV(mQueryID, GL_PIXEL_COUNT_AVAILABLE_NV, &available);
  127. }
  128. // GL_TRUE means a wait would occur
  129. return !(available == GL_TRUE);
  130. }
  131. }