2003-06-13 02:38:35 +00:00
|
|
|
/*
|
2003-06-13 02:42:57 +00:00
|
|
|
* GL_ARB_occlusion_query demo
|
2003-06-13 02:38:35 +00:00
|
|
|
*
|
|
|
|
* Brian Paul
|
|
|
|
* 12 June 2003
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Brian Paul All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
2009-01-24 16:39:49 +00:00
|
|
|
#include <GL/glew.h>
|
2003-06-13 02:38:35 +00:00
|
|
|
#include <GL/glut.h>
|
|
|
|
|
2010-02-18 16:54:14 -07:00
|
|
|
#define NUM_OCC 10
|
2003-06-13 02:38:35 +00:00
|
|
|
|
2003-06-13 02:42:57 +00:00
|
|
|
static GLboolean Anim = GL_TRUE;
|
2010-02-18 16:54:14 -07:00
|
|
|
static GLfloat Xpos[NUM_OCC], Ypos[NUM_OCC];
|
|
|
|
static GLfloat Sign[NUM_OCC];
|
|
|
|
static GLuint OccQuery[NUM_OCC];
|
2008-12-18 09:48:20 -07:00
|
|
|
static GLint Win = 0;
|
2003-06-13 02:38:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
PrintString(const char *s)
|
|
|
|
{
|
|
|
|
while (*s) {
|
|
|
|
glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Idle(void)
|
|
|
|
{
|
|
|
|
static int lastTime = 0;
|
|
|
|
int time = glutGet(GLUT_ELAPSED_TIME);
|
|
|
|
float step;
|
2010-02-18 16:54:14 -07:00
|
|
|
int i;
|
2003-06-13 02:38:35 +00:00
|
|
|
|
|
|
|
if (lastTime == 0)
|
|
|
|
lastTime = time;
|
|
|
|
else if (time - lastTime < 20) /* 50Hz update */
|
|
|
|
return;
|
|
|
|
|
2010-02-18 16:54:14 -07:00
|
|
|
for (i = 0; i < NUM_OCC; i++) {
|
2003-06-13 02:38:35 +00:00
|
|
|
|
2010-02-18 16:54:14 -07:00
|
|
|
step = (time - lastTime) / 1000.0 * Sign[i];
|
|
|
|
|
|
|
|
Xpos[i] += step;
|
|
|
|
|
|
|
|
if (Xpos[i] > 2.5) {
|
|
|
|
Xpos[i] = 2.5;
|
|
|
|
Sign[i] = -1;
|
|
|
|
}
|
|
|
|
else if (Xpos[i] < -2.5) {
|
|
|
|
Xpos[i] = -2.5;
|
|
|
|
Sign[i] = +1;
|
|
|
|
}
|
2003-06-13 02:38:35 +00:00
|
|
|
|
|
|
|
}
|
2010-02-18 16:54:14 -07:00
|
|
|
|
|
|
|
lastTime = time;
|
|
|
|
|
2003-06-13 02:38:35 +00:00
|
|
|
glutPostRedisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Display( void )
|
|
|
|
{
|
2010-02-18 16:54:14 -07:00
|
|
|
int i;
|
2003-06-13 02:38:35 +00:00
|
|
|
|
2010-02-18 16:54:14 -07:00
|
|
|
glClearColor(0.25, 0.25, 0.25, 0.0);
|
2003-06-13 02:38:35 +00:00
|
|
|
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
|
|
|
|
|
|
|
glMatrixMode( GL_PROJECTION );
|
|
|
|
glLoadIdentity();
|
|
|
|
glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
|
|
|
|
glMatrixMode( GL_MODELVIEW );
|
|
|
|
glLoadIdentity();
|
|
|
|
glTranslatef( 0.0, 0.0, -15.0 );
|
|
|
|
|
|
|
|
/* draw the occluding polygons */
|
2010-02-18 16:54:14 -07:00
|
|
|
glColor3f(0, 0.4, 0.6);
|
2003-06-13 02:38:35 +00:00
|
|
|
glBegin(GL_QUADS);
|
2010-02-18 16:54:14 -07:00
|
|
|
glVertex2f(-1.6, -2.5);
|
|
|
|
glVertex2f(-0.4, -2.5);
|
|
|
|
glVertex2f(-0.4, 2.5);
|
|
|
|
glVertex2f(-1.6, 2.5);
|
|
|
|
glVertex2f( 0.4, -2.5);
|
|
|
|
glVertex2f( 1.6, -2.5);
|
|
|
|
glVertex2f( 1.6, 2.5);
|
|
|
|
glVertex2f( 0.4, 2.5);
|
2003-06-13 02:38:35 +00:00
|
|
|
glEnd();
|
|
|
|
|
|
|
|
|
|
|
|
glColorMask(0, 0, 0, 0);
|
|
|
|
glDepthMask(GL_FALSE);
|
|
|
|
|
2010-02-18 16:54:14 -07:00
|
|
|
/* draw the test polygons with occlusion testing */
|
|
|
|
for (i = 0; i < NUM_OCC; i++) {
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(Xpos[i], Ypos[i], -0.5);
|
|
|
|
glScalef(0.2, 0.2, 1.0);
|
|
|
|
glRotatef(-90.0 * Xpos[i], 0, 0, 1);
|
|
|
|
|
|
|
|
glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery[i]);
|
|
|
|
glBegin(GL_POLYGON);
|
|
|
|
glVertex3f(-1, -1, 0);
|
|
|
|
glVertex3f( 1, -1, 0);
|
|
|
|
glVertex3f( 1, 1, 0);
|
|
|
|
glVertex3f(-1, 1, 0);
|
|
|
|
glEnd();
|
|
|
|
glEndQueryARB(GL_SAMPLES_PASSED_ARB);
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
2003-06-13 02:38:35 +00:00
|
|
|
|
|
|
|
glColorMask(1, 1, 1, 1);
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
|
2010-02-18 16:54:14 -07:00
|
|
|
/* Draw the rectangles now.
|
|
|
|
* Draw orange if result was ready
|
|
|
|
* Draw red if result was not ready.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < NUM_OCC; i++) {
|
|
|
|
GLuint passed;
|
|
|
|
GLint ready;
|
|
|
|
|
|
|
|
glGetQueryObjectivARB(OccQuery[i], GL_QUERY_RESULT_AVAILABLE_ARB, &ready);
|
|
|
|
|
|
|
|
glGetQueryObjectuivARB(OccQuery[i], GL_QUERY_RESULT_ARB, &passed);
|
|
|
|
|
|
|
|
if (!ready)
|
|
|
|
glColor3f(1, 0, 0);
|
|
|
|
else
|
|
|
|
glColor3f(0.8, 0.5, 0);
|
|
|
|
|
|
|
|
if (!ready || passed) {
|
|
|
|
glPushMatrix();
|
|
|
|
glTranslatef(Xpos[i], Ypos[i], -0.5);
|
|
|
|
glScalef(0.2, 0.2, 1.0);
|
|
|
|
glRotatef(-90.0 * Xpos[i], 0, 0, 1);
|
|
|
|
|
|
|
|
glBegin(GL_POLYGON);
|
|
|
|
glVertex3f(-1, -1, 0);
|
|
|
|
glVertex3f( 1, -1, 0);
|
|
|
|
glVertex3f( 1, 1, 0);
|
|
|
|
glVertex3f(-1, 1, 0);
|
|
|
|
glEnd();
|
|
|
|
|
|
|
|
glPopMatrix();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char s[10];
|
|
|
|
glRasterPos3f(0.45, Ypos[i], 1.0);
|
|
|
|
sprintf(s, "%4d", passed);
|
|
|
|
PrintString(s);
|
|
|
|
}
|
2003-06-13 02:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glutSwapBuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Reshape( int width, int height )
|
|
|
|
{
|
|
|
|
glViewport( 0, 0, width, height );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Key( unsigned char key, int x, int y )
|
|
|
|
{
|
|
|
|
(void) x;
|
|
|
|
(void) y;
|
|
|
|
switch (key) {
|
|
|
|
case 27:
|
2008-12-18 09:48:20 -07:00
|
|
|
glutDestroyWindow(Win);
|
2003-06-13 02:38:35 +00:00
|
|
|
exit(0);
|
|
|
|
break;
|
2003-06-13 02:42:57 +00:00
|
|
|
case ' ':
|
|
|
|
Anim = !Anim;
|
|
|
|
if (Anim)
|
|
|
|
glutIdleFunc(Idle);
|
|
|
|
else
|
|
|
|
glutIdleFunc(NULL);
|
|
|
|
break;
|
2003-06-13 02:38:35 +00:00
|
|
|
}
|
|
|
|
glutPostRedisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void SpecialKey( int key, int x, int y )
|
|
|
|
{
|
|
|
|
const GLfloat step = 0.1;
|
2010-02-18 16:54:14 -07:00
|
|
|
int i;
|
2003-06-13 02:38:35 +00:00
|
|
|
(void) x;
|
|
|
|
(void) y;
|
|
|
|
switch (key) {
|
|
|
|
case GLUT_KEY_LEFT:
|
2010-02-18 16:54:14 -07:00
|
|
|
for (i = 0; i < NUM_OCC; i++)
|
|
|
|
Xpos[i] -= step;
|
2003-06-13 02:38:35 +00:00
|
|
|
break;
|
|
|
|
case GLUT_KEY_RIGHT:
|
2010-02-18 16:54:14 -07:00
|
|
|
for (i = 0; i < NUM_OCC; i++)
|
|
|
|
Xpos[i] += step;
|
2003-06-13 02:38:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
glutPostRedisplay();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void Init( void )
|
|
|
|
{
|
|
|
|
const char *ext = (const char *) glGetString(GL_EXTENSIONS);
|
|
|
|
GLint bits;
|
2010-02-18 16:54:14 -07:00
|
|
|
int i;
|
2003-06-13 02:38:35 +00:00
|
|
|
|
|
|
|
if (!strstr(ext, "GL_ARB_occlusion_query")) {
|
|
|
|
printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
glGetQueryivARB(GL_SAMPLES_PASSED_ARB, GL_QUERY_COUNTER_BITS_ARB, &bits);
|
|
|
|
if (!bits) {
|
|
|
|
printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2004-06-28 22:03:44 +00:00
|
|
|
glGetIntegerv(GL_DEPTH_BITS, &bits);
|
|
|
|
printf("Depthbits: %d\n", bits);
|
|
|
|
|
2010-02-18 16:54:14 -07:00
|
|
|
glGenQueriesARB(NUM_OCC, OccQuery);
|
2003-06-13 02:38:35 +00:00
|
|
|
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
2010-02-18 16:54:14 -07:00
|
|
|
|
|
|
|
for (i = 0; i < NUM_OCC; i++) {
|
|
|
|
float t = (float) i / (NUM_OCC - 1);
|
|
|
|
Xpos[i] = 2.5 * t;
|
|
|
|
Ypos[i] = 4.0 * (t - 0.5);
|
|
|
|
Sign[i] = 1.0;
|
|
|
|
}
|
|
|
|
|
2003-06-13 02:38:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
glutInitWindowSize( 400, 400 );
|
2009-12-18 08:12:55 -07:00
|
|
|
glutInit( &argc, argv );
|
2003-06-13 02:38:35 +00:00
|
|
|
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
|
2008-12-18 09:48:20 -07:00
|
|
|
Win = glutCreateWindow(argv[0]);
|
2009-01-24 16:39:49 +00:00
|
|
|
glewInit();
|
2003-06-13 02:38:35 +00:00
|
|
|
glutReshapeFunc( Reshape );
|
|
|
|
glutKeyboardFunc( Key );
|
|
|
|
glutSpecialFunc( SpecialKey );
|
2010-02-18 16:54:14 -07:00
|
|
|
if (Anim)
|
|
|
|
glutIdleFunc(Idle);
|
|
|
|
else
|
|
|
|
glutIdleFunc(NULL);
|
2003-06-13 02:38:35 +00:00
|
|
|
glutDisplayFunc( Display );
|
|
|
|
Init();
|
|
|
|
glutMainLoop();
|
|
|
|
return 0;
|
|
|
|
}
|