added 1.4 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
20 glGetIntegerv(GL_STENCIL_BITS, &bits);
21 max = (1 << bits) - 1;
22
23 glClearStencil(0);
24 glEnable(GL_STENCIL_TEST);
25 glStencilFunc(GL_ALWAYS, 0, ~0);
26
27 /* test GL_INCR (saturation) */
28 glClear(GL_STENCIL_BUFFER_BIT);
29 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
30 printf("Testing GL_INCR...\n");
31 for (i = 1; i < max+10; i++) {
32 int expected = (i > max) ? max : i;
33 glBegin(prim);
34 glVertex2f(0.5, 0.5); glVertex2f(9.5, 0.5);
35 glEnd();
36 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
37 assert(val == expected);
38 }
39 printf("OK!\n");
40
41 /* test GL_INCR_WRAP_EXT (wrap around) */
42 glClear(GL_STENCIL_BUFFER_BIT);
43 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP_EXT);
44 printf("Testing GL_INCR_WRAP_EXT...\n");
45 for (i = 1; i < max+10; i++) {
46 int expected = i % (max + 1);
47 glBegin(prim);
48 glVertex2f(0.5, 0.5); glVertex2f(9.5, 0.5);
49 glEnd();
50 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
51 assert(val == expected);
52 }
53 printf("OK!\n");
54
55 glClearStencil(max);
56
57 /* test GL_INCR (saturation) */
58 glClear(GL_STENCIL_BUFFER_BIT);
59 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
60 printf("Testing GL_DECR...\n");
61 for (i = max-1; i > -10; i--) {
62 int expected = (i < 0) ? 0 : i;
63 glBegin(prim);
64 glVertex2f(0.5, 0.5); glVertex2f(9.5, 0.5);
65 glEnd();
66 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
67 assert(val == expected);
68 }
69 printf("OK!\n");
70
71 /* test GL_INCR_WRAP_EXT (wrap-around) */
72 glClear(GL_STENCIL_BUFFER_BIT);
73 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);
74 printf("Testing GL_DECR_WRAP_EXT...\n");
75 for (i = max-1; i > -10; i--) {
76 int expected = (i < 0) ? max + i + 1: i;
77 glBegin(prim);
78 glVertex2f(0.5, 0.5); glVertex2f(9.5, 0.5);
79 glEnd();
80 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
81 assert(val == expected);
82 }
83 printf("OK!\n");
84
85 glDisable(GL_STENCIL_TEST);
86 }
87
88
89 static void Display( void )
90 {
91 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
92
93 RunTest();
94
95 glutSwapBuffers();
96 }
97
98
99 static void Reshape( int width, int height )
100 {
101 glViewport( 0, 0, width, height );
102 glMatrixMode( GL_PROJECTION );
103 glLoadIdentity();
104 glOrtho(0, width, 0, height, -1, 1);
105 glMatrixMode( GL_MODELVIEW );
106 glLoadIdentity();
107 }
108
109
110 static void Key( unsigned char key, int x, int y )
111 {
112 (void) x;
113 (void) y;
114 switch (key) {
115 case 27:
116 exit(0);
117 break;
118 }
119 glutPostRedisplay();
120 }
121
122
123 static void Init( void )
124 {
125 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
126 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
127 if (!glutExtensionSupported("GL_EXT_stencil_wrap")) {
128 printf("Sorry, GL_EXT_stencil_wrap not supported.\n");
129 exit(1);
130 }
131 }
132
133
134 int main( int argc, char *argv[] )
135 {
136 glutInit( &argc, argv );
137 glutInitWindowPosition( 0, 0 );
138 glutInitWindowSize( 400, 400 );
139 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL );
140 glutCreateWindow(argv[0]);
141 glutReshapeFunc( Reshape );
142 glutKeyboardFunc( Key );
143 glutDisplayFunc( Display );
144 Init();
145 glutMainLoop();
146 return 0;
147 }