aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/extras/Mesa_6.4.1/progs/tests/stencilwrap.c
blob: 753375d0f3abda2041e0385020be056f383387be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* Test GL_EXT_stencil_wrap extension.
 * This is by no means complete, just a quick check.
 *
 * Brian Paul  30 October 2002
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

GLboolean wrapping;

static void RunTest(void)
{
   const GLenum prim = GL_QUAD_STRIP;
   GLubyte val;
   int bits, max, i;
   int expected;
   GLboolean failed;

   glGetIntegerv(GL_STENCIL_BITS, &bits);
   max = (1 << bits) - 1;

   
   glEnable(GL_STENCIL_TEST);
   glStencilFunc(GL_ALWAYS, 0, ~0);

   /* test GL_KEEP */
   glClearStencil(max);
   glClear(GL_STENCIL_BUFFER_BIT);
   glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
   failed = GL_FALSE;
   printf("Testing GL_KEEP...\n");
   expected = max;
   glBegin(prim);
   glVertex2f(0, 0);
   glVertex2f(10, 0);
   glVertex2f(0, 10);
   glVertex2f(10, 10);
   glEnd();
   glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
   if (val != expected) {
      printf("Failed GL_KEEP test(got %u, expected %u)\n", val, expected);
      failed = GL_TRUE;
   }
   else
      printf("OK!\n");

   /* test GL_ZERO */
   glClearStencil(max);
   glClear(GL_STENCIL_BUFFER_BIT);
   glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
   failed = GL_FALSE;
   printf("Testing GL_ZERO...\n");
   expected = 0;
   glBegin(prim);
   glVertex2f(0, 0);
   glVertex2f(10, 0);
   glVertex2f(0, 10);
   glVertex2f(10, 10);
   glEnd();
   glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
   if (val != expected) {
      printf("Failed GL_ZERO test(got %u, expected %u)\n", val, expected);
      failed = GL_TRUE;
   }
   else
      printf("OK!\n");

   /* test GL_REPLACE */
   glClearStencil(max);
   glClear(GL_STENCIL_BUFFER_BIT);
   glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
   failed = GL_FALSE;
   printf("Testing GL_REPLACE...\n");
   expected = 0;
   glBegin(prim);
   glVertex2f(0, 0);
   glVertex2f(10, 0);
   glVertex2f(0, 10);
   glVertex2f(10, 10);
   glEnd();
   glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
   if (val != expected) {
      printf("Failed GL_REPLACE test(got %u, expected %u)\n", val, expected);
      failed = GL_TRUE;
   }
   else
      printf("OK!\n");

   /* test GL_INCR (saturation) */
   glClearStencil(0);
   glClear(GL_STENCIL_BUFFER_BIT);
   glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
   failed = GL_FALSE;
   printf("Testing GL_INCR...\n");
   for (i = 1; i < max+10; i++) {
      expected = (i > max) ? max : i;
      glBegin(prim);
      glVertex2f(0, 0);      glVertex2f(10, 0);
      glVertex2f(0, 10);      glVertex2f(10, 10);
      glEnd();

      glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
      if (val != expected) {
	 printf( "Failed GL_INCR test on iteration #%u "
		 "(got %u, expected %u)\n", i, val, expected );
	 failed = GL_TRUE;
      }	   
   }
   if ( !failed )
      printf("OK!\n");

   /* test GL_DECR (saturation) */
   glClearStencil(max);
   glClear(GL_STENCIL_BUFFER_BIT);
   glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
   failed = GL_FALSE;
   printf("Testing GL_DECR...\n");
   for (i = max-1; i > -10; i--) {
      expected = (i < 0) ? 0 : i;
      glBegin(prim);
      glVertex2f(0, 0);      glVertex2f(10, 0);
      glVertex2f(0, 10);      glVertex2f(10, 10);
      glEnd();
      glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
      if (val != expected) {
	 printf( "Failed GL_DECR test on iteration #%u "
		 "(got %u, expected %u)\n", max - i, val, expected );
	 failed = GL_TRUE;
      }	   
   }
   if ( !failed )
      printf("OK!\n");

   /* test GL_INVERT */
   glClearStencil(0);
   glClear(GL_STENCIL_BUFFER_BIT);
   glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT);
   failed = GL_FALSE;
   printf("Testing GL_INVERT...\n");
   expected = max;
   glBegin(prim);
   glVertex2f(0, 0);
   glVertex2f(10, 0);
   glVertex2f(0, 10);
   glVertex2f(10, 10);
   glEnd();
   glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
   if (val != expected) {
      printf("Failed GL_INVERT test(got %u, expected %u)\n", val, expected);
      failed = GL_TRUE;
   }
   else
      printf("OK!\n");

   if(wrapping)
   {
      /* test GL_INCR_WRAP_EXT (wrap around) */
      glClearStencil(0);
      glClear(GL_STENCIL_BUFFER_BIT);
      glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP_EXT);
      failed = GL_FALSE;
      printf("Testing GL_INCR_WRAP_EXT...\n");
      for (i = 1; i < max+10; i++) {
         expected = i % (max + 1);
         glBegin(prim);
         glVertex2f(0, 0);      glVertex2f(10, 0);
         glVertex2f(0, 10);      glVertex2f(10, 10);
         glEnd();
         glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
         if (val != expected) {
	    printf( "Failed GL_INCR_WRAP test on iteration #%u "
		 "(got %u, expected %u)\n", i, val, expected );
	    failed = GL_TRUE;
         }	   
      }
      if ( !failed )
         printf("OK!\n");

      /* test GL_DECR_WRAP_EXT (wrap-around) */
      glClearStencil(max);
      glClear(GL_STENCIL_BUFFER_BIT);
      glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);
      failed = GL_FALSE;
      printf("Testing GL_DECR_WRAP_EXT...\n");
      for (i = max-1; i > -10; i--) {
         expected = (i < 0) ? max + i + 1: i;
         glBegin(prim);
         glVertex2f(0, 0);      glVertex2f(10, 0);
         glVertex2f(0, 10);      glVertex2f(10, 10);
         glEnd();
         glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
         if (val != expected) {
            printf( "Failed GL_DECR_WRAP test on iteration #%u "
               "(got %u, expected %u)\n", max - i, val, expected );
            failed = GL_TRUE;
         }	   
      }
      if ( !failed )
         printf("OK!\n");
   }

   glDisable(GL_STENCIL_TEST);
}


