Sync with trunk r63647.
[reactos.git] / dll / opengl / mesa / swrast / s_fog.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.2
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
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.
23 */
24
25 #include <precomp.h>
26
27 /**
28 * Used to convert current raster distance to a fog factor in [0,1].
29 */
30 GLfloat
31 _swrast_z_to_fogfactor(struct gl_context *ctx, GLfloat z)
32 {
33 GLfloat d, f;
34
35 switch (ctx->Fog.Mode) {
36 case GL_LINEAR:
37 if (ctx->Fog.Start == ctx->Fog.End)
38 d = 1.0F;
39 else
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);
43 case GL_EXP:
44 d = ctx->Fog.Density;
45 f = EXPF(-d * z);
46 f = CLAMP(f, 0.0F, 1.0F);
47 return f;
48 case GL_EXP2:
49 d = ctx->Fog.Density;
50 f = EXPF(-(d * d * z * z));
51 f = CLAMP(f, 0.0F, 1.0F);
52 return f;
53 default:
54 _mesa_problem(ctx, "Bad fog mode in _swrast_z_to_fogfactor");
55 return 0.0;
56 }
57 }
58
59
60 #define LINEAR_FOG(f, coord) f = (fogEnd - coord) * fogScale
61
62 #define EXP_FOG(f, coord) f = EXPF(density * coord)
63
64 #define EXP2_FOG(f, coord) \
65 do { \
66 GLfloat tmp = negDensitySquared * coord * coord; \
67 if (tmp < FLT_MIN_10_EXP) \
68 tmp = FLT_MIN_10_EXP; \
69 f = EXPF(tmp); \
70 } while(0)
71
72
73 #define BLEND_FOG(f, coord) f = coord
74
75
76
77 /**
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.
81 */
82 #define FOG_LOOP(TYPE, FOG_FUNC) \
83 if (span->arrayAttribs & FRAG_BIT_FOGC) { \
84 GLuint i; \
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; \
89 FOG_FUNC(f, c); \
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); \
95 } \
96 } \
97 else { \
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]; \
102 GLuint i; \
103 for (i = 0; i < span->end; i++) { \
104 const GLfloat c = FABSF(fogCoord) / w; \
105 GLfloat f, oneMinusF; \
106 FOG_FUNC(f, c); \
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; \
113 w += wStep; \
114 } \
115 }
116
117 /**
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!
123 */
124 void
125 _swrast_fog_rgba_span( const struct gl_context *ctx, SWspan *span )
126 {
127 const SWcontext *swrast = CONST_SWRAST_CONTEXT(ctx);
128 GLfloat rFog, gFog, bFog;
129
130 ASSERT(swrast->_FogEnabled);
131 ASSERT(span->arrayMask & SPAN_RGBA);
132
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;
138 }
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;
143 }
144 else {
145 rFog = ctx->Fog.Color[RCOMP];
146 gFog = ctx->Fog.Color[GCOMP];
147 bFog = ctx->Fog.Color[BCOMP];
148 }
149
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.
153 */
154 switch (ctx->Fog.Mode) {
155 case GL_LINEAR:
156 {
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);
163 }
164 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
165 GLushort (*rgba)[4] = span->array->rgba16;
166 FOG_LOOP(GLushort, LINEAR_FOG);
167 }
168 else {
169 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL];
170 ASSERT(span->array->ChanType == GL_FLOAT);
171 FOG_LOOP(GLfloat, LINEAR_FOG);
172 }
173 }
174 break;
175
176 case GL_EXP:
177 {
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);
182 }
183 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
184 GLushort (*rgba)[4] = span->array->rgba16;
185 FOG_LOOP(GLushort, EXP_FOG);
186 }
187 else {
188 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL];
189 ASSERT(span->array->ChanType == GL_FLOAT);
190 FOG_LOOP(GLfloat, EXP_FOG);
191 }
192 }
193 break;
194
195 case GL_EXP2:
196 {
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);
201 }
202 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
203 GLushort (*rgba)[4] = span->array->rgba16;
204 FOG_LOOP(GLushort, EXP2_FOG);
205 }
206 else {
207 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL];
208 ASSERT(span->array->ChanType == GL_FLOAT);
209 FOG_LOOP(GLfloat, EXP2_FOG);
210 }
211 }
212 break;
213
214 default:
215 _mesa_problem(ctx, "Bad fog mode in _swrast_fog_rgba_span");
216 return;
217 }
218 }
219 else {
220 /* The span's fog start/step/array values are blend factors in [0,1].
221 * They were previously computed per-vertex.
222 */
223 if (span->array->ChanType == GL_UNSIGNED_BYTE) {
224 GLubyte (*rgba)[4] = span->array->rgba8;
225 FOG_LOOP(GLubyte, BLEND_FOG);
226 }
227 else if (span->array->ChanType == GL_UNSIGNED_SHORT) {
228 GLushort (*rgba)[4] = span->array->rgba16;
229 FOG_LOOP(GLushort, BLEND_FOG);
230 }
231 else {
232 GLfloat (*rgba)[4] = span->array->attribs[FRAG_ATTRIB_COL];
233 ASSERT(span->array->ChanType == GL_FLOAT);
234 FOG_LOOP(GLfloat, BLEND_FOG);
235 }
236 }
237 }