| 1 |
/* $Id: fog.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.0 |
| 6 |
* Copyright (C) 1995-1996 Brian Paul |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the Free |
| 20 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <math.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include "context.h" |
| 26 |
#include "fog.h" |
| 27 |
#include "dlist.h" |
| 28 |
#include "macros.h" |
| 29 |
#include "types.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
void gl_Fogfv( GLcontext *ctx, GLenum pname, const GLfloat *params ) |
| 34 |
{ |
| 35 |
GLenum m; |
| 36 |
|
| 37 |
switch (pname) { |
| 38 |
case GL_FOG_MODE: |
| 39 |
m = (GLenum) (GLint) *params; |
| 40 |
if (m==GL_LINEAR || m==GL_EXP || m==GL_EXP2) { |
| 41 |
ctx->Fog.Mode = m; |
| 42 |
} |
| 43 |
else { |
| 44 |
gl_error( ctx, GL_INVALID_ENUM, "glFog" ); |
| 45 |
} |
| 46 |
break; |
| 47 |
case GL_FOG_DENSITY: |
| 48 |
if (*params<0.0) { |
| 49 |
gl_error( ctx, GL_INVALID_VALUE, "glFog" ); |
| 50 |
} |
| 51 |
else { |
| 52 |
ctx->Fog.Density = *params; |
| 53 |
} |
| 54 |
break; |
| 55 |
case GL_FOG_START: |
| 56 |
#ifndef GL_VERSION_1_1 |
| 57 |
if (*params<0.0F) { |
| 58 |
gl_error( ctx, GL_INVALID_VALUE, "glFog(GL_FOG_START)" ); |
| 59 |
return; |
| 60 |
} |
| 61 |
#endif |
| 62 |
ctx->Fog.Start = *params; |
| 63 |
break; |
| 64 |
case GL_FOG_END: |
| 65 |
#ifndef GL_VERSION_1_1 |
| 66 |
if (*params<0.0F) { |
| 67 |
gl_error( ctx, GL_INVALID_VALUE, "glFog(GL_FOG_END)" ); |
| 68 |
return; |
| 69 |
} |
| 70 |
#endif |
| 71 |
ctx->Fog.End = *params; |
| 72 |
break; |
| 73 |
case GL_FOG_INDEX: |
| 74 |
ctx->Fog.Index = *params; |
| 75 |
break; |
| 76 |
case GL_FOG_COLOR: |
| 77 |
ctx->Fog.Color[0] = params[0]; |
| 78 |
ctx->Fog.Color[1] = params[1]; |
| 79 |
ctx->Fog.Color[2] = params[2]; |
| 80 |
ctx->Fog.Color[3] = params[3]; |
| 81 |
break; |
| 82 |
default: |
| 83 |
gl_error( ctx, GL_INVALID_ENUM, "glFog" ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
/* |
| 91 |
* Compute the fogged color for an array of vertices. |
| 92 |
* Input: n - number of vertices |
| 93 |
* v - array of vertices |
| 94 |
* color - the original vertex colors |
| 95 |
* Output: color - the fogged colors |
| 96 |
*/ |
| 97 |
void gl_fog_color_vertices( GLcontext *ctx, |
| 98 |
GLuint n, GLfloat v[][4], GLfixed color[][4] ) |
| 99 |
{ |
| 100 |
GLuint i; |
| 101 |
GLfloat d; |
| 102 |
GLfloat shift = (GLfloat) (1 << ctx->ColorShift); |
| 103 |
GLfloat fogr = ctx->Fog.Color[0] * ctx->Visual->RedScale * shift; |
| 104 |
GLfloat fogg = ctx->Fog.Color[1] * ctx->Visual->GreenScale * shift; |
| 105 |
GLfloat fogb = ctx->Fog.Color[2] * ctx->Visual->BlueScale * shift; |
| 106 |
GLfloat foga = ctx->Fog.Color[3] * ctx->Visual->AlphaScale * shift; |
| 107 |
GLfloat end = ctx->Fog.End; |
| 108 |
|
| 109 |
switch (ctx->Fog.Mode) { |
| 110 |
case GL_LINEAR: |
| 111 |
d = 1.0F / (ctx->Fog.End - ctx->Fog.Start); |
| 112 |
for (i=0;i<n;i++) { |
| 113 |
GLfloat f = (end - ABSF(v[i][2])) * d; |
| 114 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 115 |
color[i][0] = f * color[i][0] + (1.0F-f) * fogr; |
| 116 |
color[i][1] = f * color[i][1] + (1.0F-f) * fogg; |
| 117 |
color[i][2] = f * color[i][2] + (1.0F-f) * fogb; |
| 118 |
color[i][3] = f * color[i][3] + (1.0F-f) * foga; |
| 119 |
} |
| 120 |
break; |
| 121 |
case GL_EXP: |
| 122 |
d = -ctx->Fog.Density; |
| 123 |
for (i=0;i<n;i++) { |
| 124 |
GLfloat f = exp( d * ABSF(v[i][2]) ); |
| 125 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 126 |
color[i][0] = f * color[i][0] + (1.0F-f) * fogr; |
| 127 |
color[i][1] = f * color[i][1] + (1.0F-f) * fogg; |
| 128 |
color[i][2] = f * color[i][2] + (1.0F-f) * fogb; |
| 129 |
color[i][3] = f * color[i][3] + (1.0F-f) * foga; |
| 130 |
} |
| 131 |
break; |
| 132 |
case GL_EXP2: |
| 133 |
d = -(ctx->Fog.Density*ctx->Fog.Density); |
| 134 |
for (i=0;i<n;i++) { |
| 135 |
GLfloat z = ABSF(v[i][2]); |
| 136 |
GLfloat f = exp( d * z*z ); |
| 137 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 138 |
color[i][0] = f * color[i][0] + (1.0F-f) * fogr; |
| 139 |
color[i][1] = f * color[i][1] + (1.0F-f) * fogg; |
| 140 |
color[i][2] = f * color[i][2] + (1.0F-f) * fogb; |
| 141 |
color[i][3] = f * color[i][3] + (1.0F-f) * foga; |
| 142 |
} |
| 143 |
break; |
| 144 |
default: |
| 145 |
abort(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
/* |
| 152 |
* Compute the fogged color indexes for an array of vertices. |
| 153 |
* Input: n - number of vertices |
| 154 |
* v - array of vertices |
| 155 |
* In/Out: indx - array of vertex color indexes |
| 156 |
*/ |
| 157 |
void gl_fog_index_vertices( GLcontext *ctx, |
| 158 |
GLuint n, GLfloat v[][4], GLuint indx[] ) |
| 159 |
{ |
| 160 |
/* NOTE: the extensive use of casts generates better/faster code for MIPS */ |
| 161 |
switch (ctx->Fog.Mode) { |
| 162 |
case GL_LINEAR: |
| 163 |
{ |
| 164 |
GLfloat d = 1.0F / (ctx->Fog.End - ctx->Fog.Start); |
| 165 |
GLfloat fogindex = ctx->Fog.Index; |
| 166 |
GLfloat fogend = ctx->Fog.End; |
| 167 |
GLuint i; |
| 168 |
for (i=0;i<n;i++) { |
| 169 |
GLfloat f = (fogend - ABSF(v[i][2])) * d; |
| 170 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 171 |
indx[i] = (GLint) |
| 172 |
((GLfloat) (GLint) indx[i] + (1.0F-f) * fogindex); |
| 173 |
} |
| 174 |
} |
| 175 |
break; |
| 176 |
case GL_EXP: |
| 177 |
{ |
| 178 |
GLfloat d = -ctx->Fog.Density; |
| 179 |
GLfloat fogindex = ctx->Fog.Index; |
| 180 |
GLuint i; |
| 181 |
for (i=0;i<n;i++) { |
| 182 |
GLfloat f = exp( d * ABSF(v[i][2]) ); |
| 183 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 184 |
indx[i] = (GLint) |
| 185 |
((GLfloat) (GLint) indx[i] + (1.0F-f) * fogindex); |
| 186 |
} |
| 187 |
} |
| 188 |
break; |
| 189 |
case GL_EXP2: |
| 190 |
{ |
| 191 |
GLfloat d = -(ctx->Fog.Density*ctx->Fog.Density); |
| 192 |
GLfloat fogindex = ctx->Fog.Index; |
| 193 |
GLuint i; |
| 194 |
for (i=0;i<n;i++) { |
| 195 |
GLfloat z = ABSF(v[i][2]); |
| 196 |
GLfloat f = exp( -d * z*z ); |
| 197 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 198 |
indx[i] = (GLint) |
| 199 |
((GLfloat) (GLint) indx[i] + (1.0F-f) * fogindex); |
| 200 |
} |
| 201 |
} |
| 202 |
break; |
| 203 |
default: |
| 204 |
abort(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
/* |
| 212 |
* Apply fog to an array of RGBA pixels. |
| 213 |
* Input: n - number of pixels |
| 214 |
* z - array of integer depth values |
| 215 |
* red, green, blue, alpha - pixel colors |
| 216 |
* Output: red, green, blue, alpha - fogged pixel colors |
| 217 |
*/ |
| 218 |
void gl_fog_color_pixels( GLcontext *ctx, |
| 219 |
GLuint n, const GLdepth z[], GLubyte red[], |
| 220 |
GLubyte green[], GLubyte blue[], GLubyte alpha[] ) |
| 221 |
{ |
| 222 |
GLfloat c = ctx->ProjectionMatrix[10]; |
| 223 |
GLfloat d = ctx->ProjectionMatrix[14]; |
| 224 |
GLuint i; |
| 225 |
|
| 226 |
GLfloat fog_red = ctx->Fog.Color[0] * ctx->Visual->RedScale; |
| 227 |
GLfloat fog_green = ctx->Fog.Color[1] * ctx->Visual->GreenScale; |
| 228 |
GLfloat fog_blue = ctx->Fog.Color[2] * ctx->Visual->BlueScale; |
| 229 |
GLfloat fog_alpha = ctx->Fog.Color[3] * ctx->Visual->AlphaScale; |
| 230 |
|
| 231 |
GLfloat tz = ctx->Viewport.Tz; |
| 232 |
GLfloat szInv = 1.0F / ctx->Viewport.Sz; |
| 233 |
|
| 234 |
switch (ctx->Fog.Mode) { |
| 235 |
case GL_LINEAR: |
| 236 |
{ |
| 237 |
GLfloat fogEnd = ctx->Fog.End; |
| 238 |
GLfloat fogScale = 1.0F / (ctx->Fog.End - ctx->Fog.Start); |
| 239 |
for (i=0;i<n;i++) { |
| 240 |
GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv; |
| 241 |
GLfloat eyez = -d / (c+ndcz); |
| 242 |
GLfloat f, g; |
| 243 |
if (eyez < 0.0) eyez = -eyez; |
| 244 |
f = (fogEnd - eyez) * fogScale; |
| 245 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 246 |
g = 1.0F - f; |
| 247 |
red[i] = (GLint) (f * (GLfloat) red[i] + g * fog_red); |
| 248 |
green[i] = (GLint) (f * (GLfloat) green[i] + g * fog_green); |
| 249 |
blue[i] = (GLint) (f * (GLfloat) blue[i] + g * fog_blue); |
| 250 |
alpha[i] = (GLint) (f * (GLfloat) alpha[i] + g * fog_alpha); |
| 251 |
} |
| 252 |
} |
| 253 |
break; |
| 254 |
case GL_EXP: |
| 255 |
for (i=0;i<n;i++) { |
| 256 |
GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv; |
| 257 |
GLfloat eyez = -d / (c+ndcz); |
| 258 |
GLfloat f, g; |
| 259 |
if (eyez < 0.0) eyez = -eyez; |
| 260 |
f = exp( -ctx->Fog.Density * eyez ); |
| 261 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 262 |
g = 1.0F - f; |
| 263 |
red[i] = (GLint) (f * (GLfloat) red[i] + g * fog_red); |
| 264 |
green[i] = (GLint) (f * (GLfloat) green[i] + g * fog_green); |
| 265 |
blue[i] = (GLint) (f * (GLfloat) blue[i] + g * fog_blue); |
| 266 |
alpha[i] = (GLint) (f * (GLfloat) alpha[i] + g * fog_alpha); |
| 267 |
} |
| 268 |
break; |
| 269 |
case GL_EXP2: |
| 270 |
{ |
| 271 |
GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density; |
| 272 |
for (i=0;i<n;i++) { |
| 273 |
GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv; |
| 274 |
GLfloat eyez = -d / (c+ndcz); |
| 275 |
GLfloat f, g; |
| 276 |
if (eyez < 0.0) eyez = -eyez; |
| 277 |
f = exp( negDensitySquared * eyez*eyez ); |
| 278 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 279 |
g = 1.0F - f; |
| 280 |
red[i] = (GLint) (f * (GLfloat) red[i] + g * fog_red); |
| 281 |
green[i] = (GLint) (f * (GLfloat) green[i] + g * fog_green); |
| 282 |
blue[i] = (GLint) (f * (GLfloat) blue[i] + g * fog_blue); |
| 283 |
alpha[i] = (GLint) (f * (GLfloat) alpha[i] + g * fog_alpha); |
| 284 |
} |
| 285 |
} |
| 286 |
break; |
| 287 |
default: |
| 288 |
abort(); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
/* |
| 296 |
* Apply fog to an array of color index pixels. |
| 297 |
* Input: n - number of pixels |
| 298 |
* z - array of integer depth values |
| 299 |
* index - pixel color indexes |
| 300 |
* Output: index - fogged pixel color indexes |
| 301 |
*/ |
| 302 |
void gl_fog_index_pixels( GLcontext *ctx, |
| 303 |
GLuint n, const GLdepth z[], GLuint index[] ) |
| 304 |
{ |
| 305 |
GLfloat c = ctx->ProjectionMatrix[10]; |
| 306 |
GLfloat d = ctx->ProjectionMatrix[14]; |
| 307 |
GLuint i; |
| 308 |
|
| 309 |
GLfloat tz = ctx->Viewport.Tz; |
| 310 |
GLfloat szInv = 1.0F / ctx->Viewport.Sz; |
| 311 |
|
| 312 |
switch (ctx->Fog.Mode) { |
| 313 |
case GL_LINEAR: |
| 314 |
{ |
| 315 |
GLfloat fogEnd = ctx->Fog.End; |
| 316 |
GLfloat fogScale = 1.0F / (ctx->Fog.End - ctx->Fog.Start); |
| 317 |
for (i=0;i<n;i++) { |
| 318 |
GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv; |
| 319 |
GLfloat eyez = -d / (c+ndcz); |
| 320 |
GLfloat f; |
| 321 |
if (eyez < 0.0) eyez = -eyez; |
| 322 |
f = (fogEnd - eyez) * fogScale; |
| 323 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 324 |
index[i] = (GLuint) ((GLfloat) index[i] + (1.0F-f) * ctx->Fog.Index); |
| 325 |
} |
| 326 |
} |
| 327 |
break; |
| 328 |
case GL_EXP: |
| 329 |
for (i=0;i<n;i++) { |
| 330 |
GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv; |
| 331 |
GLfloat eyez = -d / (c+ndcz); |
| 332 |
GLfloat f; |
| 333 |
if (eyez < 0.0) eyez = -eyez; |
| 334 |
f = exp( -ctx->Fog.Density * eyez ); |
| 335 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 336 |
index[i] = (GLuint) ((GLfloat) index[i] + (1.0F-f) * ctx->Fog.Index); |
| 337 |
} |
| 338 |
break; |
| 339 |
case GL_EXP2: |
| 340 |
{ |
| 341 |
GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density; |
| 342 |
for (i=0;i<n;i++) { |
| 343 |
GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv; |
| 344 |
GLfloat eyez = -d / (c+ndcz); |
| 345 |
GLfloat f; |
| 346 |
if (eyez < 0.0) eyez = -eyez; |
| 347 |
f = exp( negDensitySquared * eyez*eyez ); |
| 348 |
f = CLAMP( f, 0.0F, 1.0F ); |
| 349 |
index[i] = (GLuint) ((GLfloat) index[i] + (1.0F-f) * ctx->Fog.Index); |
| 350 |
} |
| 351 |
} |
| 352 |
break; |
| 353 |
default: |
| 354 |
abort(); |
| 355 |
} |
| 356 |
|
| 357 |
} |
| 358 |
|