2 * Copyright (c) 1993-1997, Silicon Graphics, Inc.
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.
40 * This program demonstrates polygon offset to draw a shaded
41 * polygon and its wireframe counterpart without ugly visual
42 * artifacts ("stitching").
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.
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
);
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);
78 glEnable(GL_LIGHTING
);
80 glEnable(GL_POLYGON_OFFSET_FILL
);
81 glPolygonOffset(polyfactor
, polyunits
);
83 glDisable(GL_POLYGON_OFFSET_FILL
);
86 glDisable(GL_LIGHTING
);
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
);
93 glDisable(GL_POLYGON_OFFSET_LINE
);
94 glPolygonMode(GL_FRONT_AND_BACK
, GL_FILL
);
97 glEnable(GL_LIGHTING
);
104 if (doubleBuffer
) glutSwapBuffers();
107 /* specify initial properties
108 * create display list with sphere
109 * initialize lighting and depth buffer
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);
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
);
142 gluPerspective(45.0, (GLdouble
)width
/(GLdouble
)height
,
144 glMatrixMode (GL_MODELVIEW
);
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
;
155 printf("Benchmarking...\n");
158 startTime
= glutGet(GLUT_ELAPSED_TIME
);
165 endTime
= glutGet(GLUT_ELAPSED_TIME
);
166 } while (endTime
- startTime
< 5000); /* 5 seconds */
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 */
177 void mouse(int button
, int state
, int x
, int y
) {
179 case GLUT_LEFT_BUTTON
:
189 case GLUT_MIDDLE_BUTTON
:
199 case GLUT_RIGHT_BUTTON
:
214 void keyboard (unsigned char key
, int x
, int y
)
219 tdist
= (tdist
+ 0.5);
225 tdist
= (tdist
- 0.5);
230 polyfactor
= polyfactor
+ 0.1;
231 printf ("polyfactor is %f\n", polyfactor
);
235 polyfactor
= polyfactor
- 0.1;
236 printf ("polyfactor is %f\n", polyfactor
);
240 polyunits
= polyunits
+ 1.0;
241 printf ("polyunits is %f\n", polyunits
);
245 polyunits
= polyunits
- 1.0;
246 printf ("polyunits is %f\n", polyunits
);
257 printf ("fill/line: %d\n", fill
);
260 case 27: /* Escape */
269 key(unsigned char k
, int x
, int y
)
272 case 27: /* Escape */
281 GLenum
Args(int argc
, char **argv
)
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
;
293 printf("%s (Bad option).\n", argv
[i
]);
302 * Open window with initial window size, title bar,
303 * RGBA display mode, and handle input events.
305 int main(int argc
, char** argv
)
308 glutInit(&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
);
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");