demos: all glutDestroyWindow() upon exit to test query object clean-up
[mesa.git] / progs / demos / arbocclude.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 #define GL_GLEXT_PROTOTYPES
33 #include <GL/glut.h>
34
35 #define TEST_DISPLAY_LISTS 0
36
37 static GLboolean Anim = GL_TRUE;
38 static GLfloat Xpos = 0;
39 static GLuint OccQuery;
40 static GLint Win = 0;
41
42
43 static void
44 PrintString(const char *s)
45 {
46 while (*s) {
47 glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
48 s++;
49 }
50 }
51
52
53
54 static 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
83 static 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
118 #if defined(GL_ARB_occlusion_query)
119 #if TEST_DISPLAY_LISTS
120 glNewList(10, GL_COMPILE);
121 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
122 glEndList();
123 glCallList(10);
124 #else
125 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
126 #endif
127
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
138 #if TEST_DISPLAY_LISTS
139 glNewList(11, GL_COMPILE);
140 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
141 glEndList();
142 glCallList(11);
143 #else
144 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
145 #endif
146
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);
156 #endif /* GL_ARB_occlusion_query */
157
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);
178 #if defined(GL_ARB_occlusion_query)
179 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 }
186 #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 */
190
191 glutSwapBuffers();
192 }
193
194
195 static void Reshape( int width, int height )
196 {
197 glViewport( 0, 0, width, height );
198 }
199
200
201 static void Key( unsigned char key, int x, int y )
202 {
203 (void) x;
204 (void) y;
205 switch (key) {
206 case 27:
207 glutDestroyWindow(Win);
208 exit(0);
209 break;
210 case ' ':
211 Anim = !Anim;
212 if (Anim)
213 glutIdleFunc(Idle);
214 else
215 glutIdleFunc(NULL);
216 break;
217 }
218 glutPostRedisplay();
219 }
220
221
222 static 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
239 static 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
249 #if defined(GL_ARB_occlusion_query)
250 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 }
255 #endif /* GL_ARB_occlusion_query */
256
257 glGetIntegerv(GL_DEPTH_BITS, &bits);
258 printf("Depthbits: %d\n", bits);
259
260 #if defined(GL_ARB_occlusion_query)
261 glGenQueriesARB(1, &OccQuery);
262 assert(OccQuery > 0);
263 #endif /* GL_ARB_occlusion_query */
264
265 glEnable(GL_DEPTH_TEST);
266 }
267
268
269 int main( int argc, char *argv[] )
270 {
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]);
276 glutReshapeFunc( Reshape );
277 glutKeyboardFunc( Key );
278 glutSpecialFunc( SpecialKey );
279 glutIdleFunc( Idle );
280 glutDisplayFunc( Display );
281 Init();
282 glutMainLoop();
283 return 0;
284 }