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