1 /* $Id: s_fog.c,v 1.16 2001/12/18 04:06:46 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
);
106 * Apply fog to an array of RGBA pixels.
107 * Input: n - number of pixels
108 * fog - array of fog factors in [0,1]
109 * red, green, blue, alpha - pixel colors
110 * Output: red, green, blue, alpha - fogged pixel colors
113 _old_fog_rgba_pixels( const GLcontext
*ctx
,
119 GLchan rFog
, gFog
, bFog
;
121 UNCLAMPED_FLOAT_TO_CHAN(rFog
, ctx
->Fog
.Color
[RCOMP
]);
122 UNCLAMPED_FLOAT_TO_CHAN(gFog
, ctx
->Fog
.Color
[GCOMP
]);
123 UNCLAMPED_FLOAT_TO_CHAN(bFog
, ctx
->Fog
.Color
[BCOMP
]);
125 for (i
= 0; i
< n
; i
++) {
126 const GLfloat f
= fog
[i
];
127 const GLfloat g
= 1.0F
- f
;
128 rgba
[i
][RCOMP
] = (GLchan
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
129 rgba
[i
][GCOMP
] = (GLchan
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
130 rgba
[i
][BCOMP
] = (GLchan
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
136 * Apply fog to a span of color index pixels.
138 * span - where span->fog and span->fogStep have to be set.
139 * index - pixel color indexes
140 * Output: index - fogged pixel color indexes
143 _mesa_fog_ci_pixels( const GLcontext
*ctx
, struct sw_span
*span
,
146 GLuint idx
= (GLuint
) ctx
->Fog
.Index
;
148 GLfloat fog
= span
->fog
, Dfog
= span
->fogStep
;
150 ASSERT(ctx
->Fog
.Enabled
);
151 ASSERT(span
->activeMask
& SPAN_FOG
);
152 ASSERT(span
->filledColor
== GL_TRUE
);
154 for (i
= 0; i
< span
->end
; i
++) {
155 const GLfloat f
= CLAMP(fog
, 0.0F
, 1.0F
);
156 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
- f
) * idx
);
162 * Apply fog to an array of color index pixels.
163 * Input: n - number of pixels
164 * fog - array of fog factors in [0,1]
165 * index - pixel color indexes
166 * Output: index - fogged pixel color indexes
169 _old_fog_ci_pixels( const GLcontext
*ctx
,
170 GLuint n
, const GLfloat fog
[], GLuint index
[] )
172 GLuint idx
= (GLuint
) ctx
->Fog
.Index
;
175 for (i
= 0; i
< n
; i
++) {
176 const GLfloat f
= CLAMP(fog
[i
], 0.0F
, 1.0F
);
177 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
- f
) * idx
);
184 * Calculate fog factors (in [0,1]) from window z values
185 * Input: n - number of pixels
186 * z - array of integer depth values
187 * red, green, blue, alpha - pixel colors
188 * Output: red, green, blue, alpha - fogged pixel colors
190 * Use lookup table & interpolation?
193 compute_fog_factors_from_z( const GLcontext
*ctx
,
198 const GLfloat
*proj
= ctx
->ProjectionMatrixStack
.Top
->m
;
199 const GLboolean ortho
= (proj
[15] != 0.0F
);
200 const GLfloat p10
= proj
[10];
201 const GLfloat p14
= proj
[14];
202 const GLfloat tz
= ctx
->Viewport
._WindowMap
.m
[MAT_TZ
];
206 if (ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] == 0.0)
209 szInv
= 1.0F
/ ctx
->Viewport
._WindowMap
.m
[MAT_SZ
];
212 * Note: to compute eyeZ from the ndcZ we have to solve the following:
214 * p[10] * eyeZ + p[14] * eyeW
215 * ndcZ = ---------------------------
216 * p[11] * eyeZ + p[15] * eyeW
220 * p[14] * eyeW - p[15] * eyeW * ndcZ
221 * eyeZ = ----------------------------------
222 * p[11] * ndcZ - p[10]
225 * a) if using an orthographic projection, p[11] = 0 and p[15] = 1.
226 * b) if using a perspective projection, p[11] = -1 and p[15] = 0.
227 * c) we assume eyeW = 1 (not always true- glVertex4)
229 * Then we can simplify the calculation of eyeZ quite a bit. We do
230 * separate calculations for the orthographic and perspective cases below.
231 * Note that we drop a negative sign or two since they don't matter.
234 switch (ctx
->Fog
.Mode
) {
237 GLfloat fogEnd
= ctx
->Fog
.End
;
239 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
242 fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
245 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
246 GLfloat eyez
= (ndcz
- p14
) / p10
;
249 fogFact
[i
] = (fogEnd
- eyez
) * fogScale
;
255 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
256 GLfloat eyez
= p14
/ (ndcz
+ p10
);
259 fogFact
[i
] = (fogEnd
- eyez
) * fogScale
;
267 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
268 GLfloat eyez
= (ndcz
- p14
) / p10
;
271 fogFact
[i
] = (GLfloat
) exp( -ctx
->Fog
.Density
* eyez
);
277 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
278 GLfloat eyez
= p14
/ (ndcz
+ p10
);
281 fogFact
[i
] = (GLfloat
) exp( -ctx
->Fog
.Density
* eyez
);
287 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
290 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
291 GLfloat eyez
= (ndcz
- p14
) / p10
;
292 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
293 #if defined(__alpha__) || defined(__alpha)
294 /* XXX this underflow check may be needed for other systems*/
295 if (tmp
< FLT_MIN_10_EXP
)
296 tmp
= FLT_MIN_10_EXP
;
298 fogFact
[i
] = (GLfloat
) exp( tmp
);
304 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
305 GLfloat eyez
= p14
/ (ndcz
+ p10
);
306 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
307 #if defined(__alpha__) || defined(__alpha)
308 /* XXX this underflow check may be needed for other systems*/
309 if (tmp
< FLT_MIN_10_EXP
)
310 tmp
= FLT_MIN_10_EXP
;
312 fogFact
[i
] = (GLfloat
) exp( tmp
);
318 _mesa_problem(ctx
, "Bad fog mode in compute_fog_factors_from_z");
325 * Apply fog to a span of RGBA pixels.
327 * span - where span->depth has to be filled.
328 * red, green, blue, alpha - pixel colors
329 * Output: red, green, blue, alpha - fogged pixel colors
332 _mesa_depth_fog_rgba_pixels(const GLcontext
*ctx
, struct sw_span
*span
,
335 GLfloat fogFact
[PB_SIZE
];
337 ASSERT(ctx
->Fog
.Enabled
);
338 ASSERT(span
->activeMask
& SPAN_Z
);
339 ASSERT(span
->end
<= PB_SIZE
);
340 ASSERT(span
->filledDepth
== GL_TRUE
);
341 ASSERT(span
->filledColor
== GL_TRUE
);
343 compute_fog_factors_from_z(ctx
, span
->end
, span
->depth
, fogFact
);
344 _old_fog_rgba_pixels( ctx
, span
->end
, fogFact
, rgba
);
348 * Apply fog to an array of RGBA pixels.
349 * Input: n - number of pixels
350 * z - array of integer depth values
351 * red, green, blue, alpha - pixel colors
352 * Output: red, green, blue, alpha - fogged pixel colors
355 _old_depth_fog_rgba_pixels( const GLcontext
*ctx
,
356 GLuint n
, const GLdepth z
[], GLchan rgba
[][4] )
358 GLfloat fogFact
[PB_SIZE
];
359 ASSERT(n
<= PB_SIZE
);
360 compute_fog_factors_from_z( ctx
, n
, z
, fogFact
);
361 _old_fog_rgba_pixels( ctx
, n
, fogFact
, rgba
);
366 * Apply fog to a span of color index pixels.
368 * span - where span->depth has to be filled.
369 * index - pixel color indexes
370 * Output: index - fogged pixel color indexes
373 _mesa_depth_fog_ci_pixels( const GLcontext
*ctx
, struct sw_span
*span
,
376 GLfloat fogFact
[PB_SIZE
];
378 ASSERT(ctx
->Fog
.Enabled
);
379 ASSERT(span
->activeMask
& SPAN_Z
);
380 ASSERT(span
->end
<= PB_SIZE
);
381 ASSERT(span
->filledDepth
== GL_TRUE
);
382 ASSERT(span
->filledColor
== GL_TRUE
);
384 compute_fog_factors_from_z(ctx
, span
->end
, span
->depth
, fogFact
);
385 _old_fog_ci_pixels( ctx
, span
->end
, fogFact
, index
);
390 * Apply fog to an array of color index pixels.
391 * Input: n - number of pixels
392 * z - array of integer depth values
393 * index - pixel color indexes
394 * Output: index - fogged pixel color indexes
397 _old_depth_fog_ci_pixels( const GLcontext
*ctx
,
398 GLuint n
, const GLdepth z
[], GLuint index
[] )
400 GLfloat fogFact
[PB_SIZE
];
401 ASSERT(n
<= PB_SIZE
);
402 compute_fog_factors_from_z( ctx
, n
, z
, fogFact
);
403 _old_fog_ci_pixels( ctx
, n
, fogFact
, index
);