egl: remove duplicate ARRAY_SIZE() macro declaration
[mesa.git] / progs / demos / arbocclude2.c
1 /*
2 * GL_ARB_occlusion_query demo
3 *
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>
32 #include <GL/glew.h>
33 #include <GL/glut.h>
34
35 static GLboolean Anim = GL_TRUE;
36 static GLfloat Xpos = 0;
37 static GLuint OccQuery1;
38 static GLuint OccQuery2;
39 static GLint Win = 0;
40
41
42 static void
43 PrintString(const char *s)
44 {
45 while (*s) {
46 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
47 s++;
48 }
49 }
50
51
52
53 static void Idle(void)
54 {
55 static int lastTime = 0;
56 static int sign = +1;
57 int time = glutGet(GLUT_ELAPSED_TIME);
58 float step;
59
60 if (lastTime == 0)
61 lastTime = time;
62 else if (time - lastTime < 20) /* 50Hz update */
63 return;
64
65 step = (time - lastTime) / 1000.0 * sign;
66 lastTime = time;
67
68 Xpos += step;
69
70 if (Xpos > 2.5) {
71 Xpos = 2.5;
72 sign = -1;
73 }
74 else if (Xpos < -2.5) {
75 Xpos = -2.5;
76 sign = +1;
77 }
78 glutPostRedisplay();
79 }
80
81
82 static void Display( void )
83 {
84 GLuint passed1, passed2;
85 GLint ready;
86 char s[100];
87
88 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
89
90 glMatrixMode( GL_PROJECTION );
91 glLoadIdentity();
92 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
93 glMatrixMode( GL_MODELVIEW );
94 glLoadIdentity();
95 glTranslatef( 0.0, 0.0, -15.0 );
96
97 /* draw the occluding polygons */
98 glColor3f(0, 0.6, 0.8);
99 glBegin(GL_QUADS);
100 glVertex2f(-1.6, -1.5);
101 glVertex2f(-0.4, -1.5);
102 glVertex2f(-0.4, 1.5);
103 glVertex2f(-1.6, 1.5);
104
105 glVertex2f( 0.4, -1.5);
106 glVertex2f( 1.6, -1.5);
107 glVertex2f( 1.6, 1.5);
108 glVertex2f( 0.4, 1.5);
109 glEnd();
110
111 #if defined(GL_ARB_occlusion_query)
112 glColorMask(0, 0, 0, 0);
113 glDepthMask(GL_FALSE);
114
115 /* draw the first polygon with occlusion testing */
116 glPushMatrix();
117 glTranslatef(Xpos, 0.4, -0.5);
118 glScalef(0.3, 0.3, 1.0);
119 glRotatef(-90.0 * Xpos, 0, 0, 1);
120
121 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery1);
122
123 glBegin(GL_POLYGON);
124 glVertex3f(-1, -1, 0);
125 glVertex3f( 1, -1, 0);
126 glVertex3f( 1, 1, 0);
127 glVertex3f(-1, 1, 0);
128 glEnd();
129
130 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
131
132 /* draw the second polygon with occlusion testing */
133 glPopMatrix();
134 glPushMatrix();
135 glTranslatef(Xpos, -0.4, -0.5);
136 glScalef(0.3, 0.3, 1.0);
137
138 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery2);
139
140 glBegin(GL_POLYGON);
141 glVertex3f(-1, -1, 0);
142 glVertex3f( 1, -1, 0);
143 glVertex3f( 1, 1, 0);
144 glVertex3f(-1, 1, 0);
145 glEnd();
146
147 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
148
149 /* turn off occlusion testing */
150 glColorMask(1, 1, 1, 1);
151 glDepthMask(GL_TRUE);
152
153 do {
154 /* do useful work here, if any */
155 glGetQueryObjectivARB(OccQuery1, GL_QUERY_RESULT_AVAILABLE_ARB, &ready);
156 } while (!ready);
157 glGetQueryObjectuivARB(OccQuery1, GL_QUERY_RESULT_ARB, &passed1);
158
159 do {
160 /* do useful work here, if any */
161 glGetQueryObjectivARB(OccQuery2, GL_QUERY_RESULT_AVAILABLE_ARB, &ready);
162 } while (!ready);
163 glGetQueryObjectuivARB(OccQuery2, GL_QUERY_RESULT_ARB, &passed2);
164 #endif /* GL_ARB_occlusion_query */
165
166 /* draw the second rect, so we can see what's going on */
167 glColor3f(0.8, 0.5, 0);
168 glBegin(GL_POLYGON);
169 glVertex3f(-1, -1, 0);
170 glVertex3f( 1, -1, 0);
171 glVertex3f( 1, 1, 0);
172 glVertex3f(-1, 1, 0);
173 glEnd();
174
175 glPopMatrix();
176 glPushMatrix();
177 glTranslatef(Xpos, 0.4, -0.5);
178 glScalef(0.3, 0.3, 1.0);
179 glRotatef(-90.0 * Xpos, 0, 0, 1);
180
181 /* draw the first rect, so we can see what's going on */
182 glBegin(GL_POLYGON);
183 glVertex3f(-1, -1, 0);
184 glVertex3f( 1, -1, 0);
185 glVertex3f( 1, 1, 0);
186 glVertex3f(-1, 1, 0);
187 glEnd();
188
189 glPopMatrix();
190
191 /* Print result message */
192 glMatrixMode( GL_PROJECTION );
193 glLoadIdentity();
194 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
195 glMatrixMode( GL_MODELVIEW );
196 glLoadIdentity();
197
198 glColor3f(1, 1, 1);
199 #if defined(GL_ARB_occlusion_query)
200 sprintf(s, " %4d Fragments Visible", passed1);
201 glRasterPos3f(-0.50, -0.6, 0);
202 PrintString(s);
203 if (!passed1) {
204 glRasterPos3f(-0.25, -0.7, 0);
205 PrintString("Fully Occluded");
206 }
207 sprintf(s, " %4d Fragments Visible", passed2);
208 glRasterPos3f(-0.50, -0.8, 0);
209 PrintString(s);
210 if (!passed2) {
211 glRasterPos3f(-0.25, -0.9, 0);
212 PrintString("Fully Occluded");
213 }
214 #else
215 glRasterPos3f(-0.25, -0.8, 0);
216 PrintString("GL_ARB_occlusion_query not available at compile time");
217 #endif /* GL_ARB_occlusion_query */
218
219 glutSwapBuffers();
220 }
221
222
223 static void Reshape( int width, int height )
224 {
225 glViewport( 0, 0, width, height );
226 }
227
228
229 static void Key( unsigned char key, int x, int y )
230 {
231 (void) x;
232 (void) y;
233 switch (key) {
234 case 27:
235 glutDestroyWindow(Win);
236 exit(0);
237 break;
238 case ' ':
239 Anim = !Anim;
240 if (Anim)
241 glutIdleFunc(Idle);
242 else
243 glutIdleFunc(NULL);
244 break;
245 }
246 glutPostRedisplay();
247 }
248
249
250 static void SpecialKey( int key, int x, int y )
251 {
252 const GLfloat step = 0.1;
253 (void) x;
254 (void) y;
255 switch (key) {
256 case GLUT_KEY_LEFT:
257 Xpos -= step;
258 break;
259 case GLUT_KEY_RIGHT:
260 Xpos += step;
261 break;
262 }
263 glutPostRedisplay();
264 }
265
266
267 static void Init( void )
268 {
269 const char *ext = (const char *) glGetString(GL_EXTENSIONS);
270 GLint bits;
271
272 if (!strstr(ext, "GL_ARB_occlusion_query")) {
273 printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
274 exit(-1);
275 }
276
277 #if defined(GL_ARB_occlusion_query)
278 glGetQueryivARB(GL_SAMPLES_PASSED_ARB, GL_QUERY_COUNTER_BITS_ARB, &bits);
279 if (!bits) {
280 printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
281 exit(-1);
282 }
283 #endif /* GL_ARB_occlusion_query */
284
285 glGetIntegerv(GL_DEPTH_BITS, &bits);
286 printf("Depthbits: %d\n", bits);
287
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 */
294
295 glEnable(GL_DEPTH_TEST);
296 }
297
298
299 int main( int argc, char *argv[] )
300 {
301 glutInitWindowSize( 400, 400 );
302 glutInit( &argc, argv );
303 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
304 Win = glutCreateWindow(argv[0]);
305 glewInit();
306 glutReshapeFunc( Reshape );
307 glutKeyboardFunc( Key );
308 glutSpecialFunc( SpecialKey );
309 glutIdleFunc( Idle );
310 glutDisplayFunc( Display );
311 Init();
312 glutMainLoop();
313 return 0;
314 }