Merge commit 'origin/master' into gallium-0.2
[mesa.git] / progs / redbook / polyoff.c
1 /*
2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
3 * ALL RIGHTS RESERVED
4 * Permission to use, copy, modify, and distribute this software for
5 * any purpose and without fee is hereby granted, provided that the above
6 * copyright notice appear in all copies and that both the copyright notice
7 * and this permission notice appear in supporting documentation, and that
8 * the name of Silicon Graphics, Inc. not be used in advertising
9 * or publicity pertaining to distribution of the software without specific,
10 * written prior permission.
11 *
12 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
13 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
14 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
15 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
16 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
17 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
18 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
19 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
20 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
21 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
22 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
23 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
24 *
25 * US Government Users Restricted Rights
26 * Use, duplication, or disclosure by the Government is subject to
27 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
28 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
29 * clause at DFARS 252.227-7013 and/or in similar or successor
30 * clauses in the FAR or the DOD or NASA FAR Supplement.
31 * Unpublished-- rights reserved under the copyright laws of the
32 * United States. Contractor/manufacturer is Silicon Graphics,
33 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
34 *
35 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
36 */
37
38 /*
39 * polyoff.c
40 * This program demonstrates polygon offset to draw a shaded
41 * polygon and its wireframe counterpart without ugly visual
42 * artifacts ("stitching").
43 */
44 #include <GL/glut.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48
49 #ifdef GL_VERSION_1_1
50 GLuint list;
51 GLint fill = 1;
52 GLfloat spinx = 0;
53 GLfloat spiny = 0;
54 GLfloat tdist = 0.0;
55 GLfloat polyfactor = 1.0;
56 GLfloat polyunits = 1.0;
57 GLboolean doubleBuffer;
58
59
60 /* display() draws two spheres, one with a gray, diffuse material,
61 * the other sphere with a magenta material with a specular highlight.
62 */
63 void display (void)
64 {
65 GLfloat gray[] = { 0.8, 0.8, 0.8, 1.0 };
66 GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
67
68 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
69 glPushMatrix ();
70 glTranslatef (0.0, 0.0, tdist);
71 glRotatef ((GLfloat) spinx, 1.0, 0.0, 0.0);
72 glRotatef ((GLfloat) spiny, 0.0, 1.0, 0.0);
73
74 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
75 glMaterialfv(GL_FRONT, GL_SPECULAR, black);
76 glMaterialf(GL_FRONT, GL_SHININESS, 0.0);
77 if (fill) {
78 glEnable(GL_LIGHTING);
79 glEnable(GL_LIGHT0);
80 glEnable(GL_POLYGON_OFFSET_FILL);
81 glPolygonOffset(polyfactor, polyunits);
82 glCallList (list);
83 glDisable(GL_POLYGON_OFFSET_FILL);
84 }
85
86 glDisable(GL_LIGHTING);
87 glDisable(GL_LIGHT0);
88 glColor3f (1.0, 1.0, 1.0);
89 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
90 glPolygonOffset(-polyfactor, -polyunits);
91 if (!fill) glEnable(GL_POLYGON_OFFSET_LINE);
92 glCallList (list);
93 glDisable(GL_POLYGON_OFFSET_LINE);
94 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
95
96 if (!fill) {
97 glEnable(GL_LIGHTING);
98 glEnable(GL_LIGHT0);
99 glCallList (list);
100 }
101
102 glPopMatrix ();
103 glFlush ();
104 if (doubleBuffer) glutSwapBuffers();
105 }
106
107 /* specify initial properties
108 * create display list with sphere
109 * initialize lighting and depth buffer
110 */
111 void gfxinit (void)
112 {
113 GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
114 GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
115 GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
116 GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
117
118 GLfloat global_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
119
120 glClearColor (0.0, 0.0, 0.0, 1.0);
121
122 list = glGenLists(1);
123 glNewList (list, GL_COMPILE);
124 glutSolidSphere(1.0, 20, 12);
125 glEndList ();
126
127 glEnable(GL_DEPTH_TEST);
128
129 glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
130 glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
131 glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
132 glLightfv (GL_LIGHT0, GL_POSITION, light_position);
133 glLightModelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient);
134 }
135
136 /* call when window is resized */
137 void reshape(int width, int height)
138 {
139 glViewport (0, 0, width, height);
140 glMatrixMode (GL_PROJECTION);
141 glLoadIdentity ();
142 gluPerspective(45.0, (GLdouble)width/(GLdouble)height,
143 1.0, 10.0);
144 glMatrixMode (GL_MODELVIEW);
145 glLoadIdentity ();
146 gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
147 }
148
149 static void Benchmark( float xdiff, float ydiff )
150 {
151 int startTime, endTime;
152 int draws;
153 double seconds, fps;
154
155 printf("Benchmarking...\n");
156
157 draws = 0;
158 startTime = glutGet(GLUT_ELAPSED_TIME);
159 spinx = spiny = 0.0;
160 do {
161 spinx += xdiff;
162 spiny += ydiff;
163 display();
164 draws++;
165 endTime = glutGet(GLUT_ELAPSED_TIME);
166 } while (endTime - startTime < 5000); /* 5 seconds */
167
168 /* Results */
169 seconds = (double) (endTime - startTime) / 1000.0;
170 fps = draws / seconds;
171 printf("Result: fps: %g\n", fps);
172 }
173
174
175 /* call when mouse button is pressed */
176 /* ARGSUSED2 */
177 void mouse(int button, int state, int x, int y) {
178 switch (button) {
179 case GLUT_LEFT_BUTTON:
180 switch (state) {
181 case GLUT_DOWN:
182 spinx += 5;
183 glutPostRedisplay();
184 break;
185 default:
186 break;
187 }
188 break;
189 case GLUT_MIDDLE_BUTTON:
190 switch (state) {
191 case GLUT_DOWN:
192 spiny += 5;
193 glutPostRedisplay();
194 break;
195 default:
196 break;
197 }
198 break;
199 case GLUT_RIGHT_BUTTON:
200 switch (state) {
201 case GLUT_UP:
202 exit(0);
203 break;
204 default:
205 break;
206 }
207 break;
208 default:
209 break;
210 }
211 }
212
213 /* ARGSUSED1 */
214 void keyboard (unsigned char key, int x, int y)
215 {
216 switch (key) {
217 case 't':
218 if (tdist < 4.0) {
219 tdist = (tdist + 0.5);
220 glutPostRedisplay();
221 }
222 break;
223 case 'T':
224 if (tdist > -5.0) {
225 tdist = (tdist - 0.5);
226 glutPostRedisplay();
227 }
228 break;
229 case 'F':
230 polyfactor = polyfactor + 0.1;
231 printf ("polyfactor is %f\n", polyfactor);
232 glutPostRedisplay();
233 break;
234 case 'f':
235 polyfactor = polyfactor - 0.1;
236 printf ("polyfactor is %f\n", polyfactor);
237 glutPostRedisplay();
238 break;
239 case 'U':
240 polyunits = polyunits + 1.0;
241 printf ("polyunits is %f\n", polyunits);
242 glutPostRedisplay();
243 break;
244 case 'u':
245 polyunits = polyunits - 1.0;
246 printf ("polyunits is %f\n", polyunits);
247 glutPostRedisplay();
248 break;
249 case 'b':
250 Benchmark(5.0, 0);
251 break;
252 case 'B':
253 Benchmark(0, 5.0);
254 break;
255 case ' ':
256 fill = !fill;
257 printf ("fill/line: %d\n", fill);
258 glutPostRedisplay();
259 break;
260 case 27: /* Escape */
261 exit(0);
262 break;
263 default:
264 break;
265 }
266 }
267
268 static void
269 key(unsigned char k, int x, int y)
270 {
271 switch (k) {
272 case 27: /* Escape */
273 exit(0);
274 break;
275 default:
276 return;
277 }
278 glutPostRedisplay();
279 }
280
281 GLenum Args(int argc, char **argv)
282 {
283 GLint i;
284
285 doubleBuffer = GL_FALSE;
286
287 for (i = 1; i < argc; i++) {
288 if (strcmp(argv[i], "-sb") == 0) {
289 doubleBuffer = GL_FALSE;
290 } else if (strcmp(argv[i], "-db") == 0) {
291 doubleBuffer = GL_TRUE;
292 } else {
293 printf("%s (Bad option).\n", argv[i]);
294 return GL_FALSE;
295 }
296 }
297 return GL_TRUE;
298 }
299
300
301 /* Main Loop
302 * Open window with initial window size, title bar,
303 * RGBA display mode, and handle input events.
304 */
305 int main(int argc, char** argv)
306 {
307 GLuint type;
308 glutInit(&argc, argv);
309
310 Args(argc, argv);
311
312 type = GLUT_DEPTH | GLUT_RGB;
313 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
314
315 glutInitDisplayMode(type);
316 glutCreateWindow("polyoff");
317 glutReshapeFunc(reshape);
318 glutDisplayFunc(display);
319 glutMouseFunc(mouse);
320 glutKeyboardFunc(keyboard);
321 gfxinit();
322 glutMainLoop();
323 return 0;
324 }
325 #else
326 int main(int argc, char** argv)
327 {
328 fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
329 fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
330 fprintf (stderr, "you may be able to modify this program to make it run.\n");
331 return 0;
332 }
333 #endif