mesa: Use appropriate unsigned/signed, float/integer types.
[mesa.git] / progs / redbook / teapots.c
1
2 /* Copyright (c) Mark J. Kilgard, 1994. */
3
4 /**
5 * (c) Copyright 1993, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * 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
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
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.
27 *
28 * US Government Users Restricted Rights
29 * 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 * teapots.c
42 * This program demonstrates lots of material properties.
43 * A single light source illuminates the objects.
44 */
45 #include <stdlib.h>
46 #include <GL/glut.h>
47
48 /*
49 * Initialize depth buffer, projection matrix, light source, and lighting
50 * model. Do not specify a material property here.
51 */
52 void
53 myinit(void)
54 {
55 GLfloat ambient[] =
56 {0.0, 0.0, 0.0, 1.0};
57 GLfloat diffuse[] =
58 {1.0, 1.0, 1.0, 1.0};
59 GLfloat position[] =
60 {0.0, 3.0, 3.0, 0.0};
61
62 GLfloat lmodel_ambient[] =
63 {0.2, 0.2, 0.2, 1.0};
64 GLfloat local_view[] =
65 {0.0};
66
67 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
68 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
69 glLightfv(GL_LIGHT0, GL_POSITION, position);
70 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
71 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
72
73 glFrontFace(GL_CW);
74 glEnable(GL_LIGHTING);
75 glEnable(GL_LIGHT0);
76 glEnable(GL_AUTO_NORMAL);
77 glEnable(GL_NORMALIZE);
78 glEnable(GL_DEPTH_TEST);
79 glDepthFunc(GL_LESS);
80 }
81
82 /*
83 * Move object into position. Use 3rd through 12th parameters to specify the
84 * material property. Draw a teapot.
85 */
86 void
87 renderTeapot(GLfloat x, GLfloat y,
88 GLfloat ambr, GLfloat ambg, GLfloat ambb,
89 GLfloat difr, GLfloat difg, GLfloat difb,
90 GLfloat specr, GLfloat specg, GLfloat specb, GLfloat shine)
91 {
92 float mat[4];
93
94 glPushMatrix();
95 glTranslatef(x, y, 0.0);
96 mat[0] = ambr;
97 mat[1] = ambg;
98 mat[2] = ambb;
99 mat[3] = 1.0;
100 glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
101 mat[0] = difr;
102 mat[1] = difg;
103 mat[2] = difb;
104 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
105 mat[0] = specr;
106 mat[1] = specg;
107 mat[2] = specb;
108 glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
109 glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0);
110 glutSolidTeapot(1.0);
111 glPopMatrix();
112 }
113
114 /**
115 * First column: emerald, jade, obsidian, pearl, ruby, turquoise
116 * 2nd column: brass, bronze, chrome, copper, gold, silver
117 * 3rd column: black, cyan, green, red, white, yellow plastic
118 * 4th column: black, cyan, green, red, white, yellow rubber
119 */
120 void
121 display(void)
122 {
123 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
124 renderTeapot(2.0, 17.0, 0.0215, 0.1745, 0.0215,
125 0.07568, 0.61424, 0.07568, 0.633, 0.727811, 0.633, 0.6);
126 renderTeapot(2.0, 14.0, 0.135, 0.2225, 0.1575,
127 0.54, 0.89, 0.63, 0.316228, 0.316228, 0.316228, 0.1);
128 renderTeapot(2.0, 11.0, 0.05375, 0.05, 0.06625,
129 0.18275, 0.17, 0.22525, 0.332741, 0.328634, 0.346435, 0.3);
130 renderTeapot(2.0, 8.0, 0.25, 0.20725, 0.20725,
131 1, 0.829, 0.829, 0.296648, 0.296648, 0.296648, 0.088);
132 renderTeapot(2.0, 5.0, 0.1745, 0.01175, 0.01175,
133 0.61424, 0.04136, 0.04136, 0.727811, 0.626959, 0.626959, 0.6);
134 renderTeapot(2.0, 2.0, 0.1, 0.18725, 0.1745,
135 0.396, 0.74151, 0.69102, 0.297254, 0.30829, 0.306678, 0.1);
136 renderTeapot(6.0, 17.0, 0.329412, 0.223529, 0.027451,
137 0.780392, 0.568627, 0.113725, 0.992157, 0.941176, 0.807843,
138 0.21794872);
139 renderTeapot(6.0, 14.0, 0.2125, 0.1275, 0.054,
140 0.714, 0.4284, 0.18144, 0.393548, 0.271906, 0.166721, 0.2);
141 renderTeapot(6.0, 11.0, 0.25, 0.25, 0.25,
142 0.4, 0.4, 0.4, 0.774597, 0.774597, 0.774597, 0.6);
143 renderTeapot(6.0, 8.0, 0.19125, 0.0735, 0.0225,
144 0.7038, 0.27048, 0.0828, 0.256777, 0.137622, 0.086014, 0.1);
145 renderTeapot(6.0, 5.0, 0.24725, 0.1995, 0.0745,
146 0.75164, 0.60648, 0.22648, 0.628281, 0.555802, 0.366065, 0.4);
147 renderTeapot(6.0, 2.0, 0.19225, 0.19225, 0.19225,
148 0.50754, 0.50754, 0.50754, 0.508273, 0.508273, 0.508273, 0.4);
149 renderTeapot(10.0, 17.0, 0.0, 0.0, 0.0, 0.01, 0.01, 0.01,
150 0.50, 0.50, 0.50, .25);
151 renderTeapot(10.0, 14.0, 0.0, 0.1, 0.06, 0.0, 0.50980392, 0.50980392,
152 0.50196078, 0.50196078, 0.50196078, .25);
153 renderTeapot(10.0, 11.0, 0.0, 0.0, 0.0,
154 0.1, 0.35, 0.1, 0.45, 0.55, 0.45, .25);
155 renderTeapot(10.0, 8.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0,
156 0.7, 0.6, 0.6, .25);
157 renderTeapot(10.0, 5.0, 0.0, 0.0, 0.0, 0.55, 0.55, 0.55,
158 0.70, 0.70, 0.70, .25);
159 renderTeapot(10.0, 2.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.0,
160 0.60, 0.60, 0.50, .25);
161 renderTeapot(14.0, 17.0, 0.02, 0.02, 0.02, 0.01, 0.01, 0.01,
162 0.4, 0.4, 0.4, .078125);
163 renderTeapot(14.0, 14.0, 0.0, 0.05, 0.05, 0.4, 0.5, 0.5,
164 0.04, 0.7, 0.7, .078125);
165 renderTeapot(14.0, 11.0, 0.0, 0.05, 0.0, 0.4, 0.5, 0.4,
166 0.04, 0.7, 0.04, .078125);
167 renderTeapot(14.0, 8.0, 0.05, 0.0, 0.0, 0.5, 0.4, 0.4,
168 0.7, 0.04, 0.04, .078125);
169 renderTeapot(14.0, 5.0, 0.05, 0.05, 0.05, 0.5, 0.5, 0.5,
170 0.7, 0.7, 0.7, .078125);
171 renderTeapot(14.0, 2.0, 0.05, 0.05, 0.0, 0.5, 0.5, 0.4,
172 0.7, 0.7, 0.04, .078125);
173 glFlush();
174 }
175
176 void
177 myReshape(int w, int h)
178 {
179 glViewport(0, 0, w, h);
180 glMatrixMode(GL_PROJECTION);
181 glLoadIdentity();
182 if (w <= h)
183 glOrtho(0.0, 16.0, 0.0, 18.0 * (GLfloat) h / (GLfloat) w,
184 -10.0, 10.0);
185 else
186 glOrtho(0.0, 16.0 * (GLfloat) w / (GLfloat) h, 0.0, 18.0,
187 -10.0, 10.0);
188 glMatrixMode(GL_MODELVIEW);
189 }
190
191 static void
192 key(unsigned char k, int x, int y)
193 {
194 switch (k) {
195 case 27: /* Escape */
196 exit(0);
197 break;
198 default:
199 return;
200 }
201 glutPostRedisplay();
202 }
203
204 /*
205 * Main Loop Open window with initial window size, title bar, RGBA display
206 * mode, and handle input events.
207 */
208 int
209 main(int argc, char **argv)
210 {
211 glutInit(&argc, argv);
212 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
213 glutCreateWindow(argv[0]);
214 myinit();
215 glutReshapeFunc(myReshape);
216 glutDisplayFunc(display);
217 glutKeyboardFunc(key);
218 glutMainLoop();
219 return 0; /* ANSI C requires main to return int. */
220 }