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.
28 * Used to convert current raster distance to a fog factor in [0,1].
31 _swrast_z_to_fogfactor(struct gl_context
*ctx
, GLfloat z
)
35 switch (ctx
->Fog
.Mode
) {
37 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
40 d
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
41 f
= (ctx
->Fog
.End
- z
) * d
;
42 return CLAMP(f
, 0.0F
, 1.0F
);
46 f
= CLAMP(f
, 0.0F
, 1.0F
);
50 f
= EXPF(-(d
* d
* z
* z
));
51 f
= CLAMP(f
, 0.0F
, 1.0F
);
54 _mesa_problem(ctx
, "Bad fog mode in _swrast_z_to_fogfactor");
60 #define LINEAR_FOG(f, coord) f = (fogEnd - coord) * fogScale
62 #define EXP_FOG(f, coord) f = EXPF(density * coord)
64 #define EXP2_FOG(f, coord) \
66 GLfloat tmp = negDensitySquared * coord * coord; \
67 if (tmp < FLT_MIN_10_EXP) \
68 tmp = FLT_MIN_10_EXP; \
73 #define BLEND_FOG(f, coord) f = coord
78 * Template code for computing fog blend factor and applying it to colors.
79 * \param TYPE either GLubyte, GLushort or GLfloat.
80 * \param COMPUTE_F code to compute the fog blend factor, f.
82 #define FOG_LOOP(TYPE, FOG_FUNC) \
83 if (span->arrayAttribs & FRAG_BIT_FOGC) { \
85 for (i = 0; i < span->end; i++) { \
86 const GLfloat fogCoord = span->array->attribs[FRAG_ATTRIB_FOGC][i][0]; \
87 const GLfloat c = FABSF(fogCoord); \
88 GLfloat f, oneMinusF; \
90 f = CLAMP(f, 0.0F, 1.0F); \
91 oneMinusF = 1.0F - f; \
92 rgba[i][RCOMP] = (TYPE) (f * rgba[i][RCOMP] + oneMinusF * rFog); \
93 rgba[i][GCOMP] = (TYPE) (f * rgba[i][GCOMP] + oneMinusF * gFog); \
94 rgba[i][BCOMP] = (TYPE) (f * rgba[i][BCOMP] + oneMinusF * bFog); \
98 const GLfloat fogStep = span->attrStepX[FRAG_ATTRIB_FOGC][0]; \
99 GLfloat fogCoord = span->attrStart[FRAG_ATTRIB_FOGC][0]; \
100 const GLfloat wStep = span->attrStepX[FRAG_ATTRIB_WPOS][3]; \
101 GLfloat w = span->attrStart[FRAG_ATTRIB_WPOS][3]; \
103 for (i = 0; i < span->end; i++) { \
104 const GLfloat c = FABSF(fogCoord) / w; \
105 GLfloat f, oneMinusF; \
107 f = CLAMP(f, 0.0F, 1.0F); \
108 oneMinusF = 1.0F - f; \
109 rgba[i][RCOMP] = (TYPE) (f * rgba[i][RCOMP] + oneMinusF * rFog); \
110 rgba[i][GCOMP] = (TYPE) (f * rgba[i][GCOMP] + oneMinusF * gFog); \
111 rgba[i][BCOMP] = (TYPE) (f * rgba[i][BCOMP] + oneMinusF * bFog); \
112 fogCoord += fogStep; \
118 * Apply fog to a span of RGBA pixels.
119 * The fog value are either in the span->array->fog array or interpolated from
120 * the fog/fogStep values.
121 * They fog values are either fog coordinates (Z) or fog blend factors.
122 * _PreferPixelFog should be in sync with that state!
125 _swrast_fog_rgba_span( const struct gl_context
*ctx
, SWspan
*span
)
127 const SWcontext
*swrast
= CONST_SWRAST_CONTEXT(ctx
);
128 GLfloat rFog
, gFog
, bFog
;
130 ASSERT(swrast
->_FogEnabled
);
131 ASSERT(span
->arrayMask
& SPAN_RGBA
);
133 /* compute (scaled) fog color */
134 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
135 rFog
= ctx
->Fog
.Color
[RCOMP
] * 255.0F
;
136 gFog
= ctx
->Fog
.Color
[GCOMP
] * 255.0F
;
137 bFog
= ctx
->Fog
.Color
[BCOMP
] * 255.0F
;
139 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
140 rFog
= ctx
->Fog
.Color
[RCOMP
] * 65535.0F
;
141 gFog
= ctx
->Fog
.Color
[GCOMP
] * 65535.0F
;
142 bFog
= ctx
->Fog
.Color
[BCOMP
] * 65535.0F
;
145 rFog
= ctx
->Fog
.Color
[RCOMP
];
146 gFog
= ctx
->Fog
.Color
[GCOMP
];
147 bFog
= ctx
->Fog
.Color
[BCOMP
];
150 if (swrast
->_PreferPixelFog
) {
151 /* The span's fog values are fog coordinates, now compute blend factors
152 * and blend the fragment colors with the fog color.
154 switch (ctx
->Fog
.Mode
) {
157 const GLfloat fogEnd
= ctx
->Fog
.End
;
158 const GLfloat fogScale
= (ctx
->Fog
.Start
== ctx
->Fog
.End
)
159 ? 1.0F
: 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
160 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
161 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
162 FOG_LOOP(GLubyte
, LINEAR_FOG
);
164 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
165 GLushort (*rgba
)[4] = span
->array
->rgba16
;
166 FOG_LOOP(GLushort
, LINEAR_FOG
);
169 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL
];
170 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
171 FOG_LOOP(GLfloat
, LINEAR_FOG
);
178 const GLfloat density
= -ctx
->Fog
.Density
;
179 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
180 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
181 FOG_LOOP(GLubyte
, EXP_FOG
);
183 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
184 GLushort (*rgba
)[4] = span
->array
->rgba16
;
185 FOG_LOOP(GLushort
, EXP_FOG
);
188 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL
];
189 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
190 FOG_LOOP(GLfloat
, EXP_FOG
);
197 const GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
198 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
199 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
200 FOG_LOOP(GLubyte
, EXP2_FOG
);
202 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
203 GLushort (*rgba
)[4] = span
->array
->rgba16
;
204 FOG_LOOP(GLushort
, EXP2_FOG
);
207 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL
];
208 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
209 FOG_LOOP(GLfloat
, EXP2_FOG
);
215 _mesa_problem(ctx
, "Bad fog mode in _swrast_fog_rgba_span");
220 /* The span's fog start/step/array values are blend factors in [0,1].
221 * They were previously computed per-vertex.
223 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
224 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
225 FOG_LOOP(GLubyte
, BLEND_FOG
);
227 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
228 GLushort (*rgba
)[4] = span
->array
->rgba16
;
229 FOG_LOOP(GLushort
, BLEND_FOG
);
232 GLfloat (*rgba
)[4] = span
->array
->attribs
[FRAG_ATTRIB_COL
];
233 ASSERT(span
->array
->ChanType
== GL_FLOAT
);
234 FOG_LOOP(GLfloat
, BLEND_FOG
);