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.6KB

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