simple GL_EXT_stencil_wrap test program
authorBrian Paul <brian.paul@tungstengraphics.com>
Thu, 31 Oct 2002 15:25:07 +0000 (15:25 +0000)
committerBrian Paul <brian.paul@tungstengraphics.com>
Thu, 31 Oct 2002 15:25:07 +0000 (15:25 +0000)
progs/tests/Makefile
progs/tests/stencilwrap.c [new file with mode: 0644]

index d9468cd46c2d0f712e4c7f8c3c20a54b462172d8..efa4ad03ec02157e8f7aaa49a66885fdad1c778c 100644 (file)
@@ -1,8 +1,7 @@
 
 # Simple makefile for compiling test programs on Linux
 # These programs aren't intended to be included with the normal
-# distro as they're not too interesting but good for testing during
-# development.
+# distro.  They're not too interesting but they're good for testing.
 
 
 CC = gcc
@@ -17,6 +16,7 @@ PROGS = cva \
        projtex \
        seccolor \
        sharedtex \
+       stencilwrap \
        texline \
        texwrap \
        vptest1 \
diff --git a/progs/tests/stencilwrap.c b/progs/tests/stencilwrap.c
new file mode 100644 (file)
index 0000000..e65258b
--- /dev/null
@@ -0,0 +1,147 @@
+/* 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>
+
+
+static void RunTest(void)
+{
+   const GLenum prim = GL_LINES;
+   GLubyte val;
+   int bits, max, i;
+
+   glGetIntegerv(GL_STENCIL_BITS, &bits);
+   max = (1 << bits) - 1;
+
+   glClearStencil(0);
+   glEnable(GL_STENCIL_TEST);
+   glStencilFunc(GL_ALWAYS, 0, ~0);
+
+   /* test GL_INCR (saturation) */
+   glClear(GL_STENCIL_BUFFER_BIT);
+   glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
+   printf("Testing GL_INCR...\n");
+   for (i = 1; i < max+10; i++) {
+      int expected = (i > max) ? max : i;
+      glBegin(prim);
+      glVertex2f(0.5, 0.5);      glVertex2f(9.5, 0.5);
+      glEnd();
+      glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
+      assert(val == expected);
+   }
+   printf("OK!\n");
+
+   /* test GL_INCR_WRAP_EXT (wrap around) */
+   glClear(GL_STENCIL_BUFFER_BIT);
+   glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP_EXT);
+   printf("Testing GL_INCR_WRAP_EXT...\n");
+   for (i = 1; i < max+10; i++) {
+      int expected = i % (max + 1);
+      glBegin(prim);
+      glVertex2f(0.5, 0.5);      glVertex2f(9.5, 0.5);
+      glEnd();
+      glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
+      assert(val == expected);
+   }
+   printf("OK!\n");
+
+   glClearStencil(max);
+
+   /* test GL_INCR (saturation) */
+   glClear(GL_STENCIL_BUFFER_BIT);
+   glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
+   printf("Testing GL_DECR...\n");
+   for (i = max-1; i > -10; i--) {
+      int expected = (i < 0) ? 0 : i;
+      glBegin(prim);
+      glVertex2f(0.5, 0.5);      glVertex2f(9.5, 0.5);
+      glEnd();
+      glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
+      assert(val == expected);
+   }
+   printf("OK!\n");
+
+   /* test GL_INCR_WRAP_EXT (wrap-around) */
+   glClear(GL_STENCIL_BUFFER_BIT);
+   glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);
+   printf("Testing GL_DECR_WRAP_EXT...\n");
+   for (i = max-1; i > -10; i--) {
+      int expected = (i < 0) ? max + i + 1: i;
+      glBegin(prim);
+      glVertex2f(0.5, 0.5);      glVertex2f(9.5, 0.5);
+      glEnd();
+      glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
+      assert(val == expected);
+   }
+   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 )
+{
+   printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
+   printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
+   if (!glutExtensionSupported("GL_EXT_stencil_wrap")) {
+      printf("Sorry, GL_EXT_stencil_wrap not supported.\n");
+      exit(1);
+   }
+}
+
+
+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;
+}