lots of gl_*() to _mesa_*() namespace clean-up
[mesa.git] / src / mesa / swrast / s_fog.c
1 /* $Id: s_fog.c,v 1.9 2001/03/03 20:33:30 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 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 * 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
45 */
46 void
47 _mesa_fog_rgba_pixels( const GLcontext *ctx,
48 GLuint n,
49 const GLfixed fog[],
50 GLchan rgba[][4] )
51 {
52 GLuint i;
53 GLchan rFog, gFog, bFog;
54
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]);
58
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;
67 }
68 #else
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;
75 }
76 #endif
77 }
78
79
80
81 /*
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
87 */
88 void
89 _mesa_fog_ci_pixels( const GLcontext *ctx,
90 GLuint n, const GLfixed fog[], GLuint index[] )
91 {
92 GLuint idx = ctx->Fog.Index;
93 GLuint i;
94
95 for (i=0;i<n;i++) {
96 GLfloat f = FixedToFloat(CLAMP(fog[i], 0, FIXED_ONE));
97 index[i] = (GLuint) ((GLfloat) index[i] + (1.0F-f) * idx);
98 }
99 }
100
101
102
103 /*
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
109 *
110 * Use lookup table & interpolation?
111 */
112 void
113 _mesa_win_fog_coords_from_z( const GLcontext *ctx,
114 GLuint n,
115 const GLdepth z[],
116 GLfixed fogcoord[] )
117 {
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];
122 GLfloat szInv;
123 GLuint i;
124
125 if (ctx->Viewport._WindowMap.m[MAT_SZ] == 0.0)
126 szInv = 1.0F;
127 else
128 szInv = 1.0F / ctx->Viewport._WindowMap.m[MAT_SZ];
129
130 /*
131 * Note: to compute eyeZ from the ndcZ we have to solve the following:
132 *
133 * p[10] * eyeZ + p[14] * eyeW
134 * ndcZ = ---------------------------
135 * p[11] * eyeZ + p[15] * eyeW
136 *
137 * Thus:
138 *
139 * p[14] * eyeW - p[15] * eyeW * ndcZ
140 * eyeZ = ----------------------------------
141 * p[11] * ndcZ - p[10]
142 *
143 * If we note:
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)
147 *
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.
151 */
152
153 switch (ctx->Fog.Mode) {
154 case GL_LINEAR:
155 {
156 GLfloat fogEnd = ctx->Fog.End;
157 GLfloat fogScale;
158 if (ctx->Fog.Start == ctx->Fog.End)
159 fogScale = 1.0;
160 else
161 fogScale = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
162 if (ortho) {
163 for (i=0;i<n;i++) {
164 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
165 GLfloat eyez = (ndcz - p14) / p10;
166 if (eyez < 0.0)
167 eyez = -eyez;
168 fogcoord[i] = FloatToFixed((fogEnd - eyez) * fogScale);
169 }
170 }
171 else {
172 /* perspective */
173 for (i=0;i<n;i++) {
174 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
175 GLfloat eyez = p14 / (ndcz + p10);
176 if (eyez < 0.0)
177 eyez = -eyez;
178 fogcoord[i] = FloatToFixed((fogEnd - eyez) * fogScale);
179 }
180 }
181 }
182 break;
183 case GL_EXP:
184 if (ortho) {
185 for (i=0;i<n;i++) {
186 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
187 GLfloat eyez = (ndcz - p14) / p10;
188 if (eyez < 0.0)
189 eyez = -eyez;
190 fogcoord[i] = FloatToFixed(exp( -ctx->Fog.Density * eyez ));
191 }
192 }
193 else {
194 /* perspective */
195 for (i=0;i<n;i++) {
196 GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
197 GLfloat eyez = p14 / (ndcz + p10);
198 if (eyez < 0.0)
199 eyez = -eyez;
200 fogcoord[i] = FloatToFixed(exp( -ctx->Fog.Density * eyez ));
201 }
202 }
203 break;
204 case GL_EXP2:
205 {
206 GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
207 if (ortho) {
208 for (i=0;i<n;i++) {
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;
216 #endif
217 fogcoord[i] = FloatToFixed(exp( tmp ));
218 }
219 }
220 else {
221 /* perspective */
222 for (i=0;i<n;i++) {
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;
230 #endif
231 fogcoord[i] = FloatToFixed(exp( tmp ));
232 }
233 }
234 }
235 break;
236 default:
237 _mesa_problem(ctx, "Bad fog mode in _mesa_win_fog_coords_from_z");
238 return;
239 }
240 }
241
242
243 /*
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
249 */
250 void
251 _mesa_depth_fog_rgba_pixels( const GLcontext *ctx,
252 GLuint n, const GLdepth z[], GLchan rgba[][4] )
253 {
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 );
258 }
259
260
261 /*
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
267 */
268 void
269 _mesa_depth_fog_ci_pixels( const GLcontext *ctx,
270 GLuint n, const GLdepth z[], GLuint index[] )
271 {
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 );
276 }
277