1 /* $Id: s_fog.c,v 1.15 2001/12/17 04:54:35 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 GLboolean ortho
= (ctx
->ProjectionMatrix
.m
[15] != 0.0F
);
199 const GLfloat p10
= ctx
->ProjectionMatrix
.m
[10];
200 const GLfloat p14
= ctx
->ProjectionMatrix
.m
[14];
201 const GLfloat tz
= ctx
->Viewport
._WindowMap
.m
[MAT_TZ
];
205 if (ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] == 0.0)
208 szInv
= 1.0F
/ ctx
->Viewport
._WindowMap
.m
[MAT_SZ
];
211 * Note: to compute eyeZ from the ndcZ we have to solve the following:
213 * p[10] * eyeZ + p[14] * eyeW
214 * ndcZ = ---------------------------
215 * p[11] * eyeZ + p[15] * eyeW
219 * p[14] * eyeW - p[15] * eyeW * ndcZ
220 * eyeZ = ----------------------------------
221 * p[11] * ndcZ - p[10]
224 * a) if using an orthographic projection, p[11] = 0 and p[15] = 1.
225 * b) if using a perspective projection, p[11] = -1 and p[15] = 0.
226 * c) we assume eyeW = 1 (not always true- glVertex4)
228 * Then we can simplify the calculation of eyeZ quite a bit. We do
229 * separate calculations for the orthographic and perspective cases below.
230 * Note that we drop a negative sign or two since they don't matter.
233 switch (ctx
->Fog
.Mode
) {
236 GLfloat fogEnd
= ctx
->Fog
.End
;
238 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
241 fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
244 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
245 GLfloat eyez
= (ndcz
- p14
) / p10
;
248 fogFact
[i
] = (fogEnd
- eyez
) * fogScale
;
254 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
255 GLfloat eyez
= p14
/ (ndcz
+ p10
);
258 fogFact
[i
] = (fogEnd
- eyez
) * fogScale
;
266 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
267 GLfloat eyez
= (ndcz
- p14
) / p10
;
270 fogFact
[i
] = (GLfloat
) exp( -ctx
->Fog
.Density
* eyez
);
276 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
277 GLfloat eyez
= p14
/ (ndcz
+ p10
);
280 fogFact
[i
] = (GLfloat
) exp( -ctx
->Fog
.Density
* eyez
);
286 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
289 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
290 GLfloat eyez
= (ndcz
- p14
) / p10
;
291 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
292 #if defined(__alpha__) || defined(__alpha)
293 /* XXX this underflow check may be needed for other systems*/
294 if (tmp
< FLT_MIN_10_EXP
)
295 tmp
= FLT_MIN_10_EXP
;
297 fogFact
[i
] = (GLfloat
) exp( tmp
);
303 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
304 GLfloat eyez
= p14
/ (ndcz
+ p10
);
305 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
306 #if defined(__alpha__) || defined(__alpha)
307 /* XXX this underflow check may be needed for other systems*/
308 if (tmp
< FLT_MIN_10_EXP
)
309 tmp
= FLT_MIN_10_EXP
;
311 fogFact
[i
] = (GLfloat
) exp( tmp
);
317 _mesa_problem(ctx
, "Bad fog mode in compute_fog_factors_from_z");
324 * Apply fog to a span of RGBA pixels.
326 * span - where span->depth has to be filled.
327 * red, green, blue, alpha - pixel colors
328 * Output: red, green, blue, alpha - fogged pixel colors
331 _mesa_depth_fog_rgba_pixels(const GLcontext
*ctx
, struct sw_span
*span
,
334 GLfloat fogFact
[PB_SIZE
];
336 ASSERT(ctx
->Fog
.Enabled
);
337 ASSERT(span
->activeMask
& SPAN_Z
);
338 ASSERT(span
->end
<= PB_SIZE
);
339 ASSERT(span
->filledDepth
== GL_TRUE
);
340 ASSERT(span
->filledColor
== GL_TRUE
);
342 compute_fog_factors_from_z(ctx
, span
->end
, span
->depth
, fogFact
);
343 _old_fog_rgba_pixels( ctx
, span
->end
, fogFact
, rgba
);
347 * Apply fog to an array of RGBA pixels.
348 * Input: n - number of pixels
349 * z - array of integer depth values
350 * red, green, blue, alpha - pixel colors
351 * Output: red, green, blue, alpha - fogged pixel colors
354 _old_depth_fog_rgba_pixels( const GLcontext
*ctx
,
355 GLuint n
, const GLdepth z
[], GLchan rgba
[][4] )
357 GLfloat fogFact
[PB_SIZE
];
358 ASSERT(n
<= PB_SIZE
);
359 compute_fog_factors_from_z( ctx
, n
, z
, fogFact
);
360 _old_fog_rgba_pixels( ctx
, n
, fogFact
, rgba
);
365 * Apply fog to a span of color index pixels.
367 * span - where span->depth has to be filled.
368 * index - pixel color indexes
369 * Output: index - fogged pixel color indexes
372 _mesa_depth_fog_ci_pixels( const GLcontext
*ctx
, struct sw_span
*span
,
375 GLfloat fogFact
[PB_SIZE
];
377 ASSERT(ctx
->Fog
.Enabled
);
378 ASSERT(span
->activeMask
& SPAN_Z
);
379 ASSERT(span
->end
<= PB_SIZE
);
380 ASSERT(span
->filledDepth
== GL_TRUE
);
381 ASSERT(span
->filledColor
== GL_TRUE
);
383 compute_fog_factors_from_z(ctx
, span
->end
, span
->depth
, fogFact
);
384 _old_fog_ci_pixels( ctx
, span
->end
, fogFact
, index
);
389 * Apply fog to an array of color index pixels.
390 * Input: n - number of pixels
391 * z - array of integer depth values
392 * index - pixel color indexes
393 * Output: index - fogged pixel color indexes
396 _old_depth_fog_ci_pixels( const GLcontext
*ctx
,
397 GLuint n
, const GLdepth z
[], GLuint index
[] )
399 GLfloat fogFact
[PB_SIZE
];
400 ASSERT(n
<= PB_SIZE
);
401 compute_fog_factors_from_z( ctx
, n
, z
, fogFact
);
402 _old_fog_ci_pixels( ctx
, n
, fogFact
, index
);