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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. GLboolean wrapping;
  12. static void RunTest(void)
  13. {
  14. const GLenum prim = GL_QUAD_STRIP;
  15. GLubyte val;
  16. int bits, max, i;
  17. int expected;
  18. GLboolean failed;
  19. glGetIntegerv(GL_STENCIL_BITS, &bits);
  20. max = (1 << bits) - 1;
  21. glEnable(GL_STENCIL_TEST);
  22. glStencilFunc(GL_ALWAYS, 0, ~0);
  23. /* test GL_KEEP */
  24. glClearStencil(max);
  25. glClear(GL_STENCIL_BUFFER_BIT);
  26. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  27. failed = GL_FALSE;
  28. printf("Testing GL_KEEP...\n");
  29. expected = max;
  30. glBegin(prim);
  31. glVertex2f(0, 0);
  32. glVertex2f(10, 0);
  33. glVertex2f(0, 10);
  34. glVertex2f(10, 10);
  35. glEnd();
  36. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  37. if (val != expected) {
  38. printf("Failed GL_KEEP test(got %u, expected %u)\n", val, expected);
  39. failed = GL_TRUE;
  40. }
  41. else
  42. printf("OK!\n");
  43. /* test GL_ZERO */
  44. glClearStencil(max);
  45. glClear(GL_STENCIL_BUFFER_BIT);
  46. glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO);
  47. failed = GL_FALSE;
  48. printf("Testing GL_ZERO...\n");
  49. expected = 0;
  50. glBegin(prim);
  51. glVertex2f(0, 0);
  52. glVertex2f(10, 0);
  53. glVertex2f(0, 10);
  54. glVertex2f(10, 10);
  55. glEnd();
  56. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  57. if (val != expected) {
  58. printf("Failed GL_ZERO test(got %u, expected %u)\n", val, expected);
  59. failed = GL_TRUE;
  60. }
  61. else
  62. printf("OK!\n");
  63. /* test GL_REPLACE */
  64. glClearStencil(max);
  65. glClear(GL_STENCIL_BUFFER_BIT);
  66. glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  67. failed = GL_FALSE;
  68. printf("Testing GL_REPLACE...\n");
  69. expected = 0;
  70. glBegin(prim);
  71. glVertex2f(0, 0);
  72. glVertex2f(10, 0);
  73. glVertex2f(0, 10);
  74. glVertex2f(10, 10);
  75. glEnd();
  76. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  77. if (val != expected) {
  78. printf("Failed GL_REPLACE test(got %u, expected %u)\n", val, expected);
  79. failed = GL_TRUE;
  80. }
  81. else
  82. printf("OK!\n");
  83. /* test GL_INCR (saturation) */
  84. glClearStencil(0);
  85. glClear(GL_STENCIL_BUFFER_BIT);
  86. glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
  87. failed = GL_FALSE;
  88. printf("Testing GL_INCR...\n");
  89. for (i = 1; i < max+10; i++) {
  90. expected = (i > max) ? max : i;
  91. glBegin(prim);
  92. glVertex2f(0, 0); glVertex2f(10, 0);
  93. glVertex2f(0, 10); glVertex2f(10, 10);
  94. glEnd();
  95. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  96. if (val != expected) {
  97. printf( "Failed GL_INCR test on iteration #%u "
  98. "(got %u, expected %u)\n", i, val, expected );
  99. failed = GL_TRUE;
  100. }
  101. }
  102. if ( !failed )
  103. printf("OK!\n");
  104. /* test GL_DECR (saturation) */
  105. glClearStencil(max);
  106. glClear(GL_STENCIL_BUFFER_BIT);
  107. glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
  108. failed = GL_FALSE;
  109. printf("Testing GL_DECR...\n");
  110. for (i = max-1; i > -10; i--) {
  111. expected = (i < 0) ? 0 : i;
  112. glBegin(prim);
  113. glVertex2f(0, 0); glVertex2f(10, 0);
  114. glVertex2f(0, 10); glVertex2f(10, 10);
  115. glEnd();
  116. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  117. if (val != expected) {
  118. printf( "Failed GL_DECR test on iteration #%u "
  119. "(got %u, expected %u)\n", max - i, val, expected );
  120. failed = GL_TRUE;
  121. }
  122. }
  123. if ( !failed )
  124. printf("OK!\n");
  125. /* test GL_INVERT */
  126. glClearStencil(0);
  127. glClear(GL_STENCIL_BUFFER_BIT);
  128. glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT);
  129. failed = GL_FALSE;
  130. printf("Testing GL_INVERT...\n");
  131. expected = max;
  132. glBegin(prim);
  133. glVertex2f(0, 0);
  134. glVertex2f(10, 0);
  135. glVertex2f(0, 10);
  136. glVertex2f(10, 10);
  137. glEnd();
  138. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  139. if (val != expected) {
  140. printf("Failed GL_INVERT test(got %u, expected %u)\n", val, expected);
  141. failed = GL_TRUE;
  142. }
  143. else
  144. printf("OK!\n");
  145. if(wrapping)
  146. {
  147. /* test GL_INCR_WRAP_EXT (wrap around) */
  148. glClearStencil(0);
  149. glClear(GL_STENCIL_BUFFER_BIT);
  150. glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP_EXT);
  151. failed = GL_FALSE;
  152. printf("Testing GL_INCR_WRAP_EXT...\n");
  153. for (i = 1; i < max+10; i++) {
  154. expected = i % (max + 1);
  155. glBegin(prim);
  156. glVertex2f(0, 0); glVertex2f(10, 0);
  157. glVertex2f(0, 10); glVertex2f(10, 10);
  158. glEnd();
  159. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  160. if (val != expected) {
  161. printf( "Failed GL_INCR_WRAP test on iteration #%u "
  162. "(got %u, expected %u)\n", i, val, expected );
  163. failed = GL_TRUE;
  164. }
  165. }
  166. if ( !failed )
  167. printf("OK!\n");
  168. /* test GL_DECR_WRAP_EXT (wrap-around) */
  169. glClearStencil(max);
  170. glClear(GL_STENCIL_BUFFER_BIT);
  171. glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);
  172. failed = GL_FALSE;
  173. printf("Testing GL_DECR_WRAP_EXT...\n");
  174. for (i = max-1; i > -10; i--) {
  175. expected = (i < 0) ? max + i + 1: i;
  176. glBegin(prim);
  177. glVertex2f(0, 0); glVertex2f(10, 0);
  178. glVertex2f(0, 10); glVertex2f(10, 10);
  179. glEnd();
  180. glReadPixels(0, 0, 1, 1, GL_STENCIL_INDEX, GL_UNSIGNED_BYTE, &val);
  181. if (val != expected) {
  182. printf( "Failed GL_DECR_WRAP test on iteration #%u "
  183. "(got %u, expected %u)\n", max - i, val, expected );
  184. failed = GL_TRUE;
  185. }
  186. }
  187. if ( !failed )
  188. printf("OK!\n");
  189. }
  190. glDisable(GL_STENCIL_TEST);
  191. }
  192. static void Display( void )
  193. {
  194. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  195. RunTest();
  196. glutSwapBuffers();
  197. }
  198. static void Reshape( int width, int height )
  199. {
  200. glViewport( 0, 0, width, height );
  201. glMatrixMode( GL_PROJECTION );
  202. glLoadIdentity();
  203. glOrtho(0, width, 0, height, -1, 1);
  204. glMatrixMode( GL_MODELVIEW );
  205. glLoadIdentity();
  206. }
  207. static void Key( unsigned char key, int x, int y )
  208. {
  209. (void) x;
  210. (void) y;
  211. switch (key) {
  212. case 27:
  213. exit(0);
  214. break;
  215. }
  216. glutPostRedisplay();
  217. }
  218. static void Init( void )
  219. {
  220. const char * ver_str;
  221. float version;
  222. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  223. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  224. /* Check for both the extension string and GL version 1.4 on the
  225. * outside chance that some vendor exports version 1.4 but doesn't
  226. * export the extension string. The stencil-wrap modes are a required
  227. * part of GL 1.4.
  228. */
  229. ver_str = glGetString( GL_VERSION );
  230. version = (ver_str == NULL) ? 1.0 : atof( ver_str );
  231. wrapping = (glutExtensionSupported("GL_EXT_stencil_wrap") || (version >= 1.4));
  232. if (!wrapping)
  233. printf("GL_EXT_stencil_wrap not supported. Only testing the rest.\n");
  234. }
  235. int main( int argc, char *argv[] )
  236. {
  237. glutInit( &argc, argv );
  238. glutInitWindowPosition( 0, 0 );
  239. glutInitWindowSize( 400, 400 );
  240. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_STENCIL );
  241. glutCreateWindow(argv[0]);
  242. glutReshapeFunc( Reshape );
  243. glutKeyboardFunc( Key );
  244. glutDisplayFunc( Display );
  245. Init();
  246. glutMainLoop();
  247. return 0;
  248. }