132001f49Smrg/* 232001f49Smrg * Test GL_NV_conditional_render 332001f49Smrg * 432001f49Smrg * Brian Paul 532001f49Smrg * 30 Dec 2009 632001f49Smrg * 732001f49Smrg * Copyright (C) 2009 VMware, Inc. 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 * THE AUTHORS 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 2732001f49Smrg#include <assert.h> 2832001f49Smrg#include <stdio.h> 2932001f49Smrg#include <stdlib.h> 3032001f49Smrg#include <string.h> 3132001f49Smrg#include <math.h> 3232001f49Smrg#include <GL/glew.h> 3332001f49Smrg#include "glut_wrap.h" 3432001f49Smrg 3532001f49Smrg#define TEST_DISPLAY_LISTS 0 3632001f49Smrg 3732001f49Smrgstatic GLboolean Anim = GL_TRUE; 3832001f49Smrgstatic GLfloat Xpos = 0; 3932001f49Smrgstatic GLuint OccQuery; 4032001f49Smrgstatic GLint Win = 0; 4132001f49Smrg 4232001f49Smrg 4332001f49Smrgstatic void Idle(void) 4432001f49Smrg{ 4532001f49Smrg static int lastTime = 0; 4632001f49Smrg static int sign = +1; 4732001f49Smrg int time = glutGet(GLUT_ELAPSED_TIME); 4832001f49Smrg float step; 4932001f49Smrg 5032001f49Smrg if (lastTime == 0) 5132001f49Smrg lastTime = time; 5232001f49Smrg else if (time - lastTime < 20) /* 50Hz update */ 5332001f49Smrg return; 5432001f49Smrg 5532001f49Smrg step = (time - lastTime) / 1000.0 * sign; 5632001f49Smrg lastTime = time; 5732001f49Smrg 5832001f49Smrg Xpos += step; 5932001f49Smrg 6032001f49Smrg if (Xpos > 2.5) { 6132001f49Smrg Xpos = 2.5; 6232001f49Smrg sign = -1; 6332001f49Smrg } 6432001f49Smrg else if (Xpos < -2.5) { 6532001f49Smrg Xpos = -2.5; 6632001f49Smrg sign = +1; 6732001f49Smrg } 6832001f49Smrg glutPostRedisplay(); 6932001f49Smrg} 7032001f49Smrg 7132001f49Smrg 7232001f49Smrgstatic void Display( void ) 7332001f49Smrg{ 7432001f49Smrg glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 7532001f49Smrg 7632001f49Smrg glEnable(GL_DEPTH_TEST); 7732001f49Smrg 7832001f49Smrg glMatrixMode( GL_PROJECTION ); 7932001f49Smrg glLoadIdentity(); 8032001f49Smrg glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 ); 8132001f49Smrg glMatrixMode( GL_MODELVIEW ); 8232001f49Smrg glLoadIdentity(); 8332001f49Smrg glTranslatef( 0.0, 0.0, -15.0 ); 8432001f49Smrg 8532001f49Smrg /* draw the occluding polygons */ 8632001f49Smrg glColor3f(0, 0.6, 0.8); 8732001f49Smrg glBegin(GL_QUADS); 8832001f49Smrg glVertex2f(-1.6, -1.5); 8932001f49Smrg glVertex2f(-0.4, -1.5); 9032001f49Smrg glVertex2f(-0.4, 1.5); 9132001f49Smrg glVertex2f(-1.6, 1.5); 9232001f49Smrg 9332001f49Smrg glVertex2f( 0.4, -1.5); 9432001f49Smrg glVertex2f( 1.6, -1.5); 9532001f49Smrg glVertex2f( 1.6, 1.5); 9632001f49Smrg glVertex2f( 0.4, 1.5); 9732001f49Smrg glEnd(); 9832001f49Smrg 9932001f49Smrg /* draw the test polygon with occlusion testing */ 10032001f49Smrg glPushMatrix(); 10132001f49Smrg glTranslatef(Xpos, 0, -0.5); 10232001f49Smrg glScalef(0.3, 0.3, 1.0); 10332001f49Smrg glRotatef(-90.0 * Xpos, 0, 0, 1); 10432001f49Smrg 10532001f49Smrg#if TEST_DISPLAY_LISTS 10632001f49Smrg glNewList(10, GL_COMPILE); 10732001f49Smrg glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery); 10832001f49Smrg glEndList(); 10932001f49Smrg glCallList(10); 11032001f49Smrg#else 11132001f49Smrg glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery); 11232001f49Smrg#endif 11332001f49Smrg 11432001f49Smrg glColorMask(0, 0, 0, 0); 11532001f49Smrg glDepthMask(GL_FALSE); 11632001f49Smrg 11732001f49Smrg glBegin(GL_POLYGON); 11832001f49Smrg glVertex3f(-1, -1, 0); 11932001f49Smrg glVertex3f( 1, -1, 0); 12032001f49Smrg glVertex3f( 1, 1, 0); 12132001f49Smrg glVertex3f(-1, 1, 0); 12232001f49Smrg glEnd(); 12332001f49Smrg 12432001f49Smrg#if TEST_DISPLAY_LISTS 12532001f49Smrg glNewList(11, GL_COMPILE); 12632001f49Smrg glEndQueryARB(GL_SAMPLES_PASSED_ARB); 12732001f49Smrg glEndList(); 12832001f49Smrg glCallList(11); 12932001f49Smrg#else 13032001f49Smrg glEndQueryARB(GL_SAMPLES_PASSED_ARB); 13132001f49Smrg#endif 13232001f49Smrg 13332001f49Smrg glColorMask(1, 1, 1, 1); 13432001f49Smrg glDepthMask(GL_TRUE); 13532001f49Smrg 13632001f49Smrg /* Note: disable depth test here so that we'll always see the orange 13732001f49Smrg * box, except when it's totally culled. 13832001f49Smrg */ 13932001f49Smrg glDisable(GL_DEPTH_TEST); 14032001f49Smrg 14132001f49Smrg glBeginConditionalRenderNV(OccQuery, GL_QUERY_WAIT_NV); 14232001f49Smrg /* draw the orange rect, so we can see what's going on */ 14332001f49Smrg glColor3f(0.8, 0.5, 0); 14432001f49Smrg glBegin(GL_POLYGON); 14532001f49Smrg glVertex3f(-1, -1, 0); 14632001f49Smrg glVertex3f( 1, -1, 0); 14732001f49Smrg glVertex3f( 1, 1, 0); 14832001f49Smrg glVertex3f(-1, 1, 0); 14932001f49Smrg glEnd(); 15032001f49Smrg glEndConditionalRenderNV(); 15132001f49Smrg 15232001f49Smrg /* always draw white outline around orange box */ 15332001f49Smrg glColor3f(1.0, 1.0, 1.0); 15432001f49Smrg glBegin(GL_LINE_LOOP); 15532001f49Smrg glVertex3f(-1, -1, 0); 15632001f49Smrg glVertex3f( 1, -1, 0); 15732001f49Smrg glVertex3f( 1, 1, 0); 15832001f49Smrg glVertex3f(-1, 1, 0); 15932001f49Smrg glEnd(); 16032001f49Smrg 16132001f49Smrg glPopMatrix(); 16232001f49Smrg 16332001f49Smrg glutSwapBuffers(); 16432001f49Smrg} 16532001f49Smrg 16632001f49Smrg 16732001f49Smrgstatic void Reshape( int width, int height ) 16832001f49Smrg{ 16932001f49Smrg glViewport( 0, 0, width, height ); 17032001f49Smrg} 17132001f49Smrg 17232001f49Smrg 17332001f49Smrgstatic void Key( unsigned char key, int x, int y ) 17432001f49Smrg{ 17532001f49Smrg (void) x; 17632001f49Smrg (void) y; 17732001f49Smrg switch (key) { 17832001f49Smrg case 27: 17932001f49Smrg glDeleteQueriesARB(1, &OccQuery); 18032001f49Smrg glutDestroyWindow(Win); 18132001f49Smrg exit(0); 18232001f49Smrg break; 18332001f49Smrg case ' ': 18432001f49Smrg Anim = !Anim; 18532001f49Smrg if (Anim) 18632001f49Smrg glutIdleFunc(Idle); 18732001f49Smrg else 18832001f49Smrg glutIdleFunc(NULL); 18932001f49Smrg break; 19032001f49Smrg } 19132001f49Smrg glutPostRedisplay(); 19232001f49Smrg} 19332001f49Smrg 19432001f49Smrg 19532001f49Smrgstatic void SpecialKey( int key, int x, int y ) 19632001f49Smrg{ 19732001f49Smrg const GLfloat step = 0.1; 19832001f49Smrg (void) x; 19932001f49Smrg (void) y; 20032001f49Smrg switch (key) { 20132001f49Smrg case GLUT_KEY_LEFT: 20232001f49Smrg Xpos -= step; 20332001f49Smrg break; 20432001f49Smrg case GLUT_KEY_RIGHT: 20532001f49Smrg Xpos += step; 20632001f49Smrg break; 20732001f49Smrg } 20832001f49Smrg glutPostRedisplay(); 20932001f49Smrg} 21032001f49Smrg 21132001f49Smrg 21232001f49Smrgstatic void Init( void ) 21332001f49Smrg{ 21432001f49Smrg if (!glutExtensionSupported("GL_ARB_occlusion_query") || 21532001f49Smrg !glutExtensionSupported("GL_NV_conditional_render")) { 21632001f49Smrg printf("Sorry, this demo requires the extensions:\n"); 21732001f49Smrg printf(" GL_ARB_occlusion_query\n"); 21832001f49Smrg printf(" GL_NV_conditional_render\n"); 21932001f49Smrg exit(-1); 22032001f49Smrg } 22132001f49Smrg 22232001f49Smrg glGenQueriesARB(1, &OccQuery); 22332001f49Smrg assert(OccQuery > 0); 22432001f49Smrg} 22532001f49Smrg 22632001f49Smrg 22732001f49Smrgint main( int argc, char *argv[] ) 22832001f49Smrg{ 22932001f49Smrg glutInitWindowSize( 400, 400 ); 23032001f49Smrg glutInit( &argc, argv ); 23132001f49Smrg glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 23232001f49Smrg Win = glutCreateWindow(argv[0]); 23332001f49Smrg glewInit(); 23432001f49Smrg glutReshapeFunc( Reshape ); 23532001f49Smrg glutKeyboardFunc( Key ); 23632001f49Smrg glutSpecialFunc( SpecialKey ); 23732001f49Smrg glutIdleFunc( Idle ); 23832001f49Smrg glutDisplayFunc( Display ); 23932001f49Smrg Init(); 24032001f49Smrg glutMainLoop(); 24132001f49Smrg return 0; 24232001f49Smrg} 243