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.

occlude.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * GL_HP_occlustion_test demo
  3. *
  4. * Brian Paul
  5. * 31 March 2000
  6. *
  7. * Copyright (C) 2000 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 <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <math.h>
  30. #include <GL/glut.h>
  31. #include <GL/glext.h>
  32. static GLfloat Xpos = 0;
  33. static GLboolean Anim = GL_TRUE;
  34. static void
  35. PrintString(const char *s)
  36. {
  37. while (*s) {
  38. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  39. s++;
  40. }
  41. }
  42. static void Idle(void)
  43. {
  44. static int lastTime = 0;
  45. static int sign = +1;
  46. int time = glutGet(GLUT_ELAPSED_TIME);
  47. float step;
  48. if (lastTime == 0)
  49. lastTime = time;
  50. else if (time - lastTime < 20) /* 50Hz update */
  51. return;
  52. step = (time - lastTime) / 1000.0 * sign;
  53. lastTime = time;
  54. Xpos += step;
  55. if (Xpos > 2.5) {
  56. Xpos = 2.5;
  57. sign = -1;
  58. }
  59. else if (Xpos < -2.5) {
  60. Xpos = -2.5;
  61. sign = +1;
  62. }
  63. glutPostRedisplay();
  64. }
  65. static void Display( void )
  66. {
  67. GLboolean result;
  68. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  69. glMatrixMode( GL_PROJECTION );
  70. glLoadIdentity();
  71. glFrustum( -1.0, 1.0, -1.0, 1.0, 5.0, 25.0 );
  72. glMatrixMode( GL_MODELVIEW );
  73. glLoadIdentity();
  74. glTranslatef( 0.0, 0.0, -15.0 );
  75. /* draw the occluding polygons */
  76. glColor3f(0, 0.6, 0.8);
  77. glBegin(GL_QUADS);
  78. glVertex2f(-1.6, -1.5);
  79. glVertex2f(-0.4, -1.5);
  80. glVertex2f(-0.4, 1.5);
  81. glVertex2f(-1.6, 1.5);
  82. glVertex2f( 0.4, -1.5);
  83. glVertex2f( 1.6, -1.5);
  84. glVertex2f( 1.6, 1.5);
  85. glVertex2f( 0.4, 1.5);
  86. glEnd();
  87. /* draw the test polygon with occlusion testing */
  88. glPushMatrix();
  89. glTranslatef(Xpos, 0, -0.5);
  90. glScalef(0.3, 0.3, 1.0);
  91. glRotatef(-90.0 * Xpos, 0, 0, 1);
  92. glEnable(GL_OCCLUSION_TEST_HP); /* NOTE: enabling the occlusion test */
  93. /* doesn't clear the result flag! */
  94. glColorMask(0, 0, 0, 0);
  95. glDepthMask(GL_FALSE);
  96. /* this call clear's the result flag. Not really needed for this demo. */
  97. glGetBooleanv(GL_OCCLUSION_TEST_RESULT_HP, &result);
  98. glBegin(GL_POLYGON);
  99. glVertex3f(-1, -1, 0);
  100. glVertex3f( 1, -1, 0);
  101. glVertex3f( 1, 1, 0);
  102. glVertex3f(-1, 1, 0);
  103. glEnd();
  104. glGetBooleanv(GL_OCCLUSION_TEST_RESULT_HP, &result);
  105. /* turn off occlusion testing */
  106. glDisable(GL_OCCLUSION_TEST_HP);
  107. glColorMask(1, 1, 1, 1);
  108. glDepthMask(GL_TRUE);
  109. /* draw the green rect, so we can see what's going on */
  110. glColor3f(0.8, 0.5, 0);
  111. glBegin(GL_POLYGON);
  112. glVertex3f(-1, -1, 0);
  113. glVertex3f( 1, -1, 0);
  114. glVertex3f( 1, 1, 0);
  115. glVertex3f(-1, 1, 0);
  116. glEnd();
  117. glPopMatrix();
  118. /* Print result message */
  119. glMatrixMode( GL_PROJECTION );
  120. glLoadIdentity();
  121. glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  122. glMatrixMode( GL_MODELVIEW );
  123. glLoadIdentity();
  124. glColor3f(1, 1, 1);
  125. glRasterPos3f(-0.25, -0.7, 0);
  126. if (result)
  127. PrintString(" Visible");
  128. else
  129. PrintString("Fully Occluded");
  130. glutSwapBuffers();
  131. }
  132. static void Reshape( int width, int height )
  133. {
  134. glViewport( 0, 0, width, height );
  135. }
  136. static void Key( unsigned char key, int x, int y )
  137. {
  138. (void) x;
  139. (void) y;
  140. switch (key) {
  141. case 'a':
  142. Anim = !Anim;
  143. if (Anim)
  144. glutIdleFunc( Idle );
  145. else
  146. glutIdleFunc( NULL );
  147. break;
  148. case 27:
  149. exit(0);
  150. break;
  151. }
  152. glutPostRedisplay();
  153. }
  154. static void SpecialKey( int key, int x, int y )
  155. {
  156. const GLfloat step = 0.1;
  157. (void) x;
  158. (void) y;
  159. switch (key) {
  160. case GLUT_KEY_LEFT:
  161. Xpos -= step;
  162. break;
  163. case GLUT_KEY_RIGHT:
  164. Xpos += step;
  165. break;
  166. }
  167. glutPostRedisplay();
  168. }
  169. static void Init( void )
  170. {
  171. const char *ext = (const char *) glGetString(GL_EXTENSIONS);
  172. if (!strstr(ext, "GL_HP_occlusion_test")) {
  173. printf("Sorry, this demo requires the GL_HP_occlusion_test extension\n");
  174. exit(-1);
  175. }
  176. glEnable(GL_DEPTH_TEST);
  177. }
  178. int main( int argc, char *argv[] )
  179. {
  180. glutInit( &argc, argv );
  181. glutInitWindowPosition( 0, 0 );
  182. glutInitWindowSize( 400, 400 );
  183. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
  184. glutCreateWindow(argv[0]);
  185. glutReshapeFunc( Reshape );
  186. glutKeyboardFunc( Key );
  187. glutSpecialFunc( SpecialKey );
  188. glutIdleFunc( Idle );
  189. glutDisplayFunc( Display );
  190. Init();
  191. glutMainLoop();
  192. return 0;
  193. }