2 * Test GL_NV_conditional_render
7 * Copyright (C) 2009 VMware, Inc. 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 * 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.
35 #define TEST_DISPLAY_LISTS 0
37 static GLboolean Anim
= GL_TRUE
;
38 static GLfloat Xpos
= 0;
39 static GLuint OccQuery
;
43 static void Idle(void)
45 static int lastTime
= 0;
47 int time
= glutGet(GLUT_ELAPSED_TIME
);
52 else if (time
- lastTime
< 20) /* 50Hz update */
55 step
= (time
- lastTime
) / 1000.0 * sign
;
64 else if (Xpos
< -2.5) {
72 static void Display( void )
74 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
76 glEnable(GL_DEPTH_TEST
);
78 glMatrixMode( GL_PROJECTION
);
80 glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
81 glMatrixMode( GL_MODELVIEW
);
83 glTranslatef( 0.0, 0.0, -15.0 );
85 /* draw the occluding polygons */
86 glColor3f(0, 0.6, 0.8);
88 glVertex2f(-1.6, -1.5);
89 glVertex2f(-0.4, -1.5);
90 glVertex2f(-0.4, 1.5);
91 glVertex2f(-1.6, 1.5);
93 glVertex2f( 0.4, -1.5);
94 glVertex2f( 1.6, -1.5);
95 glVertex2f( 1.6, 1.5);
96 glVertex2f( 0.4, 1.5);
99 /* draw the test polygon with occlusion testing */
101 glTranslatef(Xpos
, 0, -0.5);
102 glScalef(0.3, 0.3, 1.0);
103 glRotatef(-90.0 * Xpos
, 0, 0, 1);
105 #if TEST_DISPLAY_LISTS
106 glNewList(10, GL_COMPILE
);
107 glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, OccQuery
);
111 glBeginQueryARB(GL_SAMPLES_PASSED_ARB
, OccQuery
);
114 glColorMask(0, 0, 0, 0);
115 glDepthMask(GL_FALSE
);
118 glVertex3f(-1, -1, 0);
119 glVertex3f( 1, -1, 0);
120 glVertex3f( 1, 1, 0);
121 glVertex3f(-1, 1, 0);
124 #if TEST_DISPLAY_LISTS
125 glNewList(11, GL_COMPILE
);
126 glEndQueryARB(GL_SAMPLES_PASSED_ARB
);
130 glEndQueryARB(GL_SAMPLES_PASSED_ARB
);
133 glColorMask(1, 1, 1, 1);
134 glDepthMask(GL_TRUE
);
136 /* Note: disable depth test here so that we'll always see the orange
137 * box, except when it's totally culled.
139 glDisable(GL_DEPTH_TEST
);
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);
145 glVertex3f(-1, -1, 0);
146 glVertex3f( 1, -1, 0);
147 glVertex3f( 1, 1, 0);
148 glVertex3f(-1, 1, 0);
150 glEndConditionalRenderNV();
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);
167 static void Reshape( int width
, int height
)
169 glViewport( 0, 0, width
, height
);
173 static void Key( unsigned char key
, int x
, int y
)
179 glDeleteQueriesARB(1, &OccQuery
);
180 glutDestroyWindow(Win
);
195 static void SpecialKey( int key
, int x
, int y
)
197 const GLfloat step
= 0.1;
212 static void Init( void )
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");
222 glGenQueriesARB(1, &OccQuery
);
223 assert(OccQuery
> 0);
227 int main( int argc
, char *argv
[] )
229 glutInitWindowSize( 400, 400 );
230 glutInit( &argc
, argv
);
231 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
232 Win
= glutCreateWindow(argv
[0]);
234 glutReshapeFunc( Reshape
);
235 glutKeyboardFunc( Key
);
236 glutSpecialFunc( SpecialKey
);
237 glutIdleFunc( Idle
);
238 glutDisplayFunc( Display
);