Merge branch '7.8'
[mesa.git] / progs / redbook / material.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 * material.c
42 * This program demonstrates the use of the GL lighting model.
43 * Several objects are drawn using different material characteristics.
44 * A single light source illuminates the objects.
45 */
46 #include <stdlib.h>
47 #include <GL/glut.h>
48
49 /* Initialize z-buffer, projection matrix, light source,
50 * and lighting model. Do not specify a material property here.
51 */
52 static void myinit(void)
53 {
54 GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
55 GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
56 GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
57 GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
58 GLfloat local_view[] = { 0.0 };
59
60 glEnable(GL_DEPTH_TEST);
61 glDepthFunc(GL_LESS);
62
63 glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
64 glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
65 glLightfv(GL_LIGHT0, GL_POSITION, position);
66 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
67 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
68
69 glEnable(GL_LIGHTING);
70 glEnable(GL_LIGHT0);
71
72 glClearColor(0.0, 0.1, 0.1, 0.0);
73 }
74
75 /* Draw twelve spheres in 3 rows with 4 columns.
76 * The spheres in the first row have materials with no ambient reflection.
77 * The second row has materials with significant ambient reflection.
78 * The third row has materials with colored ambient reflection.
79 *
80 * The first column has materials with blue, diffuse reflection only.
81 * The second column has blue diffuse reflection, as well as specular
82 * reflection with a low shininess exponent.
83 * The third column has blue diffuse reflection, as well as specular
84 * reflection with a high shininess exponent (a more concentrated highlight).
85 * The fourth column has materials which also include an emissive component.
86 *
87 * glTranslatef() is used to move spheres to their appropriate locations.
88 */
89
90 static void display(void)
91 {
92 GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
93 GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
94 GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
95 GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
96 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
97 GLfloat no_shininess[] = { 0.0 };
98 GLfloat low_shininess[] = { 5.0 };
99 GLfloat high_shininess[] = { 100.0 };
100 GLfloat mat_emission[] = {0.3, 0.2, 0.2, 0.0};
101
102 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
103
104 /* draw sphere in first row, first column
105 * diffuse reflection only; no ambient or specular
106 */
107 glPushMatrix();
108 glTranslatef (-3.75, 3.0, 0.0);
109 glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
110 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
111 glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
112 glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
113 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
114 glutSolidSphere(1.0, 16, 16);
115 glPopMatrix();
116
117 /* draw sphere in first row, second column
118 * diffuse and specular reflection; low shininess; no ambient
119 */
120 glPushMatrix();
121 glTranslatef (-1.25, 3.0, 0.0);
122 glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
123 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
124 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
125 glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
126 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
127 glutSolidSphere(1.0, 16, 16);
128 glPopMatrix();
129
130 /* draw sphere in first row, third column
131 * diffuse and specular reflection; high shininess; no ambient
132 */
133 glPushMatrix();
134 glTranslatef (1.25, 3.0, 0.0);
135 glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
136 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
137 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
138 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
139 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
140 glutSolidSphere(1.0, 16, 16);
141 glPopMatrix();
142
143 /* draw sphere in first row, fourth column
144 * diffuse reflection; emission; no ambient or specular reflection
145 */
146 glPushMatrix();
147 glTranslatef (3.75, 3.0, 0.0);
148 glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat);
149 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
150 glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
151 glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
152 glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
153 glutSolidSphere(1.0, 16, 16);
154 glPopMatrix();
155
156 /* draw sphere in second row, first column
157 * ambient and diffuse reflection; no specular
158 */
159 glPushMatrix();
160 glTranslatef (-3.75, 0.0, 0.0);
161 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
162 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
163 glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
164 glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
165 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
166 glutSolidSphere(1.0, 16, 16);
167 glPopMatrix();
168
169 /* draw sphere in second row, second column
170 * ambient, diffuse and specular reflection; low shininess
171 */
172 glPushMatrix();
173 glTranslatef (-1.25, 0.0, 0.0);
174 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
175 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
176 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
177 glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
178 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
179 glutSolidSphere(1.0, 16, 16);
180 glPopMatrix();
181
182 /* draw sphere in second row, third column
183 * ambient, diffuse and specular reflection; high shininess
184 */
185 glPushMatrix();
186 glTranslatef (1.25, 0.0, 0.0);
187 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
188 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
189 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
190 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
191 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
192 glutSolidSphere(1.0, 16, 16);
193 glPopMatrix();
194
195 /* draw sphere in second row, fourth column
196 * ambient and diffuse reflection; emission; no specular
197 */
198 glPushMatrix();
199 glTranslatef (3.75, 0.0, 0.0);
200 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
201 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
202 glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
203 glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
204 glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
205 glutSolidSphere(1.0, 16, 16);
206 glPopMatrix();
207
208 /* draw sphere in third row, first column
209 * colored ambient and diffuse reflection; no specular
210 */
211 glPushMatrix();
212 glTranslatef (-3.75, -3.0, 0.0);
213 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
214 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
215 glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
216 glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
217 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
218 glutSolidSphere(1.0, 16, 16);
219 glPopMatrix();
220
221 /* draw sphere in third row, second column
222 * colored ambient, diffuse and specular reflection; low shininess
223 */
224 glPushMatrix();
225 glTranslatef (-1.25, -3.0, 0.0);
226 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
227 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
228 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
229 glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
230 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
231 glutSolidSphere(1.0, 16, 16);
232 glPopMatrix();
233
234 /* draw sphere in third row, third column
235 * colored ambient, diffuse and specular reflection; high shininess
236 */
237 glPushMatrix();
238 glTranslatef (1.25, -3.0, 0.0);
239 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
240 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
241 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
242 glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
243 glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
244 glutSolidSphere(1.0, 16, 16);
245 glPopMatrix();
246
247 /* draw sphere in third row, fourth column
248 * colored ambient and diffuse reflection; emission; no specular
249 */
250 glPushMatrix();
251 glTranslatef (3.75, -3.0, 0.0);
252 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_color);
253 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
254 glMaterialfv(GL_FRONT, GL_SPECULAR, no_mat);
255 glMaterialfv(GL_FRONT, GL_SHININESS, no_shininess);
256 glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
257 glutSolidSphere(1.0, 16, 16);
258 glPopMatrix();
259
260 glFlush();
261 }
262
263 static void myReshape(int w, int h)
264 {
265 glViewport(0, 0, w, h);
266 glMatrixMode(GL_PROJECTION);
267 glLoadIdentity();
268 if (w <= (h * 2))
269 glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w,
270 3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
271 else
272 glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2),
273 6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
274 glMatrixMode(GL_MODELVIEW);
275 }
276
277 static void
278 key(unsigned char k, int x, int y)
279 {
280 switch (k) {
281 case 27: /* Escape */
282 exit(0);
283 break;
284 default:
285 return;
286 }
287 glutPostRedisplay();
288 }
289
290 /* Main Loop
291 * Open window with initial window size, title bar,
292 * RGBA display mode, and handle input events.
293 */
294 int main(int argc, char** argv)
295 {
296 glutInit(&argc, argv);
297 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
298 glutInitWindowSize (600, 450);
299 glutCreateWindow(argv[0]);
300 myinit();
301 glutReshapeFunc(myReshape);
302 glutDisplayFunc(display);
303 glutKeyboardFunc(key);
304 glutMainLoop();
305 return 0; /* ANSI C requires main to return int. */
306 }