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 #include <glad/glad.h>
33 #include "glut_wrap.h"
37 static GLboolean Anim
= GL_TRUE
;
38 static GLfloat Xpos
[NUM_OCC
], Ypos
[NUM_OCC
];
39 static GLfloat Sign
[NUM_OCC
];
40 static GLuint OccQuery
[NUM_OCC
];
45 PrintString(const char *s
)
48 glutBitmapCharacter(GLUT_BITMAP_8_BY_13
, (int) *s
);
55 static void Idle(void)
57 static int lastTime
= 0;
58 int time
= glutGet(GLUT_ELAPSED_TIME
);
64 else if (time
- lastTime
< 20) /* 50Hz update */
67 for (i
= 0; i
< NUM_OCC
; i
++) {
69 step
= (time
- lastTime
) / 1000.0 * Sign
[i
];
77 else if (Xpos
[i
] < -2.5) {
90 static void Display( void )
94 glClearColor(0.25, 0.25, 0.25, 0.0);
95 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
97 glMatrixMode( GL_PROJECTION
);
99 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
100 glMatrixMode( GL_MODELVIEW
);
102 glTranslatef( 0.0, 0.0, -15.0 );
104 /* draw the occluding polygons */
105 glColor3f(0, 0.4, 0.6);
107 glVertex2f(-1.6, -2.5);
108 glVertex2f(-0.4, -2.5);
109 glVertex2f(-0.4, 2.5);
110 glVertex2f(-1.6, 2.5);
111 glVertex2f( 0.4, -2.5);
112 glVertex2f( 1.6, -2.5);
113 glVertex2f( 1.6, 2.5);
114 glVertex2f( 0.4, 2.5);
118 glColorMask(0, 0, 0, 0);
119 glDepthMask(GL_FALSE
);
121 /* draw the test polygons with occlusion testing */
122 for (i
= 0; i
< NUM_OCC
; i
++) {
124 glTranslatef(Xpos
[i
], Ypos
[i
], -0.5);
125 glScalef(0.2, 0.2, 1.0);
126 glRotatef(-90.0 * Xpos
[i
], 0, 0, 1);
128 glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, OccQuery
[i
]);
130 glVertex3f(-1, -1, 0);
131 glVertex3f( 1, -1, 0);
132 glVertex3f( 1, 1, 0);
133 glVertex3f(-1, 1, 0);
135 glEndQueryARB(GL_SAMPLES_PASSED_ARB
);
140 glColorMask(1, 1, 1, 1);
141 glDepthMask(GL_TRUE
);
143 /* Draw the rectangles now.
144 * Draw orange if result was ready
145 * Draw red if result was not ready.
147 for (i
= 0; i
< NUM_OCC
; i
++) {
151 glGetQueryObjectivARB(OccQuery
[i
], GL_QUERY_RESULT_AVAILABLE_ARB
, &ready
);
153 glGetQueryObjectuivARB(OccQuery
[i
], GL_QUERY_RESULT_ARB
, &passed
);
158 glColor3f(0.8, 0.5, 0);
160 if (!ready
|| passed
) {
162 glTranslatef(Xpos
[i
], Ypos
[i
], -0.5);
163 glScalef(0.2, 0.2, 1.0);
164 glRotatef(-90.0 * Xpos
[i
], 0, 0, 1);
167 glVertex3f(-1, -1, 0);
168 glVertex3f( 1, -1, 0);
169 glVertex3f( 1, 1, 0);
170 glVertex3f(-1, 1, 0);
178 glRasterPos3f(0.45, Ypos
[i
], 1.0);
179 sprintf(s
, "%4d", passed
);
188 static void Reshape( int width
, int height
)
190 glViewport( 0, 0, width
, height
);
194 static void Key( unsigned char key
, int x
, int y
)
200 glutDestroyWindow(Win
);
215 static void SpecialKey( int key
, int x
, int y
)
217 const GLfloat step
= 0.1;
223 for (i
= 0; i
< NUM_OCC
; i
++)
227 for (i
= 0; i
< NUM_OCC
; i
++)
235 static void Init( void )
237 const char *ext
= (const char *) glGetString(GL_EXTENSIONS
);
241 if (!strstr(ext
, "GL_ARB_occlusion_query")) {
242 printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
246 glGetQueryivARB(GL_SAMPLES_PASSED_ARB
, GL_QUERY_COUNTER_BITS_ARB
, &bits
);
248 printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
252 glGetIntegerv(GL_DEPTH_BITS
, &bits
);
253 printf("Depthbits: %d\n", bits
);
255 glGenQueriesARB(NUM_OCC
, OccQuery
);
257 glEnable(GL_DEPTH_TEST
);
259 for (i
= 0; i
< NUM_OCC
; i
++) {
260 float t
= (float) i
/ (NUM_OCC
- 1);
262 Ypos
[i
] = 4.0 * (t
- 0.5);
269 int main( int argc
, char *argv
[] )
271 glutInitWindowSize( 400, 400 );
272 glutInit( &argc
, argv
);
273 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
274 Win
= glutCreateWindow(argv
[0]);
276 glutReshapeFunc( Reshape
);
277 glutKeyboardFunc( Key
);
278 glutSpecialFunc( SpecialKey
);
283 glutDisplayFunc( Display
);