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.
32 #define GL_GLEXT_PROTOTYPES
36 static GLboolean Anim
= GL_TRUE
;
37 static GLfloat Xpos
= 0;
38 static GLuint OccQuery
;
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 )
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 /* draw the test polygon with occlusion testing */
113 glTranslatef(Xpos
, 0, -0.5);
114 glScalef(0.3, 0.3, 1.0);
115 glRotatef(-90.0 * Xpos
, 0, 0, 1);
117 glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, OccQuery
);
119 glColorMask(0, 0, 0, 0);
120 glDepthMask(GL_FALSE
);
123 glVertex3f(-1, -1, 0);
124 glVertex3f( 1, -1, 0);
125 glVertex3f( 1, 1, 0);
126 glVertex3f(-1, 1, 0);
129 glEndQueryARB(GL_SAMPLES_PASSED_ARB
);
132 /* do useful work here, if any */
133 glGetQueryObjectivARB(OccQuery
, GL_QUERY_RESULT_AVAILABLE_ARB
, &ready
);
135 glGetQueryObjectuivARB(OccQuery
, GL_QUERY_RESULT_ARB
, &passed
);
137 /* turn off occlusion testing */
138 glColorMask(1, 1, 1, 1);
139 glDepthMask(GL_TRUE
);
141 /* draw the orange rect, so we can see what's going on */
142 glColor3f(0.8, 0.5, 0);
144 glVertex3f(-1, -1, 0);
145 glVertex3f( 1, -1, 0);
146 glVertex3f( 1, 1, 0);
147 glVertex3f(-1, 1, 0);
153 /* Print result message */
154 glMatrixMode( GL_PROJECTION
);
156 glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
157 glMatrixMode( GL_MODELVIEW
);
161 sprintf(s
, " %4d Fragments Visible", passed
);
162 glRasterPos3f(-0.50, -0.7, 0);
165 glRasterPos3f(-0.25, -0.8, 0);
166 PrintString("Fully Occluded");
173 static void Reshape( int width
, int height
)
175 glViewport( 0, 0, width
, height
);
179 static void Key( unsigned char key
, int x
, int y
)
199 static void SpecialKey( int key
, int x
, int y
)
201 const GLfloat step
= 0.1;
216 static void Init( void )
218 const char *ext
= (const char *) glGetString(GL_EXTENSIONS
);
221 if (!strstr(ext
, "GL_ARB_occlusion_query")) {
222 printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
226 glGetQueryivARB(GL_SAMPLES_PASSED_ARB
, GL_QUERY_COUNTER_BITS_ARB
, &bits
);
228 printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
232 glGenQueriesARB(1, &OccQuery
);
233 assert( glIsQueryARB(OccQuery
) );
235 glEnable(GL_DEPTH_TEST
);
239 int main( int argc
, char *argv
[] )
241 glutInit( &argc
, argv
);
242 glutInitWindowPosition( 0, 0 );
243 glutInitWindowSize( 400, 400 );
244 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
245 glutCreateWindow(argv
[0]);
246 glutReshapeFunc( Reshape
);
247 glutKeyboardFunc( Key
);
248 glutSpecialFunc( SpecialKey
);
249 glutIdleFunc( Idle
);
250 glutDisplayFunc( Display
);