~ubuntu-branches/ubuntu/quantal/mesa-glw/quantal

« back to all changes in this revision

Viewing changes to progs/redbook/fog.c

  • Committer: Bazaar Package Importer
  • Author(s): Morten Kjeldgaard
  • Date: 2008-05-06 16:19:15 UTC
  • Revision ID: james.westby@ubuntu.com-20080506161915-uynz7nftmfixu6bq
Tags: upstream-7.0.3
ImportĀ upstreamĀ versionĀ 7.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    case GL_EXP2:
 
64
    case GL_EXP:
 
65
        glFogi(GL_FOG_MODE, mode);
 
66
        glutPostRedisplay();
 
67
        break;
 
68
    case 0:
 
69
        exit(0);
 
70
    }
 
71
}
 
72
 
 
73
/*  Initialize z-buffer, projection matrix, light source,
 
74
 *  and lighting model.  Do not specify a material property here.
 
75
 */
 
76
void
 
77
myinit(void)
 
78
{
 
79
    GLfloat position[] =
 
80
    {0.0, 3.0, 3.0, 0.0};
 
81
    GLfloat local_view[] =
 
82
    {0.0};
 
83
 
 
84
    glEnable(GL_DEPTH_TEST);
 
85
    glDepthFunc(GL_LESS);
 
86
 
 
87
    glLightfv(GL_LIGHT0, GL_POSITION, position);
 
88
    glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
 
89
 
 
90
    glFrontFace(GL_CW);
 
91
    glEnable(GL_LIGHTING);
 
92
    glEnable(GL_LIGHT0);
 
93
    glEnable(GL_AUTO_NORMAL);
 
94
    glEnable(GL_NORMALIZE);
 
95
    glEnable(GL_FOG);
 
96
    {
 
97
        GLfloat fogColor[4] =
 
98
        {0.5, 0.5, 0.5, 1.0};
 
99
 
 
100
        fogMode = GL_EXP;
 
101
        glFogi(GL_FOG_MODE, fogMode);
 
102
        glFogfv(GL_FOG_COLOR, fogColor);
 
103
        glFogf(GL_FOG_DENSITY, 0.35);
 
104
        glHint(GL_FOG_HINT, GL_DONT_CARE);
 
105
        glClearColor(0.5, 0.5, 0.5, 1.0);
 
106
    }
 
107
}
 
108
 
 
109
void
 
110
renderRedTeapot(GLfloat x, GLfloat y, GLfloat z)
 
111
{
 
112
    float mat[4];
 
113
 
 
114
    glPushMatrix();
 
115
    glTranslatef(x, y, z);
 
116
    mat[0] = 0.1745;
 
117
    mat[1] = 0.01175;
 
118
    mat[2] = 0.01175;
 
119
    mat[3] = 1.0;
 
120
    glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
 
121
    mat[0] = 0.61424;
 
122
    mat[1] = 0.04136;
 
123
    mat[2] = 0.04136;
 
124
    glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
 
125
    mat[0] = 0.727811;
 
126
    mat[1] = 0.626959;
 
127
    mat[2] = 0.626959;
 
128
    glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
 
129
    glMaterialf(GL_FRONT, GL_SHININESS, 0.6 * 128.0);
 
130
    glutSolidTeapot(1.0);
 
131
    glPopMatrix();
 
132
}
 
133
 
 
134
/*  display() draws 5 teapots at different z positions.
 
135
 */
 
136
void
 
137
display(void)
 
138
{
 
139
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
140
    renderRedTeapot(-4.0, -0.5, -1.0);
 
141
    renderRedTeapot(-2.0, -0.5, -2.0);
 
142
    renderRedTeapot(0.0, -0.5, -3.0);
 
143
    renderRedTeapot(2.0, -0.5, -4.0);
 
144
    renderRedTeapot(4.0, -0.5, -5.0);
 
145
    glFlush();
 
146
}
 
147
 
 
148
void
 
149
myReshape(int w, int h)
 
150
{
 
151
    glViewport(0, 0, w, h);
 
152
    glMatrixMode(GL_PROJECTION);
 
153
    glLoadIdentity();
 
154
    if (w <= (h * 3))
 
155
        glOrtho(-6.0, 6.0, -2.0 * ((GLfloat) h * 3) / (GLfloat) w,
 
156
            2.0 * ((GLfloat) h * 3) / (GLfloat) w, 0.0, 10.0);
 
157
    else
 
158
        glOrtho(-6.0 * (GLfloat) w / ((GLfloat) h * 3),
 
159
            6.0 * (GLfloat) w / ((GLfloat) h * 3), -2.0, 2.0, 0.0, 10.0);
 
160
    glMatrixMode(GL_MODELVIEW);
 
161
    glLoadIdentity();
 
162
}
 
163
 
 
164
static void
 
165
key(unsigned char k, int x, int y)
 
166
{
 
167
  switch (k) {
 
168
  case 27:  /* Escape */
 
169
    exit(0);
 
170
    break;
 
171
  default:
 
172
    return;
 
173
  }
 
174
  glutPostRedisplay();
 
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
 
182
main(int argc, char **argv)
 
183
{
 
184
    glutInit(&argc, argv);
 
185
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
 
186
    glutInitWindowSize(450, 150);
 
187
    glutCreateWindow(argv[0]);
 
188
    myinit();
 
189
    glutReshapeFunc(myReshape);
 
190
    glutDisplayFunc(display);
 
191
    glutCreateMenu(selectFog);
 
192
    glutAddMenuEntry("Fog EXP", GL_EXP);
 
193
    glutAddMenuEntry("Fog EXP2", GL_EXP2);
 
194
    glutAddMenuEntry("Fog LINEAR", GL_LINEAR);
 
195
    glutAddMenuEntry("Quit", 0);
 
196
    glutAttachMenu(GLUT_RIGHT_BUTTON);
 
197
    glutKeyboardFunc(key);
 
198
    glutMainLoop();
 
199
    return 0;             /* ANSI C requires main to return int. */
 
200
}