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.

stencil_wrap.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * (C) Copyright IBM Corporation 2004
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * on the rights to use, copy, modify, merge, publish, distribute, sub
  9. * license, and/or sell copies of the Software, and to permit persons to whom
  10. * the Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the next
  13. * paragraph) shall be included in all copies or substantial portions of the
  14. * Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /**
  25. * \file stencil_wrap.c
  26. *
  27. * Simple test of GL_EXT_stencil_wrap functionality. Four squares are drawn
  28. * with different stencil modes, but all should be rendered with the same
  29. * final color.
  30. *
  31. * \author Ian Romanick <idr@us.ibm.com>
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <GL/glut.h>
  36. static int Width = 550;
  37. static int Height = 200;
  38. static const GLfloat Near = 5.0, Far = 25.0;
  39. static void Display( void )
  40. {
  41. GLint max_stencil;
  42. GLint stencil_bits;
  43. unsigned i;
  44. glGetIntegerv( GL_STENCIL_BITS, & stencil_bits );
  45. max_stencil = (1U << stencil_bits) - 1;
  46. printf( "Stencil bits = %u, maximum stencil value = 0x%08x\n",
  47. stencil_bits, max_stencil );
  48. glClearStencil( 0 );
  49. glClearColor( 0.2, 0.2, 0.8, 0 );
  50. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
  51. | GL_STENCIL_BUFFER_BIT );
  52. glPushMatrix();
  53. /* This is the "reference" square.
  54. */
  55. glDisable(GL_STENCIL_TEST);
  56. glTranslatef(-6.0, 0, 0);
  57. glBegin(GL_QUADS);
  58. glColor3f( 0.5, 0.5, 0.5 );
  59. glVertex2f(-1, -1);
  60. glVertex2f( 1, -1);
  61. glVertex2f( 1, 1);
  62. glVertex2f(-1, 1);
  63. glEnd();
  64. glEnable(GL_STENCIL_TEST);
  65. /* Draw the first two squares using the two non-wrap (i.e., saturate)
  66. * modes.
  67. */
  68. glStencilFunc(GL_ALWAYS, 0, ~0);
  69. glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
  70. glTranslatef(3.0, 0, 0);
  71. glBegin(GL_QUADS);
  72. glColor3f( 0.9, 0.9, 0.9 );
  73. for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
  74. glVertex2f(-1, -1);
  75. glVertex2f( 1, -1);
  76. glVertex2f( 1, 1);
  77. glVertex2f(-1, 1);
  78. }
  79. glEnd();
  80. glStencilFunc(GL_EQUAL, max_stencil, ~0);
  81. glBegin(GL_QUADS);
  82. glColor3f( 0.5, 0.5, 0.5 );
  83. glVertex2f(-1, -1);
  84. glVertex2f( 1, -1);
  85. glVertex2f( 1, 1);
  86. glVertex2f(-1, 1);
  87. glEnd();
  88. glStencilFunc(GL_ALWAYS, 0, ~0);
  89. glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
  90. glTranslatef(3.0, 0, 0);
  91. glBegin(GL_QUADS);
  92. glColor3f( 0.9, 0.9, 0.9 );
  93. for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
  94. glVertex2f(-1, -1);
  95. glVertex2f( 1, -1);
  96. glVertex2f( 1, 1);
  97. glVertex2f(-1, 1);
  98. }
  99. glEnd();
  100. glStencilFunc(GL_EQUAL, 0, ~0);
  101. glBegin(GL_QUADS);
  102. glColor3f( 0.5, 0.5, 0.5 );
  103. glVertex2f(-1, -1);
  104. glVertex2f( 1, -1);
  105. glVertex2f( 1, 1);
  106. glVertex2f(-1, 1);
  107. glEnd();
  108. /* Draw the last two squares using the two wrap modes.
  109. */
  110. glStencilFunc(GL_ALWAYS, 0, ~0);
  111. glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP);
  112. glTranslatef(3.0, 0, 0);
  113. glBegin(GL_QUADS);
  114. glColor3f( 0.9, 0.9, 0.9 );
  115. for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
  116. glVertex2f(-1, -1);
  117. glVertex2f( 1, -1);
  118. glVertex2f( 1, 1);
  119. glVertex2f(-1, 1);
  120. }
  121. glEnd();
  122. glStencilFunc(GL_EQUAL, 4, ~0);
  123. glBegin(GL_QUADS);
  124. glColor3f( 0.5, 0.5, 0.5 );
  125. glVertex2f(-1, -1);
  126. glVertex2f( 1, -1);
  127. glVertex2f( 1, 1);
  128. glVertex2f(-1, 1);
  129. glEnd();
  130. glStencilFunc(GL_ALWAYS, 0, ~0);
  131. glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP);
  132. glTranslatef(3.0, 0, 0);
  133. glBegin(GL_QUADS);
  134. glColor3f( 0.9, 0.9, 0.9 );
  135. for ( i = 0 ; i < 5 ; i++ ) {
  136. glVertex2f(-1, -1);
  137. glVertex2f( 1, -1);
  138. glVertex2f( 1, 1);
  139. glVertex2f(-1, 1);
  140. }
  141. glEnd();
  142. glStencilFunc(GL_EQUAL, (max_stencil - 4), ~0);
  143. glBegin(GL_QUADS);
  144. glColor3f( 0.5, 0.5, 0.5 );
  145. glVertex2f(-1, -1);
  146. glVertex2f( 1, -1);
  147. glVertex2f( 1, 1);
  148. glVertex2f(-1, 1);
  149. glEnd();
  150. glPopMatrix();
  151. glutSwapBuffers();
  152. }
  153. static void Reshape( int width, int height )
  154. {
  155. GLfloat ar = (float) width / (float) height;
  156. Width = width;
  157. Height = height;
  158. glViewport( 0, 0, width, height );
  159. glMatrixMode( GL_PROJECTION );
  160. glLoadIdentity();
  161. glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
  162. glMatrixMode( GL_MODELVIEW );
  163. glLoadIdentity();
  164. glTranslatef( 0.0, 0.0, -15.0 );
  165. }
  166. static void Key( unsigned char key, int x, int y )
  167. {
  168. (void) x;
  169. (void) y;
  170. switch (key) {
  171. case 27:
  172. exit(0);
  173. break;
  174. }
  175. glutPostRedisplay();
  176. }
  177. static void Init( void )
  178. {
  179. const char * const ver_string = (const char * const)
  180. glGetString( GL_VERSION );
  181. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  182. printf("GL_VERSION = %s\n", ver_string);
  183. if ( !glutExtensionSupported("GL_EXT_stencil_wrap")
  184. && (atof( ver_string ) < 1.4) ) {
  185. printf("Sorry, this program requires either GL_EXT_stencil_wrap or OpenGL 1.4.\n");
  186. exit(1);
  187. }
  188. printf("\nAll 5 squares should be the same color.\n");
  189. glEnable( GL_BLEND );
  190. }
  191. int main( int argc, char *argv[] )
  192. {
  193. glutInit( &argc, argv );
  194. glutInitWindowPosition( 0, 0 );
  195. glutInitWindowSize( Width, Height );
  196. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL );
  197. glutCreateWindow( "GL_EXT_stencil_wrap test" );
  198. glutReshapeFunc( Reshape );
  199. glutKeyboardFunc( Key );
  200. glutDisplayFunc( Display );
  201. Init();
  202. glutMainLoop();
  203. return 0;
  204. }