blob: 3735f7de4384cd765971a7520f68af7c567a19ef [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>
32#define GL_GLEXT_PROTOTYPES
33#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;
40
41
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 Pauldbe1eca2004-06-28 22:03:44 +0000118#if TEST_DISPLAY_LISTS
119 glNewList(10, GL_COMPILE);
Brian Paul86258032003-06-13 02:38:35 +0000120 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
Brian Pauldbe1eca2004-06-28 22:03:44 +0000121 glEndList();
122 glCallList(10);
123#else
124 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
125#endif
Brian Paul86258032003-06-13 02:38:35 +0000126
127 glColorMask(0, 0, 0, 0);
128 glDepthMask(GL_FALSE);
129
130 glBegin(GL_POLYGON);
131 glVertex3f(-1, -1, 0);
132 glVertex3f( 1, -1, 0);
133 glVertex3f( 1, 1, 0);
134 glVertex3f(-1, 1, 0);
135 glEnd();
136
Brian Pauldbe1eca2004-06-28 22:03:44 +0000137#if TEST_DISPLAY_LISTS
138 glNewList(11, GL_COMPILE);
Brian Paul86258032003-06-13 02:38:35 +0000139 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
Brian Pauldbe1eca2004-06-28 22:03:44 +0000140 glEndList();
141 glCallList(11);
142#else
143 glEndQueryARB(GL_SAMPLES_PASSED_ARB);
144#endif
Brian Paul86258032003-06-13 02:38:35 +0000145
146 do {
147 /* do useful work here, if any */
148 glGetQueryObjectivARB(OccQuery, GL_QUERY_RESULT_AVAILABLE_ARB, &ready);
149 } while (!ready);
150 glGetQueryObjectuivARB(OccQuery, GL_QUERY_RESULT_ARB, &passed);
151
152 /* turn off occlusion testing */
153 glColorMask(1, 1, 1, 1);
154 glDepthMask(GL_TRUE);
155
156 /* draw the orange rect, so we can see what's going on */
157 glColor3f(0.8, 0.5, 0);
158 glBegin(GL_POLYGON);
159 glVertex3f(-1, -1, 0);
160 glVertex3f( 1, -1, 0);
161 glVertex3f( 1, 1, 0);
162 glVertex3f(-1, 1, 0);
163 glEnd();
164
165 glPopMatrix();
166
167
168 /* Print result message */
169 glMatrixMode( GL_PROJECTION );
170 glLoadIdentity();
171 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
172 glMatrixMode( GL_MODELVIEW );
173 glLoadIdentity();
174
175 glColor3f(1, 1, 1);
176 sprintf(s, " %4d Fragments Visible", passed);
177 glRasterPos3f(-0.50, -0.7, 0);
178 PrintString(s);
179 if (!passed) {
180 glRasterPos3f(-0.25, -0.8, 0);
181 PrintString("Fully Occluded");
182 }
183
184 glutSwapBuffers();
185}
186
187
188static void Reshape( int width, int height )
189{
190 glViewport( 0, 0, width, height );
191}
192
193
194static void Key( unsigned char key, int x, int y )
195{
196 (void) x;
197 (void) y;
198 switch (key) {
199 case 27:
200 exit(0);
201 break;
Brian Paul8fa50752003-06-13 02:42:57 +0000202 case ' ':
203 Anim = !Anim;
204 if (Anim)
205 glutIdleFunc(Idle);
206 else
207 glutIdleFunc(NULL);
208 break;
Brian Paul86258032003-06-13 02:38:35 +0000209 }
210 glutPostRedisplay();
211}
212
213
214static void SpecialKey( int key, int x, int y )
215{
216 const GLfloat step = 0.1;
217 (void) x;
218 (void) y;
219 switch (key) {
220 case GLUT_KEY_LEFT:
221 Xpos -= step;
222 break;
223 case GLUT_KEY_RIGHT:
224 Xpos += step;
225 break;
226 }
227 glutPostRedisplay();
228}
229
230
231static void Init( void )
232{
233 const char *ext = (const char *) glGetString(GL_EXTENSIONS);
234 GLint bits;
235
236 if (!strstr(ext, "GL_ARB_occlusion_query")) {
237 printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
238 exit(-1);
239 }
240
241 glGetQueryivARB(GL_SAMPLES_PASSED_ARB, GL_QUERY_COUNTER_BITS_ARB, &bits);
242 if (!bits) {
243 printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
244 exit(-1);
245 }
246
Brian Pauldbe1eca2004-06-28 22:03:44 +0000247 glGetIntegerv(GL_DEPTH_BITS, &bits);
248 printf("Depthbits: %d\n", bits);
249
Brian Paul86258032003-06-13 02:38:35 +0000250 glGenQueriesARB(1, &OccQuery);
Brian Pauldbe1eca2004-06-28 22:03:44 +0000251 assert(OccQuery > 0);
Brian Paul86258032003-06-13 02:38:35 +0000252
253 glEnable(GL_DEPTH_TEST);
254}
255
256
257int main( int argc, char *argv[] )
258{
259 glutInit( &argc, argv );
260 glutInitWindowPosition( 0, 0 );
261 glutInitWindowSize( 400, 400 );
262 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
263 glutCreateWindow(argv[0]);
264 glutReshapeFunc( Reshape );
265 glutKeyboardFunc( Key );
266 glutSpecialFunc( SpecialKey );
267 glutIdleFunc( Idle );
268 glutDisplayFunc( Display );
269 Init();
270 glutMainLoop();
271 return 0;
272}