Merge branch '7.8'
[mesa.git] / progs / redbook / fogindex.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 * fogindex.c
42 * This program demonstrates fog in color index mode.
43 * Three cones are drawn at different z values in a linear
44 * fog. 32 contiguous colors (from 16 to 47) are loaded
45 * with a color ramp.
46 */
47 #include <stdlib.h>
48 #include <GL/glut.h>
49
50 /* Initialize color map and fog. Set screen clear color
51 * to end of color ramp.
52 */
53 #define NUM_COLORS 32
54 #define RAMPSTART 16
55
56 static void
57 myinit(void)
58 {
59 int i;
60
61 glEnable(GL_DEPTH_TEST);
62 glDepthFunc(GL_LESS);
63 for (i = 0; i < NUM_COLORS; i++) {
64 GLfloat shade;
65 shade = (GLfloat) (NUM_COLORS - i) / (GLfloat) NUM_COLORS;
66 glutSetColor(16 + i, shade, shade, shade);
67 }
68 glEnable(GL_FOG);
69
70 glFogi(GL_FOG_MODE, GL_LINEAR);
71 glFogi(GL_FOG_INDEX, NUM_COLORS);
72 glFogf(GL_FOG_START, 0.0);
73 glFogf(GL_FOG_END, 4.0);
74 glHint(GL_FOG_HINT, GL_NICEST);
75 glClearIndex((GLfloat) (NUM_COLORS + RAMPSTART - 1));
76 }
77
78 /* display() renders 3 cones at different z positions.
79 */
80 static void
81 display(void)
82 {
83 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
84 glPushMatrix();
85 glTranslatef(-1.0, -1.0, -1.0);
86 glRotatef(-90.0, 1.0, 0.0, 0.0);
87 glIndexi(RAMPSTART);
88 glutSolidCone(1.0, 2.0, 10, 10);
89 glPopMatrix();
90
91 glPushMatrix();
92 glTranslatef(0.0, -1.0, -2.25);
93 glRotatef(-90.0, 1.0, 0.0, 0.0);
94 glIndexi(RAMPSTART);
95 glutSolidCone(1.0, 2.0, 10, 10);
96 glPopMatrix();
97
98 glPushMatrix();
99 glTranslatef(1.0, -1.0, -3.5);
100 glRotatef(-90.0, 1.0, 0.0, 0.0);
101 glIndexi(RAMPSTART);
102 glutSolidCone(1.0, 2.0, 10, 10);
103 glPopMatrix();
104 glFlush();
105 }
106
107 static void
108 myReshape(int w, int h)
109 {
110 glViewport(0, 0, w, h);
111 glMatrixMode(GL_PROJECTION);
112 glLoadIdentity();
113 if (w <= h)
114 glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
115 2.0 * (GLfloat) h / (GLfloat) w, 0.0, 10.0);
116 else
117 glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
118 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, 0.0, 10.0);
119 glMatrixMode(GL_MODELVIEW);
120 glLoadIdentity();
121 }
122
123 static void
124 key(unsigned char k, int x, int y)
125 {
126 switch (k) {
127 case 27: /* Escape */
128 exit(0);
129 break;
130 default:
131 return;
132 }
133 glutPostRedisplay();
134 }
135
136 /* Main Loop
137 * Open window with initial window size, title bar,
138 * RGBA display mode, depth buffer, and handle input events.
139 */
140 int
141 main(int argc, char **argv)
142 {
143 glutInit(&argc, argv);
144 glutInitDisplayMode(GLUT_SINGLE | GLUT_INDEX | GLUT_DEPTH);
145 glutCreateWindow(argv[0]);
146 myinit();
147 glutReshapeFunc(myReshape);
148 glutDisplayFunc(display);
149 glutKeyboardFunc(key);
150 glutMainLoop();
151 return 0; /* ANSI C requires main to return int. */
152 }