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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. static GLboolean Anim = GL_TRUE;
  34. static GLfloat Xpos = 0;
  35. static GLuint OccQuery;
  36. static void
  37. PrintString(const char *s)
  38. {
  39. while (*s) {
  40. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  41. s++;
  42. }
  43. }
  44. static void Idle(void)
  45. {
  46. static int lastTime = 0;
  47. static int sign = +1;
  48. int time = glutGet(GLUT_ELAPSED_TIME);
  49. float step;
  50. if (lastTime == 0)
  51. lastTime = time;
  52. else if (time - lastTime < 20) /* 50Hz update */
  53. return;
  54. step = (time - lastTime) / 1000.0 * sign;
  55. lastTime = time;
  56. Xpos += step;
  57. if (Xpos > 2.5) {
  58. Xpos = 2.5;
  59. sign = -1;
  60. }
  61. else if (Xpos < -2.5) {
  62. Xpos = -2.5;
  63. sign = +1;
  64. }
  65. glutPostRedisplay();
  66. }
  67. static void Display( void )
  68. {
  69. GLuint passed;
  70. GLint ready;
  71. char s[100];
  72. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  73. glMatrixMode( GL_PROJECTION );
  74. glLoadIdentity();
  75. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  76. glMatrixMode( GL_MODELVIEW );
  77. glLoadIdentity();
  78. glTranslatef( 0.0, 0.0, -15.0 );
  79. /* draw the occluding polygons */
  80. glColor3f(0, 0.6, 0.8);
  81. glBegin(GL_QUADS);
  82. glVertex2f(-1.6, -1.5);
  83. glVertex2f(-0.4, -1.5);
  84. glVertex2f(-0.4, 1.5);
  85. glVertex2f(-1.6, 1.5);
  86. glVertex2f( 0.4, -1.5);
  87. glVertex2f( 1.6, -1.5);
  88. glVertex2f( 1.6, 1.5);
  89. glVertex2f( 0.4, 1.5);
  90. glEnd();
  91. /* draw the test polygon with occlusion testing */
  92. glPushMatrix();
  93. glTranslatef(Xpos, 0, -0.5);
  94. glScalef(0.3, 0.3, 1.0);
  95. glRotatef(-90.0 * Xpos, 0, 0, 1);
  96. glBeginQueryARB(GL_SAMPLES_PASSED_ARB, OccQuery);
  97. glColorMask(0, 0, 0, 0);
  98. glDepthMask(GL_FALSE);
  99. glBegin(GL_POLYGON);
  100. glVertex3f(-1, -1, 0);
  101. glVertex3f( 1, -1, 0);
  102. glVertex3f( 1, 1, 0);
  103. glVertex3f(-1, 1, 0);
  104. glEnd();
  105. glEndQueryARB(GL_SAMPLES_PASSED_ARB);
  106. do {
  107. /* do useful work here, if any */
  108. glGetQueryObjectivARB(OccQuery, GL_QUERY_RESULT_AVAILABLE_ARB, &ready);
  109. } while (!ready);
  110. glGetQueryObjectuivARB(OccQuery, GL_QUERY_RESULT_ARB, &passed);
  111. /* turn off occlusion testing */
  112. glColorMask(1, 1, 1, 1);
  113. glDepthMask(GL_TRUE);
  114. /* draw the orange rect, so we can see what's going on */
  115. glColor3f(0.8, 0.5, 0);
  116. glBegin(GL_POLYGON);
  117. glVertex3f(-1, -1, 0);
  118. glVertex3f( 1, -1, 0);
  119. glVertex3f( 1, 1, 0);
  120. glVertex3f(-1, 1, 0);
  121. glEnd();
  122. glPopMatrix();
  123. /* Print result message */
  124. glMatrixMode( GL_PROJECTION );
  125. glLoadIdentity();
  126. glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  127. glMatrixMode( GL_MODELVIEW );
  128. glLoadIdentity();
  129. glColor3f(1, 1, 1);
  130. sprintf(s, " %4d Fragments Visible", passed);
  131. glRasterPos3f(-0.50, -0.7, 0);
  132. PrintString(s);
  133. if (!passed) {
  134. glRasterPos3f(-0.25, -0.8, 0);
  135. PrintString("Fully Occluded");
  136. }
  137. glutSwapBuffers();
  138. }
  139. static void Reshape( int width, int height )
  140. {
  141. glViewport( 0, 0, width, height );
  142. }
  143. static void Key( unsigned char key, int x, int y )
  144. {
  145. (void) x;
  146. (void) y;
  147. switch (key) {
  148. case 27:
  149. exit(0);
  150. break;
  151. case ' ':
  152. Anim = !Anim;
  153. if (Anim)
  154. glutIdleFunc(Idle);
  155. else
  156. glutIdleFunc(NULL);
  157. break;
  158. }
  159. glutPostRedisplay();
  160. }
  161. static void SpecialKey( int key, int x, int y )
  162. {
  163. const GLfloat step = 0.1;
  164. (void) x;
  165. (void) y;
  166. switch (key) {
  167. case GLUT_KEY_LEFT:
  168. Xpos -= step;
  169. break;
  170. case GLUT_KEY_RIGHT:
  171. Xpos += step;
  172. break;
  173. }
  174. glutPostRedisplay();
  175. }
  176. static void Init( void )
  177. {
  178. const char *ext = (const char *) glGetString(GL_EXTENSIONS);
  179. GLint bits;
  180. if (!strstr(ext, "GL_ARB_occlusion_query")) {
  181. printf("Sorry, this demo requires the GL_ARB_occlusion_query extension\n");
  182. exit(-1);
  183. }
  184. glGetQueryivARB(GL_SAMPLES_PASSED_ARB, GL_QUERY_COUNTER_BITS_ARB, &bits);
  185. if (!bits) {
  186. printf("Hmmm, GL_QUERY_COUNTER_BITS_ARB is zero!\n");
  187. exit(-1);
  188. }
  189. glGenQueriesARB(1, &OccQuery);
  190. assert( glIsQueryARB(OccQuery) );
  191. glEnable(GL_DEPTH_TEST);
  192. }
  193. int main( int argc, char *argv[] )
  194. {
  195. glutInit( &argc, argv );
  196. glutInitWindowPosition( 0, 0 );
  197. glutInitWindowSize( 400, 400 );
  198. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  199. glutCreateWindow(argv[0]);
  200. glutReshapeFunc( Reshape );
  201. glutKeyboardFunc( Key );
  202. glutSpecialFunc( SpecialKey );
  203. glutIdleFunc( Idle );
  204. glutDisplayFunc( Display );
  205. Init();
  206. glutMainLoop();
  207. return 0;
  208. }