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