Merge commit 'origin/master' into gallium-0.2
[mesa.git] / progs / redbook / teaambient.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 * teaambient.c
42 * This program renders three lighted, shaded teapots, with
43 * different ambient values.
44 */
45 #include <stdlib.h>
46 #include <GL/glut.h>
47
48 /* Initialize light source and lighting model.
49 */
50 void
51 myinit(void)
52 {
53 GLfloat light_ambient[] =
54 {0.0, 0.0, 0.0, 1.0};
55 GLfloat light_diffuse[] =
56 {1.0, 1.0, 1.0, 1.0};
57 GLfloat light_specular[] =
58 {1.0, 1.0, 1.0, 1.0};
59 /* light_position is NOT default value */
60 GLfloat light_position[] =
61 {1.0, 0.0, 0.0, 0.0};
62 GLfloat global_ambient[] =
63 {0.75, 0.75, 0.75, 1.0};
64
65 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
66 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
67 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
68 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
69
70 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
71
72 glFrontFace(GL_CW);
73 glEnable(GL_LIGHTING);
74 glEnable(GL_LIGHT0);
75 glEnable(GL_AUTO_NORMAL);
76 glEnable(GL_NORMALIZE);
77 glDepthFunc(GL_LESS);
78 glEnable(GL_DEPTH_TEST);
79 }
80
81 void
82 display(void)
83 {
84 GLfloat low_ambient[] =
85 {0.1, 0.1, 0.1, 1.0};
86 GLfloat more_ambient[] =
87 {0.4, 0.4, 0.4, 1.0};
88 GLfloat most_ambient[] =
89 {1.0, 1.0, 1.0, 1.0};
90
91 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92
93 /* material has small ambient reflection */
94 glMaterialfv(GL_FRONT, GL_AMBIENT, low_ambient);
95 glMaterialf(GL_FRONT, GL_SHININESS, 40.0);
96 glPushMatrix();
97 glTranslatef(0.0, 2.0, 0.0);
98 glutSolidTeapot(1.0);
99 glPopMatrix();
100
101 /* material has moderate ambient reflection */
102 glMaterialfv(GL_FRONT, GL_AMBIENT, more_ambient);
103 glPushMatrix();
104 glTranslatef(0.0, 0.0, 0.0);
105 glutSolidTeapot(1.0);
106 glPopMatrix();
107
108 /* material has large ambient reflection */
109 glMaterialfv(GL_FRONT, GL_AMBIENT, most_ambient);
110 glPushMatrix();
111 glTranslatef(0.0, -2.0, 0.0);
112 glutSolidTeapot(1.0);
113 glPopMatrix();
114 glFlush();
115 }
116
117 void
118 myReshape(int w, int h)
119 {
120 glViewport(0, 0, w, h);
121 glMatrixMode(GL_PROJECTION);
122 glLoadIdentity();
123 if (w <= h)
124 glOrtho(-4.0, 4.0, -4.0 * (GLfloat) h / (GLfloat) w,
125 4.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
126 else
127 glOrtho(-4.0 * (GLfloat) w / (GLfloat) h,
128 4.0 * (GLfloat) w / (GLfloat) h, -4.0, 4.0, -10.0, 10.0);
129 glMatrixMode(GL_MODELVIEW);
130 }
131
132 static void
133 key(unsigned char k, int x, int y)
134 {
135 switch (k) {
136 case 27: /* Escape */
137 exit(0);
138 break;
139 default:
140 return;
141 }
142 glutPostRedisplay();
143 }
144
145 /* Main Loop
146 * Open window with initial window size, title bar,
147 * RGBA display mode, and handle input events.
148 */
149 int
150 main(int argc, char **argv)
151 {
152 glutInit(&argc, argv);
153 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
154 glutInitWindowSize(500, 500);
155 glutCreateWindow(argv[0]);
156 myinit();
157 glutReshapeFunc(myReshape);
158 glutDisplayFunc(display);
159 glutKeyboardFunc(key);
160 glutMainLoop();
161 return 0; /* ANSI C requires main to return int. */
162 }