Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stencilwrap.c 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include <assert.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <math.h>
  10. #include <GL/glut.h>
  11. static void RunTest(void)
  12. {
  13. const GLenum prim = GL_QUAD_STRIP;
  14. GLubyte val;
  15. int bits, max, i;
  16. GLboolean failed;
  17. glGetIntegerv(GL_STENCIL_BITS, &bits);
  18. max = (1 << bits) - 1;
  19. glClearStencil(0);
  20. glEnable(GL_STENCIL_TEST);
  21. glStencilFunc(GL_ALWAYS, 0, ~0);
  22. /* test GL_INCR (saturation) */
  23. glClear(GL_STENCIL_BUFFER_BIT);
  24. glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
  25. failed = GL_FALSE;
  26. printf("Testing GL_INCR...\n");
  27. for (i = 1; i < max+10; i++) {
  28. int expected = (i > max) ? max : i;
  29. glBegin(prim);
  30. glVertex2f(0, 0); glVertex2f(10, 0);
  31. glVertex2f(0, 10); glVertex2f(10, 10);
  32. glEnd();
  33. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  34. if (val != expected) {
  35. printf( "Failed GL_INCR test on iteration #%u "
  36. "(got %u, expected %u)\n", i, val, expected );
  37. failed = GL_TRUE;
  38. }
  39. }
  40. if ( !failed ) printf("OK!\n");
  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. failed = GL_FALSE;
  45. printf("Testing GL_INCR_WRAP_EXT...\n");
  46. for (i = 1; i < max+10; i++) {
  47. int expected = i % (max + 1);
  48. glBegin(prim);
  49. glVertex2f(0, 0); glVertex2f(10, 0);
  50. glVertex2f(0, 10); glVertex2f(10, 10);
  51. glEnd();
  52. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  53. if (val != expected) {
  54. printf( "Failed GL_INCR_WRAP test on iteration #%u "
  55. "(got %u, expected %u)\n", i, val, expected );
  56. failed = GL_TRUE;
  57. }
  58. }
  59. if ( !failed ) printf("OK!\n");
  60. glClearStencil(max);
  61. /* test GL_INCR (saturation) */
  62. glClear(GL_STENCIL_BUFFER_BIT);
  63. glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
  64. failed = GL_FALSE;
  65. printf("Testing GL_DECR...\n");
  66. for (i = max-1; i > -10; i--) {
  67. int expected = (i < 0) ? 0 : i;
  68. glBegin(prim);
  69. glVertex2f(0, 0); glVertex2f(10, 0);
  70. glVertex2f(0, 10); glVertex2f(10, 10);
  71. glEnd();
  72. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  73. if (val != expected) {
  74. printf( "Failed GL_DECR test on iteration #%u "
  75. "(got %u, expected %u)\n", max - i, val, expected );
  76. failed = GL_TRUE;
  77. }
  78. }
  79. if ( !failed ) printf("OK!\n");
  80. /* test GL_INCR_WRAP_EXT (wrap-around) */
  81. glClear(GL_STENCIL_BUFFER_BIT);
  82. glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);
  83. failed = GL_FALSE;
  84. printf("Testing GL_DECR_WRAP_EXT...\n");
  85. for (i = max-1; i > -10; i--) {
  86. int expected = (i < 0) ? max + i + 1: i;
  87. glBegin(prim);
  88. glVertex2f(0, 0); glVertex2f(10, 0);
  89. glVertex2f(0, 10); glVertex2f(10, 10);
  90. glEnd();
  91. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  92. if (val != expected) {
  93. printf( "Failed GL_DECR_WRAP test on iteration #%u "
  94. "(got %u, expected %u)\n", max - i, val, expected );
  95. failed = GL_TRUE;
  96. }
  97. }
  98. if ( !failed ) printf("OK!\n");
  99. glDisable(GL_STENCIL_TEST);
  100. }
  101. static void Display( void )
  102. {
  103. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  104. RunTest();
  105. glutSwapBuffers();
  106. }
  107. static void Reshape( int width, int height )
  108. {
  109. glViewport( 0, 0, width, height );
  110. glMatrixMode( GL_PROJECTION );
  111. glLoadIdentity();
  112. glOrtho(0, width, 0, height, -1, 1);
  113. glMatrixMode( GL_MODELVIEW );
  114. glLoadIdentity();
  115. }
  116. static void Key( unsigned char key, int x, int y )
  117. {
  118. (void) x;
  119. (void) y;
  120. switch (key) {
  121. case 27:
  122. exit(0);
  123. break;
  124. }
  125. glutPostRedisplay();
  126. }
  127. static void Init( void )
  128. {
  129. const char * ver_str;
  130. float version;
  131. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  132. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  133. /* Check for both the extension string and GL version 1.4 on the
  134. * outside chance that some silly vendor exports version 1.4 but doesn't
  135. * export the extension string. The stencil-wrap modes are a required
  136. * part of GL 1.4.
  137. */
  138. ver_str = glGetString( GL_VERSION );
  139. version = (ver_str == NULL) ? 1.0 : atof( ver_str );
  140. if ( !glutExtensionSupported("GL_EXT_stencil_wrap")
  141. && (version < 1.4) ) {
  142. printf("Sorry, GL_EXT_stencil_wrap not supported.\n");
  143. exit(1);
  144. }
  145. }
  146. int main( int argc, char *argv[] )
  147. {
  148. glutInit( &argc, argv );
  149. glutInitWindowPosition( 0, 0 );
  150. glutInitWindowSize( 400, 400 );
  151. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL );
  152. glutCreateWindow(argv[0]);
  153. glutReshapeFunc( Reshape );
  154. glutKeyboardFunc( Key );
  155. glutDisplayFunc( Display );
  156. Init();
  157. glutMainLoop();
  158. return 0;
  159. }