blob: f7d3ecd38da509b223af5a50f37b4573c7e08333 [file] [log] [blame]
Brian Paul36481522002-10-31 15:25:07 +00001/* 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
14static void RunTest(void)
15{
Keith Whitwell72aeea42004-02-04 15:27:39 +000016 const GLenum prim = GL_QUAD_STRIP;
Brian Paul36481522002-10-31 15:25:07 +000017 GLubyte val;
18 int bits, max, i;
Brian Paul785774d2003-05-30 15:30:16 +000019 GLboolean failed;
Brian Paul36481522002-10-31 15:25:07 +000020
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);
Brian Paul785774d2003-05-30 15:30:16 +000031 failed = GL_FALSE;
Brian Paul36481522002-10-31 15:25:07 +000032 printf("Testing GL_INCR...\n");
33 for (i = 1; i < max+10; i++) {
34 int expected = (i > max) ? max : i;
35 glBegin(prim);
Keith Whitwell72aeea42004-02-04 15:27:39 +000036 glVertex2f(0, 0); glVertex2f(10, 0);
37 glVertex2f(0, 10); glVertex2f(10, 10);
Brian Paul36481522002-10-31 15:25:07 +000038 glEnd();
Brian Paul785774d2003-05-30 15:30:16 +000039
Brian Paul36481522002-10-31 15:25:07 +000040 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
Brian Paul785774d2003-05-30 15:30:16 +000041 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 }
Brian Paul36481522002-10-31 15:25:07 +000046 }
Brian Paul785774d2003-05-30 15:30:16 +000047
48 if ( !failed ) printf("OK!\n");
49
Brian Paul36481522002-10-31 15:25:07 +000050
51 /* test GL_INCR_WRAP_EXT (wrap around) */
52 glClear(GL_STENCIL_BUFFER_BIT);
53 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP_EXT);
Brian Paul785774d2003-05-30 15:30:16 +000054 failed = GL_FALSE;
Brian Paul36481522002-10-31 15:25:07 +000055 printf("Testing GL_INCR_WRAP_EXT...\n");
56 for (i = 1; i < max+10; i++) {
57 int expected = i % (max + 1);
58 glBegin(prim);
Keith Whitwell72aeea42004-02-04 15:27:39 +000059 glVertex2f(0, 0); glVertex2f(10, 0);
60 glVertex2f(0, 10); glVertex2f(10, 10);
Brian Paul36481522002-10-31 15:25:07 +000061 glEnd();
62 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
Brian Paul785774d2003-05-30 15:30:16 +000063 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 }
Brian Paul36481522002-10-31 15:25:07 +000068 }
Brian Paul785774d2003-05-30 15:30:16 +000069 if ( !failed ) printf("OK!\n");
Brian Paul36481522002-10-31 15:25:07 +000070
71 glClearStencil(max);
72
73 /* test GL_INCR (saturation) */
74 glClear(GL_STENCIL_BUFFER_BIT);
75 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
Brian Paul785774d2003-05-30 15:30:16 +000076 failed = GL_FALSE;
Brian Paul36481522002-10-31 15:25:07 +000077 printf("Testing GL_DECR...\n");
78 for (i = max-1; i > -10; i--) {
79 int expected = (i < 0) ? 0 : i;
80 glBegin(prim);
Keith Whitwell72aeea42004-02-04 15:27:39 +000081 glVertex2f(0, 0); glVertex2f(10, 0);
82 glVertex2f(0, 10); glVertex2f(10, 10);
Brian Paul36481522002-10-31 15:25:07 +000083 glEnd();
84 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
Brian Paul785774d2003-05-30 15:30:16 +000085 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 }
Brian Paul36481522002-10-31 15:25:07 +000090 }
Brian Paul785774d2003-05-30 15:30:16 +000091 if ( !failed ) printf("OK!\n");
Brian Paul36481522002-10-31 15:25:07 +000092
93 /* test GL_INCR_WRAP_EXT (wrap-around) */
94 glClear(GL_STENCIL_BUFFER_BIT);
95 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);
Brian Paul785774d2003-05-30 15:30:16 +000096 failed = GL_FALSE;
Brian Paul36481522002-10-31 15:25:07 +000097 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);
Keith Whitwell72aeea42004-02-04 15:27:39 +0000101 glVertex2f(0, 0); glVertex2f(10, 0);
102 glVertex2f(0, 10); glVertex2f(10, 10);
Brian Paul36481522002-10-31 15:25:07 +0000103 glEnd();
104 glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
Brian Paul785774d2003-05-30 15:30:16 +0000105 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 }
Brian Paul36481522002-10-31 15:25:07 +0000110 }
Brian Paul785774d2003-05-30 15:30:16 +0000111 if ( !failed ) printf("OK!\n");
Brian Paul36481522002-10-31 15:25:07 +0000112
113 glDisable(GL_STENCIL_TEST);
114}
115
116
117static void Display( void )
118{
119 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
120
121 RunTest();
122
123 glutSwapBuffers();
124}
125
126
127static 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
138static 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
151static void Init( void )
152{
Brian Paul785774d2003-05-30 15:30:16 +0000153 const char * ver_str;
154 float version;
155
Brian Paul36481522002-10-31 15:25:07 +0000156 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
157 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
Brian Paul785774d2003-05-30 15:30:16 +0000158
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) ) {
Brian Paul36481522002-10-31 15:25:07 +0000171 printf("Sorry, GL_EXT_stencil_wrap not supported.\n");
172 exit(1);
173 }
174}
175
176
177int 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}