1 /* $Id: s_fog.c,v 1.23 2002/08/07 00:45:07 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2002 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 _mesa_z_to_fogfactor");
74 * Calculate fog factors (in [0,1]) from window z values
75 * Input: n - number of pixels
76 * z - array of integer depth values
77 * red, green, blue, alpha - pixel colors
78 * Output: red, green, blue, alpha - fogged pixel colors
80 * Use lookup table & interpolation?
83 compute_fog_factors_from_z( const GLcontext
*ctx
,
88 const GLfloat
*proj
= ctx
->ProjectionMatrixStack
.Top
->m
;
89 const GLboolean ortho
= (proj
[15] != 0.0F
);
90 const GLfloat p10
= proj
[10];
91 const GLfloat p14
= proj
[14];
92 const GLfloat tz
= ctx
->Viewport
._WindowMap
.m
[MAT_TZ
];
96 if (ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] == 0.0)
99 szInv
= 1.0F
/ ctx
->Viewport
._WindowMap
.m
[MAT_SZ
];
102 * Note: to compute eyeZ from the ndcZ we have to solve the following:
104 * p[10] * eyeZ + p[14] * eyeW
105 * ndcZ = ---------------------------
106 * p[11] * eyeZ + p[15] * eyeW
110 * p[14] * eyeW - p[15] * eyeW * ndcZ
111 * eyeZ = ----------------------------------
112 * p[11] * ndcZ - p[10]
115 * a) if using an orthographic projection, p[11] = 0 and p[15] = 1.
116 * b) if using a perspective projection, p[11] = -1 and p[15] = 0.
117 * c) we assume eyeW = 1 (not always true- glVertex4)
119 * Then we can simplify the calculation of eyeZ quite a bit. We do
120 * separate calculations for the orthographic and perspective cases below.
121 * Note that we drop a negative sign or two since they don't matter.
124 switch (ctx
->Fog
.Mode
) {
127 GLfloat fogEnd
= ctx
->Fog
.End
;
129 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
132 fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
135 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
136 GLfloat eyez
= (ndcz
- p14
) / p10
;
140 f
= (fogEnd
- eyez
) * fogScale
;
141 fogFact
[i
] = CLAMP(f
, 0.0F
, 1.0F
);
147 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
148 GLfloat eyez
= p14
/ (ndcz
+ p10
);
152 f
= (fogEnd
- eyez
) * fogScale
;
153 fogFact
[i
] = CLAMP(f
, 0.0F
, 1.0F
);
161 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
162 GLfloat eyez
= (ndcz
- p14
) / p10
;
165 fogFact
[i
] = (GLfloat
) exp( -ctx
->Fog
.Density
* eyez
);
171 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
172 GLfloat eyez
= p14
/ (ndcz
+ p10
);
175 fogFact
[i
] = (GLfloat
) exp( -ctx
->Fog
.Density
* eyez
);
181 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
184 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
185 GLfloat eyez
= (ndcz
- p14
) / p10
;
186 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
187 #if defined(__alpha__) || defined(__alpha)
188 /* XXX this underflow check may be needed for other systems*/
189 if (tmp
< FLT_MIN_10_EXP
)
190 tmp
= FLT_MIN_10_EXP
;
192 fogFact
[i
] = (GLfloat
) exp( tmp
);
198 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
199 GLfloat eyez
= p14
/ (ndcz
+ p10
);
200 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
201 #if defined(__alpha__) || defined(__alpha)
202 /* XXX this underflow check may be needed for other systems*/
203 if (tmp
< FLT_MIN_10_EXP
)
204 tmp
= FLT_MIN_10_EXP
;
206 fogFact
[i
] = (GLfloat
) exp( tmp
);
212 _mesa_problem(ctx
, "Bad fog mode in compute_fog_factors_from_z");
220 * Apply fog to a span of RGBA pixels.
221 * The fog factors are either in the span->array->fog or stored as base/step.
222 * These are fog _factors_, not fog coords. Fog coords were converted to
223 * fog factors per vertex.
226 _mesa_fog_rgba_span( const GLcontext
*ctx
, struct sw_span
*span
)
228 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
229 const GLuint n
= span
->end
;
230 GLchan (*rgba
)[4] = (GLchan (*)[4]) span
->array
->rgba
;
231 GLchan rFog
, gFog
, bFog
;
233 ASSERT(ctx
->Fog
.Enabled
);
234 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_FOG
);
235 ASSERT(span
->arrayMask
& SPAN_RGBA
);
237 UNCLAMPED_FLOAT_TO_CHAN(rFog
, ctx
->Fog
.Color
[RCOMP
]);
238 UNCLAMPED_FLOAT_TO_CHAN(gFog
, ctx
->Fog
.Color
[GCOMP
]);
239 UNCLAMPED_FLOAT_TO_CHAN(bFog
, ctx
->Fog
.Color
[BCOMP
]);
241 if (swrast
->_PreferPixelFog
) {
242 /* compute fog factor from each fragment's Z value */
243 if ((span
->interpMask
& SPAN_Z
) && (span
->arrayMask
& SPAN_Z
) == 0)
244 _mesa_span_interpolate_z(ctx
, span
);
245 compute_fog_factors_from_z(ctx
, n
, span
->array
->z
, span
->array
->fog
);
246 span
->arrayMask
|= SPAN_FOG
;
249 if (span
->arrayMask
& SPAN_FOG
) {
250 /* use fog array in span */
252 for (i
= 0; i
< n
; i
++) {
253 const GLfloat fog
= span
->array
->fog
[i
];
254 const GLfloat oneMinusFog
= 1.0F
- fog
;
255 rgba
[i
][RCOMP
] = (GLchan
) (fog
* rgba
[i
][RCOMP
] + oneMinusFog
* rFog
);
256 rgba
[i
][GCOMP
] = (GLchan
) (fog
* rgba
[i
][GCOMP
] + oneMinusFog
* gFog
);
257 rgba
[i
][BCOMP
] = (GLchan
) (fog
* rgba
[i
][BCOMP
] + oneMinusFog
* bFog
);
261 /* interpolate fog factors */
262 GLfloat fog
= span
->fog
, dFog
= span
->fogStep
;
264 for (i
= 0; i
< n
; i
++) {
265 const GLfloat oneMinusFog
= 1.0F
- fog
;
266 rgba
[i
][RCOMP
] = (GLchan
) (fog
* rgba
[i
][RCOMP
] + oneMinusFog
* rFog
);
267 rgba
[i
][GCOMP
] = (GLchan
) (fog
* rgba
[i
][GCOMP
] + oneMinusFog
* gFog
);
268 rgba
[i
][BCOMP
] = (GLchan
) (fog
* rgba
[i
][BCOMP
] + oneMinusFog
* bFog
);
276 * As above, but color index mode.
279 _mesa_fog_ci_span( const GLcontext
*ctx
, struct sw_span
*span
)
281 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
282 const GLuint n
= span
->end
;
283 GLuint
*index
= span
->array
->index
;
285 ASSERT(ctx
->Fog
.Enabled
);
286 ASSERT(span
->arrayMask
& SPAN_INDEX
);
287 ASSERT((span
->interpMask
| span
->arrayMask
) & SPAN_FOG
);
289 if (swrast
->_PreferPixelFog
) {
290 /* compute fog factor from each fragment's Z value */
291 if ((span
->interpMask
& SPAN_Z
) && (span
->arrayMask
& SPAN_Z
) == 0)
292 _mesa_span_interpolate_z(ctx
, span
);
293 compute_fog_factors_from_z(ctx
, n
, span
->array
->z
, span
->array
->fog
);
294 span
->arrayMask
|= SPAN_FOG
;
297 if (span
->arrayMask
& SPAN_FOG
) {
298 const GLuint idx
= (GLuint
) ctx
->Fog
.Index
;
300 for (i
= 0; i
< n
; i
++) {
301 const GLfloat f
= CLAMP(span
->array
->fog
[i
], 0.0F
, 1.0F
);
302 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
- f
) * idx
);
306 GLfloat fog
= span
->fog
, dFog
= span
->fogStep
;
307 const GLuint idx
= (GLuint
) ctx
->Fog
.Index
;
309 for (i
= 0; i
< n
; i
++) {
310 const GLfloat f
= CLAMP(fog
, 0.0F
, 1.0F
);
311 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
- f
) * idx
);