Merge branch 'master' into crestline
[mesa.git] / progs / tests / stencilwrap.c
1 /* Test GL_EXT_stencil_wrap extension.
2 * This is by no means complete, just a quick check.
3 *
4 * Brian Paul 30 October 2002
5 */
6
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <GL/glut.h>
12
13 GLboolean wrapping;
14
15 static void RunTest(void)
16 {
17 const GLenum prim = GL_QUAD_STRIP;
18 GLubyte val;
19 int bits, max, i;
20 int expected;
21 GLboolean failed;
22
23 glGetIntegerv(GL_STENCIL_BITS, &bits);
24 max = (1 << bits) - 1;
25
26
27 glEnable(GL_STENCIL_TEST);
28 glStencilFunc(GL_ALWAYS, 0, ~0);
29
30 /* test GL_KEEP */
31 glClearStencil(max);
32 glClear(GL_STENCIL_BUFFER_BIT);
33 glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
34 failed = GL_FALSE;
35 printf("Testing GL_KEEP...\n");
36 expected = max;
37 glBegin(prim);
38 glVertex2f(0, 0);
39 glVertex2f(10, 0);
40 glVertex2f(0, 10);
41 glVertex2f(10, 10);
42 glEnd();
43 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
44 if (val != expected) {
45 printf("Failed GL_KEEP test(got %u, expected %u)\n", val, expected);
46 failed = GL_TRUE;
47 }
48 else
49 printf("OK!\n");
50
51 /* test GL_ZERO */
52 glClearStencil(max);
53 glClear(GL_STENCIL_BUFFER_BIT);
54 glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
55 failed = GL_FALSE;
56 printf("Testing GL_ZERO...\n");
57 expected = 0;
58 glBegin(prim);
59 glVertex2f(0, 0);
60 glVertex2f(10, 0);
61 glVertex2f(0, 10);
62 glVertex2f(10, 10);
63 glEnd();
64 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
65 if (val != expected) {
66 printf("Failed GL_ZERO test(got %u, expected %u)\n", val, expected);
67 failed = GL_TRUE;
68 }
69 else
70 printf("OK!\n");
71
72 /* test GL_REPLACE */
73 glClearStencil(max);
74 glClear(GL_STENCIL_BUFFER_BIT);
75 glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
76 failed = GL_FALSE;
77 printf("Testing GL_REPLACE...\n");
78 expected = 0;
79 glBegin(prim);
80 glVertex2f(0, 0);
81 glVertex2f(10, 0);
82 glVertex2f(0, 10);
83 glVertex2f(10, 10);
84 glEnd();
85 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
86 if (val != expected) {
87 printf("Failed GL_REPLACE test(got %u, expected %u)\n", val, expected);
88 failed = GL_TRUE;
89 }
90 else
91 printf("OK!\n");
92
93 /* test GL_INCR (saturation) */
94 glClearStencil(0);
95 glClear(GL_STENCIL_BUFFER_BIT);
96 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
97 failed = GL_FALSE;
98 printf("Testing GL_INCR...\n");
99 for (i = 1; i < max+10; i++) {
100 expected = (i > max) ? max : i;
101 glBegin(prim);
102 glVertex2f(0, 0); glVertex2f(10, 0);
103 glVertex2f(0, 10); glVertex2f(10, 10);
104 glEnd();
105
106 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
107 if (val != expected) {
108 printf( "Failed GL_INCR test on iteration #%u "
109 "(got %u, expected %u)\n", i, val, expected );
110 failed = GL_TRUE;
111 }
112 }
113 if ( !failed )
114 printf("OK!\n");
115
116 /* test GL_DECR (saturation) */
117 glClearStencil(max);
118 glClear(GL_STENCIL_BUFFER_BIT);
119 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
120 failed = GL_FALSE;
121 printf("Testing GL_DECR...\n");
122 for (i = max-1; i > -10; i--) {
123 expected = (i < 0) ? 0 : i;
124 glBegin(prim);
125 glVertex2f(0, 0); glVertex2f(10, 0);
126 glVertex2f(0, 10); glVertex2f(10, 10);
127 glEnd();
128 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
129 if (val != expected) {
130 printf( "Failed GL_DECR test on iteration #%u "
131 "(got %u, expected %u)\n", max - i, val, expected );
132 failed = GL_TRUE;
133 }
134 }
135 if ( !failed )
136 printf("OK!\n");
137
138 /* test GL_INVERT */
139 glClearStencil(0);
140 glClear(GL_STENCIL_BUFFER_BIT);
141 glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT);
142 failed = GL_FALSE;
143 printf("Testing GL_INVERT...\n");
144 expected = max;
145 glBegin(prim);
146 glVertex2f(0, 0);
147 glVertex2f(10, 0);
148 glVertex2f(0, 10);
149 glVertex2f(10, 10);
150 glEnd();
151 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
152 if (val != expected) {
153 printf("Failed GL_INVERT test(got %u, expected %u)\n", val, expected);
154 failed = GL_TRUE;
155 }
156 else
157 printf("OK!\n");
158
159 if(wrapping)
160 {
161 /* test GL_INCR_WRAP_EXT (wrap around) */
162 glClearStencil(0);
163 glClear(GL_STENCIL_BUFFER_BIT);
164 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP_EXT);
165 failed = GL_FALSE;
166 printf("Testing GL_INCR_WRAP_EXT...\n");
167 for (i = 1; i < max+10; i++) {
168 expected = i % (max + 1);
169 glBegin(prim);
170 glVertex2f(0, 0); glVertex2f(10, 0);
171 glVertex2f(0, 10); glVertex2f(10, 10);
172 glEnd();
173 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
174 if (val != expected) {
175 printf( "Failed GL_INCR_WRAP test on iteration #%u "
176 "(got %u, expected %u)\n", i, val, expected );
177 failed = GL_TRUE;
178 }
179 }
180 if ( !failed )
181 printf("OK!\n");
182
183 /* test GL_DECR_WRAP_EXT (wrap-around) */
184 glClearStencil(max);
185 glClear(GL_STENCIL_BUFFER_BIT);
186 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);
187 failed = GL_FALSE;
188 printf("Testing GL_DECR_WRAP_EXT...\n");
189 for (i = max-1; i > -10; i--) {
190 expected = (i < 0) ? max + i + 1: i;
191 glBegin(prim);
192 glVertex2f(0, 0); glVertex2f(10, 0);
193 glVertex2f(0, 10); glVertex2f(10, 10);
194 glEnd();
195 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
196 if (val != expected) {
197 printf( "Failed GL_DECR_WRAP test on iteration #%u "
198 "(got %u, expected %u)\n", max - i, val, expected );
199 failed = GL_TRUE;
200 }
201 }
202 if ( !failed )
203 printf("OK!\n");
204 }
205
206 glDisable(GL_STENCIL_TEST);
207 }
208
209
210 static void Display( void )
211 {
212 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
213
214 RunTest();
215
216 glutSwapBuffers();
217 }
218
219
220 static void Reshape( int width, int height )
221 {
222 glViewport( 0, 0, width, height );
223 glMatrixMode( GL_PROJECTION );
224 glLoadIdentity();
225 glOrtho(0, width, 0, height, -1, 1);
226 glMatrixMode( GL_MODELVIEW );
227 glLoadIdentity();
228 }
229
230
231 static void Key( unsigned char key, int x, int y )
232 {
233 (void) x;
234 (void) y;
235 switch (key) {
236 case 27:
237 exit(0);
238 break;
239 }
240 glutPostRedisplay();
241 }
242
243
244 static void Init( void )
245 {
246 const char * ver_str;
247 float version;
248
249 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
250 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
251
252
253 /* Check for both the extension string and GL version 1.4 on the
254 * outside chance that some vendor exports version 1.4 but doesn't
255 * export the extension string. The stencil-wrap modes are a required
256 * part of GL 1.4.
257 */
258
259 ver_str = glGetString( GL_VERSION );
260 version = (ver_str == NULL) ? 1.0 : atof( ver_str );
261
262 wrapping = (glutExtensionSupported("GL_EXT_stencil_wrap") || (version >= 1.4));
263 if (!wrapping)
264 printf("GL_EXT_stencil_wrap not supported. Only testing the rest.\n");
265 }
266
267
268 int main( int argc, char *argv[] )
269 {
270 glutInit( &argc, argv );
271 glutInitWindowPosition( 0, 0 );
272 glutInitWindowSize( 400, 400 );
273 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL );
274 glutCreateWindow(argv[0]);
275 glutReshapeFunc( Reshape );
276 glutKeyboardFunc( Key );
277 glutDisplayFunc( Display );
278 Init();
279 glutMainLoop();
280 return 0;
281 }