1/************************************************************************** 2 3Copyright 2000, 2001 ATI Technologies Inc., Ontario, Canada, and 4 VMware, Inc. 5 6All Rights Reserved. 7 8Permission is hereby granted, free of charge, to any person obtaining 9a copy of this software and associated documentation files (the 10"Software"), to deal in the Software without restriction, including 11without limitation the rights to use, copy, modify, merge, publish, 12distribute, sublicense, and/or sell copies of the Software, and to 13permit persons to whom the Software is furnished to do so, subject to 14the following conditions: 15 16The above copyright notice and this permission notice (including the 17next paragraph) shall be included in all copies or substantial 18portions of the Software. 19 20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 23IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 24LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 27 28**************************************************************************/ 29 30/* 31 * Authors: 32 * Keith Whitwell <keithw@vmware.com> 33 */ 34 35#include "c99_math.h" 36#include "main/glheader.h" 37#include "main/imports.h" 38#include "main/context.h" 39#include "main/mtypes.h" 40#include "main/enums.h" 41#include "main/macros.h" 42 43#include "radeon_screen.h" 44#include "radeon_fog.h" 45 46/**********************************************************************/ 47/* Fog blend factor computation for hw tcl */ 48/* same calculation used as in t_vb_fog.c */ 49/**********************************************************************/ 50 51#define FOG_EXP_TABLE_SIZE 256 52#define FOG_MAX (10.0) 53#define EXP_FOG_MAX .0006595 54#define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE) 55static GLfloat exp_table[FOG_EXP_TABLE_SIZE]; 56 57#if 1 58#define NEG_EXP( result, narg ) \ 59do { \ 60 GLfloat f = (GLfloat) (narg * (1.0/FOG_INCR)); \ 61 GLint k = (GLint) f; \ 62 if (k > FOG_EXP_TABLE_SIZE-2) \ 63 result = (GLfloat) EXP_FOG_MAX; \ 64 else \ 65 result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \ 66} while (0) 67#else 68#define NEG_EXP( result, narg ) \ 69do { \ 70 result = exp(-narg); \ 71} while (0) 72#endif 73 74 75/** 76 * Initialize the exp_table[] lookup table for approximating exp(). 77 */ 78void 79radeonInitStaticFogData( void ) 80{ 81 GLfloat f = 0.0F; 82 GLint i = 0; 83 for ( ; i < FOG_EXP_TABLE_SIZE ; i++, f += FOG_INCR) { 84 exp_table[i] = (GLfloat) exp(-f); 85 } 86} 87 88/** 89 * Compute per-vertex fog blend factors from fog coordinates by 90 * evaluating the GL_LINEAR, GL_EXP or GL_EXP2 fog function. 91 * Fog coordinates are distances from the eye (typically between the 92 * near and far clip plane distances). 93 * Note the fog (eye Z) coords may be negative so we use ABS(z) below. 94 * Fog blend factors are in the range [0,1]. 95 */ 96float 97radeonComputeFogBlendFactor( struct gl_context *ctx, GLfloat fogcoord ) 98{ 99 GLfloat end = ctx->Fog.End; 100 GLfloat d, temp; 101 const GLfloat z = fabsf(fogcoord); 102 103 switch (ctx->Fog.Mode) { 104 case GL_LINEAR: 105 if (ctx->Fog.Start == ctx->Fog.End) 106 d = 1.0F; 107 else 108 d = 1.0F / (ctx->Fog.End - ctx->Fog.Start); 109 temp = (end - z) * d; 110 return CLAMP(temp, 0.0F, 1.0F); 111 break; 112 case GL_EXP: 113 d = ctx->Fog.Density; 114 NEG_EXP( temp, d * z ); 115 return temp; 116 break; 117 case GL_EXP2: 118 d = ctx->Fog.Density*ctx->Fog.Density; 119 NEG_EXP( temp, d * z * z ); 120 return temp; 121 break; 122 default: 123 _mesa_problem(ctx, "Bad fog mode in make_fog_coord"); 124 return 0; 125 } 126} 127 128