1/**************************************************************************
2
3Copyright 2000, 2001 ATI Technologies Inc., Ontario, Canada, and
4                     Tungsten Graphics Inc., Austin, Texas.
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 <keith@tungstengraphics.com>
33 */
34
35#include "main/glheader.h"
36#include "main/imports.h"
37#include "main/context.h"
38#include "main/mtypes.h"
39#include "main/enums.h"
40#include "main/macros.h"
41
42#include "radeon_fog.h"
43
44/**********************************************************************/
45/*             Fog blend factor computation for hw tcl                */
46/*             same calculation used as in t_vb_fog.c                 */
47/**********************************************************************/
48
49#define FOG_EXP_TABLE_SIZE 256
50#define FOG_MAX (10.0)
51#define EXP_FOG_MAX .0006595
52#define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE)
53static GLfloat exp_table[FOG_EXP_TABLE_SIZE];
54
55#if 1
56#define NEG_EXP( result, narg )						\
57do {									\
58   GLfloat f = (GLfloat) (narg * (1.0/FOG_INCR));			\
59   GLint k = (GLint) f;							\
60   if (k > FOG_EXP_TABLE_SIZE-2) 					\
61      result = (GLfloat) EXP_FOG_MAX;					\
62   else									\
63      result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]);	\
64} while (0)
65#else
66#define NEG_EXP( result, narg )					\
67do {								\
68   result = exp(-narg);						\
69} while (0)
70#endif
71
72
73/**
74 * Initialize the exp_table[] lookup table for approximating exp().
75 */
76void
77radeonInitStaticFogData( void )
78{
79   GLfloat f = 0.0F;
80   GLint i = 0;
81   for ( ; i < FOG_EXP_TABLE_SIZE ; i++, f += FOG_INCR) {
82      exp_table[i] = (GLfloat) exp(-f);
83   }
84}
85
86/**
87 * Compute per-vertex fog blend factors from fog coordinates by
88 * evaluating the GL_LINEAR, GL_EXP or GL_EXP2 fog function.
89 * Fog coordinates are distances from the eye (typically between the
90 * near and far clip plane distances).
91 * Note the fog (eye Z) coords may be negative so we use ABS(z) below.
92 * Fog blend factors are in the range [0,1].
93 */
94float
95radeonComputeFogBlendFactor( struct gl_context *ctx, GLfloat fogcoord )
96{
97	GLfloat end  = ctx->Fog.End;
98	GLfloat d, temp;
99	const GLfloat z = FABSF(fogcoord);
100
101	switch (ctx->Fog.Mode) {
102	case GL_LINEAR:
103		if (ctx->Fog.Start == ctx->Fog.End)
104			d = 1.0F;
105		else
106			d = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
107		temp = (end - z) * d;
108		return CLAMP(temp, 0.0F, 1.0F);
109		break;
110	case GL_EXP:
111		d = ctx->Fog.Density;
112		NEG_EXP( temp, d * z );
113		return temp;
114		break;
115	case GL_EXP2:
116		d = ctx->Fog.Density*ctx->Fog.Density;
117		NEG_EXP( temp, d * z * z );
118		return temp;
119		break;
120	default:
121		_mesa_problem(ctx, "Bad fog mode in make_fog_coord");
122		return 0;
123	}
124}
125
126