132001f49Smrg/* 232001f49Smrg * GL_ARB_occlusion_query(2) demo 332001f49Smrg * 432001f49Smrg * Brian Paul 532001f49Smrg * 12 June 2003 632001f49Smrg * 732001f49Smrg * Copyright (C) 2003 Brian Paul All Rights Reserved. 832001f49Smrg * 932001f49Smrg * Permission is hereby granted, free of charge, to any person obtaining a 1032001f49Smrg * copy of this software and associated documentation files (the "Software"), 1132001f49Smrg * to deal in the Software without restriction, including without limitation 1232001f49Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 1332001f49Smrg * and/or sell copies of the Software, and to permit persons to whom the 1432001f49Smrg * Software is furnished to do so, subject to the following conditions: 1532001f49Smrg * 1632001f49Smrg * The above copyright notice and this permission notice shall be included 1732001f49Smrg * in all copies or substantial portions of the Software. 1832001f49Smrg * 1932001f49Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 2032001f49Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2132001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2232001f49Smrg * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 2332001f49Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 2432001f49Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2532001f49Smrg * 2632001f49Smrg * Enhanced by Dave Airlie to add ARB_occlusion_query2 2732001f49Smrg */ 2832001f49Smrg 2932001f49Smrg#include <assert.h> 3032001f49Smrg#include <stdio.h> 3132001f49Smrg#include <stdlib.h> 3232001f49Smrg#include <string.h> 3332001f49Smrg#include <math.h> 3432001f49Smrg#include <GL/glew.h> 3532001f49Smrg#include "glut_wrap.h" 3632001f49Smrg 3732001f49Smrgstatic GLboolean Anim = GL_TRUE; 3832001f49Smrgstatic GLfloat Xpos = 0; 3932001f49Smrgstatic GLuint OccQuery1; 4032001f49Smrg#if defined(GL_ARB_occlusion_query2) 4132001f49Smrgstatic GLuint OccQuery2; 4232001f49Smrg#endif 4332001f49Smrgstatic GLint Win = 0; 4432001f49Smrgstatic GLboolean has_oq2 = GL_FALSE; 4532001f49Smrg 4632001f49Smrg 4732001f49Smrgstatic void 4832001f49SmrgPrintString(const char *s) 4932001f49Smrg{ 5032001f49Smrg while (*s) { 5132001f49Smrg glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s); 5232001f49Smrg s++; 5332001f49Smrg } 5432001f49Smrg} 5532001f49Smrg 5632001f49Smrg 5732001f49Smrg 5832001f49Smrgstatic void Idle(void) 5932001f49Smrg{ 6032001f49Smrg static int lastTime = 0; 6132001f49Smrg static int sign = +1; 6232001f49Smrg int time = glutGet(GLUT_ELAPSED_TIME); 6332001f49Smrg float step; 6432001f49Smrg 6532001f49Smrg if (lastTime == 0) 6632001f49Smrg lastTime = time; 6732001f49Smrg else if (time - lastTime < 20) /* 50Hz update */ 6832001f49Smrg return; 6932001f49Smrg 7032001f49Smrg step = (time - lastTime) / 1000.0 * sign; 7132001f49Smrg lastTime = time; 7232001f49Smrg 7332001f49Smrg Xpos += step; 7432001f49Smrg 7532001f49Smrg if (Xpos > 2.5) { 7632001f49Smrg Xpos = 2.5; 7732001f49Smrg sign = -1; 7832001f49Smrg } 7932001f49Smrg else if (Xpos < -2.5) { 8032001f49Smrg Xpos = -2.5; 8132001f49Smrg sign = +1; 8232001f49Smrg } 8332001f49Smrg glutPostRedisplay(); 8432001f49Smrg} 8532001f49Smrg 8632001f49Smrg 8732001f49Smrgstatic void Display( void ) 8832001f49Smrg{ 8932001f49Smrg GLuint passed1; 9032001f49Smrg#if defined(GL_ARB_occlusion_query2) 9132001f49Smrg GLuint passed2_boolean; 9232001f49Smrg#endif 9332001f49Smrg GLint ready; 9432001f49Smrg char s[100]; 9532001f49Smrg 9632001f49Smrg glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 9732001f49Smrg 9832001f49Smrg glMatrixMode( GL_PROJECTION ); 9932001f49Smrg glLoadIdentity(); 10032001f49Smrg glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 ); 10132001f49Smrg glMatrixMode( GL_MODELVIEW ); 10232001f49Smrg glLoadIdentity(); 10332001f49Smrg glTranslatef( 0.0, 0.0, -15.0 ); 10432001f49Smrg 10532001f49Smrg /* draw the occluding polygons */ 10632001f49Smrg glColor3f(0, 0.6, 0.8); 10732001f49Smrg glBegin(GL_QUADS); 10832001f49Smrg glVertex2f(-1.6, -1.5); 10932001f49Smrg glVertex2f(-0.4, -1.5); 11032001f49Smrg glVertex2f(-0.4, 1.5); 11132001f49Smrg glVertex2f(-1.6, 1.5); 11232001f49Smrg 11332001f49Smrg glVertex2f( 0.4, -1.5); 11432001f49Smrg glVertex2f( 1.6, -1.5); 11532001f49Smrg glVertex2f( 1.6, 1.5); 11632001f49Smrg glVertex2f( 0.4, 1.5); 11732001f49Smrg glEnd(); 11832001f49Smrg 11932001f49Smrg#if defined(GL_ARB_occlusion_query) 12032001f49Smrg /* disable all buffer updates */ 12132001f49Smrg glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 12232001f49Smrg glDepthMask(GL_FALSE); 12332001f49Smrg 12432001f49Smrg /* draw the first polygon with occlusion testing */ 12532001f49Smrg glPushMatrix(); 12632001f49Smrg glTranslatef(Xpos, 0.4, -0.5); 12732001f49Smrg glScalef(0.3, 0.3, 1.0); 12832001f49Smrg glRotatef(-90.0 * Xpos, 0, 0, 1); 12932001f49Smrg 13032001f49Smrg glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery1); 13132001f49Smrg 13232001f49Smrg glBegin(GL_POLYGON); 13332001f49Smrg glVertex3f(-1, -1, 0); 13432001f49Smrg glVertex3f( 1, -1, 0); 13532001f49Smrg glVertex3f( 1, 1, 0); 13632001f49Smrg glVertex3f(-1, 1, 0); 13732001f49Smrg glEnd(); 13832001f49Smrg 13932001f49Smrg glEndQueryARB(GL_SAMPLES_PASSED_ARB); 14032001f49Smrg glPopMatrix(); 14132001f49Smrg#endif 14232001f49Smrg 14332001f49Smrg /* draw the second polygon with occlusion testing */ 14432001f49Smrg glPushMatrix(); 14532001f49Smrg glTranslatef(Xpos, -0.4, -0.5); 14632001f49Smrg glScalef(0.3, 0.3, 1.0); 14732001f49Smrg#if defined(GL_ARB_occlusion_query2) 14832001f49Smrg if (has_oq2) { 14932001f49Smrg glBeginQueryARB(GL_ANY_SAMPLES_PASSED, OccQuery2); 15032001f49Smrg 15132001f49Smrg glBegin(GL_POLYGON); 15232001f49Smrg glVertex3f(-1, -1, 0); 15332001f49Smrg glVertex3f( 1, -1, 0); 15432001f49Smrg glVertex3f( 1, 1, 0); 15532001f49Smrg glVertex3f(-1, 1, 0); 15632001f49Smrg glEnd(); 15732001f49Smrg 15832001f49Smrg glEndQueryARB(GL_ANY_SAMPLES_PASSED); 15932001f49Smrg } 16032001f49Smrg#endif 16132001f49Smrg 16232001f49Smrg /* re-enable buffer updates */ 16332001f49Smrg glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 16432001f49Smrg glDepthMask(GL_TRUE); 16532001f49Smrg 16632001f49Smrg#if defined(GL_ARB_occlusion_query) 16732001f49Smrg do { 16832001f49Smrg /* do useful work here, if any */ 16932001f49Smrg glGetQueryObjectivARB(OccQuery1, GL_QUERY_RESULT_AVAILABLE_ARB, &ready); 17032001f49Smrg } while (!ready); 17132001f49Smrg glGetQueryObjectuivARB(OccQuery1, GL_QUERY_RESULT_ARB, &passed1); 17232001f49Smrg#endif 17332001f49Smrg#if defined(GL_ARB_occlusion_query2) 17432001f49Smrg if (has_oq2) { 17532001f49Smrg do { 17632001f49Smrg /* do useful work here, if any */ 17732001f49Smrg glGetQueryObjectivARB(OccQuery2, GL_QUERY_RESULT_AVAILABLE_ARB, &ready); 17832001f49Smrg } while (!ready); 17932001f49Smrg glGetQueryObjectuivARB(OccQuery2, GL_QUERY_RESULT_ARB, &passed2_boolean); 18032001f49Smrg } 18132001f49Smrg#endif /* GL_ARB_occlusion_query2 */ 18232001f49Smrg 18332001f49Smrg /* draw the second rect, so we can see what's going on */ 18432001f49Smrg glColor3f(0.8, 0.5, 0); 18532001f49Smrg if (has_oq2) { 18632001f49Smrg glBegin(GL_POLYGON); 18732001f49Smrg glVertex3f(-1, -1, 0); 18832001f49Smrg glVertex3f( 1, -1, 0); 18932001f49Smrg glVertex3f( 1, 1, 0); 19032001f49Smrg glVertex3f(-1, 1, 0); 19132001f49Smrg glEnd(); 19232001f49Smrg } 19332001f49Smrg glPopMatrix(); 19432001f49Smrg 19532001f49Smrg glPushMatrix(); 19632001f49Smrg glTranslatef(Xpos, 0.4, -0.5); 19732001f49Smrg glScalef(0.3, 0.3, 1.0); 19832001f49Smrg glRotatef(-90.0 * Xpos, 0, 0, 1); 19932001f49Smrg 20032001f49Smrg /* draw the first rect, so we can see what's going on */ 20132001f49Smrg glBegin(GL_POLYGON); 20232001f49Smrg glVertex3f(-1, -1, 0); 20332001f49Smrg glVertex3f( 1, -1, 0); 20432001f49Smrg glVertex3f( 1, 1, 0); 20532001f49Smrg glVertex3f(-1, 1, 0); 20632001f49Smrg glEnd(); 20732001f49Smrg glPopMatrix(); 20832001f49Smrg 20932001f49Smrg /* Print result message */ 21032001f49Smrg glMatrixMode( GL_PROJECTION ); 21132001f49Smrg glLoadIdentity(); 21232001f49Smrg glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 ); 21332001f49Smrg glMatrixMode( GL_MODELVIEW ); 21432001f49Smrg glLoadIdentity(); 21532001f49Smrg 21632001f49Smrg glColor3f(1, 1, 1); 21732001f49Smrg#if defined(GL_ARB_occlusion_query) 21832001f49Smrg sprintf(s, " %4d Fragments Visible", passed1); 21932001f49Smrg glRasterPos3f(-0.50, -0.6, 0); 22032001f49Smrg PrintString(s); 22132001f49Smrg if (!passed1) { 22232001f49Smrg glRasterPos3f(-0.25, -0.7, 0); 22332001f49Smrg PrintString("Fully Occluded"); 22432001f49Smrg } 22532001f49Smrg#if defined(GL_ARB_occlusion_query2) 22632001f49Smrg if (has_oq2) { 22732001f49Smrg glRasterPos3f(-0.25, -0.9, 0); 22832001f49Smrg if (!passed2_boolean) { 22932001f49Smrg PrintString("Fully Occluded"); 23032001f49Smrg } else { 23132001f49Smrg PrintString("Partly Visible"); 23232001f49Smrg } 23332001f49Smrg } 23432001f49Smrg#endif 23532001f49Smrg#else 23632001f49Smrg glRasterPos3f(-0.25, -0.8, 0); 23732001f49Smrg PrintString("GL_ARB_occlusion_query not available at compile time"); 23832001f49Smrg#endif /* GL_ARB_occlusion_query */ 23932001f49Smrg 24032001f49Smrg glutSwapBuffers(); 24132001f49Smrg} 24232001f49Smrg 24332001f49Smrg 24432001f49Smrgstatic void Reshape( int width, int height ) 24532001f49Smrg{ 24632001f49Smrg glViewport( 0, 0, width, height ); 24732001f49Smrg} 24832001f49Smrg 24932001f49Smrg 25032001f49Smrgstatic void Key( unsigned char key, int x, int y ) 25132001f49Smrg{ 25232001f49Smrg (void) x; 25332001f49Smrg (void) y; 25432001f49Smrg switch (key) { 25532001f49Smrg case 27: 25632001f49Smrg glutDestroyWindow(Win); 25732001f49Smrg exit(0); 25832001f49Smrg break; 25932001f49Smrg case ' ': 26032001f49Smrg Anim = !Anim; 26132001f49Smrg if (Anim) 26232001f49Smrg glutIdleFunc(Idle); 26332001f49Smrg else 26432001f49Smrg glutIdleFunc(NULL); 26532001f49Smrg break; 26632001f49Smrg } 26732001f49Smrg glutPostRedisplay(); 26832001f49Smrg} 26932001f49Smrg 27032001f49Smrg 27132001f49Smrgstatic void SpecialKey( int key, int x, int y ) 27232001f49Smrg{ 27332001f49Smrg const GLfloat step = 0.1; 27432001f49Smrg (void) x; 27532001f49Smrg (void) y; 27632001f49Smrg switch (key) { 27732001f49Smrg case GLUT_KEY_LEFT: 27832001f49Smrg Xpos -= step; 27932001f49Smrg break; 28032001f49Smrg case GLUT_KEY_RIGHT: 28132001f49Smrg Xpos += step; 28232001f49Smrg break; 28332001f49Smrg } 28432001f49Smrg glutPostRedisplay(); 28532001f49Smrg} 28632001f49Smrg 28732001f49Smrg 28832001f49Smrgstatic void Init( void ) 28932001f49Smrg{ 29032001f49Smrg const char *ext = (const char *) glGetString(GL_EXTENSIONS); 29132001f49Smrg GLint bits; 29232001f49Smrg 29332001f49Smrg if (!strstr(ext, "GL_ARB_occlusion_query")) { 29432001f49Smrg printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n"); 29532001f49Smrg exit(-1); 29632001f49Smrg } 29732001f49Smrg 29832001f49Smrg if (strstr(ext, "GL_ARB_occlusion_query2")) { 29932001f49Smrg has_oq2 = GL_TRUE; 30032001f49Smrg printf("OQ2 supported\n"); 30132001f49Smrg } 30232001f49Smrg#if defined(GL_ARB_occlusion_query) 30332001f49Smrg glGetQueryivARB(GL_SAMPLES_PASSED_ARB, GL_QUERY_COUNTER_BITS_ARB, &bits); 30432001f49Smrg if (!bits) { 30532001f49Smrg printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n"); 30632001f49Smrg exit(-1); 30732001f49Smrg } 30832001f49Smrg#endif /* GL_ARB_occlusion_query */ 30932001f49Smrg 31032001f49Smrg glGetIntegerv(GL_DEPTH_BITS, &bits); 31132001f49Smrg printf("Depthbits: %d\n", bits); 31232001f49Smrg 31332001f49Smrg#if defined(GL_ARB_occlusion_query) 31432001f49Smrg glGenQueriesARB(1, &OccQuery1); 31532001f49Smrg assert(OccQuery1 > 0); 31632001f49Smrg#if defined(GL_ARB_occlusion_query2) 31732001f49Smrg if (has_oq2) { 31832001f49Smrg glGenQueriesARB(1, &OccQuery2); 31932001f49Smrg assert(OccQuery2 > 0); 32032001f49Smrg } 32132001f49Smrg#endif 32232001f49Smrg#endif /* GL_ARB_occlusion_query */ 32332001f49Smrg 32432001f49Smrg glEnable(GL_DEPTH_TEST); 32532001f49Smrg} 32632001f49Smrg 32732001f49Smrg 32832001f49Smrgint main( int argc, char *argv[] ) 32932001f49Smrg{ 33032001f49Smrg glutInitWindowSize( 400, 400 ); 33132001f49Smrg glutInit( &argc, argv ); 33232001f49Smrg glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 33332001f49Smrg Win = glutCreateWindow(argv[0]); 33432001f49Smrg glewInit(); 33532001f49Smrg glutReshapeFunc( Reshape ); 33632001f49Smrg glutKeyboardFunc( Key ); 33732001f49Smrg glutSpecialFunc( SpecialKey ); 33832001f49Smrg glutIdleFunc( Idle ); 33932001f49Smrg glutDisplayFunc( Display ); 34032001f49Smrg Init(); 34132001f49Smrg glutMainLoop(); 34232001f49Smrg return 0; 34332001f49Smrg} 344