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