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