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