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.

arbocclude.c 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * GL_ARB_occlusion_query demo
  3. *
  4. * Brian Paul
  5. * 12 June 2003
  6. *
  7. * Copyright (C) 2003 Brian Paul All Rights Reserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included
  17. * in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  23. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  24. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include <assert.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <math.h>
  31. #define GL_GLEXT_PROTOTYPES
  32. #include <GL/glut.h>
  33. #define TEST_DISPLAY_LISTS 0
  34. static GLboolean Anim = GL_TRUE;
  35. static GLfloat Xpos = 0;
  36. static GLuint OccQuery;
  37. static void
  38. PrintString(const char *s)
  39. {
  40. while (*s) {
  41. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  42. s++;
  43. }
  44. }
  45. static void Idle(void)
  46. {
  47. static int lastTime = 0;
  48. static int sign = +1;
  49. int time = glutGet(GLUT_ELAPSED_TIME);
  50. float step;
  51. if (lastTime == 0)
  52. lastTime = time;
  53. else if (time - lastTime < 20) /* 50Hz update */
  54. return;
  55. step = (time - lastTime) / 1000.0 * sign;
  56. lastTime = time;
  57. Xpos += step;
  58. if (Xpos > 2.5) {
  59. Xpos = 2.5;
  60. sign = -1;
  61. }
  62. else if (Xpos < -2.5) {
  63. Xpos = -2.5;
  64. sign = +1;
  65. }
  66. glutPostRedisplay();
  67. }
  68. static void Display( void )
  69. {
  70. GLuint passed;
  71. GLint ready;
  72. char s[100];
  73. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  74. glMatrixMode( GL_PROJECTION );
  75. glLoadIdentity();
  76. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  77. glMatrixMode( GL_MODELVIEW );
  78. glLoadIdentity();
  79. glTranslatef( 0.0, 0.0, -15.0 );
  80. /* draw the occluding polygons */
  81. glColor3f(0, 0.6, 0.8);
  82. glBegin(GL_QUADS);
  83. glVertex2f(-1.6, -1.5);
  84. glVertex2f(-0.4, -1.5);
  85. glVertex2f(-0.4, 1.5);
  86. glVertex2f(-1.6, 1.5);
  87. glVertex2f( 0.4, -1.5);
  88. glVertex2f( 1.6, -1.5);
  89. glVertex2f( 1.6, 1.5);
  90. glVertex2f( 0.4, 1.5);
  91. glEnd();
  92. /* draw the test polygon with occlusion testing */
  93. glPushMatrix();
  94. glTranslatef(Xpos, 0, -0.5);
  95. glScalef(0.3, 0.3, 1.0);
  96. glRotatef(-90.0 * Xpos, 0, 0, 1);
  97. #if defined(GL_ARB_occlusion_query)
  98. #if TEST_DISPLAY_LISTS
  99. glNewList(10, GL_COMPILE);
  100. glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
  101. glEndList();
  102. glCallList(10);
  103. #else
  104. glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
  105. #endif
  106. glColorMask(0, 0, 0, 0);
  107. glDepthMask(GL_FALSE);
  108. glBegin(GL_POLYGON);
  109. glVertex3f(-1, -1, 0);
  110. glVertex3f( 1, -1, 0);
  111. glVertex3f( 1, 1, 0);
  112. glVertex3f(-1, 1, 0);
  113. glEnd();
  114. #if TEST_DISPLAY_LISTS
  115. glNewList(11, GL_COMPILE);
  116. glEndQueryARB(GL_SAMPLES_PASSED_ARB);
  117. glEndList();
  118. glCallList(11);
  119. #else
  120. glEndQueryARB(GL_SAMPLES_PASSED_ARB);
  121. #endif
  122. do {
  123. /* do useful work here, if any */
  124. glGetQueryObjectivARB(OccQuery, GL_QUERY_RESULT_AVAILABLE_ARB, &ready);
  125. } while (!ready);
  126. glGetQueryObjectuivARB(OccQuery, GL_QUERY_RESULT_ARB, &passed);
  127. /* turn off occlusion testing */
  128. glColorMask(1, 1, 1, 1);
  129. glDepthMask(GL_TRUE);
  130. #endif /* GL_ARB_occlusion_query */
  131. /* draw the orange rect, so we can see what's going on */
  132. glColor3f(0.8, 0.5, 0);
  133. glBegin(GL_POLYGON);
  134. glVertex3f(-1, -1, 0);
  135. glVertex3f( 1, -1, 0);
  136. glVertex3f( 1, 1, 0);
  137. glVertex3f(-1, 1, 0);
  138. glEnd();
  139. glPopMatrix();
  140. /* Print result message */
  141. glMatrixMode( GL_PROJECTION );
  142. glLoadIdentity();
  143. glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  144. glMatrixMode( GL_MODELVIEW );
  145. glLoadIdentity();
  146. glColor3f(1, 1, 1);
  147. #if defined(GL_ARB_occlusion_query)
  148. sprintf(s, " %4d Fragments Visible", passed);
  149. glRasterPos3f(-0.50, -0.7, 0);
  150. PrintString(s);
  151. if (!passed) {
  152. glRasterPos3f(-0.25, -0.8, 0);
  153. PrintString("Fully Occluded");
  154. }
  155. #else
  156. glRasterPos3f(-0.25, -0.8, 0);
  157. PrintString("GL_ARB_occlusion_query not available at compile time");
  158. #endif /* GL_ARB_occlusion_query */
  159. glutSwapBuffers();
  160. }
  161. static void Reshape( int width, int height )
  162. {
  163. glViewport( 0, 0, width, height );
  164. }
  165. static void Key( unsigned char key, int x, int y )
  166. {
  167. (void) x;
  168. (void) y;
  169. switch (key) {
  170. case 27:
  171. exit(0);
  172. break;
  173. case ' ':
  174. Anim = !Anim;
  175. if (Anim)
  176. glutIdleFunc(Idle);
  177. else
  178. glutIdleFunc(NULL);
  179. break;
  180. }
  181. glutPostRedisplay();
  182. }
  183. static void SpecialKey( int key, int x, int y )
  184. {
  185. const GLfloat step = 0.1;
  186. (void) x;
  187. (void) y;
  188. switch (key) {
  189. case GLUT_KEY_LEFT:
  190. Xpos -= step;
  191. break;
  192. case GLUT_KEY_RIGHT:
  193. Xpos += step;
  194. break;
  195. }
  196. glutPostRedisplay();
  197. }
  198. static void Init( void )
  199. {
  200. const char *ext = (const char *) glGetString(GL_EXTENSIONS);
  201. GLint bits;
  202. if (!strstr(ext, "GL_ARB_occlusion_query")) {
  203. printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
  204. exit(-1);
  205. }
  206. #if defined(GL_ARB_occlusion_query)
  207. glGetQueryivARB(GL_SAMPLES_PASSED_ARB, GL_QUERY_COUNTER_BITS_ARB, &bits);
  208. if (!bits) {
  209. printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
  210. exit(-1);
  211. }
  212. #endif /* GL_ARB_occlusion_query */
  213. glGetIntegerv(GL_DEPTH_BITS, &bits);
  214. printf("Depthbits: %d\n", bits);
  215. #if defined(GL_ARB_occlusion_query)
  216. glGenQueriesARB(1, &OccQuery);
  217. assert(OccQuery > 0);
  218. #endif /* GL_ARB_occlusion_query */
  219. glEnable(GL_DEPTH_TEST);
  220. }
  221. int main( int argc, char *argv[] )
  222. {
  223. glutInit( &argc, argv );
  224. glutInitWindowPosition( 0, 0 );
  225. glutInitWindowSize( 400, 400 );
  226. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  227. glutCreateWindow(argv[0]);
  228. glutReshapeFunc( Reshape );
  229. glutKeyboardFunc( Key );
  230. glutSpecialFunc( SpecialKey );
  231. glutIdleFunc( Idle );
  232. glutDisplayFunc( Display );
  233. Init();
  234. glutMainLoop();
  235. return 0;
  236. }