blob: 10891fb99777be8c9d8d5a5873552635c02a41e2 [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001
2/* Copyright (c) Mark J. Kilgard, 1994. */
3
4/*
5 * (c) Copyright 1993, Silicon Graphics, Inc.
Ted Jump83c02ef1999-09-17 02:40:51 +00006 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
jtgafb833d1999-08-19 00:55:39 +00008 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
Ted Jump83c02ef1999-09-17 02:40:51 +000010 * and this permission notice appear in supporting documentation, and that
jtgafb833d1999-08-19 00:55:39 +000011 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
Ted Jump83c02ef1999-09-17 02:40:51 +000013 * written prior permission.
jtgafb833d1999-08-19 00:55:39 +000014 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
Ted Jump83c02ef1999-09-17 02:40:51 +000027 *
28 * US Government Users Restricted Rights
jtgafb833d1999-08-19 00:55:39 +000029 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States. Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40/*
41 * sceneflat.c
42 * This program draws lighted objects with flat shading.
43 */
44#include <stdlib.h>
45#include <GL/glut.h>
46
47/* Initialize light source and shading model (GL_FLAT).
48 */
49void myinit(void)
50{
51 GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
52 GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
53 GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
54/* light_position is NOT default value */
55 GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
56
57 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
58 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
59 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
60 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
Ted Jump83c02ef1999-09-17 02:40:51 +000061
jtgafb833d1999-08-19 00:55:39 +000062 glEnable(GL_LIGHTING);
63 glEnable(GL_LIGHT0);
64 glDepthFunc(GL_LESS);
65 glEnable(GL_DEPTH_TEST);
66 glShadeModel (GL_FLAT);
67}
68
69void display(void)
70{
71 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
72
73 glPushMatrix ();
74 glRotatef (20.0, 1.0, 0.0, 0.0);
75
76 glPushMatrix ();
Ted Jump83c02ef1999-09-17 02:40:51 +000077 glTranslatef (-0.75, 0.5, 0.0);
jtgafb833d1999-08-19 00:55:39 +000078 glRotatef (90.0, 1.0, 0.0, 0.0);
79 glutSolidTorus (0.275, 0.85, 15, 15);
80 glPopMatrix ();
81
82 glPushMatrix ();
Ted Jump83c02ef1999-09-17 02:40:51 +000083 glTranslatef (-0.75, -0.5, 0.0);
jtgafb833d1999-08-19 00:55:39 +000084 glRotatef (270.0, 1.0, 0.0, 0.0);
85 glutSolidCone (1.0, 2.0, 15, 15);
86 glPopMatrix ();
87
88 glPushMatrix ();
Ted Jump83c02ef1999-09-17 02:40:51 +000089 glTranslatef (0.75, 0.0, -1.0);
jtgafb833d1999-08-19 00:55:39 +000090 glutSolidSphere (1.0, 15, 15);
91 glPopMatrix ();
92
93 glPopMatrix ();
94 glFlush();
95}
96
97void myReshape(int w, int h)
98{
99 glViewport(0, 0, w, h);
100 glMatrixMode(GL_PROJECTION);
101 glLoadIdentity();
Ted Jump83c02ef1999-09-17 02:40:51 +0000102 if (w <= h)
103 glOrtho (-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w,
jtgafb833d1999-08-19 00:55:39 +0000104 2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
Ted Jump83c02ef1999-09-17 02:40:51 +0000105 else
106 glOrtho (-2.5*(GLfloat)w/(GLfloat)h,
jtgafb833d1999-08-19 00:55:39 +0000107 2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
108 glMatrixMode(GL_MODELVIEW);
109}
110
Ted Jump83c02ef1999-09-17 02:40:51 +0000111static void
112key(unsigned char k, int x, int y)
113{
114 switch (k) {
115 case 27: /* Escape */
116 exit(0);
117 break;
118 default:
119 return;
120 }
121 glutPostRedisplay();
122}
123
jtgafb833d1999-08-19 00:55:39 +0000124/* Main Loop
Ted Jump83c02ef1999-09-17 02:40:51 +0000125 * Open window with initial window size, title bar,
jtgafb833d1999-08-19 00:55:39 +0000126 * RGBA display mode, and handle input events.
127 */
128int main(int argc, char** argv)
129{
130 glutInit(&argc, argv);
131 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
132 glutInitWindowSize (500, 500);
133 glutCreateWindow (argv[0]);
134 myinit();
135 glutReshapeFunc (myReshape);
136 glutDisplayFunc(display);
Ted Jump83c02ef1999-09-17 02:40:51 +0000137 glutKeyboardFunc(key);
jtgafb833d1999-08-19 00:55:39 +0000138 glutMainLoop();
139 return 0; /* ANSI C requires main to return int. */
140}