2 * (C) Copyright IBM Corporation 2004
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * \file stencil_twoside.c
28 * Simple test of GL_ATI_separate_stencil (or the OGL 2.0 equivalent) functionality.
29 * Five squares (or six if stencil wrap is available) are drawn
30 * with different stencil modes, but all should be rendered with the same
39 static int use20syntax
= 1;
40 static int Width
= 650;
41 static int Height
= 200;
42 static const GLfloat Near
= 5.0, Far
= 25.0;
45 static PFNGLSTENCILFUNCSEPARATEPROC stencil_func_separate
= NULL
;
46 static PFNGLSTENCILFUNCSEPARATEATIPROC stencil_func_separate_ati
= NULL
;
47 static PFNGLSTENCILOPSEPARATEPROC stencil_op_separate
= NULL
;
49 static void Display( void )
56 glGetIntegerv( GL_STENCIL_BITS
, & stencil_bits
);
57 max_stencil
= (1U << stencil_bits
) - 1;
58 printf( "Stencil bits = %u, maximum stencil value = 0x%08x\n",
59 stencil_bits
, max_stencil
);
62 glClearColor( 0.2, 0.2, 0.8, 0 );
63 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
64 | GL_STENCIL_BUFFER_BIT
);
69 /* This is the "reference" square.
72 glDisable(GL_STENCIL_TEST
);
73 glTranslatef(-7.0, 0, 0);
75 glColor3f( 0.5, 0.5, 0.5 );
83 glEnable(GL_STENCIL_TEST
);
85 /* Draw the first two squares using incr for the affected face
88 /*************************************************************************
92 stencil_func_separate(GL_FRONT
, GL_ALWAYS
, 0, ~0);
93 stencil_func_separate(GL_BACK
, GL_ALWAYS
, 0, ~0);
96 stencil_func_separate_ati(GL_ALWAYS
, GL_ALWAYS
, 0, ~0);
98 stencil_op_separate(GL_FRONT
, GL_KEEP
, GL_KEEP
, GL_INCR
);
99 stencil_op_separate(GL_BACK
, GL_KEEP
, GL_KEEP
, GL_DECR
);
101 glTranslatef(3.0, 0, 0);
103 glColor3f( 0.9, 0.9, 0.9 );
104 for ( i
= 0 ; i
< (max_stencil
+ 5) ; i
++ ) {
105 /* this should be front facing */
113 /* stencil vals should be equal to max_stencil */
114 glStencilFunc(GL_EQUAL
, max_stencil
, ~0);
116 glColor3f( 0.5, 0.5, 0.5 );
123 /*************************************************************************
127 stencil_func_separate(GL_FRONT
, GL_ALWAYS
, 0, ~0);
128 stencil_func_separate(GL_BACK
, GL_ALWAYS
, 0, ~0);
131 stencil_func_separate_ati(GL_ALWAYS
, GL_ALWAYS
, 0, ~0);
133 stencil_op_separate(GL_FRONT
, GL_KEEP
, GL_KEEP
, GL_DECR
);
134 stencil_op_separate(GL_BACK
, GL_KEEP
, GL_KEEP
, GL_INCR
);
136 glTranslatef(3.0, 0, 0);
138 glColor3f( 0.9, 0.9, 0.9 );
139 for ( i
= 0 ; i
< (max_stencil
+ 5) ; i
++ ) {
140 /* this should be back facing */
148 /* stencil vals should be equal to max_stencil */
149 glStencilFunc(GL_EQUAL
, max_stencil
, ~0);
151 glColor3f( 0.5, 0.5, 0.5 );
158 /*************************************************************************
162 stencil_func_separate(GL_FRONT
, GL_NEVER
, 0, ~0);
163 stencil_func_separate(GL_BACK
, GL_ALWAYS
, 0, ~0);
166 stencil_func_separate_ati(GL_NEVER
, GL_ALWAYS
, 0, ~0);
168 stencil_op_separate(GL_FRONT
, GL_KEEP
, GL_KEEP
, GL_DECR
);
169 stencil_op_separate(GL_BACK
, GL_KEEP
, GL_KEEP
, GL_INCR
);
171 glTranslatef(3.0, 0, 0);
173 glColor3f( 0.9, 0.9, 0.9 );
174 for ( i
= 0 ; i
< (max_stencil
+ 5) ; i
++ ) {
175 /* this should be back facing */
180 /* this should be front facing */
188 /* stencil vals should be equal to max_stencil */
189 glStencilFunc(GL_EQUAL
, max_stencil
, ~0);
191 glColor3f( 0.5, 0.5, 0.5 );
198 /*************************************************************************
202 stencil_func_separate(GL_FRONT
, GL_ALWAYS
, 0, ~0);
203 stencil_func_separate(GL_BACK
, GL_ALWAYS
, 0, ~0);
206 stencil_func_separate_ati(GL_ALWAYS
, GL_ALWAYS
, 0, ~0);
208 stencil_op_separate(GL_FRONT
, GL_KEEP
, GL_KEEP
, GL_INCR
);
209 stencil_op_separate(GL_BACK
, GL_KEEP
, GL_KEEP
, GL_DECR
);
211 glTranslatef(3.0, 0, 0);
213 glColor3f( 0.9, 0.9, 0.9 );
214 for ( i
= 0 ; i
< (max_stencil
+ 5) ; i
++ ) {
215 /* this should be back facing */
220 /* this should be front facing */
228 glStencilFunc(GL_EQUAL
, 1, ~0);
230 glColor3f( 0.5, 0.5, 0.5 );
237 /*************************************************************************
240 if (glutExtensionSupported("GL_EXT_stencil_wrap")) {
242 stencil_func_separate(GL_FRONT
, GL_ALWAYS
, 0, ~0);
243 stencil_func_separate(GL_BACK
, GL_ALWAYS
, 0, ~0);
246 stencil_func_separate_ati(GL_ALWAYS
, GL_ALWAYS
, 0, ~0);
248 stencil_op_separate(GL_FRONT
, GL_KEEP
, GL_KEEP
, GL_KEEP
);
249 stencil_op_separate(GL_BACK
, GL_KEEP
, GL_KEEP
, GL_INCR_WRAP
);
251 glTranslatef(3.0, 0, 0);
253 glColor3f( 0.9, 0.9, 0.9 );
254 for ( i
= 0 ; i
< (max_stencil
+ 5) ; i
++ ) {
255 /* this should be back facing */
260 /* this should be front facing */
268 glStencilFunc(GL_EQUAL
, 260 - 255, ~0);
270 glColor3f( 0.5, 0.5, 0.5 );
284 static void Reshape( int width
, int height
)
286 GLfloat ar
= (float) width
/ (float) height
;
289 glViewport( 0, 0, width
, height
);
290 glMatrixMode( GL_PROJECTION
);
292 glFrustum( -ar
, ar
, -1.0, 1.0, Near
, Far
);
293 glMatrixMode( GL_MODELVIEW
);
295 glTranslatef( 0.0, 0.0, -15.0 );
299 static void Key( unsigned char key
, int x
, int y
)
312 static void Init( void )
314 const char * const ver_string
= (const char * const)
315 glGetString( GL_VERSION
);
317 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
318 printf("GL_VERSION = %s\n", ver_string
);
320 if ( !glutExtensionSupported("GL_ATI_separate_stencil")
321 && (atof( ver_string
) < 2.0) ) {
322 printf("Sorry, this program requires either GL_ATI_separate_stencil or OpenGL 2.0.\n");
325 if (atof( ver_string
) < 2.0) {
328 stencil_func_separate
= (PFNGLSTENCILFUNCSEPARATEPROC
) glutGetProcAddress( "glStencilFuncSeparate" );
329 stencil_func_separate_ati
= (PFNGLSTENCILFUNCSEPARATEATIPROC
) glutGetProcAddress( "glStencilFuncSeparateATI" );
330 stencil_op_separate
= (PFNGLSTENCILOPSEPARATEPROC
) glutGetProcAddress( "glStencilOpSeparate" );
332 printf("\nAll 5 (or 6) squares should be the same color.\n");
336 int main( int argc
, char *argv
[] )
338 glutInit( &argc
, argv
);
339 glutInitWindowPosition( 0, 0 );
340 glutInitWindowSize( Width
, Height
);
341 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
| GLUT_STENCIL
);
342 glutCreateWindow( "GL_ATI_separate_stencil test" );
344 glutReshapeFunc( Reshape
);
345 glutKeyboardFunc( Key
);
346 glutDisplayFunc( Display
);