Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

stencilwrap.c 7.3KB

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