condrender.c revision 32001f49
1/* 2 * Test GL_NV_conditional_render 3 * 4 * Brian Paul 5 * 30 Dec 2009 6 * 7 * Copyright (C) 2009 VMware, Inc. All Rights Reserved. 8 * 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: 15 * 16 * The above copyright notice and this permission notice shall be included 17 * in all copies or substantial portions of the Software. 18 * 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 * THE AUTHORS 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. 25 */ 26 27#include <assert.h> 28#include <stdio.h> 29#include <stdlib.h> 30#include <string.h> 31#include <math.h> 32#include <GL/glew.h> 33#include "glut_wrap.h" 34 35#define TEST_DISPLAY_LISTS 0 36 37static GLboolean Anim = GL_TRUE; 38static GLfloat Xpos = 0; 39static GLuint OccQuery; 40static GLint Win = 0; 41 42 43static void Idle(void) 44{ 45 static int lastTime = 0; 46 static int sign = +1; 47 int time = glutGet(GLUT_ELAPSED_TIME); 48 float step; 49 50 if (lastTime == 0) 51 lastTime = time; 52 else if (time - lastTime < 20) /* 50Hz update */ 53 return; 54 55 step = (time - lastTime) / 1000.0 * sign; 56 lastTime = time; 57 58 Xpos += step; 59 60 if (Xpos > 2.5) { 61 Xpos = 2.5; 62 sign = -1; 63 } 64 else if (Xpos < -2.5) { 65 Xpos = -2.5; 66 sign = +1; 67 } 68 glutPostRedisplay(); 69} 70 71 72static void Display( void ) 73{ 74 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 75 76 glEnable(GL_DEPTH_TEST); 77 78 glMatrixMode( GL_PROJECTION ); 79 glLoadIdentity(); 80 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 ); 81 glMatrixMode( GL_MODELVIEW ); 82 glLoadIdentity(); 83 glTranslatef( 0.0, 0.0, -15.0 ); 84 85 /* draw the occluding polygons */ 86 glColor3f(0, 0.6, 0.8); 87 glBegin(GL_QUADS); 88 glVertex2f(-1.6, -1.5); 89 glVertex2f(-0.4, -1.5); 90 glVertex2f(-0.4, 1.5); 91 glVertex2f(-1.6, 1.5); 92 93 glVertex2f( 0.4, -1.5); 94 glVertex2f( 1.6, -1.5); 95 glVertex2f( 1.6, 1.5); 96 glVertex2f( 0.4, 1.5); 97 glEnd(); 98 99 /* draw the test polygon with occlusion testing */ 100 glPushMatrix(); 101 glTranslatef(Xpos, 0, -0.5); 102 glScalef(0.3, 0.3, 1.0); 103 glRotatef(-90.0 * Xpos, 0, 0, 1); 104 105#if TEST_DISPLAY_LISTS 106 glNewList(10, GL_COMPILE); 107 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery); 108 glEndList(); 109 glCallList(10); 110#else 111 glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery); 112#endif 113 114 glColorMask(0, 0, 0, 0); 115 glDepthMask(GL_FALSE); 116 117 glBegin(GL_POLYGON); 118 glVertex3f(-1, -1, 0); 119 glVertex3f( 1, -1, 0); 120 glVertex3f( 1, 1, 0); 121 glVertex3f(-1, 1, 0); 122 glEnd(); 123 124#if TEST_DISPLAY_LISTS 125 glNewList(11, GL_COMPILE); 126 glEndQueryARB(GL_SAMPLES_PASSED_ARB); 127 glEndList(); 128 glCallList(11); 129#else 130 glEndQueryARB(GL_SAMPLES_PASSED_ARB); 131#endif 132 133 glColorMask(1, 1, 1, 1); 134 glDepthMask(GL_TRUE); 135 136 /* Note: disable depth test here so that we'll always see the orange 137 * box, except when it's totally culled. 138 */ 139 glDisable(GL_DEPTH_TEST); 140 141 glBeginConditionalRenderNV(OccQuery, GL_QUERY_WAIT_NV); 142 /* draw the orange rect, so we can see what's going on */ 143 glColor3f(0.8, 0.5, 0); 144 glBegin(GL_POLYGON); 145 glVertex3f(-1, -1, 0); 146 glVertex3f( 1, -1, 0); 147 glVertex3f( 1, 1, 0); 148 glVertex3f(-1, 1, 0); 149 glEnd(); 150 glEndConditionalRenderNV(); 151 152 /* always draw white outline around orange box */ 153 glColor3f(1.0, 1.0, 1.0); 154 glBegin(GL_LINE_LOOP); 155 glVertex3f(-1, -1, 0); 156 glVertex3f( 1, -1, 0); 157 glVertex3f( 1, 1, 0); 158 glVertex3f(-1, 1, 0); 159 glEnd(); 160 161 glPopMatrix(); 162 163 glutSwapBuffers(); 164} 165 166 167static void Reshape( int width, int height ) 168{ 169 glViewport( 0, 0, width, height ); 170} 171 172 173static void Key( unsigned char key, int x, int y ) 174{ 175 (void) x; 176 (void) y; 177 switch (key) { 178 case 27: 179 glDeleteQueriesARB(1, &OccQuery); 180 glutDestroyWindow(Win); 181 exit(0); 182 break; 183 case ' ': 184 Anim = !Anim; 185 if (Anim) 186 glutIdleFunc(Idle); 187 else 188 glutIdleFunc(NULL); 189 break; 190 } 191 glutPostRedisplay(); 192} 193 194 195static void SpecialKey( int key, int x, int y ) 196{ 197 const GLfloat step = 0.1; 198 (void) x; 199 (void) y; 200 switch (key) { 201 case GLUT_KEY_LEFT: 202 Xpos -= step; 203 break; 204 case GLUT_KEY_RIGHT: 205 Xpos += step; 206 break; 207 } 208 glutPostRedisplay(); 209} 210 211 212static void Init( void ) 213{ 214 if (!glutExtensionSupported("GL_ARB_occlusion_query") || 215 !glutExtensionSupported("GL_NV_conditional_render")) { 216 printf("Sorry, this demo requires the extensions:\n"); 217 printf(" GL_ARB_occlusion_query\n"); 218 printf(" GL_NV_conditional_render\n"); 219 exit(-1); 220 } 221 222 glGenQueriesARB(1, &OccQuery); 223 assert(OccQuery > 0); 224} 225 226 227int main( int argc, char *argv[] ) 228{ 229 glutInitWindowSize( 400, 400 ); 230 glutInit( &argc, argv ); 231 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); 232 Win = glutCreateWindow(argv[0]); 233 glewInit(); 234 glutReshapeFunc( Reshape ); 235 glutKeyboardFunc( Key ); 236 glutSpecialFunc( SpecialKey ); 237 glutIdleFunc( Idle ); 238 glutDisplayFunc( Display ); 239 Init(); 240 glutMainLoop(); 241 return 0; 242} 243