1 /* $Id: s_fog.c,v 1.17 2002/01/21 18:12:34 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2001 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
34 #include "s_context.h"
42 * Used to convert current raster distance to a fog factor in [0,1].
45 _mesa_z_to_fogfactor(GLcontext
*ctx
, GLfloat z
)
49 switch (ctx
->Fog
.Mode
) {
51 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
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
);
59 f
= (GLfloat
) exp(-d
* z
);
63 f
= (GLfloat
) exp(-(d
* d
* z
* z
));
66 _mesa_problem(ctx
, "Bad fog mode in make_fog_coord");
74 * Apply fog to a span of RGBA pixels.
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
81 _mesa_fog_rgba_pixels( const GLcontext
*ctx
, struct sw_span
*span
,
85 GLfloat fog
= span
->fog
, Dfog
= span
->fogStep
;
86 GLchan rFog
, gFog
, bFog
;
88 ASSERT(ctx
->Fog
.Enabled
);
89 ASSERT(span
->activeMask
& SPAN_FOG
);
90 ASSERT(span
->filledColor
== GL_TRUE
);
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
]);
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
);
107 * Apply fog given in an array to RGBA pixels.
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
115 _mesa_fog_rgba_pixels_with_array( const GLcontext
*ctx
, struct sw_span
*span
,
116 const GLfloat fog
[], GLchan rgba
[][4] )
119 GLchan rFog
, gFog
, bFog
;
122 ASSERT(ctx
->Fog
.Enabled
);
123 ASSERT(span
->filledColor
== GL_TRUE
);
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
]);
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
);
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
146 _old_fog_rgba_pixels( const GLcontext
*ctx
,
152 GLchan rFog
, gFog
, bFog
;
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
]);
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
);
169 * Apply fog to a span of color index pixels.
171 * span - where span->fog and span->fogStep have to be set.
172 * index - pixel color indexes
173 * Output: index - fogged pixel color indexes
176 _mesa_fog_ci_pixels( const GLcontext
*ctx
, struct sw_span
*span
,
179 GLuint idx
= (GLuint
) ctx
->Fog
.Index
;
181 GLfloat fog
= span
->fog
, Dfog
= span
->fogStep
;
183 ASSERT(ctx
->Fog
.Enabled
);
184 ASSERT(span
->activeMask
& SPAN_FOG
);
185 ASSERT(span
->filledColor
== GL_TRUE
);
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
);
196 * Apply fog given in an array to a span of color index pixels.
199 * fog - array of fog factors in [0,1]
200 * index - pixel color indexes
201 * Output: index - fogged pixel color indexes
204 _mesa_fog_ci_pixels_with_array( const GLcontext
*ctx
, struct sw_span
*span
,
205 const GLfloat fog
[], GLuint index
[] )
207 GLuint idx
= (GLuint
) ctx
->Fog
.Index
;
211 ASSERT(ctx
->Fog
.Enabled
);
212 ASSERT(span
->filledColor
== GL_TRUE
);
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
);
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
228 _old_fog_ci_pixels( const GLcontext
*ctx
,
229 GLuint n
, const GLfloat fog
[], GLuint index
[] )
231 GLuint idx
= (GLuint
) ctx
->Fog
.Index
;
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
);
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
249 * Use lookup table & interpolation?
252 compute_fog_factors_from_z( const GLcontext
*ctx
,
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
];
265 if (ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] == 0.0)
268 szInv
= 1.0F
/ ctx
->Viewport
._WindowMap
.m
[MAT_SZ
];
271 * Note: to compute eyeZ from the ndcZ we have to solve the following:
273 * p[10] * eyeZ + p[14] * eyeW
274 * ndcZ = ---------------------------
275 * p[11] * eyeZ + p[15] * eyeW
279 * p[14] * eyeW - p[15] * eyeW * ndcZ
280 * eyeZ = ----------------------------------
281 * p[11] * ndcZ - p[10]
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)
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.
293 switch (ctx
->Fog
.Mode
) {
296 GLfloat fogEnd
= ctx
->Fog
.End
;
298 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
301 fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
304 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
305 GLfloat eyez
= (ndcz
- p14
) / p10
;
308 fogFact
[i
] = (fogEnd
- eyez
) * fogScale
;
314 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
315 GLfloat eyez
= p14
/ (ndcz
+ p10
);
318 fogFact
[i
] = (fogEnd
- eyez
) * fogScale
;
326 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
327 GLfloat eyez
= (ndcz
- p14
) / p10
;
330 fogFact
[i
] = (GLfloat
) exp( -ctx
->Fog
.Density
* eyez
);
336 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
337 GLfloat eyez
= p14
/ (ndcz
+ p10
);
340 fogFact
[i
] = (GLfloat
) exp( -ctx
->Fog
.Density
* eyez
);
346 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
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
;
357 fogFact
[i
] = (GLfloat
) exp( tmp
);
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
;
371 fogFact
[i
] = (GLfloat
) exp( tmp
);
377 _mesa_problem(ctx
, "Bad fog mode in compute_fog_factors_from_z");
384 * Apply fog to a span of RGBA pixels.
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
391 _mesa_depth_fog_rgba_pixels(const GLcontext
*ctx
, struct sw_span
*span
,
394 GLfloat fogFact
[PB_SIZE
];
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
);
402 compute_fog_factors_from_z(ctx
, span
->end
, span
->depth
, fogFact
);
403 _mesa_fog_rgba_pixels_with_array( ctx
, span
, fogFact
, rgba
);
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
415 _old_depth_fog_rgba_pixels( const GLcontext
*ctx
,
416 GLuint n
, const GLdepth z
[], GLchan rgba
[][4] )
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
);
426 * Apply fog to a span of color index pixels.
428 * span - where span->depth has to be filled.
429 * index - pixel color indexes
430 * Output: index - fogged pixel color indexes
433 _mesa_depth_fog_ci_pixels( const GLcontext
*ctx
, struct sw_span
*span
,
436 GLfloat fogFact
[PB_SIZE
];
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
);
444 compute_fog_factors_from_z(ctx
, span
->end
, span
->depth
, fogFact
);
445 _mesa_fog_ci_pixels_with_array( ctx
, span
, fogFact
, index
);
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
457 _old_depth_fog_ci_pixels( const GLcontext
*ctx
,
458 GLuint n
, const GLdepth z
[], GLuint index
[] )
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
);