2 * Mesa 3-D graphics library
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "main/glheader.h"
28 #include "main/colormac.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(struct gl_context
*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 & VARYING_BIT_FOGC) { \
93 for (i = 0; i < span->end; i++) { \
94 const GLfloat fogCoord = span->array->attribs[VARYING_SLOT_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[VARYING_SLOT_FOGC][0]; \
107 GLfloat fogCoord = span->attrStart[VARYING_SLOT_FOGC][0]; \
108 const GLfloat wStep = span->attrStepX[VARYING_SLOT_POS][3]; \
109 GLfloat w = span->attrStart[VARYING_SLOT_POS][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; \
126 * Apply fog to a span of RGBA pixels.
127 * The fog value are either in the span->array->fog array or interpolated from
128 * the fog/fogStep values.
129 * They fog values are either fog coordinates (Z) or fog blend factors.
130 * _PreferPixelFog should be in sync with that state!
133 _swrast_fog_rgba_span( const struct gl_context
*ctx
, SWspan
*span
)
135 const SWcontext
*swrast
= CONST_SWRAST_CONTEXT(ctx
);
136 GLfloat rFog
, gFog
, bFog
;
138 assert(swrast
->_FogEnabled
);
139 assert(span
->arrayMask
& SPAN_RGBA
);
141 /* compute (scaled) fog color */
142 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
143 rFog
= ctx
->Fog
.Color
[RCOMP
] * 255.0F
;
144 gFog
= ctx
->Fog
.Color
[GCOMP
] * 255.0F
;
145 bFog
= ctx
->Fog
.Color
[BCOMP
] * 255.0F
;
147 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
148 rFog
= ctx
->Fog
.Color
[RCOMP
] * 65535.0F
;
149 gFog
= ctx
->Fog
.Color
[GCOMP
] * 65535.0F
;
150 bFog
= ctx
->Fog
.Color
[BCOMP
] * 65535.0F
;
153 rFog
= ctx
->Fog
.Color
[RCOMP
];
154 gFog
= ctx
->Fog
.Color
[GCOMP
];
155 bFog
= ctx
->Fog
.Color
[BCOMP
];
158 if (swrast
->_PreferPixelFog
) {
159 /* The span's fog values are fog coordinates, now compute blend factors
160 * and blend the fragment colors with the fog color.
162 switch (ctx
->Fog
.Mode
) {
165 const GLfloat fogEnd
= ctx
->Fog
.End
;
166 const GLfloat fogScale
= (ctx
->Fog
.Start
== ctx
->Fog
.End
)
167 ? 1.0F
: 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
168 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
169 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
170 FOG_LOOP(GLubyte
, LINEAR_FOG
);
172 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
173 GLushort (*rgba
)[4] = span
->array
->rgba16
;
174 FOG_LOOP(GLushort
, LINEAR_FOG
);
177 GLfloat (*rgba
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
178 assert(span
->array
->ChanType
== GL_FLOAT
);
179 FOG_LOOP(GLfloat
, LINEAR_FOG
);
186 const GLfloat density
= -ctx
->Fog
.Density
;
187 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
188 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
189 FOG_LOOP(GLubyte
, EXP_FOG
);
191 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
192 GLushort (*rgba
)[4] = span
->array
->rgba16
;
193 FOG_LOOP(GLushort
, EXP_FOG
);
196 GLfloat (*rgba
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
197 assert(span
->array
->ChanType
== GL_FLOAT
);
198 FOG_LOOP(GLfloat
, EXP_FOG
);
205 const GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
206 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
207 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
208 FOG_LOOP(GLubyte
, EXP2_FOG
);
210 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
211 GLushort (*rgba
)[4] = span
->array
->rgba16
;
212 FOG_LOOP(GLushort
, EXP2_FOG
);
215 GLfloat (*rgba
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
216 assert(span
->array
->ChanType
== GL_FLOAT
);
217 FOG_LOOP(GLfloat
, EXP2_FOG
);
223 _mesa_problem(ctx
, "Bad fog mode in _swrast_fog_rgba_span");
228 /* The span's fog start/step/array values are blend factors in [0,1].
229 * They were previously computed per-vertex.
231 if (span
->array
->ChanType
== GL_UNSIGNED_BYTE
) {
232 GLubyte (*rgba
)[4] = span
->array
->rgba8
;
233 FOG_LOOP(GLubyte
, BLEND_FOG
);
235 else if (span
->array
->ChanType
== GL_UNSIGNED_SHORT
) {
236 GLushort (*rgba
)[4] = span
->array
->rgba16
;
237 FOG_LOOP(GLushort
, BLEND_FOG
);
240 GLfloat (*rgba
)[4] = span
->array
->attribs
[VARYING_SLOT_COL0
];
241 assert(span
->array
->ChanType
== GL_FLOAT
);
242 FOG_LOOP(GLfloat
, BLEND_FOG
);