132001f49Smrg/* Test GL_EXT_stencil_wrap extension.
232001f49Smrg * This is by no means complete, just a quick check.
332001f49Smrg *
432001f49Smrg * Brian Paul  30 October 2002
532001f49Smrg */
632001f49Smrg
732001f49Smrg#include <assert.h>
832001f49Smrg#include <stdio.h>
932001f49Smrg#include <stdlib.h>
1032001f49Smrg#include <math.h>
1132001f49Smrg#include <GL/glew.h>
1232001f49Smrg#include "glut_wrap.h"
1332001f49Smrg
1432001f49SmrgGLboolean wrapping;
1532001f49Smrg
1632001f49Smrgstatic void RunTest(void)
1732001f49Smrg{
1832001f49Smrg   const GLenum prim = GL_QUAD_STRIP;
1932001f49Smrg   GLubyte val;
2032001f49Smrg   int bits, max, i;
2132001f49Smrg   int expected;
2232001f49Smrg   GLboolean failed;
2332001f49Smrg
2432001f49Smrg   glGetIntegerv(GL_STENCIL_BITS, &bits);
2532001f49Smrg   max = (1 << bits) - 1;
2632001f49Smrg
2732001f49Smrg
2832001f49Smrg   glEnable(GL_STENCIL_TEST);
2932001f49Smrg   glStencilFunc(GL_ALWAYS, 0, ~0);
3032001f49Smrg
3132001f49Smrg   /* test GL_KEEP */
3232001f49Smrg   glClearStencil(max);
3332001f49Smrg   glClear(GL_STENCIL_BUFFER_BIT);
3432001f49Smrg   glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
3532001f49Smrg   failed = GL_FALSE;
3632001f49Smrg   printf("Testing GL_KEEP...\n");
3732001f49Smrg   expected = max;
3832001f49Smrg   glBegin(prim);
3932001f49Smrg   glVertex2f(0, 0);
4032001f49Smrg   glVertex2f(10, 0);
4132001f49Smrg   glVertex2f(0, 10);
4232001f49Smrg   glVertex2f(10, 10);
4332001f49Smrg   glEnd();
4432001f49Smrg   glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
4532001f49Smrg   if (val != expected) {
4632001f49Smrg      printf("Failed GL_KEEP test(got %u, expected %u)\n", val, expected);
4732001f49Smrg      failed = GL_TRUE;
4832001f49Smrg   }
4932001f49Smrg   else
5032001f49Smrg      printf("OK!\n");
5132001f49Smrg
5232001f49Smrg   /* test GL_ZERO */
5332001f49Smrg   glClearStencil(max);
5432001f49Smrg   glClear(GL_STENCIL_BUFFER_BIT);
5532001f49Smrg   glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
5632001f49Smrg   failed = GL_FALSE;
5732001f49Smrg   printf("Testing GL_ZERO...\n");
5832001f49Smrg   expected = 0;
5932001f49Smrg   glBegin(prim);
6032001f49Smrg   glVertex2f(0, 0);
6132001f49Smrg   glVertex2f(10, 0);
6232001f49Smrg   glVertex2f(0, 10);
6332001f49Smrg   glVertex2f(10, 10);
6432001f49Smrg   glEnd();
6532001f49Smrg   glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
6632001f49Smrg   if (val != expected) {
6732001f49Smrg      printf("Failed GL_ZERO test(got %u, expected %u)\n", val, expected);
6832001f49Smrg      failed = GL_TRUE;
6932001f49Smrg   }
7032001f49Smrg   else
7132001f49Smrg      printf("OK!\n");
7232001f49Smrg
7332001f49Smrg   /* test GL_REPLACE */
7432001f49Smrg   glClearStencil(max);
7532001f49Smrg   glClear(GL_STENCIL_BUFFER_BIT);
7632001f49Smrg   glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
7732001f49Smrg   failed = GL_FALSE;
7832001f49Smrg   printf("Testing GL_REPLACE...\n");
7932001f49Smrg   expected = 0;
8032001f49Smrg   glBegin(prim);
8132001f49Smrg   glVertex2f(0, 0);
8232001f49Smrg   glVertex2f(10, 0);
8332001f49Smrg   glVertex2f(0, 10);
8432001f49Smrg   glVertex2f(10, 10);
8532001f49Smrg   glEnd();
8632001f49Smrg   glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
8732001f49Smrg   if (val != expected) {
8832001f49Smrg      printf("Failed GL_REPLACE test(got %u, expected %u)\n", val, expected);
8932001f49Smrg      failed = GL_TRUE;
9032001f49Smrg   }
9132001f49Smrg   else
9232001f49Smrg      printf("OK!\n");
9332001f49Smrg
9432001f49Smrg   /* test GL_INCR (saturation) */
9532001f49Smrg   glClearStencil(0);
9632001f49Smrg   glClear(GL_STENCIL_BUFFER_BIT);
9732001f49Smrg   glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
9832001f49Smrg   failed = GL_FALSE;
9932001f49Smrg   printf("Testing GL_INCR...\n");
10032001f49Smrg   for (i = 1; i < max+10; i++) {
10132001f49Smrg      expected = (i > max) ? max : i;
10232001f49Smrg      glBegin(prim);
10332001f49Smrg      glVertex2f(0, 0);      glVertex2f(10, 0);
10432001f49Smrg      glVertex2f(0, 10);      glVertex2f(10, 10);
10532001f49Smrg      glEnd();
10632001f49Smrg
10732001f49Smrg      glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
10832001f49Smrg      if (val != expected) {
10932001f49Smrg	 printf( "Failed GL_INCR test on iteration #%u "
11032001f49Smrg		 "(got %u, expected %u)\n", i, val, expected );
11132001f49Smrg	 failed = GL_TRUE;
11232001f49Smrg      }
11332001f49Smrg   }
11432001f49Smrg   if ( !failed )
11532001f49Smrg      printf("OK!\n");
11632001f49Smrg
11732001f49Smrg   /* test GL_DECR (saturation) */
11832001f49Smrg   glClearStencil(max);
11932001f49Smrg   glClear(GL_STENCIL_BUFFER_BIT);
12032001f49Smrg   glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
12132001f49Smrg   failed = GL_FALSE;
12232001f49Smrg   printf("Testing GL_DECR...\n");
12332001f49Smrg   for (i = max-1; i > -10; i--) {
12432001f49Smrg      expected = (i < 0) ? 0 : i;
12532001f49Smrg      glBegin(prim);
12632001f49Smrg      glVertex2f(0, 0);      glVertex2f(10, 0);
12732001f49Smrg      glVertex2f(0, 10);      glVertex2f(10, 10);
12832001f49Smrg      glEnd();
12932001f49Smrg      glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
13032001f49Smrg      if (val != expected) {
13132001f49Smrg	 printf( "Failed GL_DECR test on iteration #%u "
13232001f49Smrg		 "(got %u, expected %u)\n", max - i, val, expected );
13332001f49Smrg	 failed = GL_TRUE;
13432001f49Smrg      }
13532001f49Smrg   }
13632001f49Smrg   if ( !failed )
13732001f49Smrg      printf("OK!\n");
13832001f49Smrg
13932001f49Smrg   /* test GL_INVERT */
14032001f49Smrg   glClearStencil(0);
14132001f49Smrg   glClear(GL_STENCIL_BUFFER_BIT);
14232001f49Smrg   glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT);
14332001f49Smrg   failed = GL_FALSE;
14432001f49Smrg   printf("Testing GL_INVERT...\n");
14532001f49Smrg   expected = max;
14632001f49Smrg   glBegin(prim);
14732001f49Smrg   glVertex2f(0, 0);
14832001f49Smrg   glVertex2f(10, 0);
14932001f49Smrg   glVertex2f(0, 10);
15032001f49Smrg   glVertex2f(10, 10);
15132001f49Smrg   glEnd();
15232001f49Smrg   glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
15332001f49Smrg   if (val != expected) {
15432001f49Smrg      printf("Failed GL_INVERT test(got %u, expected %u)\n", val, expected);
15532001f49Smrg      failed = GL_TRUE;
15632001f49Smrg   }
15732001f49Smrg   else
15832001f49Smrg      printf("OK!\n");
15932001f49Smrg
16032001f49Smrg   if(wrapping)
16132001f49Smrg   {
16232001f49Smrg      /* test GL_INCR_WRAP_EXT (wrap around) */
16332001f49Smrg      glClearStencil(0);
16432001f49Smrg      glClear(GL_STENCIL_BUFFER_BIT);
16532001f49Smrg      glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP_EXT);
16632001f49Smrg      failed = GL_FALSE;
16732001f49Smrg      printf("Testing GL_INCR_WRAP_EXT...\n");
16832001f49Smrg      for (i = 1; i < max+10; i++) {
16932001f49Smrg         expected = i % (max + 1);
17032001f49Smrg         glBegin(prim);
17132001f49Smrg         glVertex2f(0, 0);      glVertex2f(10, 0);
17232001f49Smrg         glVertex2f(0, 10);      glVertex2f(10, 10);
17332001f49Smrg         glEnd();
17432001f49Smrg         glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
17532001f49Smrg         if (val != expected) {
17632001f49Smrg	    printf( "Failed GL_INCR_WRAP test on iteration #%u "
17732001f49Smrg		 "(got %u, expected %u)\n", i, val, expected );
17832001f49Smrg	    failed = GL_TRUE;
17932001f49Smrg         }
18032001f49Smrg      }
18132001f49Smrg      if ( !failed )
18232001f49Smrg         printf("OK!\n");
18332001f49Smrg
18432001f49Smrg      /* test GL_DECR_WRAP_EXT (wrap-around) */
18532001f49Smrg      glClearStencil(max);
18632001f49Smrg      glClear(GL_STENCIL_BUFFER_BIT);
18732001f49Smrg      glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);
18832001f49Smrg      failed = GL_FALSE;
18932001f49Smrg      printf("Testing GL_DECR_WRAP_EXT...\n");
19032001f49Smrg      for (i = max-1; i > -10; i--) {
19132001f49Smrg         expected = (i < 0) ? max + i + 1: i;
19232001f49Smrg         glBegin(prim);
19332001f49Smrg         glVertex2f(0, 0);      glVertex2f(10, 0);
19432001f49Smrg         glVertex2f(0, 10);      glVertex2f(10, 10);
19532001f49Smrg         glEnd();
19632001f49Smrg         glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
19732001f49Smrg         if (val != expected) {
19832001f49Smrg            printf( "Failed GL_DECR_WRAP test on iteration #%u "
19932001f49Smrg               "(got %u, expected %u)\n", max - i, val, expected );
20032001f49Smrg            failed = GL_TRUE;
20132001f49Smrg         }
20232001f49Smrg      }
20332001f49Smrg      if ( !failed )
20432001f49Smrg         printf("OK!\n");
20532001f49Smrg   }
20632001f49Smrg
20732001f49Smrg   glDisable(GL_STENCIL_TEST);
20832001f49Smrg}
20932001f49Smrg
21032001f49Smrg
21132001f49Smrgstatic void Display( void )
21232001f49Smrg{
21332001f49Smrg   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
21432001f49Smrg
21532001f49Smrg   RunTest();
21632001f49Smrg
21732001f49Smrg   glutSwapBuffers();
21832001f49Smrg}
21932001f49Smrg
22032001f49Smrg
22132001f49Smrgstatic void Reshape( int width, int height )
22232001f49Smrg{
22332001f49Smrg   glViewport( 0, 0, width, height );
22432001f49Smrg   glMatrixMode( GL_PROJECTION );
22532001f49Smrg   glLoadIdentity();
22632001f49Smrg   glOrtho(0, width, 0, height, -1, 1);
22732001f49Smrg   glMatrixMode( GL_MODELVIEW );
22832001f49Smrg   glLoadIdentity();
22932001f49Smrg}
23032001f49Smrg
23132001f49Smrg
23232001f49Smrgstatic void Key( unsigned char key, int x, int y )
23332001f49Smrg{
23432001f49Smrg   (void) x;
23532001f49Smrg   (void) y;
23632001f49Smrg   switch (key) {
23732001f49Smrg      case 27:
23832001f49Smrg         exit(0);
23932001f49Smrg         break;
24032001f49Smrg   }
24132001f49Smrg   glutPostRedisplay();
24232001f49Smrg}
24332001f49Smrg
24432001f49Smrg
24532001f49Smrgstatic void Init( void )
24632001f49Smrg{
24732001f49Smrg   const char * ver_str;
24832001f49Smrg   float        version;
24932001f49Smrg
25032001f49Smrg   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
25132001f49Smrg   printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
25232001f49Smrg
25332001f49Smrg
25432001f49Smrg   /* Check for both the extension string and GL version 1.4 on the
25532001f49Smrg    * outside chance that some vendor exports version 1.4 but doesn't
25632001f49Smrg    * export the extension string.  The stencil-wrap modes are a required
25732001f49Smrg    * part of GL 1.4.
25832001f49Smrg    */
25932001f49Smrg
26032001f49Smrg   ver_str = (char *) glGetString( GL_VERSION );
26132001f49Smrg   version = (ver_str == NULL) ? 1.0 : atof( ver_str );
26232001f49Smrg
26332001f49Smrg   wrapping = (glutExtensionSupported("GL_EXT_stencil_wrap") || (version >= 1.4));
26432001f49Smrg   if (!wrapping)
26532001f49Smrg      printf("GL_EXT_stencil_wrap not supported. Only testing the rest.\n");
26632001f49Smrg}
26732001f49Smrg
26832001f49Smrg
26932001f49Smrgint main( int argc, char *argv[] )
27032001f49Smrg{
27132001f49Smrg   glutInit( &argc, argv );
27232001f49Smrg   glutInitWindowPosition( 0, 0 );
27332001f49Smrg   glutInitWindowSize( 400, 400 );
27432001f49Smrg   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL );
27532001f49Smrg   glutCreateWindow(argv[0]);
27632001f49Smrg   glewInit();
27732001f49Smrg   glutReshapeFunc( Reshape );
27832001f49Smrg   glutKeyboardFunc( Key );
27932001f49Smrg   glutDisplayFunc( Display );
28032001f49Smrg   Init();
28132001f49Smrg   glutMainLoop();
28232001f49Smrg   return 0;
28332001f49Smrg}
284