mesa: Restore 78-column wrapping of license text in C-style comments.
[mesa.git] / src / mesa / main / fog.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 5.1
4 *
5 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 #include "glheader.h"
28 #include "colormac.h"
29 #include "context.h"
30 #include "fog.h"
31 #include "macros.h"
32 #include "mtypes.h"
33
34
35
36 void GLAPIENTRY
37 _mesa_Fogf(GLenum pname, GLfloat param)
38 {
39 GLfloat fparam[4];
40 fparam[0] = param;
41 fparam[1] = fparam[2] = fparam[3] = 0.0F;
42 _mesa_Fogfv(pname, fparam);
43 }
44
45
46 void GLAPIENTRY
47 _mesa_Fogi(GLenum pname, GLint param )
48 {
49 GLfloat fparam[4];
50 fparam[0] = (GLfloat) param;
51 fparam[1] = fparam[2] = fparam[3] = 0.0F;
52 _mesa_Fogfv(pname, fparam);
53 }
54
55
56 void GLAPIENTRY
57 _mesa_Fogiv(GLenum pname, const GLint *params )
58 {
59 GLfloat p[4];
60 switch (pname) {
61 case GL_FOG_MODE:
62 case GL_FOG_DENSITY:
63 case GL_FOG_START:
64 case GL_FOG_END:
65 case GL_FOG_INDEX:
66 case GL_FOG_COORDINATE_SOURCE_EXT:
67 p[0] = (GLfloat) *params;
68 break;
69 case GL_FOG_COLOR:
70 p[0] = INT_TO_FLOAT( params[0] );
71 p[1] = INT_TO_FLOAT( params[1] );
72 p[2] = INT_TO_FLOAT( params[2] );
73 p[3] = INT_TO_FLOAT( params[3] );
74 break;
75 default:
76 /* Error will be caught later in _mesa_Fogfv */
77 ASSIGN_4V(p, 0.0F, 0.0F, 0.0F, 0.0F);
78 }
79 _mesa_Fogfv(pname, p);
80 }
81
82
83 /**
84 * Update the gl_fog_attrib::_Scale field.
85 */
86 static void
87 update_fog_scale(struct gl_context *ctx)
88 {
89 if (ctx->Fog.End == ctx->Fog.Start)
90 ctx->Fog._Scale = 1.0f;
91 else
92 ctx->Fog._Scale = 1.0f / (ctx->Fog.End - ctx->Fog.Start);
93 }
94
95
96 void GLAPIENTRY
97 _mesa_Fogfv( GLenum pname, const GLfloat *params )
98 {
99 GET_CURRENT_CONTEXT(ctx);
100 GLenum m;
101
102 switch (pname) {
103 case GL_FOG_MODE:
104 m = (GLenum) (GLint) *params;
105 switch (m) {
106 case GL_LINEAR:
107 case GL_EXP:
108 case GL_EXP2:
109 break;
110 default:
111 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" );
112 return;
113 }
114 if (ctx->Fog.Mode == m)
115 return;
116 FLUSH_VERTICES(ctx, _NEW_FOG);
117 ctx->Fog.Mode = m;
118 break;
119 case GL_FOG_DENSITY:
120 if (*params<0.0) {
121 _mesa_error( ctx, GL_INVALID_VALUE, "glFog" );
122 return;
123 }
124 if (ctx->Fog.Density == *params)
125 return;
126 FLUSH_VERTICES(ctx, _NEW_FOG);
127 ctx->Fog.Density = *params;
128 break;
129 case GL_FOG_START:
130 if (ctx->Fog.Start == *params)
131 return;
132 FLUSH_VERTICES(ctx, _NEW_FOG);
133 ctx->Fog.Start = *params;
134 update_fog_scale(ctx);
135 break;
136 case GL_FOG_END:
137 if (ctx->Fog.End == *params)
138 return;
139 FLUSH_VERTICES(ctx, _NEW_FOG);
140 ctx->Fog.End = *params;
141 update_fog_scale(ctx);
142 break;
143 case GL_FOG_INDEX:
144 if (ctx->API != API_OPENGL_COMPAT)
145 goto invalid_pname;
146 if (ctx->Fog.Index == *params)
147 return;
148 FLUSH_VERTICES(ctx, _NEW_FOG);
149 ctx->Fog.Index = *params;
150 break;
151 case GL_FOG_COLOR:
152 if (TEST_EQ_4V(ctx->Fog.Color, params))
153 return;
154 FLUSH_VERTICES(ctx, _NEW_FOG);
155 ctx->Fog.ColorUnclamped[0] = params[0];
156 ctx->Fog.ColorUnclamped[1] = params[1];
157 ctx->Fog.ColorUnclamped[2] = params[2];
158 ctx->Fog.ColorUnclamped[3] = params[3];
159 ctx->Fog.Color[0] = CLAMP(params[0], 0.0F, 1.0F);
160 ctx->Fog.Color[1] = CLAMP(params[1], 0.0F, 1.0F);
161 ctx->Fog.Color[2] = CLAMP(params[2], 0.0F, 1.0F);
162 ctx->Fog.Color[3] = CLAMP(params[3], 0.0F, 1.0F);
163 break;
164 case GL_FOG_COORDINATE_SOURCE_EXT: {
165 GLenum p = (GLenum) (GLint) *params;
166 if (ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.EXT_fog_coord ||
167 (p != GL_FOG_COORDINATE_EXT && p != GL_FRAGMENT_DEPTH_EXT)) {
168 _mesa_error(ctx, GL_INVALID_ENUM, "glFog");
169 return;
170 }
171 if (ctx->Fog.FogCoordinateSource == p)
172 return;
173 FLUSH_VERTICES(ctx, _NEW_FOG);
174 ctx->Fog.FogCoordinateSource = p;
175 break;
176 }
177 case GL_FOG_DISTANCE_MODE_NV: {
178 GLenum p = (GLenum) (GLint) *params;
179 if (ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.NV_fog_distance ||
180 (p != GL_EYE_RADIAL_NV && p != GL_EYE_PLANE && p != GL_EYE_PLANE_ABSOLUTE_NV)) {
181 _mesa_error(ctx, GL_INVALID_ENUM, "glFog");
182 return;
183 }
184 if (ctx->Fog.FogDistanceMode == p)
185 return;
186 FLUSH_VERTICES(ctx, _NEW_FOG);
187 ctx->Fog.FogDistanceMode = p;
188 break;
189 }
190 default:
191 goto invalid_pname;
192 }
193
194 if (ctx->Driver.Fogfv) {
195 (*ctx->Driver.Fogfv)( ctx, pname, params );
196 }
197
198 return;
199
200 invalid_pname:
201 _mesa_error( ctx, GL_INVALID_ENUM, "glFog" );
202 return;
203 }
204
205
206 /**********************************************************************/
207 /***** Initialization *****/
208 /**********************************************************************/
209
210 void _mesa_init_fog( struct gl_context * ctx )
211 {
212 /* Fog group */
213 ctx->Fog.Enabled = GL_FALSE;
214 ctx->Fog.Mode = GL_EXP;
215 ASSIGN_4V( ctx->Fog.Color, 0.0, 0.0, 0.0, 0.0 );
216 ASSIGN_4V( ctx->Fog.ColorUnclamped, 0.0, 0.0, 0.0, 0.0 );
217 ctx->Fog.Index = 0.0;
218 ctx->Fog.Density = 1.0;
219 ctx->Fog.Start = 0.0;
220 ctx->Fog.End = 1.0;
221 ctx->Fog.ColorSumEnabled = GL_FALSE;
222 ctx->Fog.FogCoordinateSource = GL_FRAGMENT_DEPTH_EXT;
223 ctx->Fog._Scale = 1.0f;
224 ctx->Fog.FogDistanceMode = GL_EYE_PLANE_ABSOLUTE_NV;
225 }