Merge commit 'origin/master' into gallium-0.2
[mesa.git] / progs / redbook / plane.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 * plane.c
42 * This program demonstrates the use of local versus
43 * infinite lighting on a flat plane.
44 */
45 #include <stdlib.h>
46 #include <GL/glut.h>
47
48 /* Initialize material property, light source, and lighting model.
49 */
50 void myinit(void)
51 {
52 GLfloat mat_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
53 /* mat_specular and mat_shininess are NOT default values */
54 GLfloat mat_diffuse[] = { 0.4, 0.4, 0.4, 1.0 };
55 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
56 GLfloat mat_shininess[] = { 15.0 };
57
58 GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
59 GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
60 GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
61 GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
62
63 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
64 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
65 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
66 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
67 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
68 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
69 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
70 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
71
72 glEnable(GL_LIGHTING);
73 glEnable(GL_LIGHT0);
74 glDepthFunc(GL_LESS);
75 glEnable(GL_DEPTH_TEST);
76 }
77
78 void drawPlane(void)
79 {
80 glBegin (GL_QUADS);
81 glNormal3f (0.0, 0.0, 1.0);
82 glVertex3f (-1.0, -1.0, 0.0);
83 glVertex3f (0.0, -1.0, 0.0);
84 glVertex3f (0.0, 0.0, 0.0);
85 glVertex3f (-1.0, 0.0, 0.0);
86
87 glNormal3f (0.0, 0.0, 1.0);
88 glVertex3f (0.0, -1.0, 0.0);
89 glVertex3f (1.0, -1.0, 0.0);
90 glVertex3f (1.0, 0.0, 0.0);
91 glVertex3f (0.0, 0.0, 0.0);
92
93 glNormal3f (0.0, 0.0, 1.0);
94 glVertex3f (0.0, 0.0, 0.0);
95 glVertex3f (1.0, 0.0, 0.0);
96 glVertex3f (1.0, 1.0, 0.0);
97 glVertex3f (0.0, 1.0, 0.0);
98
99 glNormal3f (0.0, 0.0, 1.0);
100 glVertex3f (0.0, 0.0, 0.0);
101 glVertex3f (0.0, 1.0, 0.0);
102 glVertex3f (-1.0, 1.0, 0.0);
103 glVertex3f (-1.0, 0.0, 0.0);
104 glEnd();
105 }
106
107 void display (void)
108 {
109 GLfloat infinite_light[] = { 1.0, 1.0, 1.0, 0.0 };
110 GLfloat local_light[] = { 1.0, 1.0, 1.0, 1.0 };
111
112 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
113
114 glPushMatrix ();
115 glTranslatef (-1.5, 0.0, 0.0);
116 glLightfv (GL_LIGHT0, GL_POSITION, infinite_light);
117 drawPlane ();
118 glPopMatrix ();
119
120 glPushMatrix ();
121 glTranslatef (1.5, 0.0, 0.0);
122 glLightfv (GL_LIGHT0, GL_POSITION, local_light);
123 drawPlane ();
124 glPopMatrix ();
125 glFlush ();
126 }
127
128 void myReshape(int w, int h)
129 {
130 glViewport (0, 0, w, h);
131 glMatrixMode (GL_PROJECTION);
132 glLoadIdentity ();
133 if (w <= h)
134 glOrtho (-1.5, 1.5, -1.5*(GLdouble)h/(GLdouble)w,
135 1.5*(GLdouble)h/(GLdouble)w, -10.0, 10.0);
136 else
137 glOrtho (-1.5*(GLdouble)w/(GLdouble)h,
138 1.5*(GLdouble)w/(GLdouble)h, -1.5, 1.5, -10.0, 10.0);
139 glMatrixMode (GL_MODELVIEW);
140 }
141
142 static void
143 key(unsigned char k, int x, int y)
144 {
145 switch (k) {
146 case 27: /* Escape */
147 exit(0);
148 break;
149 default:
150 return;
151 }
152 glutPostRedisplay();
153 }
154
155 /* Main Loop
156 * Open window with initial window size, title bar,
157 * RGBA display mode, and handle input events.
158 */
159 int main(int argc, char** argv)
160 {
161 glutInit(&argc, argv);
162 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
163 glutInitWindowSize (500, 200);
164 glutCreateWindow (argv[0]);
165 myinit();
166 glutReshapeFunc (myReshape);
167 glutDisplayFunc(display);
168 glutKeyboardFunc(key);
169 glutMainLoop();
170 return 0; /* ANSI C requires main to return int. */
171 }