progs/tests: also test stencil INCR_WRAP mode if supported
[mesa.git] / progs / tests / stencil_twoside.c
index 8826c46fc265515a804b1cef2f259fd626ac6703..1010139a20ec5cf9ab65fea4a2c1fe684229451c 100644 (file)
  * \file stencil_twoside.c
  * 
  * Simple test of GL_ATI_separate_stencil (or the OGL 2.0 equivalent) functionality.
- * Four squares are drawn
+ * Five squares (or six if stencil wrap is available) are drawn
  * with different stencil modes, but all should be rendered with the same
  * final color.
  */
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <GL/glew.h>
 #include <GL/glut.h>
 
 static int use20syntax = 1;
-static int Width = 550;
+static int Width = 650;
 static int Height = 200;
 static const GLfloat Near = 5.0, Far = 25.0;
 
@@ -69,7 +70,7 @@ static void Display( void )
     */
 
    glDisable(GL_STENCIL_TEST);
-   glTranslatef(-6.0, 0, 0);
+   glTranslatef(-7.0, 0, 0);
    glBegin(GL_QUADS);
    glColor3f( 0.5, 0.5, 0.5 );
    glVertex2f(-1, -1);
@@ -84,6 +85,9 @@ static void Display( void )
    /* Draw the first two squares using incr for the affected face
     */
 
+   /*************************************************************************
+    * 2nd square
+    */
    if (use20syntax) {
       stencil_func_separate(GL_FRONT, GL_ALWAYS, 0, ~0);
       stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
@@ -97,8 +101,8 @@ static void Display( void )
    glTranslatef(3.0, 0, 0);
    glBegin(GL_QUADS);
    glColor3f( 0.9, 0.9, 0.9 );
-   /* this should be front facing */
    for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
+      /* this should be front facing */
       glVertex2f(-1, -1);
       glVertex2f( 1, -1);
       glVertex2f( 1,  1);
@@ -106,6 +110,7 @@ static void Display( void )
    }
    glEnd();
 
+   /* stencil vals should be equal to max_stencil */
    glStencilFunc(GL_EQUAL, max_stencil, ~0);
    glBegin(GL_QUADS);
    glColor3f( 0.5, 0.5, 0.5 );
@@ -115,6 +120,9 @@ static void Display( void )
    glVertex2f(-1,  1);
    glEnd();
 
+   /*************************************************************************
+    * 3rd square
+    */
    if (use20syntax) {
       stencil_func_separate(GL_FRONT, GL_ALWAYS, 0, ~0);
       stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
@@ -128,9 +136,8 @@ static void Display( void )
    glTranslatef(3.0, 0, 0);
    glBegin(GL_QUADS);
    glColor3f( 0.9, 0.9, 0.9 );
-
-   /* this should be back facing */
    for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
+      /* this should be back facing */
       glVertex2f(-1, -1);
       glVertex2f(-1,  1);
       glVertex2f( 1,  1);
@@ -138,6 +145,7 @@ static void Display( void )
    }
    glEnd();
 
+   /* stencil vals should be equal to max_stencil */
    glStencilFunc(GL_EQUAL, max_stencil, ~0);
    glBegin(GL_QUADS);
    glColor3f( 0.5, 0.5, 0.5 );
@@ -147,6 +155,9 @@ static void Display( void )
    glVertex2f(-1,  1);
    glEnd();
 
+   /*************************************************************************
+    * 4th square
+    */
    if (use20syntax) {
       stencil_func_separate(GL_FRONT, GL_NEVER, 0, ~0);
       stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
@@ -160,15 +171,13 @@ static void Display( void )
    glTranslatef(3.0, 0, 0);
    glBegin(GL_QUADS);
    glColor3f( 0.9, 0.9, 0.9 );
-
-   /* this should be back facing */
    for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
-   /* this should be back facing */
+      /* this should be back facing */
       glVertex2f(-1, -1);
       glVertex2f(-1,  1);
       glVertex2f( 1,  1);
       glVertex2f( 1, -1);
-   /* this should be front facing */
+      /* this should be front facing */
       glVertex2f(-1, -1);
       glVertex2f( 1, -1);
       glVertex2f( 1,  1);
@@ -176,6 +185,7 @@ static void Display( void )
    }
    glEnd();
 
+   /* stencil vals should be equal to max_stencil */
    glStencilFunc(GL_EQUAL, max_stencil, ~0);
    glBegin(GL_QUADS);
    glColor3f( 0.5, 0.5, 0.5 );
