s_fog.c revision 22144ab7552f0799bcfca506bf4ffa7f70a06649
122144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes/* $Id: s_fog.c,v 1.11 2001/03/12 00:48:42 gareth Exp $ */
2e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
3e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
4e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Mesa 3-D graphics library
5e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Version:  3.5
622144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
722144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * Copyright (C) 1999-2001  Brian Paul   All Rights Reserved.
822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
9e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
10e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * copy of this software and associated documentation files (the "Software"),
11e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * to deal in the Software without restriction, including without limitation
12e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * and/or sell copies of the Software, and to permit persons to whom the
14e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Software is furnished to do so, subject to the following conditions:
1522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
16e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * The above copyright notice and this permission notice shall be included
17e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * in all copies or substantial portions of the Software.
1822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes *
19e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
22e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
26e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
27e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
28e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "glheader.h"
29e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "colormac.h"
30e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "context.h"
31e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "macros.h"
32e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "mmath.h"
33e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
34cd03ed4f54444d96e4e47cdb118a3dfd94d92bb0Keith Whitwell#include "s_context.h"
35e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#include "s_fog.h"
366f89984fede5ba137b5987201cabcfd280b3773fBrian Paul#include "s_pb.h"
37e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
38e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
39e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
40e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Apply fog to an array of RGBA pixels.
41e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Input:  n - number of pixels
42e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         fog - array of interpolated screen-space fog coordinates in [0..1]
43e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         red, green, blue, alpha - pixel colors
44e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Output:  red, green, blue, alpha - fogged pixel colors
45e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
46e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwellvoid
47e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell_mesa_fog_rgba_pixels( const GLcontext *ctx,
4822144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes                       GLuint n,
4922144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes		       const GLfixed fog[],
50e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell		       GLchan rgba[][4] )
51e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
52e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   GLuint i;
536532db986883ea1a431c043126de4947465834a1Brian Paul   GLchan rFog, gFog, bFog;
546532db986883ea1a431c043126de4947465834a1Brian Paul
556532db986883ea1a431c043126de4947465834a1Brian Paul   UNCLAMPED_FLOAT_TO_CHAN(rFog, ctx->Fog.Color[RCOMP]);
566532db986883ea1a431c043126de4947465834a1Brian Paul   UNCLAMPED_FLOAT_TO_CHAN(gFog, ctx->Fog.Color[GCOMP]);
576532db986883ea1a431c043126de4947465834a1Brian Paul   UNCLAMPED_FLOAT_TO_CHAN(bFog, ctx->Fog.Color[BCOMP]);
58e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
596f89984fede5ba137b5987201cabcfd280b3773fBrian Paul#if CHAN_TYPE == GL_FLOAT
6074e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul   for (i = 0; i < n; i++) {
616f89984fede5ba137b5987201cabcfd280b3773fBrian Paul      const GLfixed cf = CLAMP(fog[i], 0, FIXED_ONE);
626f89984fede5ba137b5987201cabcfd280b3773fBrian Paul      const GLfloat f = FixedToFloat(cf);
636f89984fede5ba137b5987201cabcfd280b3773fBrian Paul      const GLfloat g = 1.0F - f;
646f89984fede5ba137b5987201cabcfd280b3773fBrian Paul      rgba[i][RCOMP] = f * rgba[i][RCOMP] + g * rFog;
656f89984fede5ba137b5987201cabcfd280b3773fBrian Paul      rgba[i][GCOMP] = f * rgba[i][GCOMP] + g * gFog;
666f89984fede5ba137b5987201cabcfd280b3773fBrian Paul      rgba[i][BCOMP] = f * rgba[i][BCOMP] + g * bFog;
676f89984fede5ba137b5987201cabcfd280b3773fBrian Paul   }
686f89984fede5ba137b5987201cabcfd280b3773fBrian Paul#else
696f89984fede5ba137b5987201cabcfd280b3773fBrian Paul   for (i = 0; i < n; i++) {
706f89984fede5ba137b5987201cabcfd280b3773fBrian Paul      const GLfixed f = CLAMP(fog[i], 0, FIXED_ONE);
716f89984fede5ba137b5987201cabcfd280b3773fBrian Paul      const GLfixed g = FIXED_ONE - f;
7274e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul      rgba[i][0] = (f * rgba[i][0] + g * rFog) >> FIXED_SHIFT;
7374e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul      rgba[i][1] = (f * rgba[i][1] + g * gFog) >> FIXED_SHIFT;
7474e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul      rgba[i][2] = (f * rgba[i][2] + g * bFog) >> FIXED_SHIFT;
75e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
766f89984fede5ba137b5987201cabcfd280b3773fBrian Paul#endif
77e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
78e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
79e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
80e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
81e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
82e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Apply fog to an array of color index pixels.
83e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Input:  n - number of pixels
84e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         z - array of integer depth values
85e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         index - pixel color indexes
86e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Output:  index - fogged pixel color indexes
87e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
88e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwellvoid
89e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell_mesa_fog_ci_pixels( const GLcontext *ctx,
90e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell                     GLuint n, const GLfixed fog[], GLuint index[] )
91e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
92b51b0a847d7e7daaea69f77ab569086ef81c24a2Brian Paul   GLuint idx = (GLuint) ctx->Fog.Index;
93e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   GLuint i;
94e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
95e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   for (i=0;i<n;i++) {
96b0dbd3ed131c1097177d9091ad52b03374eda748Brian Paul      GLfloat f = FixedToFloat(CLAMP(fog[i], 0, FIXED_ONE));
97e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      index[i] = (GLuint) ((GLfloat) index[i] + (1.0F-f) * idx);
98e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
99e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
100e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
101e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
102e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
103e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
10422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * Calculate fog coords from window z values
105e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Input:  n - number of pixels
106e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         z - array of integer depth values
107e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         red, green, blue, alpha - pixel colors
108e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Output:  red, green, blue, alpha - fogged pixel colors
109e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *
11022144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes * Use lookup table & interpolation?
111e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
112e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwellvoid
113e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell_mesa_win_fog_coords_from_z( const GLcontext *ctx,
11422144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes			     GLuint n,
11522144ab7552f0799bcfca506bf4ffa7f70a06649Gareth Hughes			     const GLdepth z[],
116e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell			     GLfixed fogcoord[] )
117e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
11830971cd098d147a4363df0dec0c338587dc1478fBrian Paul   const GLboolean ortho = (ctx->ProjectionMatrix.m[15] != 0.0F);
11930971cd098d147a4363df0dec0c338587dc1478fBrian Paul   const GLfloat p10 = ctx->ProjectionMatrix.m[10];
12030971cd098d147a4363df0dec0c338587dc1478fBrian Paul   const GLfloat p14 = ctx->ProjectionMatrix.m[14];
12130971cd098d147a4363df0dec0c338587dc1478fBrian Paul   const GLfloat tz = ctx->Viewport._WindowMap.m[MAT_TZ];
122d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul   GLfloat szInv;
123e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   GLuint i;
124e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
125d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul   if (ctx->Viewport._WindowMap.m[MAT_SZ] == 0.0)
126d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul      szInv = 1.0F;
127d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul   else
128d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul      szInv = 1.0F / ctx->Viewport._WindowMap.m[MAT_SZ];
129d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul
13030971cd098d147a4363df0dec0c338587dc1478fBrian Paul   /*
13130971cd098d147a4363df0dec0c338587dc1478fBrian Paul    * Note: to compute eyeZ from the ndcZ we have to solve the following:
13230971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *
13330971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *        p[10] * eyeZ + p[14] * eyeW
13430971cd098d147a4363df0dec0c338587dc1478fBrian Paul    * ndcZ = ---------------------------
13530971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *        p[11] * eyeZ + p[15] * eyeW
13630971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *
13730971cd098d147a4363df0dec0c338587dc1478fBrian Paul    * Thus:
13830971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *
13930971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *        p[14] * eyeW - p[15] * eyeW * ndcZ
14030971cd098d147a4363df0dec0c338587dc1478fBrian Paul    * eyeZ = ----------------------------------
14130971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *             p[11] * ndcZ - p[10]
14230971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *
14330971cd098d147a4363df0dec0c338587dc1478fBrian Paul    * If we note:
14430971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *    a) if using an orthographic projection, p[11] = 0 and p[15] = 1.
14530971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *    b) if using a perspective projection, p[11] = -1 and p[15] = 0.
14630971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *    c) we assume eyeW = 1 (not always true- glVertex4)
14730971cd098d147a4363df0dec0c338587dc1478fBrian Paul    *
14830971cd098d147a4363df0dec0c338587dc1478fBrian Paul    * Then we can simplify the calculation of eyeZ quite a bit.  We do
14930971cd098d147a4363df0dec0c338587dc1478fBrian Paul    * separate calculations for the orthographic and perspective cases below.
15030971cd098d147a4363df0dec0c338587dc1478fBrian Paul    * Note that we drop a negative sign or two since they don't matter.
15130971cd098d147a4363df0dec0c338587dc1478fBrian Paul    */
152e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
153e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   switch (ctx->Fog.Mode) {
154e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      case GL_LINEAR:
155e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         {
156e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell            GLfloat fogEnd = ctx->Fog.End;
157d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul            GLfloat fogScale;
158d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul            if (ctx->Fog.Start == ctx->Fog.End)
159d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul               fogScale = 1.0;
160d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul            else
161d14fce03f0b5db720cfc52a3c81f407626e28111Brian Paul               fogScale = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
16230971cd098d147a4363df0dec0c338587dc1478fBrian Paul            if (ortho) {
16330971cd098d147a4363df0dec0c338587dc1478fBrian Paul               for (i=0;i<n;i++) {
16430971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
16530971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat eyez = (ndcz - p14) / p10;
16674e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul                  if (eyez < 0.0)
16774e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul                     eyez = -eyez;
16874e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul                  fogcoord[i] = FloatToFixed((fogEnd - eyez) * fogScale);
16930971cd098d147a4363df0dec0c338587dc1478fBrian Paul               }
17030971cd098d147a4363df0dec0c338587dc1478fBrian Paul            }
17130971cd098d147a4363df0dec0c338587dc1478fBrian Paul            else {
17230971cd098d147a4363df0dec0c338587dc1478fBrian Paul               /* perspective */
17330971cd098d147a4363df0dec0c338587dc1478fBrian Paul               for (i=0;i<n;i++) {
17430971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
17530971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat eyez = p14 / (ndcz + p10);
17674e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul                  if (eyez < 0.0)
17774e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul                     eyez = -eyez;
17874e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul                  fogcoord[i] = FloatToFixed((fogEnd - eyez) * fogScale);
17930971cd098d147a4363df0dec0c338587dc1478fBrian Paul               }
180e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell            }
181e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         }
182e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	 break;
183e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      case GL_EXP:
18430971cd098d147a4363df0dec0c338587dc1478fBrian Paul         if (ortho) {
18530971cd098d147a4363df0dec0c338587dc1478fBrian Paul            for (i=0;i<n;i++) {
18630971cd098d147a4363df0dec0c338587dc1478fBrian Paul               GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
18730971cd098d147a4363df0dec0c338587dc1478fBrian Paul               GLfloat eyez = (ndcz - p14) / p10;
18874e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul               if (eyez < 0.0)
18974e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul                  eyez = -eyez;
19030971cd098d147a4363df0dec0c338587dc1478fBrian Paul               fogcoord[i] = FloatToFixed(exp( -ctx->Fog.Density * eyez ));
19130971cd098d147a4363df0dec0c338587dc1478fBrian Paul            }
19230971cd098d147a4363df0dec0c338587dc1478fBrian Paul         }
19330971cd098d147a4363df0dec0c338587dc1478fBrian Paul         else {
19430971cd098d147a4363df0dec0c338587dc1478fBrian Paul            /* perspective */
19530971cd098d147a4363df0dec0c338587dc1478fBrian Paul            for (i=0;i<n;i++) {
19630971cd098d147a4363df0dec0c338587dc1478fBrian Paul               GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
19730971cd098d147a4363df0dec0c338587dc1478fBrian Paul               GLfloat eyez = p14 / (ndcz + p10);
19874e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul               if (eyez < 0.0)
19974e2dd38ed2d4be3454683c3b3f6959738ef235aBrian Paul                  eyez = -eyez;
20030971cd098d147a4363df0dec0c338587dc1478fBrian Paul               fogcoord[i] = FloatToFixed(exp( -ctx->Fog.Density * eyez ));
20130971cd098d147a4363df0dec0c338587dc1478fBrian Paul            }
20230971cd098d147a4363df0dec0c338587dc1478fBrian Paul         }
203e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	 break;
204e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      case GL_EXP2:
205e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         {
206e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell            GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density;
20730971cd098d147a4363df0dec0c338587dc1478fBrian Paul            if (ortho) {
20830971cd098d147a4363df0dec0c338587dc1478fBrian Paul               for (i=0;i<n;i++) {
20930971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
21030971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat eyez = (ndcz - p14) / p10;
21130971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat tmp = negDensitySquared * eyez * eyez;
21230971cd098d147a4363df0dec0c338587dc1478fBrian Paul#if defined(__alpha__) || defined(__alpha)
21330971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  /* XXX this underflow check may be needed for other systems*/
21430971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  if (tmp < FLT_MIN_10_EXP)
21530971cd098d147a4363df0dec0c338587dc1478fBrian Paul                     tmp = FLT_MIN_10_EXP;
21630971cd098d147a4363df0dec0c338587dc1478fBrian Paul#endif
21730971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  fogcoord[i] = FloatToFixed(exp( tmp ));
21830971cd098d147a4363df0dec0c338587dc1478fBrian Paul               }
21930971cd098d147a4363df0dec0c338587dc1478fBrian Paul            }
22030971cd098d147a4363df0dec0c338587dc1478fBrian Paul            else {
22130971cd098d147a4363df0dec0c338587dc1478fBrian Paul               /* perspective */
22230971cd098d147a4363df0dec0c338587dc1478fBrian Paul               for (i=0;i<n;i++) {
22330971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat ndcz = ((GLfloat) z[i] - tz) * szInv;
22430971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat eyez = p14 / (ndcz + p10);
22530971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  GLfloat tmp = negDensitySquared * eyez * eyez;
226e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#if defined(__alpha__) || defined(__alpha)
22730971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  /* XXX this underflow check may be needed for other systems*/
22830971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  if (tmp < FLT_MIN_10_EXP)
22930971cd098d147a4363df0dec0c338587dc1478fBrian Paul                     tmp = FLT_MIN_10_EXP;
230e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell#endif
23130971cd098d147a4363df0dec0c338587dc1478fBrian Paul                  fogcoord[i] = FloatToFixed(exp( tmp ));
23230971cd098d147a4363df0dec0c338587dc1478fBrian Paul               }
233e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell            }
234e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         }
235e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell	 break;
236e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell      default:
23708836341788a9f9d638d9dc8328510ccd18ddeb5Brian Paul         _mesa_problem(ctx, "Bad fog mode in _mesa_win_fog_coords_from_z");
238e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell         return;
239e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   }
240e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
241e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
242e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
243e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
244e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Apply fog to an array of RGBA pixels.
245e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Input:  n - number of pixels
246e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         z - array of integer depth values
247e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         red, green, blue, alpha - pixel colors
248e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Output:  red, green, blue, alpha - fogged pixel colors
249e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
250e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwellvoid
251e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell_mesa_depth_fog_rgba_pixels( const GLcontext *ctx,
252e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell			     GLuint n, const GLdepth z[], GLchan rgba[][4] )
253e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
2546f89984fede5ba137b5987201cabcfd280b3773fBrian Paul   GLfixed fog[PB_SIZE];
2556f89984fede5ba137b5987201cabcfd280b3773fBrian Paul   ASSERT(n <= PB_SIZE);
256e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   _mesa_win_fog_coords_from_z( ctx, n, z, fog );
257e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   _mesa_fog_rgba_pixels( ctx, n, fog, rgba );
258e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
259e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
260e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell
261e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell/*
262e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Apply fog to an array of color index pixels.
263e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Input:  n - number of pixels
264e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         z - array of integer depth values
265e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell *         index - pixel color indexes
266e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell * Output:  index - fogged pixel color indexes
267e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell */
268e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwellvoid
269e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell_mesa_depth_fog_ci_pixels( const GLcontext *ctx,
270e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell                     GLuint n, const GLdepth z[], GLuint index[] )
271e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell{
2726f89984fede5ba137b5987201cabcfd280b3773fBrian Paul   GLfixed fog[PB_SIZE];
2736f89984fede5ba137b5987201cabcfd280b3773fBrian Paul   ASSERT(n <= PB_SIZE);
274e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   _mesa_win_fog_coords_from_z( ctx, n, z, fog );
275e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell   _mesa_fog_ci_pixels( ctx, n, fog, index );
276e3a051e0538a605551f4d58294c94f5eb00ed07fKeith Whitwell}
277