2 * GL_ARB_occlusion_query demo
7 * Copyright (C) 2003 Brian Paul All Rights Reserved.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #define TEST_DISPLAY_LISTS 0
37 static GLboolean Anim = GL_TRUE;
38 static GLfloat Xpos = 0;
39 static GLuint OccQuery;
44 PrintString(const char *s)
47 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
54 static void Idle(void)
56 static int lastTime = 0;
58 int time = glutGet(GLUT_ELAPSED_TIME);
63 else if (time - lastTime < 20) /* 50Hz update */
66 step = (time - lastTime) / 1000.0 * sign;
75 else if (Xpos < -2.5) {
83 static void Display( void )
89 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
91 glMatrixMode( GL_PROJECTION );
93 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
94 glMatrixMode( GL_MODELVIEW );
96 glTranslatef( 0.0, 0.0, -15.0 );
98 /* draw the occluding polygons */
99 glColor3f(0, 0.6, 0.8);
101 glVertex2f(-1.6, -1.5);
102 glVertex2f(-0.4, -1.5);
103 glVertex2f(-0.4, 1.5);
104 glVertex2f(-1.6, 1.5);
106 glVertex2f( 0.4, -1.5);
107 glVertex2f( 1.6, -1.5);
108 glVertex2f( 1.6, 1.5);
109 glVertex2f( 0.4, 1.5);
112 /* draw the test polygon with occlusion testing */
114 glTranslatef(Xpos, 0, -0.5);
115 glScalef(0.3, 0.3, 1.0);
116 glRotatef(-90.0 * Xpos, 0, 0, 1);
118 #if defined(GL_ARB_occlusion_query)
119 #if TEST_DISPLAY_LISTS
120 glNewList(10, GL_COMPILE);
121 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
125 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
128 glColorMask(0, 0, 0, 0);
129 glDepthMask(GL_FALSE);
132 glVertex3f(-1, -1, 0);
133 glVertex3f( 1, -1, 0);
134 glVertex3f( 1, 1, 0);
135 glVertex3f(-1, 1, 0);
138 #if TEST_DISPLAY_LISTS
139 glNewList(11, GL_COMPILE);
140 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
144 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
148 /* do useful work here, if any */
149 glGetQueryObjectivARB(OccQuery, GL_QUERY_RESULT_AVAILABLE_ARB, &ready);
151 glGetQueryObjectuivARB(OccQuery, GL_QUERY_RESULT_ARB, &passed);
153 /* turn off occlusion testing */
154 glColorMask(1, 1, 1, 1);
155 glDepthMask(GL_TRUE);
156 #endif /* GL_ARB_occlusion_query */
158 /* draw the orange rect, so we can see what's going on */
159 glColor3f(0.8, 0.5, 0);
161 glVertex3f(-1, -1, 0);
162 glVertex3f( 1, -1, 0);
163 glVertex3f( 1, 1, 0);
164 glVertex3f(-1, 1, 0);
170 /* Print result message */
171 glMatrixMode( GL_PROJECTION );
173 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
174 glMatrixMode( GL_MODELVIEW );
178 #if defined(GL_ARB_occlusion_query)
179 sprintf(s, " %4d Fragments Visible", passed);
180 glRasterPos3f(-0.50, -0.7, 0);
183 glRasterPos3f(-0.25, -0.8, 0);
184 PrintString("Fully Occluded");
187 glRasterPos3f(-0.25, -0.8, 0);
188 PrintString("GL_ARB_occlusion_query not available at compile time");
189 #endif /* GL_ARB_occlusion_query */
195 static void Reshape( int width, int height )
197 glViewport( 0, 0, width, height );
201 static void Key( unsigned char key, int x, int y )
207 glutDestroyWindow(Win);
222 static void SpecialKey( int key, int x, int y )
224 const GLfloat step = 0.1;
239 static void Init( void )
241 const char *ext = (const char *) glGetString(GL_EXTENSIONS);
244 if (!strstr(ext, "GL_ARB_occlusion_query")) {
245 printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
249 #if defined(GL_ARB_occlusion_query)
250 glGetQueryivARB(GL_SAMPLES_PASSED_ARB, GL_QUERY_COUNTER_BITS_ARB, &bits);
252 printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
255 #endif /* GL_ARB_occlusion_query */
257 glGetIntegerv(GL_DEPTH_BITS, &bits);
258 printf("Depthbits: %d\n", bits);
260 #if defined(GL_ARB_occlusion_query)
261 glGenQueriesARB(1, &OccQuery);
262 assert(OccQuery > 0);
263 #endif /* GL_ARB_occlusion_query */
265 glEnable(GL_DEPTH_TEST);
269 int main( int argc, char *argv[] )
271 glutInit( &argc, argv );
272 glutInitWindowPosition( 0, 0 );
273 glutInitWindowSize( 400, 400 );
274 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
275 Win = glutCreateWindow(argv[0]);
277 glutReshapeFunc( Reshape );
278 glutKeyboardFunc( Key );
279 glutSpecialFunc( SpecialKey );
280 glutIdleFunc( Idle );
281 glutDisplayFunc( Display );