first checkpoint commit of Klaus's new span code (struct sw_span)
[mesa.git] / src / mesa / swrast / s_fog.c
1 /* $Id: s_fog.c,v 1.15 2001/12/17 04:54:35 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 * 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
111 */
112 void
113 _old_fog_rgba_pixels( const GLcontext *ctx,
114 GLuint n,
115 const GLfloat fog[],
116 GLchan rgba[][4] )
117 {
118 GLuint i;
119 GLchan rFog, gFog, bFog;
120
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]);
124
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);
131 }
132 }
133
134
135 /*
136 * Apply fog to a span of color index pixels.
137 * Input: ctx -
138 * span - where span->fog and span->fogStep have to be set.
139 * index - pixel color indexes
140 * Output: index - fogged pixel color indexes
141 */
142 void
143 _mesa_fog_ci_pixels( const GLcontext *ctx, struct sw_span *span,
144 GLuint index[] )
145 {
146 GLuint idx = (GLuint) ctx->Fog.Index;
147 GLuint i;
148 GLfloat fog = span->fog, Dfog = span->fogStep;
149
150 ASSERT(ctx->Fog.Enabled);
151 ASSERT(span->activeMask & SPAN_FOG);
152 ASSERT(span->filledColor == GL_TRUE);
153
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);
157 fog += Dfog;
158 }
159 }
160
161 /*
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
167 */
168 void
169 _old_fog_ci_pixels( const GLcontext *ctx,
170 GLuint n, const GLfloat fog[], GLuint index[] )
171 {
172 GLuint idx = (GLuint) ctx->Fog.Index;
173 GLuint i;
174
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);
178 }
179 }
180
181
182
183 /*
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
189 *
190 * Use lookup table & interpolation?
191 */
192 static void
193 compute_fog_factors_from_z( const GLcontext *ctx,
194 GLuint n,
195 const GLdepth z[],
196 GLfloat fogFact[] )
197 {
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];
202 GLfloat szInv;
203 GLuint i;
204
205 if (ctx->Viewport._WindowMap.m[MAT_SZ] == 0.0)
206 szInv = 1.0F;
207 else
208 szInv = 1.0F / ctx->Viewport._WindowMap.m[MAT_SZ];
209
210 /*
211 * Note: to compute eyeZ from the ndcZ we have to solve the following:
212 *
213 * p[10] * eyeZ + p[14] * eyeW
214 * ndcZ = ---------------------------
215 * p[11] * eyeZ + p[15] * eyeW
216 *
217 * Thus:
218 *
219 * p[14] * eyeW - p[15] * eyeW * ndcZ
220 * eyeZ = ----------------------------------
221 * p[11] * ndcZ - p[10]
222 *
223 * If we note:
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)
227 *
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.
231 */
232
233 switch (ctx->Fog.Mode) {
234 case GL_LINEAR:
235 {
236 GLfloat fogEnd = ctx->Fog.End;
237 GLfloat fogScale;
238 if (ctx->Fog.Start == ctx->Fog.End)
239 fogScale = 1.0;
240 else
241 fogScale = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
242 if (ortho) {
243 for (i=0;i<n;i++) {
244 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
245 GLfloat eyez = (ndcz - p14) / p10;
246 if (eyez < 0.0)
247 eyez = -eyez;
248 fogFact[i] = (fogEnd - eyez) * fogScale;
249 }
250 }
251 else {
252 /* perspective */
253 for (i=0;i<n;i++) {
254 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
255 GLfloat eyez = p14 / (ndcz + p10);
256 if (eyez < 0.0)
257 eyez = -eyez;
258 fogFact[i] = (fogEnd - eyez) * fogScale;
259 }
260 }
261 }
262 break;
263 case GL_EXP:
264 if (ortho) {
265 for (i=0;i<n;i++) {
266 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
267 GLfloat eyez = (ndcz - p14) / p10;
268 if (eyez < 0.0)
269 eyez = -eyez;
270 fogFact[i] = (GLfloat) exp( -ctx->Fog.Density * eyez );
271 }
272 }
273 else {
274 /* perspective */
275 for (i=0;i<n;i++) {
276 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
277 GLfloat eyez = p14 / (ndcz + p10);
278 if (eyez < 0.0)
279 eyez = -eyez;
280 fogFact[i] = (GLfloat) exp( -ctx->Fog.Density * eyez );
281 }
282 }
283 break;
284 case GL_EXP2:
285 {
286 GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
287 if (ortho) {
288 for (i=0;i<n;i++) {
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;
296 #endif
297 fogFact[i] = (GLfloat) exp( tmp );
298 }
299 }
300 else {
301 /* perspective */
302 for (i=0;i<n;i++) {
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;
310 #endif
311 fogFact[i] = (GLfloat) exp( tmp );
312 }
313 }
314 }
315 break;
316 default:
317 _mesa_problem(ctx, "Bad fog mode in compute_fog_factors_from_z");
318 return;
319 }
320 }
321
322
323 /*
324 * Apply fog to a span of RGBA pixels.
325 * Input: ctx -
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
329 */
330 void
331 _mesa_depth_fog_rgba_pixels(const GLcontext *ctx, struct sw_span *span,
332 GLchan rgba[][4])
333 {
334 GLfloat fogFact[PB_SIZE];
335
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);
341
342 compute_fog_factors_from_z(ctx, span->end, span->depth, fogFact );
343 _old_fog_rgba_pixels( ctx, span->end, fogFact, rgba );
344 }
345
346 /*
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
352 */
353 void
354 _old_depth_fog_rgba_pixels( const GLcontext *ctx,
355 GLuint n, const GLdepth z[], GLchan rgba[][4] )
356 {
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 );
361 }
362
363
364 /*
365 * Apply fog to a span of color index pixels.
366 * Input: ctx -
367 * span - where span->depth has to be filled.
368 * index - pixel color indexes
369 * Output: index - fogged pixel color indexes
370 */
371 void
372 _mesa_depth_fog_ci_pixels( const GLcontext *ctx, struct sw_span *span,
373 GLuint index[] )
374 {
375 GLfloat fogFact[PB_SIZE];
376
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);
382
383 compute_fog_factors_from_z(ctx, span->end, span->depth, fogFact );
384 _old_fog_ci_pixels( ctx, span->end, fogFact, index );
385 }
386
387
388 /*
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
394 */
395 void
396 _old_depth_fog_ci_pixels( const GLcontext *ctx,
397 GLuint n, const GLdepth z[], GLuint index[] )
398 {
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 );
403 }