Klaus's latest patches and some clean-up
[mesa.git] / src / mesa / swrast / s_fog.c
1 /* $Id: s_fog.c,v 1.17 2002/01/21 18:12:34 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 = (GLfloat) exp(-d * z);
60 return f;
61 case GL_EXP2:
62 d = ctx->Fog.Density;
63 f = (GLfloat) 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 a span of RGBA pixels.
75 * Input: ctx -
76 * span - where span->fog and span->fogStep have to be set.
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, struct sw_span *span,
82 GLchan rgba[][4] )
83 {
84 GLuint i;
85 GLfloat fog = span->fog, Dfog = span->fogStep;
86 GLchan rFog, gFog, bFog;
87
88 ASSERT(ctx->Fog.Enabled);
89 ASSERT(span->activeMask & SPAN_FOG);
90 ASSERT(span->filledColor == GL_TRUE);
91
92 UNCLAMPED_FLOAT_TO_CHAN(rFog, ctx->Fog.Color[RCOMP]);
93 UNCLAMPED_FLOAT_TO_CHAN(gFog, ctx->Fog.Color[GCOMP]);
94 UNCLAMPED_FLOAT_TO_CHAN(bFog, ctx->Fog.Color[BCOMP]);
95
96 for (i = 0; i < span->end; i++) {
97 const GLfloat one_min_fog = 1.0F - fog;
98 rgba[i][RCOMP] = (GLchan) (fog * rgba[i][RCOMP] + one_min_fog * rFog);
99 rgba[i][GCOMP] = (GLchan) (fog * rgba[i][GCOMP] + one_min_fog * gFog);
100 rgba[i][BCOMP] = (GLchan) (fog * rgba[i][BCOMP] + one_min_fog * bFog);
101 fog += Dfog;
102 }
103 }
104
105
106 /**
107 * Apply fog given in an array to RGBA pixels.
108 * Input: ctx -
109 * span -
110 * fog - array of fog factors in [0,1]
111 * red, green, blue, alpha - pixel colors
112 * Output: red, green, blue, alpha - fogged pixel colors
113 */
114 void
115 _mesa_fog_rgba_pixels_with_array( const GLcontext *ctx, struct sw_span *span,
116 const GLfloat fog[], GLchan rgba[][4] )
117 {
118 GLuint i;
119 GLchan rFog, gFog, bFog;
120
121 ASSERT(fog != NULL);
122 ASSERT(ctx->Fog.Enabled);
123 ASSERT(span->filledColor == GL_TRUE);
124
125 UNCLAMPED_FLOAT_TO_CHAN(rFog, ctx->Fog.Color[RCOMP]);
126 UNCLAMPED_FLOAT_TO_CHAN(gFog, ctx->Fog.Color[GCOMP]);
127 UNCLAMPED_FLOAT_TO_CHAN(bFog, ctx->Fog.Color[BCOMP]);
128
129 for (i = span->start; i < span->end; i++) {
130 const GLfloat f = fog[i];
131 const GLfloat g = 1.0F - f;
132 rgba[i][RCOMP] = (GLchan) (f * rgba[i][RCOMP] + g * rFog);
133 rgba[i][GCOMP] = (GLchan) (f * rgba[i][GCOMP] + g * gFog);
134 rgba[i][BCOMP] = (GLchan) (f * rgba[i][BCOMP] + g * bFog);
135 }
136 }
137
138 /**
139 * Apply fog to an array of RGBA pixels.
140 * Input: n - number of pixels
141 * fog - array of fog factors in [0,1]
142 * red, green, blue, alpha - pixel colors
143 * Output: red, green, blue, alpha - fogged pixel colors
144 */
145 void
146 _old_fog_rgba_pixels( const GLcontext *ctx,
147 GLuint n,
148 const GLfloat fog[],
149 GLchan rgba[][4] )
150 {
151 GLuint i;
152 GLchan rFog, gFog, bFog;
153
154 UNCLAMPED_FLOAT_TO_CHAN(rFog, ctx->Fog.Color[RCOMP]);
155 UNCLAMPED_FLOAT_TO_CHAN(gFog, ctx->Fog.Color[GCOMP]);
156 UNCLAMPED_FLOAT_TO_CHAN(bFog, ctx->Fog.Color[BCOMP]);
157
158 for (i = 0; i < n; i++) {
159 const GLfloat f = fog[i];
160 const GLfloat g = 1.0F - f;
161 rgba[i][RCOMP] = (GLchan) (f * rgba[i][RCOMP] + g * rFog);
162 rgba[i][GCOMP] = (GLchan) (f * rgba[i][GCOMP] + g * gFog);
163 rgba[i][BCOMP] = (GLchan) (f * rgba[i][BCOMP] + g * bFog);
164 }
165 }
166
167
168 /**
169 * Apply fog to a span of color index pixels.
170 * Input: ctx -
171 * span - where span->fog and span->fogStep have to be set.
172 * index - pixel color indexes
173 * Output: index - fogged pixel color indexes
174 */
175 void
176 _mesa_fog_ci_pixels( const GLcontext *ctx, struct sw_span *span,
177 GLuint index[] )
178 {
179 GLuint idx = (GLuint) ctx->Fog.Index;
180 GLuint i;
181 GLfloat fog = span->fog, Dfog = span->fogStep;
182
183 ASSERT(ctx->Fog.Enabled);
184 ASSERT(span->activeMask & SPAN_FOG);
185 ASSERT(span->filledColor == GL_TRUE);
186
187 for (i = 0; i < span->end; i++) {
188 const GLfloat f = CLAMP(fog, 0.0F, 1.0F);
189 index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * idx);
190 fog += Dfog;
191 }
192 }
193
194
195 /**
196 * Apply fog given in an array to a span of color index pixels.
197 * Input: ctx -
198 * span -
199 * fog - array of fog factors in [0,1]
200 * index - pixel color indexes
201 * Output: index - fogged pixel color indexes
202 */
203 void
204 _mesa_fog_ci_pixels_with_array( const GLcontext *ctx, struct sw_span *span,
205 const GLfloat fog[], GLuint index[] )
206 {
207 GLuint idx = (GLuint) ctx->Fog.Index;
208 GLuint i;
209
210 ASSERT(fog != NULL);
211 ASSERT(ctx->Fog.Enabled);
212 ASSERT(span->filledColor == GL_TRUE);
213
214 for (i = span->start; i < span->end; i++) {
215 const GLfloat f = CLAMP(fog[i], 0.0F, 1.0F);
216 index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * idx);
217 }
218 }
219
220 /**
221 * Apply fog to an array of color index pixels.
222 * Input: n - number of pixels
223 * fog - array of fog factors in [0,1]
224 * index - pixel color indexes
225 * Output: index - fogged pixel color indexes
226 */
227 void
228 _old_fog_ci_pixels( const GLcontext *ctx,
229 GLuint n, const GLfloat fog[], GLuint index[] )
230 {
231 GLuint idx = (GLuint) ctx->Fog.Index;
232 GLuint i;
233
234 for (i = 0; i < n; i++) {
235 const GLfloat f = CLAMP(fog[i], 0.0F, 1.0F);
236 index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * idx);
237 }
238 }
239
240
241
242 /**
243 * Calculate fog factors (in [0,1]) from window z values
244 * Input: n - number of pixels
245 * z - array of integer depth values
246 * red, green, blue, alpha - pixel colors
247 * Output: red, green, blue, alpha - fogged pixel colors
248 *
249 * Use lookup table & interpolation?
250 */
251 static void
252 compute_fog_factors_from_z( const GLcontext *ctx,
253 GLuint n,
254 const GLdepth z[],
255 GLfloat fogFact[] )
256 {
257 const GLfloat *proj = ctx->ProjectionMatrixStack.Top->m;
258 const GLboolean ortho = (proj[15] != 0.0F);
259 const GLfloat p10 = proj[10];
260 const GLfloat p14 = proj[14];
261 const GLfloat tz = ctx->Viewport._WindowMap.m[MAT_TZ];
262 GLfloat szInv;
263 GLuint i;
264
265 if (ctx->Viewport._WindowMap.m[MAT_SZ] == 0.0)
266 szInv = 1.0F;
267 else
268 szInv = 1.0F / ctx->Viewport._WindowMap.m[MAT_SZ];
269
270 /*
271 * Note: to compute eyeZ from the ndcZ we have to solve the following:
272 *
273 * p[10] * eyeZ + p[14] * eyeW
274 * ndcZ = ---------------------------
275 * p[11] * eyeZ + p[15] * eyeW
276 *
277 * Thus:
278 *
279 * p[14] * eyeW - p[15] * eyeW * ndcZ
280 * eyeZ = ----------------------------------
281 * p[11] * ndcZ - p[10]
282 *
283 * If we note:
284 * a) if using an orthographic projection, p[11] = 0 and p[15] = 1.
285 * b) if using a perspective projection, p[11] = -1 and p[15] = 0.
286 * c) we assume eyeW = 1 (not always true- glVertex4)
287 *
288 * Then we can simplify the calculation of eyeZ quite a bit. We do
289 * separate calculations for the orthographic and perspective cases below.
290 * Note that we drop a negative sign or two since they don't matter.
291 */
292
293 switch (ctx->Fog.Mode) {
294 case GL_LINEAR:
295 {
296 GLfloat fogEnd = ctx->Fog.End;
297 GLfloat fogScale;
298 if (ctx->Fog.Start == ctx->Fog.End)
299 fogScale = 1.0;
300 else
301 fogScale = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
302 if (ortho) {
303 for (i=0;i<n;i++) {
304 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
305 GLfloat eyez = (ndcz - p14) / p10;
306 if (eyez < 0.0)
307 eyez = -eyez;
308 fogFact[i] = (fogEnd - eyez) * fogScale;
309 }
310 }
311 else {
312 /* perspective */
313 for (i=0;i<n;i++) {
314 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
315 GLfloat eyez = p14 / (ndcz + p10);
316 if (eyez < 0.0)
317 eyez = -eyez;
318 fogFact[i] = (fogEnd - eyez) * fogScale;
319 }
320 }
321 }
322 break;
323 case GL_EXP:
324 if (ortho) {
325 for (i=0;i<n;i++) {
326 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
327 GLfloat eyez = (ndcz - p14) / p10;
328 if (eyez < 0.0)
329 eyez = -eyez;
330 fogFact[i] = (GLfloat) exp( -ctx->Fog.Density * eyez );
331 }
332 }
333 else {
334 /* perspective */
335 for (i=0;i<n;i++) {
336 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
337 GLfloat eyez = p14 / (ndcz + p10);
338 if (eyez < 0.0)
339 eyez = -eyez;
340 fogFact[i] = (GLfloat) exp( -ctx->Fog.Density * eyez );
341 }
342 }
343 break;
344 case GL_EXP2:
345 {
346 GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
347 if (ortho) {
348 for (i=0;i<n;i++) {
349 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
350 GLfloat eyez = (ndcz - p14) / p10;
351 GLfloat tmp = negDensitySquared * eyez * eyez;
352 #if defined(__alpha__) || defined(__alpha)
353 /* XXX this underflow check may be needed for other systems*/
354 if (tmp < FLT_MIN_10_EXP)
355 tmp = FLT_MIN_10_EXP;
356 #endif
357 fogFact[i] = (GLfloat) exp( tmp );
358 }
359 }
360 else {
361 /* perspective */
362 for (i=0;i<n;i++) {
363 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
364 GLfloat eyez = p14 / (ndcz + p10);
365 GLfloat tmp = negDensitySquared * eyez * eyez;
366 #if defined(__alpha__) || defined(__alpha)
367 /* XXX this underflow check may be needed for other systems*/
368 if (tmp < FLT_MIN_10_EXP)
369 tmp = FLT_MIN_10_EXP;
370 #endif
371 fogFact[i] = (GLfloat) exp( tmp );
372 }
373 }
374 }
375 break;
376 default:
377 _mesa_problem(ctx, "Bad fog mode in compute_fog_factors_from_z");
378 return;
379 }
380 }
381
382
383 /**
384 * Apply fog to a span of RGBA pixels.
385 * Input: ctx -
386 * span - where span->depth has to be filled.
387 * red, green, blue, alpha - pixel colors
388 * Output: red, green, blue, alpha - fogged pixel colors
389 */
390 void
391 _mesa_depth_fog_rgba_pixels(const GLcontext *ctx, struct sw_span *span,
392 GLchan rgba[][4])
393 {
394 GLfloat fogFact[PB_SIZE];
395
396 ASSERT(ctx->Fog.Enabled);
397 ASSERT(span->activeMask & SPAN_Z);
398 ASSERT(span->end <= PB_SIZE);
399 ASSERT(span->filledDepth == GL_TRUE);
400 ASSERT(span->filledColor == GL_TRUE);
401
402 compute_fog_factors_from_z(ctx, span->end, span->depth, fogFact );
403 _mesa_fog_rgba_pixels_with_array( ctx, span, fogFact, rgba );
404 }
405
406
407 /**
408 * Apply fog to an array of RGBA pixels.
409 * Input: n - number of pixels
410 * z - array of integer depth values
411 * red, green, blue, alpha - pixel colors
412 * Output: red, green, blue, alpha - fogged pixel colors
413 */
414 void
415 _old_depth_fog_rgba_pixels( const GLcontext *ctx,
416 GLuint n, const GLdepth z[], GLchan rgba[][4] )
417 {
418 GLfloat fogFact[PB_SIZE];
419 ASSERT(n <= PB_SIZE);
420 compute_fog_factors_from_z( ctx, n, z, fogFact );
421 _old_fog_rgba_pixels( ctx, n, fogFact, rgba );
422 }
423
424
425 /**
426 * Apply fog to a span of color index pixels.
427 * Input: ctx -
428 * span - where span->depth has to be filled.
429 * index - pixel color indexes
430 * Output: index - fogged pixel color indexes
431 */
432 void
433 _mesa_depth_fog_ci_pixels( const GLcontext *ctx, struct sw_span *span,
434 GLuint index[] )
435 {
436 GLfloat fogFact[PB_SIZE];
437
438 ASSERT(ctx->Fog.Enabled);
439 ASSERT(span->activeMask & SPAN_Z);
440 ASSERT(span->end <= PB_SIZE);
441 ASSERT(span->filledDepth == GL_TRUE);
442 ASSERT(span->filledColor == GL_TRUE);
443
444 compute_fog_factors_from_z(ctx, span->end, span->depth, fogFact );
445 _mesa_fog_ci_pixels_with_array( ctx, span, fogFact, index );
446 }
447
448
449 /**
450 * Apply fog to an array of color index pixels.
451 * Input: n - number of pixels
452 * z - array of integer depth values
453 * index - pixel color indexes
454 * Output: index - fogged pixel color indexes
455 */
456 void
457 _old_depth_fog_ci_pixels( const GLcontext *ctx,
458 GLuint n, const GLdepth z[], GLuint index[] )
459 {
460 GLfloat fogFact[PB_SIZE];
461 ASSERT(n <= PB_SIZE);
462 compute_fog_factors_from_z( ctx, n, z, fogFact );
463 _old_fog_ci_pixels( ctx, n, fogFact, index );
464 }