silence compiler warnings
[mesa.git] / progs / redbook / fog.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 * fog.c
42 * This program draws 5 red teapots, each at a different
43 * z distance from the eye, in different types of fog.
44 * Pressing the left mouse button chooses between 3 types of
45 * fog: exponential, exponential squared, and linear.
46 * In this program, there is a fixed density value, as well
47 * as fixed start and end values for the linear fog.
48 */
49 #include <stdlib.h>
50 #include <math.h>
51 #include <GL/glut.h>
52
53 GLint fogMode;
54
55 void
56 selectFog(int mode)
57 {
58 switch(mode) {
59 case GL_LINEAR:
60 glFogf(GL_FOG_START, 1.0);
61 glFogf(GL_FOG_END, 5.0);
62 /* falls through */
63 mode = 0xfff;
64 case GL_EXP2:
65 case GL_EXP:
66 glFogiv(0xf/*GL_FOG_MODE*/, (int *) &mode);
67 glutPostRedisplay();
68 break;
69 case 0:
70 exit(0);
71 }
72 }
73
74 /* Initialize z-buffer, projection matrix, light source,
75 * and lighting model. Do not specify a material property here.
76 */
77 void
78 myinit(void)
79 {
80 GLfloat position[] =
81 {0.0, 3.0, 3.0, 0.0};
82 GLfloat local_view[] =
83 {0.0};
84
85 glEnable(GL_DEPTH_TEST);
86 glDepthFunc(GL_LESS);
87
88 glLightfv(GL_LIGHT0, GL_POSITION, position);
89 glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
90
91 glFrontFace(GL_CW);
92 glEnable(GL_LIGHTING);
93 glEnable(GL_LIGHT0);
94 glEnable(GL_AUTO_NORMAL);
95 glEnable(GL_NORMALIZE);
96 glEnable(GL_FOG);
97 {
98 GLfloat fogColor[4] =
99 {0.5, 0.5, 0.5, 1.0};
100
101 fogMode = GL_EXP;
102 glFogi(GL_FOG_MODE, fogMode);
103 glFogfv(GL_FOG_COLOR, fogColor);
104 glFogf(GL_FOG_DENSITY, 0.35);
105 glHint(GL_FOG_HINT, GL_DONT_CARE);
106 glClearColor(0.5, 0.5, 0.5, 1.0);
107 }
108 }
109
110 void
111 renderRedTeapot(GLfloat x, GLfloat y, GLfloat z)
112 {
113 float mat[4];
114
115 glPushMatrix();
116 glTranslatef(x, y, z);
117 mat[0] = 0.1745;
118 mat[1] = 0.01175;
119 mat[2] = 0.01175;
120 mat[3] = 1.0;
121 glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
122 mat[0] = 0.61424;
123 mat[1] = 0.04136;
124 mat[2] = 0.04136;
125 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
126 mat[0] = 0.727811;
127 mat[1] = 0.626959;
128 mat[2] = 0.626959;
129 glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
130 glMaterialf(GL_FRONT, GL_SHININESS, 0.6 * 128.0);
131 glutSolidTeapot(1.0);
132 glPopMatrix();
133 }
134
135 /* display() draws 5 teapots at different z positions.
136 */
137 void
138 display(void)
139 {
140 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
141 renderRedTeapot(-4.0, -0.5, -1.0);
142 renderRedTeapot(-2.0, -0.5, -2.0);
143 renderRedTeapot(0.0, -0.5, -3.0);
144 renderRedTeapot(2.0, -0.5, -4.0);
145 renderRedTeapot(4.0, -0.5, -5.0);
146 glFlush();
147 }
148
149 void
150 myReshape(int w, int h)
151 {
152 glViewport(0, 0, w, h);
153 glMatrixMode(GL_PROJECTION);
154 glLoadIdentity();
155 if (w <= (h * 3))
156 glOrtho(-6.0, 6.0, -2.0 * ((GLfloat) h * 3) / (GLfloat) w,
157 2.0 * ((GLfloat) h * 3) / (GLfloat) w, 0.0, 10.0);
158 else
159 glOrtho(-6.0 * (GLfloat) w / ((GLfloat) h * 3),
160 6.0 * (GLfloat) w / ((GLfloat) h * 3), -2.0, 2.0, 0.0, 10.0);
161 glMatrixMode(GL_MODELVIEW);
162 glLoadIdentity();
163 }
164
165 static void
166 key(unsigned char k, int x, int y)
167 {
168 switch (k) {
169 case 27: /* Escape */
170 exit(0);
171 break;
172 default:
173 return;
174 }
175 glutPostRedisplay();
176 }
177
178 /* Main Loop
179 * Open window with initial window size, title bar,
180 * RGBA display mode, depth buffer, and handle input events.
181 */
182 int
183 main(int argc, char **argv)
184 {
185 glutInit(&argc, argv);
186 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
187 glutInitWindowSize(450, 150);
188 glutCreateWindow(argv[0]);
189 myinit();
190 glutReshapeFunc(myReshape);
191 glutDisplayFunc(display);
192 glutCreateMenu(selectFog);
193 glutAddMenuEntry("Fog EXP", GL_EXP);
194 glutAddMenuEntry("Fog EXP2", /*GL_EXP2*/ 0xffff);
195 glutAddMenuEntry("Fog LINEAR", GL_LINEAR);
196 glutAddMenuEntry("Quit", 0);
197 glutAttachMenu(GLUT_RIGHT_BUTTON);
198 glutKeyboardFunc(key);
199 glutMainLoop();
200 return 0; /* ANSI C requires main to return int. */
201 }