fixed a bunch of g++ warnings/errors. Compiling with g++ can help find lots of poten...
[mesa.git] / src / mesa / tnl / t_vb_fog.c
1 /* $Id: t_vb_fog.c,v 1.7 2001/03/07 05:06:13 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.5
6 *
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
23 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Author:
27 * Keith Whitwell <keithw@valinux.com>
28 */
29
30
31 #include "glheader.h"
32 #include "colormac.h"
33 #include "context.h"
34 #include "macros.h"
35 #include "mem.h"
36 #include "mmath.h"
37 #include "mtypes.h"
38
39 #include "math/m_xform.h"
40
41 #include "t_context.h"
42 #include "t_pipeline.h"
43
44
45 struct fog_stage_data {
46 GLvector1f fogcoord; /* has actual storage allocated */
47 GLvector1f input; /* points into VB->EyePtr Z values */
48 };
49
50 #define FOG_STAGE_DATA(stage) ((struct fog_stage_data *)stage->privatePtr)
51
52 #define FOG_EXP_TABLE_SIZE 256
53 #define FOG_MAX (5.0)
54 #define EXP_FOG_MAX .0006595
55 #define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE)
56 static GLfloat exp_table[FOG_EXP_TABLE_SIZE];
57 static GLfloat inited = 0;
58
59 #if 1
60 #define NEG_EXP( result, narg ) \
61 do { \
62 float f = (narg * (1.0/FOG_INCR)); \
63 int k = (int) f; \
64 if (k > FOG_EXP_TABLE_SIZE-2) \
65 result = EXP_FOG_MAX; \
66 else \
67 result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \
68 } while (0)
69 #else
70 #define NEG_EXP( result, narg ) \
71 do { \
72 result = exp(-narg); \
73 } while (0)
74 #endif
75
76
77 static void init_static_data( void )
78 {
79 float f = 0;
80 int i = 0;
81 for ( ; i < FOG_EXP_TABLE_SIZE ; i++, f += FOG_INCR) {
82 exp_table[i] = exp(-f);
83 }
84 inited = 1;
85 }
86
87
88 static void make_win_fog_coords( GLcontext *ctx, GLvector1f *out,
89 const GLvector1f *in )
90 {
91 GLfloat end = ctx->Fog.End;
92 GLfloat *v = in->start;
93 GLuint stride = in->stride;
94 GLuint n = in->count;
95 GLfloat *data = out->data;
96 GLfloat d;
97 GLuint i;
98
99 out->count = in->count;
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 for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) {
108 GLfloat f = (end - ABSF(*v)) * d;
109 data[i] = CLAMP(f, 0.0F, 1.0F);
110 }
111 break;
112 case GL_EXP:
113 d = ctx->Fog.Density;
114 for ( i = 0 ; i < n ; i++, STRIDE_F(v,stride))
115 NEG_EXP( data[i], d*ABSF(*v) );
116 break;
117 case GL_EXP2:
118 d = ctx->Fog.Density*ctx->Fog.Density;
119 for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) {
120 GLfloat z = *v;
121 NEG_EXP( data[i], d*z*z );
122 }
123 break;
124 default:
125 _mesa_problem(ctx, "Bad fog mode in make_fog_coord");
126 return;
127 }
128 }
129
130
131 static GLboolean run_fog_stage( GLcontext *ctx,
132 struct gl_pipeline_stage *stage )
133 {
134 struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
135 struct fog_stage_data *store = FOG_STAGE_DATA(stage);
136 GLvector1f *input;
137
138 VB->FogCoordPtr = &store->fogcoord;
139
140 if (stage->changed_inputs == 0)
141 return GL_TRUE;
142
143 if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT) {
144 if (!ctx->_NeedEyeCoords) {
145 GLfloat *m = ctx->ModelView.m;
146 GLfloat plane[4];
147
148 /* Use this to store calculated eye z values:
149 */
150 input = &store->fogcoord;
151
152 plane[0] = m[2];
153 plane[1] = m[6];
154 plane[2] = m[10];
155 plane[3] = m[14];
156
157 /* Full eye coords weren't required, just calculate the
158 * eye Z values.
159 */
160 _mesa_dotprod_tab[0][VB->ObjPtr->size](input->data, sizeof(GLfloat),
161 VB->ObjPtr, plane, 0 );
162
163 input->count = VB->ObjPtr->count;
164 }
165 else
166 {
167 input = &store->input;
168
169 if (VB->EyePtr->size < 2)
170 _mesa_vector4f_clean_elem( VB->EyePtr, VB->Count, 2 );
171
172 input->data = &(VB->EyePtr->data[0][2]);
173 input->start = VB->EyePtr->start+2;
174 input->stride = VB->EyePtr->stride;
175 input->count = VB->EyePtr->count;
176 }
177 } else
178 input = VB->FogCoordPtr;
179
180 make_win_fog_coords( ctx, VB->FogCoordPtr, input );
181 return GL_TRUE;
182 }
183
184 static void check_fog_stage( GLcontext *ctx, struct gl_pipeline_stage *stage )
185 {
186 stage->active = ctx->Fog.Enabled;
187
188 if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT)
189 stage->inputs = VERT_EYE;
190 else
191 stage->inputs = VERT_FOG_COORD;
192 }
193
194
195 /* Called the first time stage->run() is invoked.
196 */
197 static GLboolean alloc_fog_data( GLcontext *ctx,
198 struct gl_pipeline_stage *stage )
199 {
200 TNLcontext *tnl = TNL_CONTEXT(ctx);
201 struct fog_stage_data *store;
202 stage->privatePtr = MALLOC(sizeof(*store));
203 store = FOG_STAGE_DATA(stage);
204 if (!store)
205 return GL_FALSE;
206
207 _mesa_vector1f_alloc( &store->fogcoord, 0, tnl->vb.Size, 32 );
208 _mesa_vector1f_init( &store->input, 0, 0 );
209
210 if (!inited)
211 init_static_data();
212
213 /* Now run the stage.
214 */
215 stage->run = run_fog_stage;
216 return stage->run( ctx, stage );
217 }
218
219
220 static void free_fog_data( struct gl_pipeline_stage *stage )
221 {
222 struct fog_stage_data *store = FOG_STAGE_DATA(stage);
223 if (store) {
224 _mesa_vector1f_free( &store->fogcoord );
225 FREE( store );
226 stage->privatePtr = NULL;
227 }
228 }
229
230
231 const struct gl_pipeline_stage _tnl_fog_coordinate_stage =
232 {
233 "build fog coordinates",
234 _NEW_FOG,
235 _NEW_FOG,
236 0, 0, VERT_FOG_COORD, /* active, inputs, outputs */
237 0, 0, /* changed_inputs, private_data */
238 free_fog_data, /* dtr */
239 check_fog_stage, /* check */
240 alloc_fog_data /* run -- initially set to init. */
241 };