Use quads instead of lines to ensure this is testing stencil functionality
[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
14 static void RunTest(void)
15 {
16 const GLenum prim = GL_QUAD_STRIP;
17 GLubyte val;
18 int bits, max, i;
19 GLboolean failed;
20
21 glGetIntegerv(GL_STENCIL_BITS, &bits);
22 max = (1 << bits) - 1;
23
24 glClearStencil(0);
25 glEnable(GL_STENCIL_TEST);
26 glStencilFunc(GL_ALWAYS, 0, ~0);
27
28 /* test GL_INCR (saturation) */
29 glClear(GL_STENCIL_BUFFER_BIT);
30 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
31 failed = GL_FALSE;
32 printf("Testing GL_INCR...\n");
33 for (i = 1; i < max+10; i++) {
34 int expected = (i > max) ? max : i;
35 glBegin(prim);
36 glVertex2f(0, 0); glVertex2f(10, 0);
37 glVertex2f(0, 10); glVertex2f(10, 10);
38 glEnd();
39
40 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
41 if (val != expected) {
42 printf( "Failed GL_INCR test on iteration #%u "
43 "(got %u, expected %u)\n", i, val, expected );
44 failed = GL_TRUE;
45 }
46 }
47
48 if ( !failed ) printf("OK!\n");
49
50
51 /* test GL_INCR_WRAP_EXT (wrap around) */
52 glClear(GL_STENCIL_BUFFER_BIT);
53 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP_EXT);
54 failed = GL_FALSE;
55 printf("Testing GL_INCR_WRAP_EXT...\n");
56 for (i = 1; i < max+10; i++) {
57 int expected = i % (max + 1);
58 glBegin(prim);
59 glVertex2f(0, 0); glVertex2f(10, 0);
60 glVertex2f(0, 10); glVertex2f(10, 10);
61 glEnd();
62 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
63 if (val != expected) {
64 printf( "Failed GL_INCR_WRAP test on iteration #%u "
65 "(got %u, expected %u)\n", i, val, expected );
66 failed = GL_TRUE;
67 }
68 }
69 if ( !failed ) printf("OK!\n");
70
71 glClearStencil(max);
72
73 /* test GL_INCR (saturation) */
74 glClear(GL_STENCIL_BUFFER_BIT);
75 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
76 failed = GL_FALSE;
77 printf("Testing GL_DECR...\n");
78 for (i = max-1; i > -10; i--) {
79 int expected = (i < 0) ? 0 : i;
80 glBegin(prim);
81 glVertex2f(0, 0); glVertex2f(10, 0);
82 glVertex2f(0, 10); glVertex2f(10, 10);
83 glEnd();
84 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
85 if (val != expected) {
86 printf( "Failed GL_DECR test on iteration #%u "
87 "(got %u, expected %u)\n", max - i, val, expected );
88 failed = GL_TRUE;
89 }
90 }
91 if ( !failed ) printf("OK!\n");
92
93 /* test GL_INCR_WRAP_EXT (wrap-around) */
94 glClear(GL_STENCIL_BUFFER_BIT);
95 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);
96 failed = GL_FALSE;
97 printf("Testing GL_DECR_WRAP_EXT...\n");
98 for (i = max-1; i > -10; i--) {
99 int expected = (i < 0) ? max + i + 1: i;
100 glBegin(prim);
101 glVertex2f(0, 0); glVertex2f(10, 0);
102 glVertex2f(0, 10); glVertex2f(10, 10);
103 glEnd();
104 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
105 if (val != expected) {
106 printf( "Failed GL_DECR_WRAP test on iteration #%u "
107 "(got %u, expected %u)\n", max - i, val, expected );
108 failed = GL_TRUE;
109 }
110 }
111 if ( !failed ) printf("OK!\n");
112
113 glDisable(GL_STENCIL_TEST);
114 }
115
116
117 static void Display( void )
118 {
119 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
120
121 RunTest();
122
123 glutSwapBuffers();
124 }
125
126
127 static void Reshape( int width, int height )
128 {
129 glViewport( 0, 0, width, height );
130 glMatrixMode( GL_PROJECTION );
131 glLoadIdentity();
132 glOrtho(0, width, 0, height, -1, 1);
133 glMatrixMode( GL_MODELVIEW );
134 glLoadIdentity();
135 }
136
137
138 static void Key( unsigned char key, int x, int y )
139 {
140 (void) x;
141 (void) y;
142 switch (key) {
143 case 27:
144 exit(0);
145 break;
146 }
147 glutPostRedisplay();
148 }
149
150
151 static void Init( void )
152 {
153 const char * ver_str;
154 float version;
155
156 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
157 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
158
159
160 /* Check for both the extension string and GL version 1.4 on the
161 * outside chance that some silly vendor exports version 1.4 but doesn't
162 * export the extension string. The stencil-wrap modes are a required
163 * part of GL 1.4.
164 */
165
166 ver_str = glGetString( GL_VERSION );
167 version = (ver_str == NULL) ? 1.0 : strtof( ver_str, NULL );
168
169 if ( !glutExtensionSupported("GL_EXT_stencil_wrap")
170 && (version < 1.4) ) {
171 printf("Sorry, GL_EXT_stencil_wrap not supported.\n");
172 exit(1);
173 }
174 }
175
176
177 int main( int argc, char *argv[] )
178 {
179 glutInit( &argc, argv );
180 glutInitWindowPosition( 0, 0 );
181 glutInitWindowSize( 400, 400 );
182 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL );
183 glutCreateWindow(argv[0]);
184 glutReshapeFunc( Reshape );
185 glutKeyboardFunc( Key );
186 glutDisplayFunc( Display );
187 Init();
188 glutMainLoop();
189 return 0;
190 }