Replace old matrix stacks with new code based on struct matrix_stack.
[mesa.git] / src / mesa / swrast / s_fog.c
1 /* $Id: s_fog.c,v 1.16 2001/12/18 04:06:46 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 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];
203 GLfloat szInv;
204 GLuint i;
205
206 if (ctx->Viewport._WindowMap.m[MAT_SZ] == 0.0)
207 szInv = 1.0F;
208 else
209 szInv = 1.0F / ctx->Viewport._WindowMap.m[MAT_SZ];
210
211 /*
212 * Note: to compute eyeZ from the ndcZ we have to solve the following:
213 *
214 * p[10] * eyeZ + p[14] * eyeW
215 * ndcZ = ---------------------------
216 * p[11] * eyeZ + p[15] * eyeW
217 *
218 * Thus:
219 *
220 * p[14] * eyeW - p[15] * eyeW * ndcZ
221 * eyeZ = ----------------------------------
222 * p[11] * ndcZ - p[10]
223 *
224 * If we note:
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)
228 *
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.
232 */
233
234 switch (ctx->Fog.Mode) {
235 case GL_LINEAR:
236 {
237 GLfloat fogEnd = ctx->Fog.End;
238 GLfloat fogScale;
239 if (ctx->Fog.Start == ctx->Fog.End)
240 fogScale = 1.0;
241 else
242 fogScale = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
243 if (ortho) {
244 for (i=0;i<n;i++) {
245 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
246 GLfloat eyez = (ndcz - p14) / p10;
247 if (eyez < 0.0)
248 eyez = -eyez;
249 fogFact[i] = (fogEnd - eyez) * fogScale;
250 }
251 }
252 else {
253 /* perspective */
254 for (i=0;i<n;i++) {
255 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
256 GLfloat eyez = p14 / (ndcz + p10);
257 if (eyez < 0.0)
258 eyez = -eyez;
259 fogFact[i] = (fogEnd - eyez) * fogScale;
260 }
261 }
262 }
263 break;
264 case GL_EXP:
265 if (ortho) {
266 for (i=0;i<n;i++) {
267 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
268 GLfloat eyez = (ndcz - p14) / p10;
269 if (eyez < 0.0)
270 eyez = -eyez;
271 fogFact[i] = (GLfloat) exp( -ctx->Fog.Density * eyez );
272 }
273 }
274 else {
275 /* perspective */
276 for (i=0;i<n;i++) {
277 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
278 GLfloat eyez = p14 / (ndcz + p10);
279 if (eyez < 0.0)
280 eyez = -eyez;
281 fogFact[i] = (GLfloat) exp( -ctx->Fog.Density * eyez );
282 }
283 }
284 break;
285 case GL_EXP2:
286 {
287 GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
288 if (ortho) {
289 for (i=0;i<n;i++) {
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;
297 #endif
298 fogFact[i] = (GLfloat) exp( tmp );
299 }
300 }
301 else {
302 /* perspective */
303 for (i=0;i<n;i++) {
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;
311 #endif
312 fogFact[i] = (GLfloat) exp( tmp );
313 }
314 }
315 }
316 break;
317 default:
318 _mesa_problem(ctx, "Bad fog mode in compute_fog_factors_from_z");
319 return;
320 }
321 }
322
323
324 /*
325 * Apply fog to a span of RGBA pixels.
326 * Input: ctx -
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
330 */
331 void
332 _mesa_depth_fog_rgba_pixels(const GLcontext *ctx, struct sw_span *span,
333 GLchan rgba[][4])
334 {
335 GLfloat fogFact[PB_SIZE];
336
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);
342
343 compute_fog_factors_from_z(ctx, span->end, span->depth, fogFact );
344 _old_fog_rgba_pixels( ctx, span->end, fogFact, rgba );
345 }
346
347 /*
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
353 */
354 void
355 _old_depth_fog_rgba_pixels( const GLcontext *ctx,
356 GLuint n, const GLdepth z[], GLchan rgba[][4] )
357 {
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 );
362 }
363
364
365 /*
366 * Apply fog to a span of color index pixels.
367 * Input: ctx -
368 * span - where span->depth has to be filled.
369 * index - pixel color indexes
370 * Output: index - fogged pixel color indexes
371 */
372 void
373 _mesa_depth_fog_ci_pixels( const GLcontext *ctx, struct sw_span *span,
374 GLuint index[] )
375 {
376 GLfloat fogFact[PB_SIZE];
377
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);
383
384 compute_fog_factors_from_z(ctx, span->end, span->depth, fogFact );
385 _old_fog_ci_pixels( ctx, span->end, fogFact, index );
386 }
387
388
389 /*
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
395 */
396 void
397 _old_depth_fog_ci_pixels( const GLcontext *ctx,
398 GLuint n, const GLdepth z[], GLuint index[] )
399 {
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 );
404 }