fix glDraw/CopyPixels w/ fog bug. minor fog code clean-ups.
[mesa.git] / src / mesa / swrast / s_fog.c
1 /* $Id: s_fog.c,v 1.13 2001/06/18 23:55:18 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27
28 #include "glheader.h"
29 #include "colormac.h"
30 #include "context.h"
31 #include "macros.h"
32 #include "mmath.h"
33
34 #include "s_context.h"
35 #include "s_fog.h"
36 #include "s_pb.h"
37
38
39
40
41 /*
42 * Used to convert current raster distance to a fog factor in [0,1].
43 */
44 GLfloat
45 _mesa_z_to_fogfactor(GLcontext *ctx, GLfloat z)
46 {
47 GLfloat d, f;
48
49 switch (ctx->Fog.Mode) {
50 case GL_LINEAR:
51 if (ctx->Fog.Start == ctx->Fog.End)
52 d = 1.0F;
53 else
54 d = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
55 f = (ctx->Fog.End - z) * d;
56 return CLAMP(f, 0.0F, 1.0F);
57 case GL_EXP:
58 d = ctx->Fog.Density;
59 f = exp(-d * z);
60 return f;
61 case GL_EXP2:
62 d = ctx->Fog.Density;
63 f = exp(-(d * d * z * z));
64 return f;
65 default:
66 _mesa_problem(ctx, "Bad fog mode in make_fog_coord");
67 return 0.0;
68 }
69 }
70
71
72
73 /*
74 * Apply fog to an array of RGBA pixels.
75 * Input: n - number of pixels
76 * fog - array of fog factors in [0,1]
77 * red, green, blue, alpha - pixel colors
78 * Output: red, green, blue, alpha - fogged pixel colors
79 */
80 void
81 _mesa_fog_rgba_pixels( const GLcontext *ctx,
82 GLuint n,
83 const GLfloat fog[],
84 GLchan rgba[][4] )
85 {
86 GLuint i;
87 GLchan rFog, gFog, bFog;
88
89 UNCLAMPED_FLOAT_TO_CHAN(rFog, ctx->Fog.Color[RCOMP]);
90 UNCLAMPED_FLOAT_TO_CHAN(gFog, ctx->Fog.Color[GCOMP]);
91 UNCLAMPED_FLOAT_TO_CHAN(bFog, ctx->Fog.Color[BCOMP]);
92
93 for (i = 0; i < n; i++) {
94 const GLfloat f = fog[i];
95 const GLfloat g = 1.0 - f;
96 rgba[i][RCOMP] = f * rgba[i][RCOMP] + g * rFog;
97 rgba[i][GCOMP] = f * rgba[i][GCOMP] + g * gFog;
98 rgba[i][BCOMP] = f * rgba[i][BCOMP] + g * bFog;
99 }
100 }
101
102
103
104 /*
105 * Apply fog to an array of color index pixels.
106 * Input: n - number of pixels
107 * fog - array of fog factors in [0,1]
108 * index - pixel color indexes
109 * Output: index - fogged pixel color indexes
110 */
111 void
112 _mesa_fog_ci_pixels( const GLcontext *ctx,
113 GLuint n, const GLfloat fog[], GLuint index[] )
114 {
115 GLuint idx = (GLuint) ctx->Fog.Index;
116 GLuint i;
117
118 for (i = 0; i < n; i++) {
119 const GLfloat f = CLAMP(fog[i], 0.0, 1.0);
120 index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * idx);
121 }
122 }
123
124
125
126 /*
127 * Calculate fog factors (in [0,1]) from window z values
128 * Input: n - number of pixels
129 * z - array of integer depth values
130 * red, green, blue, alpha - pixel colors
131 * Output: red, green, blue, alpha - fogged pixel colors
132 *
133 * Use lookup table & interpolation?
134 */
135 static void
136 compute_fog_factors_from_z( const GLcontext *ctx,
137 GLuint n,
138 const GLdepth z[],
139 GLfloat fogFact[] )
140 {
141 const GLboolean ortho = (ctx->ProjectionMatrix.m[15] != 0.0F);
142 const GLfloat p10 = ctx->ProjectionMatrix.m[10];
143 const GLfloat p14 = ctx->ProjectionMatrix.m[14];
144 const GLfloat tz = ctx->Viewport._WindowMap.m[MAT_TZ];
145 GLfloat szInv;
146 GLuint i;
147
148 if (ctx->Viewport._WindowMap.m[MAT_SZ] == 0.0)
149 szInv = 1.0F;
150 else
151 szInv = 1.0F / ctx->Viewport._WindowMap.m[MAT_SZ];
152
153 /*
154 * Note: to compute eyeZ from the ndcZ we have to solve the following:
155 *
156 * p[10] * eyeZ + p[14] * eyeW
157 * ndcZ = ---------------------------
158 * p[11] * eyeZ + p[15] * eyeW
159 *
160 * Thus:
161 *
162 * p[14] * eyeW - p[15] * eyeW * ndcZ
163 * eyeZ = ----------------------------------
164 * p[11] * ndcZ - p[10]
165 *
166 * If we note:
167 * a) if using an orthographic projection, p[11] = 0 and p[15] = 1.
168 * b) if using a perspective projection, p[11] = -1 and p[15] = 0.
169 * c) we assume eyeW = 1 (not always true- glVertex4)
170 *
171 * Then we can simplify the calculation of eyeZ quite a bit. We do
172 * separate calculations for the orthographic and perspective cases below.
173 * Note that we drop a negative sign or two since they don't matter.
174 */
175
176 switch (ctx->Fog.Mode) {
177 case GL_LINEAR:
178 {
179 GLfloat fogEnd = ctx->Fog.End;
180 GLfloat fogScale;
181 if (ctx->Fog.Start == ctx->Fog.End)
182 fogScale = 1.0;
183 else
184 fogScale = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
185 if (ortho) {
186 for (i=0;i<n;i++) {
187 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
188 GLfloat eyez = (ndcz - p14) / p10;
189 if (eyez < 0.0)
190 eyez = -eyez;
191 fogFact[i] = (fogEnd - eyez) * fogScale;
192 }
193 }
194 else {
195 /* perspective */
196 for (i=0;i<n;i++) {
197 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
198 GLfloat eyez = p14 / (ndcz + p10);
199 if (eyez < 0.0)
200 eyez = -eyez;
201 fogFact[i] = (fogEnd - eyez) * fogScale;
202 }
203 }
204 }
205 break;
206 case GL_EXP:
207 if (ortho) {
208 for (i=0;i<n;i++) {
209 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
210 GLfloat eyez = (ndcz - p14) / p10;
211 if (eyez < 0.0)
212 eyez = -eyez;
213 fogFact[i] = exp( -ctx->Fog.Density * eyez );
214 }
215 }
216 else {
217 /* perspective */
218 for (i=0;i<n;i++) {
219 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
220 GLfloat eyez = p14 / (ndcz + p10);
221 if (eyez < 0.0)
222 eyez = -eyez;
223 fogFact[i] = exp( -ctx->Fog.Density * eyez );
224 }
225 }
226 break;
227 case GL_EXP2:
228 {
229 GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
230 if (ortho) {
231 for (i=0;i<n;i++) {
232 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
233 GLfloat eyez = (ndcz - p14) / p10;
234 GLfloat tmp = negDensitySquared * eyez * eyez;
235 #if defined(__alpha__) || defined(__alpha)
236 /* XXX this underflow check may be needed for other systems*/
237 if (tmp < FLT_MIN_10_EXP)
238 tmp = FLT_MIN_10_EXP;
239 #endif
240 fogFact[i] = exp( tmp );
241 }
242 }
243 else {
244 /* perspective */
245 for (i=0;i<n;i++) {
246 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
247 GLfloat eyez = p14 / (ndcz + p10);
248 GLfloat tmp = negDensitySquared * eyez * eyez;
249 #if defined(__alpha__) || defined(__alpha)
250 /* XXX this underflow check may be needed for other systems*/
251 if (tmp < FLT_MIN_10_EXP)
252 tmp = FLT_MIN_10_EXP;
253 #endif
254 fogFact[i] = exp( tmp );
255 }
256 }
257 }
258 break;
259 default:
260 _mesa_problem(ctx, "Bad fog mode in compute_fog_factors_from_z");
261 return;
262 }
263 }
264
265
266 /*
267 * Apply fog to an array of RGBA pixels.
268 * Input: n - number of pixels
269 * z - array of integer depth values
270 * red, green, blue, alpha - pixel colors
271 * Output: red, green, blue, alpha - fogged pixel colors
272 */
273 void
274 _mesa_depth_fog_rgba_pixels( const GLcontext *ctx,
275 GLuint n, const GLdepth z[], GLchan rgba[][4] )
276 {
277 GLfloat fogFact[PB_SIZE];
278 ASSERT(n <= PB_SIZE);
279 compute_fog_factors_from_z( ctx, n, z, fogFact );
280 _mesa_fog_rgba_pixels( ctx, n, fogFact, rgba );
281 }
282
283
284 /*
285 * Apply fog to an array of color index pixels.
286 * Input: n - number of pixels
287 * z - array of integer depth values
288 * index - pixel color indexes
289 * Output: index - fogged pixel color indexes
290 */
291 void
292 _mesa_depth_fog_ci_pixels( const GLcontext *ctx,
293 GLuint n, const GLdepth z[], GLuint index[] )
294 {
295 GLfloat fogFact[PB_SIZE];
296 ASSERT(n <= PB_SIZE);
297 compute_fog_factors_from_z( ctx, n, z, fogFact );
298 _mesa_fog_ci_pixels( ctx, n, fogFact, index );
299 }