Merge branch '7.8'
[mesa.git] / progs / redbook / fogcoord.c
1 /*
2 * Copyright (c) 1993-2003, Silicon Graphics, Inc.
3 * All Rights Reserved
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose and without fee is hereby granted, provided that the above
7 * copyright notice appear in all copies and that both the copyright
8 * notice and this permission notice appear in supporting documentation,
9 * and that the name of Silicon Graphics, Inc. not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission.
12 *
13 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND
14 * WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
16 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
17 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
18 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF
20 * PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD
21 * PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN ADVISED OF
22 * THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE
24 * OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * US Government Users Restricted Rights
27 * Use, duplication, or disclosure by the Government is subject to
28 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
29 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
30 * clause at DFARS 252.227-7013 and/or in similar or successor clauses
31 * in the FAR or the DOD or NASA FAR Supplement. Unpublished - rights
32 * reserved under the copyright laws of the United States.
33 *
34 * Contractor/manufacturer is:
35 * Silicon Graphics, Inc.
36 * 1500 Crittenden Lane
37 * Mountain View, CA 94043
38 * United State of America
39 *
40 * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
41 */
42
43 /*
44 * fogcoord.c
45 *
46 * This program demonstrates the use of explicit fog
47 * coordinates. You can press the keyboard and change
48 * the fog coordinate value at any vertex. You can
49 * also switch between using explicit fog coordinates
50 * and the default fog generation mode.
51 *
52 * Pressing the 'f' and 'b' keys move the viewer forward
53 * and backwards.
54 * Pressing 'c' initiates the default fog generation.
55 * Pressing capital 'C' restores explicit fog coordinates.
56 * Pressing '1', '2', '3', '8', '9', and '0' add or
57 * subtract from the fog coordinate values at one of the
58 * three vertices of the triangle.
59 */
60
61 #include <GL/glew.h>
62 #include <GL/glut.h>
63 #include <math.h>
64 #include <stdlib.h>
65 #include <stdio.h>
66
67 static GLfloat f1, f2, f3;
68
69 /* Initialize fog
70 */
71 static void init(void)
72 {
73 GLfloat fogColor[4] = {0.0, 0.25, 0.25, 1.0};
74 f1 = 1.0f;
75 f2 = 5.0f;
76 f3 = 10.0f;
77
78 glEnable(GL_FOG);
79 glFogi (GL_FOG_MODE, GL_EXP);
80 glFogfv (GL_FOG_COLOR, fogColor);
81 glFogf (GL_FOG_DENSITY, 0.25);
82 glHint (GL_FOG_HINT, GL_DONT_CARE);
83 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
84 glClearColor(0.0, 0.25, 0.25, 1.0); /* fog color */
85 }
86
87 /* display() draws a triangle at an angle.
88 */
89 static void display(void)
90 {
91 glClear(GL_COLOR_BUFFER_BIT);
92
93 glColor3f (1.0f, 0.75f, 0.0f);
94 glBegin (GL_TRIANGLES);
95 glFogCoordfEXT (f1);
96 glVertex3f (2.0f, -2.0f, 0.0f);
97 glFogCoordfEXT (f2);
98 glVertex3f (-2.0f, 0.0f, -5.0f);
99 glFogCoordfEXT (f3);
100 glVertex3f (0.0f, 2.0f, -10.0f);
101 glEnd();
102
103 glutSwapBuffers();
104 }
105
106 static void reshape(int w, int h)
107 {
108 glViewport(0, 0, (GLsizei) w, (GLsizei) h);
109 glMatrixMode(GL_PROJECTION);
110 glLoadIdentity();
111 gluPerspective (45.0, 1.0, 0.25, 25.0);
112 glMatrixMode(GL_MODELVIEW);
113 glLoadIdentity ();
114 glTranslatef (0.0, 0.0, -5.0);
115 }
116
117 static void keyboard(unsigned char key, int x, int y)
118 {
119 switch (key) {
120 case 'c':
121 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FRAGMENT_DEPTH_EXT);
122 glutPostRedisplay();
123 break;
124 case 'C':
125 glFogi(GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT);
126 glutPostRedisplay();
127 break;
128 case '1':
129 f1 = f1 + 0.25;
130 glutPostRedisplay();
131 break;
132 case '2':
133 f2 = f2 + 0.25;
134 glutPostRedisplay();
135 break;
136 case '3':
137 f3 = f3 + 0.25;
138 glutPostRedisplay();
139 break;
140 case '8':
141 if (f1 > 0.25) {
142 f1 = f1 - 0.25;
143 glutPostRedisplay();
144 }
145 break;
146 case '9':
147 if (f2 > 0.25) {
148 f2 = f2 - 0.25;
149 glutPostRedisplay();
150 }
151 break;
152 case '0':
153 if (f3 > 0.25) {
154 f3 = f3 - 0.25;
155 glutPostRedisplay();
156 }
157 break;
158 case 'b':
159 glMatrixMode (GL_MODELVIEW);
160 glTranslatef (0.0, 0.0, -0.25);
161 glutPostRedisplay();
162 break;
163 case 'f':
164 glMatrixMode (GL_MODELVIEW);
165 glTranslatef (0.0, 0.0, 0.25);
166 glutPostRedisplay();
167 break;
168 case 27:
169 exit(0);
170 break;
171 default:
172 break;
173 }
174 }
175
176
177 /* Main Loop
178 * Open window with initial window size, title bar,
179 * RGBA display mode, depth buffer, and handle input events.
180 */
181 int main(int argc, char** argv)
182 {
183 glutInit(&argc, argv);
184 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
185 glutInitWindowSize(500, 500);
186 glutCreateWindow(argv[0]);
187 glewInit();
188 init();
189 glutReshapeFunc (reshape);
190 glutKeyboardFunc (keyboard);
191 glutDisplayFunc (display);
192 glutMainLoop();
193 return 0;
194 }