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.
26 #include "main/glheader.h"
27 #include "main/colormac.h"
28 #include "main/context.h"
29 #include "main/macros.h"
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
= CONST_SWRAST_CONTEXT(ctx
);
168 GLfloat rFog
, gFog
, bFog
;
170 ASSERT(swrast
->_FogEnabled
);
171 ASSERT(span
->arrayMask
& SPAN_RGBA
);
173 /* compute (scaled) fog color */
174 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
175 rFog
= ctx
->Fog
.Color
[RCOMP
] * 255.0;
176 gFog
= ctx
->Fog
.Color
[GCOMP
] * 255.0;
177 bFog
= ctx
->Fog
.Color
[BCOMP
] * 255.0;
179 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
180 rFog
= ctx
->Fog
.Color
[RCOMP
] * 65535.0;
181 gFog
= ctx
->Fog
.Color
[GCOMP
] * 65535.0;
182 bFog
= ctx
->Fog
.Color
[BCOMP
] * 65535.0;
185 rFog
= ctx
->Fog
.Color
[RCOMP
];
186 gFog
= ctx
->Fog
.Color
[GCOMP
];
187 bFog
= ctx
->Fog
.Color
[BCOMP
];
190 if (swrast
->_PreferPixelFog
) {
191 /* The span's fog values are fog coordinates, now compute blend factors
192 * and blend the fragment colors with the fog color.
194 switch (swrast
->_FogMode
) {
197 const GLfloat fogEnd
= ctx
->Fog
.End
;
198 const GLfloat fogScale
= (ctx
->Fog
.Start
== ctx
->Fog
.End
)
199 ? 1.0F
: 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
200 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
201 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
202 FOG_LOOP(GLubyte
, LINEAR_FOG
);
204 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
205 GLushort (*rgba
)[4] = span
->array
->rgba16
;
206 FOG_LOOP(GLushort
, LINEAR_FOG
);
209 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
210 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
211 FOG_LOOP(GLfloat
, LINEAR_FOG
);
218 const GLfloat density
= -ctx
->Fog
.Density
;
219 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
220 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
221 FOG_LOOP(GLubyte
, EXP_FOG
);
223 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
224 GLushort (*rgba
)[4] = span
->array
->rgba16
;
225 FOG_LOOP(GLushort
, EXP_FOG
);
228 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
229 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
230 FOG_LOOP(GLfloat
, EXP_FOG
);
237 const GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
238 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
239 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
240 FOG_LOOP(GLubyte
, EXP2_FOG
);
242 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
243 GLushort (*rgba
)[4] = span
->array
->rgba16
;
244 FOG_LOOP(GLushort
, EXP2_FOG
);
247 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
248 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
249 FOG_LOOP(GLfloat
, EXP2_FOG
);
255 _mesa_problem(ctx
, "Bad fog mode in _swrast_fog_rgba_span");
260 /* The span's fog start/step/array values are blend factors in [0,1].
261 * They were previously computed per-vertex.
263 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
264 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
265 FOG_LOOP(GLubyte
, BLEND_FOG
);
267 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
268 GLushort (*rgba
)[4] = span
->array
->rgba16
;
269 FOG_LOOP(GLushort
, BLEND_FOG
);
272 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL0
];
273 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
274 FOG_LOOP(GLfloat
, BLEND_FOG
);
281 * As above, but color index mode.
284 _swrast_fog_ci_span( const GLcontext
*ctx
, SWspan
*span
)
286 const SWcontext
*swrast
= CONST_SWRAST_CONTEXT(ctx
);
287 const GLuint fogIndex
= (GLuint
) ctx
->Fog
.Index
;
288 GLuint
*index
= span
->array
->index
;
290 ASSERT(swrast
->_FogEnabled
);
291 ASSERT(span
->arrayMask
& SPAN_INDEX
);
293 /* we need to compute fog blend factors */
294 if (swrast
->_PreferPixelFog
) {
295 /* The span's fog values are fog coordinates, now compute blend factors
296 * and blend the fragment colors with the fog color.
298 switch (ctx
->Fog
.Mode
) {
301 const GLfloat fogEnd
= ctx
->Fog
.End
;
302 const GLfloat fogScale
= (ctx
->Fog
.Start
== ctx
->Fog
.End
)
303 ? 1.0F
: 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
304 FOG_LOOP_CI(LINEAR_FOG
);
309 const GLfloat density
= -ctx
->Fog
.Density
;
310 FOG_LOOP_CI(EXP_FOG
);
315 const GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
316 FOG_LOOP_CI(EXP2_FOG
);
320 _mesa_problem(ctx
, "Bad fog mode in _swrast_fog_ci_span");
325 /* The span's fog start/step/array values are blend factors in [0,1].
326 * They were previously computed per-vertex.
328 FOG_LOOP_CI(BLEND_FOG
);