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 static GLboolean Anim
= GL_TRUE
;
36 static GLfloat Xpos
= 0;
37 static GLuint OccQuery1
;
38 static GLuint OccQuery2
;
43 PrintString(const char *s
)
46 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
53 static void Idle(void)
55 static int lastTime
= 0;
57 int time
= glutGet(GLUT_ELAPSED_TIME
);
62 else if (time
- lastTime
< 20) /* 50Hz update */
65 step
= (time
- lastTime
) / 1000.0 * sign
;
74 else if (Xpos
< -2.5) {
82 static void Display( void )
84 GLuint passed1
, passed2
;
88 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
90 glMatrixMode( GL_PROJECTION
);
92 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
93 glMatrixMode( GL_MODELVIEW
);
95 glTranslatef( 0.0, 0.0, -15.0 );
97 /* draw the occluding polygons */
98 glColor3f(0, 0.6, 0.8);
100 glVertex2f(-1.6, -1.5);
101 glVertex2f(-0.4, -1.5);
102 glVertex2f(-0.4, 1.5);
103 glVertex2f(-1.6, 1.5);
105 glVertex2f( 0.4, -1.5);
106 glVertex2f( 1.6, -1.5);
107 glVertex2f( 1.6, 1.5);
108 glVertex2f( 0.4, 1.5);
111 #if defined(GL_ARB_occlusion_query)
112 glColorMask(0, 0, 0, 0);
113 glDepthMask(GL_FALSE
);
115 /* draw the first polygon with occlusion testing */
117 glTranslatef(Xpos
, 0.4, -0.5);
118 glScalef(0.3, 0.3, 1.0);
119 glRotatef(-90.0 * Xpos
, 0, 0, 1);
121 glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, OccQuery1
);
124 glVertex3f(-1, -1, 0);
125 glVertex3f( 1, -1, 0);
126 glVertex3f( 1, 1, 0);
127 glVertex3f(-1, 1, 0);
130 glEndQueryARB(GL_SAMPLES_PASSED_ARB
);
132 /* draw the second polygon with occlusion testing */
135 glTranslatef(Xpos
, -0.4, -0.5);
136 glScalef(0.3, 0.3, 1.0);
138 glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, OccQuery2
);
141 glVertex3f(-1, -1, 0);
142 glVertex3f( 1, -1, 0);
143 glVertex3f( 1, 1, 0);
144 glVertex3f(-1, 1, 0);
147 glEndQueryARB(GL_SAMPLES_PASSED_ARB
);
149 /* turn off occlusion testing */
150 glColorMask(1, 1, 1, 1);
151 glDepthMask(GL_TRUE
);
154 /* do useful work here, if any */
155 glGetQueryObjectivARB(OccQuery1
, GL_QUERY_RESULT_AVAILABLE_ARB
, &ready
);
157 glGetQueryObjectuivARB(OccQuery1
, GL_QUERY_RESULT_ARB
, &passed1
);
160 /* do useful work here, if any */
161 glGetQueryObjectivARB(OccQuery2
, GL_QUERY_RESULT_AVAILABLE_ARB
, &ready
);
163 glGetQueryObjectuivARB(OccQuery2
, GL_QUERY_RESULT_ARB
, &passed2
);
164 #endif /* GL_ARB_occlusion_query */
166 /* draw the second rect, so we can see what's going on */
167 glColor3f(0.8, 0.5, 0);
169 glVertex3f(-1, -1, 0);
170 glVertex3f( 1, -1, 0);
171 glVertex3f( 1, 1, 0);
172 glVertex3f(-1, 1, 0);
177 glTranslatef(Xpos
, 0.4, -0.5);
178 glScalef(0.3, 0.3, 1.0);
179 glRotatef(-90.0 * Xpos
, 0, 0, 1);
181 /* draw the first rect, so we can see what's going on */
183 glVertex3f(-1, -1, 0);
184 glVertex3f( 1, -1, 0);
185 glVertex3f( 1, 1, 0);
186 glVertex3f(-1, 1, 0);
191 /* Print result message */
192 glMatrixMode( GL_PROJECTION
);
194 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
195 glMatrixMode( GL_MODELVIEW
);
199 #if defined(GL_ARB_occlusion_query)
200 sprintf(s
, " %4d Fragments Visible", passed1
);
201 glRasterPos3f(-0.50, -0.6, 0);
204 glRasterPos3f(-0.25, -0.7, 0);
205 PrintString("Fully Occluded");
207 sprintf(s
, " %4d Fragments Visible", passed2
);
208 glRasterPos3f(-0.50, -0.8, 0);
211 glRasterPos3f(-0.25, -0.9, 0);
212 PrintString("Fully Occluded");
215 glRasterPos3f(-0.25, -0.8, 0);
216 PrintString("GL_ARB_occlusion_query not available at compile time");
217 #endif /* GL_ARB_occlusion_query */
223 static void Reshape( int width
, int height
)
225 glViewport( 0, 0, width
, height
);
229 static void Key( unsigned char key
, int x
, int y
)
235 glutDestroyWindow(Win
);
250 static void SpecialKey( int key
, int x
, int y
)
252 const GLfloat step
= 0.1;
267 static void Init( void )
269 const char *ext
= (const char *) glGetString(GL_EXTENSIONS
);
272 if (!strstr(ext
, "GL_ARB_occlusion_query")) {
273 printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
277 #if defined(GL_ARB_occlusion_query)
278 glGetQueryivARB(GL_SAMPLES_PASSED_ARB
, GL_QUERY_COUNTER_BITS_ARB
, &bits
);
280 printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
283 #endif /* GL_ARB_occlusion_query */
285 glGetIntegerv(GL_DEPTH_BITS
, &bits
);
286 printf("Depthbits: %d\n", bits
);
288 #if defined(GL_ARB_occlusion_query)
289 glGenQueriesARB(1, &OccQuery1
);
290 assert(OccQuery1
> 0);
291 glGenQueriesARB(1, &OccQuery2
);
292 assert(OccQuery2
> 0);
293 #endif /* GL_ARB_occlusion_query */
295 glEnable(GL_DEPTH_TEST
);
299 int main( int argc
, char *argv
[] )
301 glutInitWindowSize( 400, 400 );
302 glutInit( &argc
, argv
);
303 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
304 Win
= glutCreateWindow(argv
[0]);
306 glutReshapeFunc( Reshape
);
307 glutKeyboardFunc( Key
);
308 glutSpecialFunc( SpecialKey
);
309 glutIdleFunc( Idle
);
310 glutDisplayFunc( Display
);