@@ -185,6 +195,9 @@ static void Display( void )
    glVertex2f(-1,  1);
    glEnd();
 
+   /*************************************************************************
+    * 5th square
+    */
    if (use20syntax) {
       stencil_func_separate(GL_FRONT, GL_ALWAYS, 0, ~0);
       stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
@@ -192,21 +205,19 @@ static void Display( void )
    else {
       stencil_func_separate_ati(GL_ALWAYS, GL_ALWAYS, 0, ~0);
    }
-   stencil_op_separate(GL_FRONT, GL_KEEP, GL_KEEP, GL_DECR);
-   stencil_op_separate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR);
+   stencil_op_separate(GL_FRONT, GL_KEEP, GL_KEEP, GL_INCR);
+   stencil_op_separate(GL_BACK, GL_KEEP, GL_KEEP, GL_DECR);
 
    glTranslatef(3.0, 0, 0);
    glBegin(GL_QUADS);
    glColor3f( 0.9, 0.9, 0.9 );
-
-   /* this should be back facing */
    for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
-   /* this should be back facing */
+      /* this should be back facing */
       glVertex2f(-1, -1);
       glVertex2f(-1,  1);
       glVertex2f( 1,  1);
       glVertex2f( 1, -1);
-   /* this should be front facing */
+      /* this should be front facing */
       glVertex2f(-1, -1);
       glVertex2f( 1, -1);
       glVertex2f( 1,  1);
@@ -223,6 +234,47 @@ static void Display( void )
    glVertex2f(-1,  1);
    glEnd();
 
+   /*************************************************************************
+    * 6th square
+    */
+   if (glutExtensionSupported("GL_EXT_stencil_wrap")) {
+      if (use20syntax) {
+         stencil_func_separate(GL_FRONT, GL_ALWAYS, 0, ~0);
+         stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
+      }
+      else {
+         stencil_func_separate_ati(GL_ALWAYS, GL_ALWAYS, 0, ~0);
+      }
+      stencil_op_separate(GL_FRONT, GL_KEEP, GL_KEEP, GL_KEEP);
+      stencil_op_separate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR_WRAP);
+
+      glTranslatef(3.0, 0, 0);
+      glBegin(GL_QUADS);
+      glColor3f( 0.9, 0.9, 0.9 );
+      for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
+         /* this should be back facing */
+         glVertex2f(-1, -1);
+         glVertex2f(-1,  1);
+         glVertex2f( 1,  1);
+         glVertex2f( 1, -1);
+         /* this should be front facing */
+         glVertex2f(-1, -1);
+         glVertex2f( 1, -1);
+         glVertex2f( 1,  1);
+         glVertex2f(-1,  1);
+      }
+      glEnd();
+
+      glStencilFunc(GL_EQUAL, 260 - 255, ~0);
+      glBegin(GL_QUADS);
+      glColor3f( 0.5, 0.5, 0.5 );
+      glVertex2f(-1, -1);
+      glVertex2f( 1, -1);
+      glVertex2f( 1,  1);
+      glVertex2f(-1,  1);
+      glEnd();
+   }
+
    glPopMatrix();
 
    glutSwapBuffers();
@@ -273,11 +325,11 @@ static void Init( void )
    if (atof( ver_string ) < 2.0) {
       use20syntax = 0;
    }
-   stencil_func_separate = glutGetProcAddress( "glStencilFuncSeparate" );
-   stencil_func_separate_ati = glutGetProcAddress( "glStencilFuncSeparateATI" );
-   stencil_op_separate = glutGetProcAddress( "glStencilOpSeparate" );
+   stencil_func_separate = (PFNGLSTENCILFUNCSEPARATEPROC) glutGetProcAddress( "glStencilFuncSeparate" );
+   stencil_func_separate_ati = (PFNGLSTENCILFUNCSEPARATEATIPROC) glutGetProcAddress( "glStencilFuncSeparateATI" );
+   stencil_op_separate = (PFNGLSTENCILOPSEPARATEPROC) glutGetProcAddress( "glStencilOpSeparate" );
 
-   printf("\nAll 5 squares should be the same color.\n");
+   printf("\nAll 5 (or 6) squares should be the same color.\n");
 }
 
 
@@ -288,6 +340,7 @@ int main( int argc, char *argv[] )
    glutInitWindowSize( Width, Height );
    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL );
    glutCreateWindow( "GL_ATI_separate_stencil test" );
+   glewInit();
    glutReshapeFunc( Reshape );
    glutKeyboardFunc( Key );
    glutDisplayFunc( Display );