2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #include "s_context.h"
36 * Used to convert current raster distance to a fog factor in [0,1].
39 _swrast_z_to_fogfactor(GLcontext
*ctx
, GLfloat z
)
43 switch (ctx
->Fog
.Mode
) {
45 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
48 d
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
49 f
= (ctx
->Fog
.End
- z
) * d
;
50 return CLAMP(f
, 0.0F
, 1.0F
);
54 f
= CLAMP(f
, 0.0F
, 1.0F
);
58 f
= EXPF(-(d
* d
* z
* z
));
59 f
= CLAMP(f
, 0.0F
, 1.0F
);
62 _mesa_problem(ctx
, "Bad fog mode in _swrast_z_to_fogfactor");
68 #define LINEAR_FOG(f, coord) f = (fogEnd - coord) * fogScale
70 #define EXP_FOG(f, coord) f = EXPF(density * coord)
72 #define EXP2_FOG(f, coord) \
74 GLfloat tmp = negDensitySquared * coord * coord; \
75 if (tmp < FLT_MIN_10_EXP) \
76 tmp = FLT_MIN_10_EXP; \
81 #define BLEND_FOG(f, coord) f = coord
86 * Template code for computing fog blend factor and applying it to colors.
87 * \param TYPE either GLubyte, GLushort or GLfloat.
88 * \param COMPUTE_F code to compute the fog blend factor, f.
90 #define FOG_LOOP(TYPE, FOG_FUNC) \
91 if (span->arrayAttribs & FRAG_BIT_FOGC) { \
93 for (i = 0; i < span->end; i++) { \
94 const GLfloat fogCoord = span->array->attribs[FRAG_ATTRIB_FOGC][i][0]; \
95 const GLfloat c = FABSF(fogCoord); \
96 GLfloat f, oneMinusF; \
98 f = CLAMP(f, 0.0F, 1.0F); \
99 oneMinusF = 1.0F - f; \
100 rgba[i][RCOMP] = (TYPE) (f * rgba[i][RCOMP] + oneMinusF * rFog); \
101 rgba[i][GCOMP] = (TYPE) (f * rgba[i][GCOMP] + oneMinusF * gFog); \
102 rgba[i][BCOMP] = (TYPE) (f * rgba[i][BCOMP] + oneMinusF * bFog); \
106 const GLfloat fogStep = span->attrStepX[FRAG_ATTRIB_FOGC][0]; \
107 GLfloat fogCoord = span->attrStart[FRAG_ATTRIB_FOGC][0]; \
108 const GLfloat wStep = span->attrStepX[FRAG_ATTRIB_WPOS][3]; \
109 GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3]; \
111 for (i = 0; i < span->end; i++) { \
112 const GLfloat c = FABSF(fogCoord) / w; \
113 GLfloat f, oneMinusF; \
115 f = CLAMP(f, 0.0F, 1.0F); \
116 oneMinusF = 1.0F - f; \
117 rgba[i][RCOMP] = (TYPE) (f * rgba[i][RCOMP] + oneMinusF * rFog); \
118 rgba[i][GCOMP] = (TYPE) (f * rgba[i][GCOMP] + oneMinusF * gFog); \
119 rgba[i][BCOMP] = (TYPE) (f * rgba[i][BCOMP] + oneMinusF * bFog); \
120 fogCoord += fogStep; \
125 /* As above, but CI mode (XXX try to merge someday) */
126 #define FOG_LOOP_CI(FOG_FUNC) \
127 if (span->arrayAttribs & FRAG_BIT_FOGC) { \
129 for (i = 0; i < span->end; i++) { \
130 const GLfloat fogCoord = span->array->attribs[FRAG_ATTRIB_FOGC][i][0]; \
131 const GLfloat c = FABSF(fogCoord); \
134 f = CLAMP(f, 0.0F, 1.0F); \
135 index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex); \
139 const GLfloat fogStep = span->attrStepX[FRAG_ATTRIB_FOGC][0]; \
140 GLfloat fogCoord = span->attrStart[FRAG_ATTRIB_FOGC][0]; \
141 const GLfloat wStep = span->attrStepX[FRAG_ATTRIB_WPOS][3]; \
142 GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3]; \
144 for (i = 0; i < span->end; i++) { \
145 const GLfloat c = FABSF(fogCoord) / w; \
148 f = CLAMP(f, 0.0F, 1.0F); \
149 index[i] = (GLuint) ((GLfloat) index[i] + (1.0F - f) * fogIndex); \
150 fogCoord += fogStep; \
158 * Apply fog to a span of RGBA pixels.
159 * The fog value are either in the span->array->fog array or interpolated from
160 * the fog/fogStep values.
161 * They fog values are either fog coordinates (Z) or fog blend factors.
162 * _PreferPixelFog should be in sync with that state!
165 _swrast_fog_rgba_span( const GLcontext
*ctx
, SWspan
*span
)
167 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
168 GLfloat rFog
, gFog
, bFog
;
170 ASSERT(swrast
->_FogEnabled
);
171 ASSERT(swrast
->_ActiveAttribMask
& FRAG_BIT_FOGC
);
172 ASSERT(span
->arrayMask
& SPAN_RGBA
);
174 /* compute (scaled) fog color */
175 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
176 rFog
= ctx
->Fog
.Color
[RCOMP
] * 255.0;
177 gFog
= ctx
->Fog
.Color
[GCOMP
] * 255.0;
178 bFog
= ctx
->Fog
.Color
[BCOMP
] * 255.0;
180 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
181 rFog
= ctx
->Fog
.Color
[RCOMP
] * 65535.0;
182 gFog
= ctx
->Fog
.Color
[GCOMP
] * 65535.0;
183 bFog
= ctx
->Fog
.Color
[BCOMP
] * 65535.0;
186 rFog
= ctx
->Fog
.Color
[RCOMP
];
187 gFog
= ctx
->Fog
.Color
[GCOMP
];
188 bFog
= ctx
->Fog
.Color
[BCOMP
];
191 if (swrast
->_PreferPixelFog
) {
192 /* The span's fog values are fog coordinates, now compute blend factors
193 * and blend the fragment colors with the fog color.
195 switch (swrast
->_FogMode
) {
198 const GLfloat fogEnd
= ctx
->Fog
.End
;
199 const GLfloat fogScale
= (ctx
->Fog
.Start
== ctx
->Fog
.End
)
200 ? 1.0F
: 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
201 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
202 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
203 FOG_LOOP(GLubyte
, LINEAR_FOG
);
205 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
206 GLushort (*rgba
)[4] = span
->array
->rgba16
;
207 FOG_LOOP(GLushort
, LINEAR_FOG
);
210 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
211 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
212 FOG_LOOP(GLfloat
, LINEAR_FOG
);
219 const GLfloat density
= -ctx
->Fog
.Density
;
220 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
221 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
222 FOG_LOOP(GLubyte
, EXP_FOG
);
224 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
225 GLushort (*rgba
)[4] = span
->array
->rgba16
;
226 FOG_LOOP(GLushort
, EXP_FOG
);
229 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
230 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
231 FOG_LOOP(GLfloat
, EXP_FOG
);
238 const GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
239 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
240 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
241 FOG_LOOP(GLubyte
, EXP2_FOG
);
243 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
244 GLushort (*rgba
)[4] = span
->array
->rgba16
;
245 FOG_LOOP(GLushort
, EXP2_FOG
);
248 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
249 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
250 FOG_LOOP(GLfloat
, EXP2_FOG
);
256 _mesa_problem(ctx
, "Bad fog mode in _swrast_fog_rgba_span");
261 /* The span's fog start/step/array values are blend factors in [0,1].
262 * They were previously computed per-vertex.
264 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
265 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
266 FOG_LOOP(GLubyte
, BLEND_FOG
);
268 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
269 GLushort (*rgba
)[4] = span
->array
->rgba16
;
270 FOG_LOOP(GLushort
, BLEND_FOG
);
273 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
274 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
275 FOG_LOOP(GLfloat
, BLEND_FOG
);
282 * As above, but color index mode.
285 _swrast_fog_ci_span( const GLcontext
*ctx
, SWspan
*span
)
287 const SWcontext
*swrast
= SWRAST_CONTEXT(ctx
);
288 const GLuint fogIndex
= (GLuint
) ctx
->Fog
.Index
;
289 GLuint
*index
= span
->array
->index
;
291 ASSERT(swrast
->_FogEnabled
);
292 ASSERT(span
->arrayMask
& SPAN_INDEX
);
294 /* we need to compute fog blend factors */
295 if (swrast
->_PreferPixelFog
) {
296 /* The span's fog values are fog coordinates, now compute blend factors
297 * and blend the fragment colors with the fog color.
299 switch (ctx
->Fog
.Mode
) {
302 const GLfloat fogEnd
= ctx
->Fog
.End
;
303 const GLfloat fogScale
= (ctx
->Fog
.Start
== ctx
->Fog
.End
)
304 ? 1.0F
: 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
305 FOG_LOOP_CI(LINEAR_FOG
);
310 const GLfloat density
= -ctx
->Fog
.Density
;
311 FOG_LOOP_CI(EXP_FOG
);
316 const GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
317 FOG_LOOP_CI(EXP2_FOG
);
321 _mesa_problem(ctx
, "Bad fog mode in _swrast_fog_ci_span");
326 /* The span's fog start/step/array values are blend factors in [0,1].
327 * They were previously computed per-vertex.
329 FOG_LOOP_CI(BLEND_FOG
);