Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

stencil_twoside.c 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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_twoside.c
  26. *
  27. * Simple test of GL_ATI_separate_stencil (or the OGL 2.0 equivalent) functionality.
  28. * Five squares (or six if stencil wrap is available) are drawn
  29. * with different stencil modes, but all should be rendered with the same
  30. * final color.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <GL/glew.h>
  35. #include <GL/glut.h>
  36. static int use20syntax = 1;
  37. static int Width = 650;
  38. static int Height = 200;
  39. static const GLfloat Near = 5.0, Far = 25.0;
  40. static PFNGLSTENCILFUNCSEPARATEPROC stencil_func_separate = NULL;
  41. static PFNGLSTENCILFUNCSEPARATEATIPROC stencil_func_separate_ati = NULL;
  42. static PFNGLSTENCILOPSEPARATEPROC stencil_op_separate = NULL;
  43. static void Display( void )
  44. {
  45. GLint max_stencil;
  46. GLint stencil_bits;
  47. unsigned i;
  48. glGetIntegerv( GL_STENCIL_BITS, & stencil_bits );
  49. max_stencil = (1U << stencil_bits) - 1;
  50. printf( "Stencil bits = %u, maximum stencil value = 0x%08x\n",
  51. stencil_bits, max_stencil );
  52. glClearStencil( 1 );
  53. glClearColor( 0.2, 0.2, 0.8, 0 );
  54. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
  55. | GL_STENCIL_BUFFER_BIT );
  56. glPushMatrix();
  57. /* This is the "reference" square.
  58. */
  59. glDisable(GL_STENCIL_TEST);
  60. glTranslatef(-7.0, 0, 0);
  61. glBegin(GL_QUADS);
  62. glColor3f( 0.5, 0.5, 0.5 );
  63. glVertex2f(-1, -1);
  64. glVertex2f( 1, -1);
  65. glVertex2f( 1, 1);
  66. glVertex2f(-1, 1);
  67. glEnd();
  68. glEnable(GL_STENCIL_TEST);
  69. /* Draw the first two squares using incr for the affected face
  70. */
  71. /*************************************************************************
  72. * 2nd square
  73. */
  74. if (use20syntax) {
  75. stencil_func_separate(GL_FRONT, GL_ALWAYS, 0, ~0);
  76. stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
  77. }
  78. else {
  79. stencil_func_separate_ati(GL_ALWAYS, GL_ALWAYS, 0, ~0);
  80. }
  81. stencil_op_separate(GL_FRONT, GL_KEEP, GL_KEEP, GL_INCR);
  82. stencil_op_separate(GL_BACK, GL_KEEP, GL_KEEP, GL_DECR);
  83. glTranslatef(3.0, 0, 0);
  84. glBegin(GL_QUADS);
  85. glColor3f( 0.9, 0.9, 0.9 );
  86. for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
  87. /* this should be front facing */
  88. glVertex2f(-1, -1);
  89. glVertex2f( 1, -1);
  90. glVertex2f( 1, 1);
  91. glVertex2f(-1, 1);
  92. }
  93. glEnd();
  94. /* stencil vals should be equal to max_stencil */
  95. glStencilFunc(GL_EQUAL, max_stencil, ~0);
  96. glBegin(GL_QUADS);
  97. glColor3f( 0.5, 0.5, 0.5 );
  98. glVertex2f(-1, -1);
  99. glVertex2f( 1, -1);
  100. glVertex2f( 1, 1);
  101. glVertex2f(-1, 1);
  102. glEnd();
  103. /*************************************************************************
  104. * 3rd square
  105. */
  106. if (use20syntax) {
  107. stencil_func_separate(GL_FRONT, GL_ALWAYS, 0, ~0);
  108. stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
  109. }
  110. else {
  111. stencil_func_separate_ati(GL_ALWAYS, GL_ALWAYS, 0, ~0);
  112. }
  113. stencil_op_separate(GL_FRONT, GL_KEEP, GL_KEEP, GL_DECR);
  114. stencil_op_separate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR);
  115. glTranslatef(3.0, 0, 0);
  116. glBegin(GL_QUADS);
  117. glColor3f( 0.9, 0.9, 0.9 );
  118. for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
  119. /* this should be back facing */
  120. glVertex2f(-1, -1);
  121. glVertex2f(-1, 1);
  122. glVertex2f( 1, 1);
  123. glVertex2f( 1, -1);
  124. }
  125. glEnd();
  126. /* stencil vals should be equal to max_stencil */
  127. glStencilFunc(GL_EQUAL, max_stencil, ~0);
  128. glBegin(GL_QUADS);
  129. glColor3f( 0.5, 0.5, 0.5 );
  130. glVertex2f(-1, -1);
  131. glVertex2f( 1, -1);
  132. glVertex2f( 1, 1);
  133. glVertex2f(-1, 1);
  134. glEnd();
  135. /*************************************************************************
  136. * 4th square
  137. */
  138. if (use20syntax) {
  139. stencil_func_separate(GL_FRONT, GL_NEVER, 0, ~0);
  140. stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
  141. }
  142. else {
  143. stencil_func_separate_ati(GL_NEVER, GL_ALWAYS, 0, ~0);
  144. }
  145. stencil_op_separate(GL_FRONT, GL_KEEP, GL_KEEP, GL_DECR);
  146. stencil_op_separate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR);
  147. glTranslatef(3.0, 0, 0);
  148. glBegin(GL_QUADS);
  149. glColor3f( 0.9, 0.9, 0.9 );
  150. for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
  151. /* this should be back facing */
  152. glVertex2f(-1, -1);
  153. glVertex2f(-1, 1);
  154. glVertex2f( 1, 1);
  155. glVertex2f( 1, -1);
  156. /* this should be front facing */
  157. glVertex2f(-1, -1);
  158. glVertex2f( 1, -1);
  159. glVertex2f( 1, 1);
  160. glVertex2f(-1, 1);
  161. }
  162. glEnd();
  163. /* stencil vals should be equal to max_stencil */
  164. glStencilFunc(GL_EQUAL, max_stencil, ~0);
  165. glBegin(GL_QUADS);
  166. glColor3f( 0.5, 0.5, 0.5 );
  167. glVertex2f(-1, -1);
  168. glVertex2f( 1, -1);
  169. glVertex2f( 1, 1);
  170. glVertex2f(-1, 1);
  171. glEnd();
  172. /*************************************************************************
  173. * 5th square
  174. */
  175. if (use20syntax) {
  176. stencil_func_separate(GL_FRONT, GL_ALWAYS, 0, ~0);
  177. stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
  178. }
  179. else {
  180. stencil_func_separate_ati(GL_ALWAYS, GL_ALWAYS, 0, ~0);
  181. }
  182. stencil_op_separate(GL_FRONT, GL_KEEP, GL_KEEP, GL_INCR);
  183. stencil_op_separate(GL_BACK, GL_KEEP, GL_KEEP, GL_DECR);
  184. glTranslatef(3.0, 0, 0);
  185. glBegin(GL_QUADS);
  186. glColor3f( 0.9, 0.9, 0.9 );
  187. for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
  188. /* this should be back facing */
  189. glVertex2f(-1, -1);
  190. glVertex2f(-1, 1);
  191. glVertex2f( 1, 1);
  192. glVertex2f( 1, -1);
  193. /* this should be front facing */
  194. glVertex2f(-1, -1);
  195. glVertex2f( 1, -1);
  196. glVertex2f( 1, 1);
  197. glVertex2f(-1, 1);
  198. }
  199. glEnd();
  200. glStencilFunc(GL_EQUAL, 1, ~0);
  201. glBegin(GL_QUADS);
  202. glColor3f( 0.5, 0.5, 0.5 );
  203. glVertex2f(-1, -1);
  204. glVertex2f( 1, -1);
  205. glVertex2f( 1, 1);
  206. glVertex2f(-1, 1);
  207. glEnd();
  208. /*************************************************************************
  209. * 6th square
  210. */
  211. if (glutExtensionSupported("GL_EXT_stencil_wrap")) {
  212. if (use20syntax) {
  213. stencil_func_separate(GL_FRONT, GL_ALWAYS, 0, ~0);
  214. stencil_func_separate(GL_BACK, GL_ALWAYS, 0, ~0);
  215. }
  216. else {
  217. stencil_func_separate_ati(GL_ALWAYS, GL_ALWAYS, 0, ~0);
  218. }
  219. stencil_op_separate(GL_FRONT, GL_KEEP, GL_KEEP, GL_KEEP);
  220. stencil_op_separate(GL_BACK, GL_KEEP, GL_KEEP, GL_INCR_WRAP);
  221. glTranslatef(3.0, 0, 0);
  222. glBegin(GL_QUADS);
  223. glColor3f( 0.9, 0.9, 0.9 );
  224. for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
  225. /* this should be back facing */
  226. glVertex2f(-1, -1);
  227. glVertex2f(-1, 1);
  228. glVertex2f( 1, 1);
  229. glVertex2f( 1, -1);
  230. /* this should be front facing */
  231. glVertex2f(-1, -1);
  232. glVertex2f( 1, -1);
  233. glVertex2f( 1, 1);
  234. glVertex2f(-1, 1);
  235. }
  236. glEnd();
  237. glStencilFunc(GL_EQUAL, 260 - 255, ~0);
  238. glBegin(GL_QUADS);
  239. glColor3f( 0.5, 0.5, 0.5 );
  240. glVertex2f(-1, -1);
  241. glVertex2f( 1, -1);
  242. glVertex2f( 1, 1);
  243. glVertex2f(-1, 1);
  244. glEnd();
  245. }
  246. glPopMatrix();
  247. glutSwapBuffers();
  248. }
  249. static void Reshape( int width, int height )
  250. {
  251. GLfloat ar = (float) width / (float) height;
  252. Width = width;
  253. Height = height;
  254. glViewport( 0, 0, width, height );
  255. glMatrixMode( GL_PROJECTION );
  256. glLoadIdentity();
  257. glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
  258. glMatrixMode( GL_MODELVIEW );
  259. glLoadIdentity();
  260. glTranslatef( 0.0, 0.0, -15.0 );
  261. }
  262. static void Key( unsigned char key, int x, int y )
  263. {
  264. (void) x;
  265. (void) y;
  266. switch (key) {
  267. case 27:
  268. exit(0);
  269. break;
  270. }
  271. glutPostRedisplay();
  272. }
  273. static void Init( void )
  274. {
  275. const char * const ver_string = (const char * const)
  276. glGetString( GL_VERSION );
  277. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  278. printf("GL_VERSION = %s\n", ver_string);
  279. if ( !glutExtensionSupported("GL_ATI_separate_stencil")
  280. && (atof( ver_string ) < 2.0) ) {
  281. printf("Sorry, this program requires either GL_ATI_separate_stencil or OpenGL 2.0.\n");
  282. exit(1);
  283. }
  284. if (atof( ver_string ) < 2.0) {
  285. use20syntax = 0;
  286. }
  287. stencil_func_separate = (PFNGLSTENCILFUNCSEPARATEPROC) glutGetProcAddress( "glStencilFuncSeparate" );
  288. stencil_func_separate_ati = (PFNGLSTENCILFUNCSEPARATEATIPROC) glutGetProcAddress( "glStencilFuncSeparateATI" );
  289. stencil_op_separate = (PFNGLSTENCILOPSEPARATEPROC) glutGetProcAddress( "glStencilOpSeparate" );
  290. printf("\nAll 5 (or 6) squares should be the same color.\n");
  291. }
  292. int main( int argc, char *argv[] )
  293. {
  294. glutInit( &argc, argv );
  295. glutInitWindowPosition( 0, 0 );
  296. glutInitWindowSize( Width, Height );
  297. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL );
  298. glutCreateWindow( "GL_ATI_separate_stencil test" );
  299. glewInit();
  300. glutReshapeFunc( Reshape );
  301. glutKeyboardFunc( Key );
  302. glutDisplayFunc( Display );
  303. Init();
  304. glutMainLoop();
  305. return 0;
  306. }