chmod a-x **/glslnoise.c
[mesa-demos.git] / src / redbook / polyoff.c
blob2017b4d8eed80353ece7fd4278f5b638795ee4f0
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.
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.
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.
35 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
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").
44 #include <GL/glut.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
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;
60 /* display() draws two spheres, one with a gray, diffuse material,
61 * the other sphere with a magenta material with a specular highlight.
63 void display (void)
65 GLfloat gray[] = { 0.8, 0.8, 0.8, 1.0 };
66 GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
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);
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);
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);
96 if (!fill) {
97 glEnable(GL_LIGHTING);
98 glEnable(GL_LIGHT0);
99 glCallList (list);
102 glPopMatrix ();
103 glFlush ();
104 if (doubleBuffer) glutSwapBuffers();
107 /* specify initial properties
108 * create display list with sphere
109 * initialize lighting and depth buffer
111 void gfxinit (void)
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 };
118 GLfloat global_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
120 glClearColor (0.0, 0.0, 0.0, 1.0);
122 list = glGenLists(1);
123 glNewList (list, GL_COMPILE);
124 glutSolidSphere(1.0, 20, 12);
125 glEndList ();
127 glEnable(GL_DEPTH_TEST);
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);
136 /* call when window is resized */
137 void reshape(int width, int height)
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);
149 static void Benchmark( float xdiff, float ydiff )
151 int startTime, endTime;
152 int draws;
153 double seconds, fps;
155 printf("Benchmarking...\n");
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 */
168 /* Results */
169 seconds = (double) (endTime - startTime) / 1000.0;
170 fps = draws / seconds;
171 printf("Result: fps: %g\n", fps);
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;
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;
198 break;
199 case GLUT_RIGHT_BUTTON:
200 switch (state) {
201 case GLUT_UP:
202 exit(0);
203 break;
204 default:
205 break;
207 break;
208 default:
209 break;
213 /* ARGSUSED1 */
214 void keyboard (unsigned char key, int x, int y)
216 switch (key) {
217 case 't':
218 if (tdist < 4.0) {
219 tdist = (tdist + 0.5);
220 glutPostRedisplay();
222 break;
223 case 'T':
224 if (tdist > -5.0) {
225 tdist = (tdist - 0.5);
226 glutPostRedisplay();
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;
268 static void
269 key(unsigned char k, int x, int y)
271 switch (k) {
272 case 27: /* Escape */
273 exit(0);
274 break;
275 default:
276 return;
278 glutPostRedisplay();
281 GLenum Args(int argc, char **argv)
283 GLint i;
285 doubleBuffer = GL_FALSE;
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;
297 return GL_TRUE;
301 /* Main Loop
302 * Open window with initial window size, title bar,
303 * RGBA display mode, and handle input events.
305 int main(int argc, char** argv)
307 GLuint type;
308 glutInit(&argc, argv);
310 Args(argc, argv);
312 type = GLUT_DEPTH | GLUT_RGB;
313 type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
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;
325 #else
326 int main(int argc, char** argv)
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;
333 #endif