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