summaryrefslogtreecommitdiff
path: root/src/mesa/tnl/t_vb_fog.c
blob: e7f740dfa3c2d0d59b2ac3e47f678b8df3732ee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* $Id: t_vb_fog.c,v 1.3 2001/02/06 04:06:36 keithw Exp $ */

/*
 * Mesa 3-D graphics library
 * Version:  3.5
 *
 * Copyright (C) 1999-2000  Brian Paul   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Author:
 *    Keith Whitwell <keithw@valinux.com>
 */


#include "glheader.h"
#include "colormac.h"
#include "context.h"
#include "macros.h"
#include "mem.h"
#include "mmath.h"
#include "mtypes.h"

#include "math/m_xform.h"

#include "t_context.h"
#include "t_pipeline.h"


struct fog_stage_data {
   GLvector1f fogcoord;		/* has actual storage allocated */
   GLvector1f input;		/* points into VB->EyePtr Z values */
};

#define FOG_STAGE_DATA(stage) ((struct fog_stage_data *)stage->private)

#define FOG_EXP_TABLE_SIZE 256
#define FOG_MAX (5.0)
#define EXP_FOG_MAX .0006595
#define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE)
static GLfloat exp_table[FOG_EXP_TABLE_SIZE];
static GLfloat inited = 0;

#if 1
#define NEG_EXP( result, narg )						\
do {									\
   float f = (narg * (1.0/FOG_INCR));					\
   int k = (int) f;							\
   if (k > FOG_EXP_TABLE_SIZE-2) 					\
      result = EXP_FOG_MAX;						\
   else									\
      result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]);	\
} while (0)
#else
#define NEG_EXP( result, narg )					\
do {								\
   result = exp(-narg);						\
} while (0)
#endif


static void init_static_data( void )
{
   float f = 0;
   int i = 0;
   for ( ; i < FOG_EXP_TABLE_SIZE ; i++, f += FOG_INCR) {
      exp_table[i] = exp(-f);
   }
   inited = 1;
}


static void make_win_fog_coords( GLcontext *ctx, GLvector1f *out, 
				 const GLvector1f *in )
{
   GLfloat end  = ctx->Fog.End;
   GLfloat *v = in->start;
   GLuint stride = in->stride;
   GLuint n = in->count;
   GLfloat *data = out->data;		
   GLfloat d;
   GLuint i;

   out->count = in->count;

   switch (ctx->Fog.Mode) {
   case GL_LINEAR:
      if (ctx->Fog.Start == ctx->Fog.End)
         d = 1.0F;
      else
         d = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
      for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) 
	 data[i] = (end - ABSF(*v)) * d;
      break;
   case GL_EXP:
      d = ctx->Fog.Density;
      for ( i = 0 ; i < n ; i++, STRIDE_F(v,stride)) 
	 NEG_EXP( data[i], d*ABSF(*v) );
      break;
   case GL_EXP2:
      d = ctx->Fog.Density*ctx->Fog.Density;
      for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) {
	 GLfloat z = *v;
	 NEG_EXP( data[i], d*z*z );
      }
      break;
   default:
      gl_problem(ctx, "Bad fog mode in make_fog_coord");
      return;
   }
}


static GLboolean run_fog_stage( GLcontext *ctx, 
				struct gl_pipeline_stage *stage )
{
   struct vertex_buffer *VB = &TNL_CONTEXT(ctx)->vb;
   struct fog_stage_data *store = FOG_STAGE_DATA(stage);
   GLvector1f *input;

   VB->FogCoordPtr = &store->fogcoord;

   if (stage->changed_inputs == 0)
      return GL_TRUE;

   if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT) {
      if (!ctx->_NeedEyeCoords) {
	 GLfloat *m = ctx->ModelView.m;
	 GLfloat plane[4];
	 
	 /* Use this to store calculated eye z values:
	  */
	 input = &store->fogcoord;

	 plane[0] = m[2];
	 plane[1] = m[6];
	 plane[2] = m[10];
	 plane[3] = m[14];

	 /* Full eye coords weren't required, just calculate the
	  * eye Z values.
	  */
	 gl_dotprod_tab[0][VB->ObjPtr->size](input->data, sizeof(GLfloat),
					     VB->ObjPtr, plane, 0 );

	 input->count = VB->ObjPtr->count;
      }
      else
      {
	 input = &store->input;

	 if (VB->EyePtr->size < 2)
	    gl_vector4f_clean_elem( VB->EyePtr, VB->Count, 2 );

	 input->data = &(VB->EyePtr->data[0][2]);
	 input->start = VB->EyePtr->start+2;
	 input->stride = VB->EyePtr->stride;
	 input->count = VB->EyePtr->count;
      }
   } else
      input = VB->FogCoordPtr;

   make_win_fog_coords( ctx, VB->FogCoordPtr, input );
   return GL_TRUE;
}

static void check_fog_stage( GLcontext *ctx, struct gl_pipeline_stage *stage )
{
   stage->active = ctx->Fog.Enabled;

   if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT)
      stage->inputs = VERT_EYE;
   else
      stage->inputs = VERT_FOG_COORD;
}


/* Called the first time stage->run() is invoked.
 */
static GLboolean alloc_fog_data( GLcontext *ctx, 
				 struct gl_pipeline_stage *stage )
{
   TNLcontext *tnl = TNL_CONTEXT(ctx);
   struct fog_stage_data *store;
   stage->private = MALLOC(sizeof(*store));
   store = FOG_STAGE_DATA(stage);
   if (!store)
      return GL_FALSE;

   gl_vector1f_alloc( &store->fogcoord, 0, tnl->vb.Size, 32 );
   gl_vector1f_init( &store->input, 0, 0 );

   if (!inited)
      init_static_data();

   /* Now run the stage.
    */
   stage->run = run_fog_stage;
   return stage->run( ctx, stage );
}


static void free_fog_data( struct gl_pipeline_stage *stage )
{
   struct fog_stage_data *store = FOG_STAGE_DATA(stage);
   if (store) {
      gl_vector1f_free( &store->fogcoord );
      FREE( store );
      stage->private = 0;
   }
}


const struct gl_pipeline_stage _tnl_fog_coordinate_stage = 
{ 
   "build fog coordinates",
   _NEW_FOG,
   _NEW_FOG,			
   0, 0, VERT_FOG_COORD,	/* active, inputs, outputs */
   0, 0,			/* changed_inputs, private_data */
   free_fog_data,		/* dtr */
   check_fog_stage,		/* check */
   alloc_fog_data		/* run -- initially set to init. */
};