blob: f669a1f26ba328d49904dd2738ccc5f684da7415 [file] [log] [blame]
Brian Paul86258032003-06-13 02:38:35 +00001/*
Brian Paul8fa50752003-06-13 02:42:57 +00002 * GL_ARB_occlusion_query demo
Brian Paul86258032003-06-13 02:38:35 +00003 *
4 * Brian Paul
5 * 12 June 2003
6 *
7 * Copyright (C) 2003 Brian Paul All Rights Reserved.
8 *
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:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
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.
25 */
26
27#include <assert.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <math.h>
José Fonseca2e61d132009-01-24 16:39:49 +000032#include <GL/glew.h>
Brian Paul86258032003-06-13 02:38:35 +000033#include <GL/glut.h>
34
Brian Pauldbe1eca2004-06-28 22:03:44 +000035#define TEST_DISPLAY_LISTS 0
Brian Paul86258032003-06-13 02:38:35 +000036
Brian Paul8fa50752003-06-13 02:42:57 +000037static GLboolean Anim = GL_TRUE;
Brian Paul86258032003-06-13 02:38:35 +000038static GLfloat Xpos = 0;
39static GLuint OccQuery;
Brian Paul6c14bdc2008-12-18 09:48:20 -070040static GLint Win = 0;
Brian Paul86258032003-06-13 02:38:35 +000041
42
43static void
44PrintString(const char *s)
45{
46 while (*s) {
47 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
48 s++;
49 }
50}
51
52
53
54static void Idle(void)
55{
56 static int lastTime = 0;
57 static int sign = +1;
58 int time = glutGet(GLUT_ELAPSED_TIME);
59 float step;
60
61 if (lastTime == 0)
62 lastTime = time;
63 else if (time - lastTime < 20) /* 50Hz update */
64 return;
65
66 step = (time - lastTime) / 1000.0 * sign;
67 lastTime = time;
68
69 Xpos += step;
70
71 if (Xpos > 2.5) {
72 Xpos = 2.5;
73 sign = -1;
74 }
75 else if (Xpos < -2.5) {
76 Xpos = -2.5;
77 sign = +1;
78 }
79 glutPostRedisplay();
80}
81
82
83static void Display( void )
84{
85 GLuint passed;
86 GLint ready;
87 char s[100];
88
89 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
90
91 glMatrixMode( GL_PROJECTION );
92 glLoadIdentity();
93 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
94 glMatrixMode( GL_MODELVIEW );
95 glLoadIdentity();
96 glTranslatef( 0.0, 0.0, -15.0 );
97
98 /* draw the occluding polygons */
99 glColor3f(0, 0.6, 0.8);
100 glBegin(GL_QUADS);
101 glVertex2f(-1.6, -1.5);
102 glVertex2f(-0.4, -1.5);
103 glVertex2f(-0.4, 1.5);
104 glVertex2f(-1.6, 1.5);
105
106 glVertex2f( 0.4, -1.5);
107 glVertex2f( 1.6, -1.5);
108 glVertex2f( 1.6, 1.5);
109 glVertex2f( 0.4, 1.5);
110 glEnd();
111
112 /* draw the test polygon with occlusion testing */
113 glPushMatrix();
114 glTranslatef(Xpos, 0, -0.5);
115 glScalef(0.3, 0.3, 1.0);
116 glRotatef(-90.0 * Xpos, 0, 0, 1);
117
Brian Paulb58091a2005-01-09 17:00:57 +0000118#if defined(GL_ARB_occlusion_query)
Brian Pauldbe1eca2004-06-28 22:03:44 +0000119#if TEST_DISPLAY_LISTS
120 glNewList(10, GL_COMPILE);
Brian Paul86258032003-06-13 02:38:35 +0000121 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
Brian Pauldbe1eca2004-06-28 22:03:44 +0000122 glEndList();
123 glCallList(10);
124#else
125 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
126#endif
Brian Paul86258032003-06-13 02:38:35 +0000127
128 glColorMask(0, 0, 0, 0);
129 glDepthMask(GL_FALSE);
130
131 glBegin(GL_POLYGON);
132 glVertex3f(-1, -1, 0);
133 glVertex3f( 1, -1, 0);
134 glVertex3f( 1, 1, 0);
135 glVertex3f(-1, 1, 0);
136 glEnd();
137
Brian Pauldbe1eca2004-06-28 22:03:44 +0000138#if TEST_DISPLAY_LISTS
139 glNewList(11, GL_COMPILE);
Brian Paul86258032003-06-13 02:38:35 +0000140 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
Brian Pauldbe1eca2004-06-28 22:03:44 +0000141 glEndList();
142 glCallList(11);
143#else
144 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
145#endif
Brian Paul86258032003-06-13 02:38:35 +0000146
147 do {
148 /* do useful work here, if any */
149 glGetQueryObjectivARB(OccQuery, GL_QUERY_RESULT_AVAILABLE_ARB, &ready);
150 } while (!ready);
151 glGetQueryObjectuivARB(OccQuery, GL_QUERY_RESULT_ARB, &passed);
152
153 /* turn off occlusion testing */
154 glColorMask(1, 1, 1, 1);
155 glDepthMask(GL_TRUE);
Brian Paulb58091a2005-01-09 17:00:57 +0000156#endif /* GL_ARB_occlusion_query */
Brian Paul86258032003-06-13 02:38:35 +0000157
158 /* draw the orange rect, so we can see what's going on */
159 glColor3f(0.8, 0.5, 0);
160 glBegin(GL_POLYGON);
161 glVertex3f(-1, -1, 0);
162 glVertex3f( 1, -1, 0);
163 glVertex3f( 1, 1, 0);
164 glVertex3f(-1, 1, 0);
165 glEnd();
166
167 glPopMatrix();
168
169
170 /* Print result message */
171 glMatrixMode( GL_PROJECTION );
172 glLoadIdentity();
173 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
174 glMatrixMode( GL_MODELVIEW );
175 glLoadIdentity();
176
177 glColor3f(1, 1, 1);
Brian Paulb58091a2005-01-09 17:00:57 +0000178#if defined(GL_ARB_occlusion_query)
Brian Paul86258032003-06-13 02:38:35 +0000179 sprintf(s, " %4d Fragments Visible", passed);
180 glRasterPos3f(-0.50, -0.7, 0);
181 PrintString(s);
182 if (!passed) {
183 glRasterPos3f(-0.25, -0.8, 0);
184 PrintString("Fully Occluded");
185 }
Brian Paulb58091a2005-01-09 17:00:57 +0000186#else
187 glRasterPos3f(-0.25, -0.8, 0);
188 PrintString("GL_ARB_occlusion_query not available at compile time");
189#endif /* GL_ARB_occlusion_query */
Brian Paul86258032003-06-13 02:38:35 +0000190
191 glutSwapBuffers();
192}
193
194
195static void Reshape( int width, int height )
196{
197 glViewport( 0, 0, width, height );
198}
199
200
201static void Key( unsigned char key, int x, int y )
202{
203 (void) x;
204 (void) y;
205 switch (key) {
206 case 27:
Brian Paul6c14bdc2008-12-18 09:48:20 -0700207 glutDestroyWindow(Win);
Brian Paul86258032003-06-13 02:38:35 +0000208 exit(0);
209 break;
Brian Paul8fa50752003-06-13 02:42:57 +0000210 case ' ':
211 Anim = !Anim;
212 if (Anim)
213 glutIdleFunc(Idle);
214 else
215 glutIdleFunc(NULL);
216 break;
Brian Paul86258032003-06-13 02:38:35 +0000217 }
218 glutPostRedisplay();
219}
220
221
222static void SpecialKey( int key, int x, int y )
223{
224 const GLfloat step = 0.1;
225 (void) x;
226 (void) y;
227 switch (key) {
228 case GLUT_KEY_LEFT:
229 Xpos -= step;
230 break;
231 case GLUT_KEY_RIGHT:
232 Xpos += step;
233 break;
234 }
235 glutPostRedisplay();
236}
237
238
239static void Init( void )
240{
241 const char *ext = (const char *) glGetString(GL_EXTENSIONS);
242 GLint bits;
243
244 if (!strstr(ext, "GL_ARB_occlusion_query")) {
245 printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
246 exit(-1);
247 }
248
Brian Paulb58091a2005-01-09 17:00:57 +0000249#if defined(GL_ARB_occlusion_query)
Brian Paul86258032003-06-13 02:38:35 +0000250 glGetQueryivARB(GL_SAMPLES_PASSED_ARB, GL_QUERY_COUNTER_BITS_ARB, &bits);
251 if (!bits) {
252 printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
253 exit(-1);
254 }
Brian Paulb58091a2005-01-09 17:00:57 +0000255#endif /* GL_ARB_occlusion_query */
Brian Paul86258032003-06-13 02:38:35 +0000256
Brian Pauldbe1eca2004-06-28 22:03:44 +0000257 glGetIntegerv(GL_DEPTH_BITS, &bits);
258 printf("Depthbits: %d\n", bits);
259
Brian Paulb58091a2005-01-09 17:00:57 +0000260#if defined(GL_ARB_occlusion_query)
Brian Paul86258032003-06-13 02:38:35 +0000261 glGenQueriesARB(1, &OccQuery);
Brian Pauldbe1eca2004-06-28 22:03:44 +0000262 assert(OccQuery > 0);
Brian Paulb58091a2005-01-09 17:00:57 +0000263#endif /* GL_ARB_occlusion_query */
Brian Paul86258032003-06-13 02:38:35 +0000264
265 glEnable(GL_DEPTH_TEST);
266}
267
268
269int main( int argc, char *argv[] )
270{
Brian Paul86258032003-06-13 02:38:35 +0000271 glutInitWindowSize( 400, 400 );
Brian Paul263f4322009-12-18 08:12:55 -0700272 glutInit( &argc, argv );
Brian Paul86258032003-06-13 02:38:35 +0000273 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
Brian Paul6c14bdc2008-12-18 09:48:20 -0700274 Win = glutCreateWindow(argv[0]);
José Fonseca2e61d132009-01-24 16:39:49 +0000275 glewInit();
Brian Paul86258032003-06-13 02:38:35 +0000276 glutReshapeFunc( Reshape );
277 glutKeyboardFunc( Key );
278 glutSpecialFunc( SpecialKey );
279 glutIdleFunc( Idle );
280 glutDisplayFunc( Display );
281 Init();
282 glutMainLoop();
283 return 0;
284}