1 /* $Id: s_fog.c,v 1.9 2001/03/03 20:33:30 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 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"
40 * Apply fog to an array of RGBA pixels.
41 * Input: n - number of pixels
42 * fog - array of interpolated screen-space fog coordinates in [0..1]
43 * red, green, blue, alpha - pixel colors
44 * Output: red, green, blue, alpha - fogged pixel colors
47 _mesa_fog_rgba_pixels( const GLcontext
*ctx
,
53 GLchan rFog
, gFog
, bFog
;
55 UNCLAMPED_FLOAT_TO_CHAN(rFog
, ctx
->Fog
.Color
[RCOMP
]);
56 UNCLAMPED_FLOAT_TO_CHAN(gFog
, ctx
->Fog
.Color
[GCOMP
]);
57 UNCLAMPED_FLOAT_TO_CHAN(bFog
, ctx
->Fog
.Color
[BCOMP
]);
59 #if CHAN_TYPE == GL_FLOAT
60 for (i
= 0; i
< n
; i
++) {
61 const GLfixed cf
= CLAMP(fog
[i
], 0, FIXED_ONE
);
62 const GLfloat f
= FixedToFloat(cf
);
63 const GLfloat g
= 1.0F
- f
;
64 rgba
[i
][RCOMP
] = f
* rgba
[i
][RCOMP
] + g
* rFog
;
65 rgba
[i
][GCOMP
] = f
* rgba
[i
][GCOMP
] + g
* gFog
;
66 rgba
[i
][BCOMP
] = f
* rgba
[i
][BCOMP
] + g
* bFog
;
69 for (i
= 0; i
< n
; i
++) {
70 const GLfixed f
= CLAMP(fog
[i
], 0, FIXED_ONE
);
71 const GLfixed g
= FIXED_ONE
- f
;
72 rgba
[i
][0] = (f
* rgba
[i
][0] + g
* rFog
) >> FIXED_SHIFT
;
73 rgba
[i
][1] = (f
* rgba
[i
][1] + g
* gFog
) >> FIXED_SHIFT
;
74 rgba
[i
][2] = (f
* rgba
[i
][2] + g
* bFog
) >> FIXED_SHIFT
;
82 * Apply fog to an array of color index pixels.
83 * Input: n - number of pixels
84 * z - array of integer depth values
85 * index - pixel color indexes
86 * Output: index - fogged pixel color indexes
89 _mesa_fog_ci_pixels( const GLcontext
*ctx
,
90 GLuint n
, const GLfixed fog
[], GLuint index
[] )
92 GLuint idx
= ctx
->Fog
.Index
;
96 GLfloat f
= FixedToFloat(CLAMP(fog
[i
], 0, FIXED_ONE
));
97 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * idx
);
104 * Calculate fog coords from window z values
105 * Input: n - number of pixels
106 * z - array of integer depth values
107 * red, green, blue, alpha - pixel colors
108 * Output: red, green, blue, alpha - fogged pixel colors
110 * Use lookup table & interpolation?
113 _mesa_win_fog_coords_from_z( const GLcontext
*ctx
,
118 const GLboolean ortho
= (ctx
->ProjectionMatrix
.m
[15] != 0.0F
);
119 const GLfloat p10
= ctx
->ProjectionMatrix
.m
[10];
120 const GLfloat p14
= ctx
->ProjectionMatrix
.m
[14];
121 const GLfloat tz
= ctx
->Viewport
._WindowMap
.m
[MAT_TZ
];
125 if (ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] == 0.0)
128 szInv
= 1.0F
/ ctx
->Viewport
._WindowMap
.m
[MAT_SZ
];
131 * Note: to compute eyeZ from the ndcZ we have to solve the following:
133 * p[10] * eyeZ + p[14] * eyeW
134 * ndcZ = ---------------------------
135 * p[11] * eyeZ + p[15] * eyeW
139 * p[14] * eyeW - p[15] * eyeW * ndcZ
140 * eyeZ = ----------------------------------
141 * p[11] * ndcZ - p[10]
144 * a) if using an orthographic projection, p[11] = 0 and p[15] = 1.
145 * b) if using a perspective projection, p[11] = -1 and p[15] = 0.
146 * c) we assume eyeW = 1 (not always true- glVertex4)
148 * Then we can simplify the calculation of eyeZ quite a bit. We do
149 * separate calculations for the orthographic and perspective cases below.
150 * Note that we drop a negative sign or two since they don't matter.
153 switch (ctx
->Fog
.Mode
) {
156 GLfloat fogEnd
= ctx
->Fog
.End
;
158 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
161 fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
164 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
165 GLfloat eyez
= (ndcz
- p14
) / p10
;
168 fogcoord
[i
] = FloatToFixed((fogEnd
- eyez
) * fogScale
);
174 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
175 GLfloat eyez
= p14
/ (ndcz
+ p10
);
178 fogcoord
[i
] = FloatToFixed((fogEnd
- eyez
) * fogScale
);
186 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
187 GLfloat eyez
= (ndcz
- p14
) / p10
;
190 fogcoord
[i
] = FloatToFixed(exp( -ctx
->Fog
.Density
* eyez
));
196 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
197 GLfloat eyez
= p14
/ (ndcz
+ p10
);
200 fogcoord
[i
] = FloatToFixed(exp( -ctx
->Fog
.Density
* eyez
));
206 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
209 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
210 GLfloat eyez
= (ndcz
- p14
) / p10
;
211 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
212 #if defined(__alpha__) || defined(__alpha)
213 /* XXX this underflow check may be needed for other systems*/
214 if (tmp
< FLT_MIN_10_EXP
)
215 tmp
= FLT_MIN_10_EXP
;
217 fogcoord
[i
] = FloatToFixed(exp( tmp
));
223 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
224 GLfloat eyez
= p14
/ (ndcz
+ p10
);
225 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
226 #if defined(__alpha__) || defined(__alpha)
227 /* XXX this underflow check may be needed for other systems*/
228 if (tmp
< FLT_MIN_10_EXP
)
229 tmp
= FLT_MIN_10_EXP
;
231 fogcoord
[i
] = FloatToFixed(exp( tmp
));
237 _mesa_problem(ctx
, "Bad fog mode in _mesa_win_fog_coords_from_z");
244 * Apply fog to an array of RGBA pixels.
245 * Input: n - number of pixels
246 * z - array of integer depth values
247 * red, green, blue, alpha - pixel colors
248 * Output: red, green, blue, alpha - fogged pixel colors
251 _mesa_depth_fog_rgba_pixels( const GLcontext
*ctx
,
252 GLuint n
, const GLdepth z
[], GLchan rgba
[][4] )
254 GLfixed fog
[PB_SIZE
];
255 ASSERT(n
<= PB_SIZE
);
256 _mesa_win_fog_coords_from_z( ctx
, n
, z
, fog
);
257 _mesa_fog_rgba_pixels( ctx
, n
, fog
, rgba
);
262 * Apply fog to an array of color index pixels.
263 * Input: n - number of pixels
264 * z - array of integer depth values
265 * index - pixel color indexes
266 * Output: index - fogged pixel color indexes
269 _mesa_depth_fog_ci_pixels( const GLcontext
*ctx
,
270 GLuint n
, const GLdepth z
[], GLuint index
[] )
272 GLfixed fog
[PB_SIZE
];
273 ASSERT(n
<= PB_SIZE
);
274 _mesa_win_fog_coords_from_z( ctx
, n
, z
, fog
);
275 _mesa_fog_ci_pixels( ctx
, n
, fog
, index
);