1 /* $Id: s_fog.c,v 1.12 2001/05/03 22:13:32 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"
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 for (i
= 0; i
< n
; i
++) {
60 const GLfloat f
= fog
[i
];
61 const GLfloat g
= 1.0 - f
;
62 rgba
[i
][RCOMP
] = f
* rgba
[i
][RCOMP
] + g
* rFog
;
63 rgba
[i
][GCOMP
] = f
* rgba
[i
][GCOMP
] + g
* gFog
;
64 rgba
[i
][BCOMP
] = f
* rgba
[i
][BCOMP
] + g
* bFog
;
71 * Apply fog to an array of color index pixels.
72 * Input: n - number of pixels
73 * z - array of integer depth values
74 * index - pixel color indexes
75 * Output: index - fogged pixel color indexes
78 _mesa_fog_ci_pixels( const GLcontext
*ctx
,
79 GLuint n
, const GLfloat fog
[], GLuint index
[] )
81 GLuint idx
= (GLuint
) ctx
->Fog
.Index
;
84 for (i
= 0; i
< n
; i
++) {
85 const GLfloat f
= CLAMP(fog
[i
], 0.0, 1.0);
86 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
- f
) * idx
);
93 * Calculate fog coords from window z values
94 * Input: n - number of pixels
95 * z - array of integer depth values
96 * red, green, blue, alpha - pixel colors
97 * Output: red, green, blue, alpha - fogged pixel colors
99 * Use lookup table & interpolation?
102 _mesa_win_fog_coords_from_z( const GLcontext
*ctx
,
107 const GLboolean ortho
= (ctx
->ProjectionMatrix
.m
[15] != 0.0F
);
108 const GLfloat p10
= ctx
->ProjectionMatrix
.m
[10];
109 const GLfloat p14
= ctx
->ProjectionMatrix
.m
[14];
110 const GLfloat tz
= ctx
->Viewport
._WindowMap
.m
[MAT_TZ
];
114 if (ctx
->Viewport
._WindowMap
.m
[MAT_SZ
] == 0.0)
117 szInv
= 1.0F
/ ctx
->Viewport
._WindowMap
.m
[MAT_SZ
];
120 * Note: to compute eyeZ from the ndcZ we have to solve the following:
122 * p[10] * eyeZ + p[14] * eyeW
123 * ndcZ = ---------------------------
124 * p[11] * eyeZ + p[15] * eyeW
128 * p[14] * eyeW - p[15] * eyeW * ndcZ
129 * eyeZ = ----------------------------------
130 * p[11] * ndcZ - p[10]
133 * a) if using an orthographic projection, p[11] = 0 and p[15] = 1.
134 * b) if using a perspective projection, p[11] = -1 and p[15] = 0.
135 * c) we assume eyeW = 1 (not always true- glVertex4)
137 * Then we can simplify the calculation of eyeZ quite a bit. We do
138 * separate calculations for the orthographic and perspective cases below.
139 * Note that we drop a negative sign or two since they don't matter.
142 switch (ctx
->Fog
.Mode
) {
145 GLfloat fogEnd
= ctx
->Fog
.End
;
147 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
150 fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
153 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
154 GLfloat eyez
= (ndcz
- p14
) / p10
;
157 fogcoord
[i
] = (fogEnd
- eyez
) * fogScale
;
163 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
164 GLfloat eyez
= p14
/ (ndcz
+ p10
);
167 fogcoord
[i
] = (fogEnd
- eyez
) * fogScale
;
175 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
176 GLfloat eyez
= (ndcz
- p14
) / p10
;
179 fogcoord
[i
] = exp( -ctx
->Fog
.Density
* eyez
);
185 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
186 GLfloat eyez
= p14
/ (ndcz
+ p10
);
189 fogcoord
[i
] = exp( -ctx
->Fog
.Density
* eyez
);
195 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
198 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
199 GLfloat eyez
= (ndcz
- p14
) / p10
;
200 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
201 #if defined(__alpha__) || defined(__alpha)
202 /* XXX this underflow check may be needed for other systems*/
203 if (tmp
< FLT_MIN_10_EXP
)
204 tmp
= FLT_MIN_10_EXP
;
206 fogcoord
[i
] = exp( tmp
);
212 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
213 GLfloat eyez
= p14
/ (ndcz
+ p10
);
214 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
215 #if defined(__alpha__) || defined(__alpha)
216 /* XXX this underflow check may be needed for other systems*/
217 if (tmp
< FLT_MIN_10_EXP
)
218 tmp
= FLT_MIN_10_EXP
;
220 fogcoord
[i
] = exp( tmp
);
226 _mesa_problem(ctx
, "Bad fog mode in _mesa_win_fog_coords_from_z");
233 * Apply fog to an array of RGBA pixels.
234 * Input: n - number of pixels
235 * z - array of integer depth values
236 * red, green, blue, alpha - pixel colors
237 * Output: red, green, blue, alpha - fogged pixel colors
240 _mesa_depth_fog_rgba_pixels( const GLcontext
*ctx
,
241 GLuint n
, const GLdepth z
[], GLchan rgba
[][4] )
243 GLfloat fog
[PB_SIZE
];
244 ASSERT(n
<= PB_SIZE
);
245 _mesa_win_fog_coords_from_z( ctx
, n
, z
, fog
);
246 _mesa_fog_rgba_pixels( ctx
, n
, fog
, rgba
);
251 * Apply fog to an array of color index pixels.
252 * Input: n - number of pixels
253 * z - array of integer depth values
254 * index - pixel color indexes
255 * Output: index - fogged pixel color indexes
258 _mesa_depth_fog_ci_pixels( const GLcontext
*ctx
,
259 GLuint n
, const GLdepth z
[], GLuint index
[] )
261 GLfloat fog
[PB_SIZE
];
262 ASSERT(n
<= PB_SIZE
);
263 _mesa_win_fog_coords_from_z( ctx
, n
, z
, fog
);
264 _mesa_fog_ci_pixels( ctx
, n
, fog
, index
);