static void Display( void )
{
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   RunTest();

   glutSwapBuffers();
}


static void Reshape( int width, int height )
{
   glViewport( 0, 0, width, height );
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   glOrtho(0, width, 0, height, -1, 1);
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
}


static void Key( unsigned char key, int x, int y )
{
   (void) x;
   (void) y;
   switch (key) {
      case 27:
         exit(0);
         break;
   }
   glutPostRedisplay();
}


static void Init( void )
{
   const char * ver_str;
   float        version;

   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
   printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));


   /* Check for both the extension string and GL version 1.4 on the
    * outside chance that some vendor exports version 1.4 but doesn't
    * export the extension string.  The stencil-wrap modes are a required
    * part of GL 1.4.
    */

   ver_str = glGetString( GL_VERSION );
   version = (ver_str == NULL) ? 1.0 : atof( ver_str );

   wrapping = (glutExtensionSupported("GL_EXT_stencil_wrap") || (version >= 1.4));
   if (!wrapping)
      printf("GL_EXT_stencil_wrap not supported. Only testing the rest.\n");
}


int main( int argc, char *argv[] )
{
   glutInit( &argc, argv );
   glutInitWindowPosition( 0, 0 );
   glutInitWindowSize( 400, 400 );
   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL );
   glutCreateWindow(argv[0]);
   glutReshapeFunc( Reshape );
   glutKeyboardFunc( Key );
   glutDisplayFunc( Display );
   Init();
   glutMainLoop();
   return 0;
}