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 * Four squares are drawn
30 * with different stencil modes, but all should be rendered with the same
38 static int use20syntax
= 1;
39 static int Width
= 550;
40 static int Height
= 200;
41 static const GLfloat Near
= 5.0, Far
= 25.0;
44 static PFNGLSTENCILFUNCSEPARATEPROC stencil_func_separate
= NULL
;
45 static PFNGLSTENCILFUNCSEPARATEATIPROC stencil_func_separate_ati
= NULL
;
46 static PFNGLSTENCILOPSEPARATEPROC stencil_op_separate
= NULL
;
48 static void Display( void )
55 glGetIntegerv( GL_STENCIL_BITS
, & stencil_bits
);
56 max_stencil
= (1U << stencil_bits
) - 1;
57 printf( "Stencil bits = %u, maximum stencil value = 0x%08x\n",
58 stencil_bits
, max_stencil
);
61 glClearColor( 0.2, 0.2, 0.8, 0 );
62 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
63 | GL_STENCIL_BUFFER_BIT
);
68 /* This is the "reference" square.
71 glDisable(GL_STENCIL_TEST
);
72 glTranslatef(-6.0, 0, 0);
74 glColor3f( 0.5, 0.5, 0.5 );
82 glEnable(GL_STENCIL_TEST
);
84 /* Draw the first two squares using incr for the affected face
88 stencil_func_separate(GL_FRONT
, GL_ALWAYS
, 0, ~0);
89 stencil_func_separate(GL_BACK
, GL_ALWAYS
, 0, ~0);
92 stencil_func_separate_ati(GL_ALWAYS
, GL_ALWAYS
, 0, ~0);
94 stencil_op_separate(GL_FRONT
, GL_KEEP
, GL_KEEP
, GL_INCR
);
95 stencil_op_separate(GL_BACK
, GL_KEEP
, GL_KEEP
, GL_DECR
);
97 glTranslatef(3.0, 0, 0);
99 glColor3f( 0.9, 0.9, 0.9 );
100 /* this should be front facing */
101 for ( i
= 0 ; i
< (max_stencil
+ 5) ; i
++ ) {
109 glStencilFunc(GL_EQUAL
, max_stencil
, ~0);
111 glColor3f( 0.5, 0.5, 0.5 );
120 stencil_func_separate(GL_FRONT
, GL_ALWAYS
, 0, ~0);
121 stencil_func_separate(GL_BACK
, GL_ALWAYS
, 0, ~0);
124 stencil_func_separate_ati(GL_ALWAYS
, GL_ALWAYS
, 0, ~0);
126 stencil_op_separate(GL_FRONT
, GL_KEEP
, GL_KEEP
, GL_DECR
);
127 stencil_op_separate(GL_BACK
, GL_KEEP
, GL_KEEP
, GL_INCR
);
129 glTranslatef(3.0, 0, 0);
131 glColor3f( 0.9, 0.9, 0.9 );
133 /* this should be back facing */
134 for ( i
= 0 ; i
< (max_stencil
+ 5) ; i
++ ) {
142 glStencilFunc(GL_EQUAL
, max_stencil
, ~0);
144 glColor3f( 0.5, 0.5, 0.5 );
152 stencil_func_separate(GL_FRONT
, GL_NEVER
, 0, ~0);
153 stencil_func_separate(GL_BACK
, GL_ALWAYS
, 0, ~0);
156 stencil_func_separate_ati(GL_NEVER
, GL_ALWAYS
, 0, ~0);
158 stencil_op_separate(GL_FRONT
, GL_KEEP
, GL_KEEP
, GL_DECR
);
159 stencil_op_separate(GL_BACK
, GL_KEEP
, GL_KEEP
, GL_INCR
);
161 glTranslatef(3.0, 0, 0);
163 glColor3f( 0.9, 0.9, 0.9 );
165 /* this should be back facing */
166 for ( i
= 0 ; i
< (max_stencil
+ 5) ; i
++ ) {
167 /* this should be back facing */
172 /* this should be front facing */
180 glStencilFunc(GL_EQUAL
, max_stencil
, ~0);
182 glColor3f( 0.5, 0.5, 0.5 );
190 stencil_func_separate(GL_FRONT
, GL_ALWAYS
, 0, ~0);
191 stencil_func_separate(GL_BACK
, GL_ALWAYS
, 0, ~0);
194 stencil_func_separate_ati(GL_ALWAYS
, GL_ALWAYS
, 0, ~0);
196 stencil_op_separate(GL_FRONT
, GL_KEEP
, GL_KEEP
, GL_DECR
);
197 stencil_op_separate(GL_BACK
, GL_KEEP
, GL_KEEP
, GL_INCR
);
199 glTranslatef(3.0, 0, 0);
201 glColor3f( 0.9, 0.9, 0.9 );
203 /* this should be back facing */
204 for ( i
= 0 ; i
< (max_stencil
+ 5) ; i
++ ) {
205 /* this should be back facing */
210 /* this should be front facing */
218 glStencilFunc(GL_EQUAL
, 1, ~0);
220 glColor3f( 0.5, 0.5, 0.5 );
233 static void Reshape( int width
, int height
)
235 GLfloat ar
= (float) width
/ (float) height
;
238 glViewport( 0, 0, width
, height
);
239 glMatrixMode( GL_PROJECTION
);
241 glFrustum( -ar
, ar
, -1.0, 1.0, Near
, Far
);
242 glMatrixMode( GL_MODELVIEW
);
244 glTranslatef( 0.0, 0.0, -15.0 );
248 static void Key( unsigned char key
, int x
, int y
)
261 static void Init( void )
263 const char * const ver_string
= (const char * const)
264 glGetString( GL_VERSION
);
266 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
267 printf("GL_VERSION = %s\n", ver_string
);
269 if ( !glutExtensionSupported("GL_ATI_separate_stencil")
270 && (atof( ver_string
) < 2.0) ) {
271 printf("Sorry, this program requires either GL_ATI_separate_stencil or OpenGL 2.0.\n");
274 if (atof( ver_string
) < 2.0) {
277 stencil_func_separate
= glutGetProcAddress( "glStencilFuncSeparate" );
278 stencil_func_separate_ati
= glutGetProcAddress( "glStencilFuncSeparateATI" );
279 stencil_op_separate
= glutGetProcAddress( "glStencilOpSeparate" );
281 printf("\nAll 5 squares should be the same color.\n");
282 glEnable( GL_BLEND
);
286 int main( int argc
, char *argv
[] )
288 glutInit( &argc
, argv
);
289 glutInitWindowPosition( 0, 0 );
290 glutInitWindowSize( Width
, Height
);
291 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
| GLUT_STENCIL
);
292 glutCreateWindow( "GL_ATI_separate_stencil test" );
293 glutReshapeFunc( Reshape
);
294 glutKeyboardFunc( Key
);
295 glutDisplayFunc( Display
);