88cf3809caf5ae344be0547cc1452720912fa9db
[mesa.git] / progs / tests / stencil_wrap.c
1 /*
2 * (C) Copyright IBM Corporation 2004
3 * All Rights Reserved.
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25 /**
26 * \file stencil_wrap.c
27 *
28 * Simple test of GL_EXT_stencil_wrap functionality. Four squares are drawn
29 * with different stencil modes, but all should be rendered with the same
30 * final color.
31 *
32 * \author Ian Romanick <idr@us.ibm.com>
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <GL/glut.h>
38
39 static int Width = 550;
40 static int Height = 200;
41 static const GLfloat Near = 5.0, Far = 25.0;
42
43
44 static void Display( void )
45 {
46 GLint max_stencil;
47 GLint stencil_bits;
48 unsigned i;
49
50
51 glGetIntegerv( GL_STENCIL_BITS, & stencil_bits );
52 max_stencil = (1U << stencil_bits) - 1;
53 printf( "Stencil bits = %u, maximum stencil value = 0x%08x\n",
54 stencil_bits, max_stencil );
55
56 glClearStencil( 0 );
57 glClearColor( 0.2, 0.2, 0.8, 0 );
58 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
59 | GL_STENCIL_BUFFER_BIT );
60
61
62 glPushMatrix();
63
64 /* This is the "reference" square.
65 */
66
67 glDisable(GL_STENCIL_TEST);
68 glTranslatef(-6.0, 0, 0);
69 glBegin(GL_QUADS);
70 glColor3f( 0.5, 0.5, 0.5 );
71 glVertex2f(-1, -1);
72 glVertex2f( 1, -1);
73 glVertex2f( 1, 1);
74 glVertex2f(-1, 1);
75 glEnd();
76
77
78 glEnable(GL_STENCIL_TEST);
79
80 /* Draw the first two squares using the two non-wrap (i.e., saturate)
81 * modes.
82 */
83
84 glStencilFunc(GL_ALWAYS, 0, ~0);
85 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
86
87 glTranslatef(3.0, 0, 0);
88 glBegin(GL_QUADS);
89 glColor3f( 0.9, 0.9, 0.9 );
90
91 for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
92 glVertex2f(-1, -1);
93 glVertex2f( 1, -1);
94 glVertex2f( 1, 1);
95 glVertex2f(-1, 1);
96 }
97 glEnd();
98
99 glStencilFunc(GL_EQUAL, max_stencil, ~0);
100 glBegin(GL_QUADS);
101 glColor3f( 0.5, 0.5, 0.5 );
102 glVertex2f(-1, -1);
103 glVertex2f( 1, -1);
104 glVertex2f( 1, 1);
105 glVertex2f(-1, 1);
106 glEnd();
107
108
109 glStencilFunc(GL_ALWAYS, 0, ~0);
110 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
111
112 glTranslatef(3.0, 0, 0);
113 glBegin(GL_QUADS);
114 glColor3f( 0.9, 0.9, 0.9 );
115
116 for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
117 glVertex2f(-1, -1);
118 glVertex2f( 1, -1);
119 glVertex2f( 1, 1);
120 glVertex2f(-1, 1);
121 }
122 glEnd();
123
124 glStencilFunc(GL_EQUAL, 0, ~0);
125 glBegin(GL_QUADS);
126 glColor3f( 0.5, 0.5, 0.5 );
127 glVertex2f(-1, -1);
128 glVertex2f( 1, -1);
129 glVertex2f( 1, 1);
130 glVertex2f(-1, 1);
131 glEnd();
132
133
134
135
136 /* Draw the last two squares using the two wrap modes.
137 */
138
139 glStencilFunc(GL_ALWAYS, 0, ~0);
140 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP);
141
142 glTranslatef(3.0, 0, 0);
143 glBegin(GL_QUADS);
144 glColor3f( 0.9, 0.9, 0.9 );
145
146 for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
147 glVertex2f(-1, -1);
148 glVertex2f( 1, -1);
149 glVertex2f( 1, 1);
150 glVertex2f(-1, 1);
151 }
152 glEnd();
153
154 glStencilFunc(GL_EQUAL, 4, ~0);
155 glBegin(GL_QUADS);
156 glColor3f( 0.5, 0.5, 0.5 );
157 glVertex2f(-1, -1);
158 glVertex2f( 1, -1);
159 glVertex2f( 1, 1);
160 glVertex2f(-1, 1);
161 glEnd();
162
163
164
165 glStencilFunc(GL_ALWAYS, 0, ~0);
166 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP);
167
168 glTranslatef(3.0, 0, 0);
169 glBegin(GL_QUADS);
170 glColor3f( 0.9, 0.9, 0.9 );
171
172 for ( i = 0 ; i < 5 ; i++ ) {
173 glVertex2f(-1, -1);
174 glVertex2f( 1, -1);
175 glVertex2f( 1, 1);
176 glVertex2f(-1, 1);
177 }
178 glEnd();
179
180 glStencilFunc(GL_EQUAL, (max_stencil - 4), ~0);
181 glBegin(GL_QUADS);
182 glColor3f( 0.5, 0.5, 0.5 );
183 glVertex2f(-1, -1);
184 glVertex2f( 1, -1);
185 glVertex2f( 1, 1);
186 glVertex2f(-1, 1);
187 glEnd();
188
189
190
191 glPopMatrix();
192
193 glutSwapBuffers();
194 }
195
196
197 static void Reshape( int width, int height )
198 {
199 GLfloat ar = (float) width / (float) height;
200 Width = width;
201 Height = height;
202 glViewport( 0, 0, width, height );
203 glMatrixMode( GL_PROJECTION );
204 glLoadIdentity();
205 glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
206 glMatrixMode( GL_MODELVIEW );
207 glLoadIdentity();
208 glTranslatef( 0.0, 0.0, -15.0 );
209 }
210
211
212 static void Key( unsigned char key, int x, int y )
213 {
214 (void) x;
215 (void) y;
216 switch (key) {
217 case 27:
218 exit(0);
219 break;
220 }
221 glutPostRedisplay();
222 }
223
224
225 static void Init( void )
226 {
227 const char * const ver_string = (const char * const)
228 glGetString( GL_VERSION );
229
230 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
231 printf("GL_VERSION = %s\n", ver_string);
232
233 if ( !glutExtensionSupported("GL_EXT_stencil_wrap")
234 && (atof( ver_string ) < 1.4) ) {
235 printf("Sorry, this program requires either GL_EXT_stencil_wrap or OpenGL 1.4.\n");
236 exit(1);
237 }
238
239 printf("\nAll 5 squares should be the same color.\n");
240 glEnable( GL_BLEND );
241 }
242
243
244 int main( int argc, char *argv[] )
245 {
246 glutInit( &argc, argv );
247 glutInitWindowPosition( 0, 0 );
248 glutInitWindowSize( Width, Height );
249 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL );
250 glutCreateWindow( "GL_EXT_stencil_wrap test" );
251 glutReshapeFunc( Reshape );
252 glutKeyboardFunc( Key );
253 glutDisplayFunc( Display );
254 Init();
255 glutMainLoop();
256 return 0;
257 }