Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

occlude.c 5.2KB

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