1848b8605Smrg/*
2848b8605Smrg * Mesa 3-D graphics library
3848b8605Smrg *
4848b8605Smrg * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the "Software"),
8848b8605Smrg * to deal in the Software without restriction, including without limitation
9848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
11848b8605Smrg * Software is furnished to do so, subject to the following conditions:
12848b8605Smrg *
13848b8605Smrg * The above copyright notice and this permission notice shall be included
14848b8605Smrg * in all copies or substantial portions of the Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20848b8605Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21848b8605Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22848b8605Smrg * OTHER DEALINGS IN THE SOFTWARE.
23848b8605Smrg *
24848b8605Smrg * Authors:
25848b8605Smrg *    Keith Whitwell <keithw@vmware.com>
26848b8605Smrg */
27848b8605Smrg
28848b8605Smrg
29b8e80941Smrg#include "c99_math.h"
30b8e80941Smrg#include "main/errors.h"
31848b8605Smrg#include "main/glheader.h"
32848b8605Smrg#include "main/macros.h"
33848b8605Smrg#include "main/imports.h"
34848b8605Smrg#include "main/mtypes.h"
35848b8605Smrg
36848b8605Smrg#include "math/m_xform.h"
37848b8605Smrg
38848b8605Smrg#include "t_context.h"
39848b8605Smrg#include "t_pipeline.h"
40848b8605Smrg
41848b8605Smrg
42848b8605Smrgstruct fog_stage_data {
43848b8605Smrg   GLvector4f fogcoord;		/* has actual storage allocated */
44848b8605Smrg};
45848b8605Smrg
46848b8605Smrg#define FOG_STAGE_DATA(stage) ((struct fog_stage_data *)stage->privatePtr)
47848b8605Smrg
48848b8605Smrg#define FOG_EXP_TABLE_SIZE 256
49b8e80941Smrg#define FOG_MAX (10.0F)
50b8e80941Smrg#define EXP_FOG_MAX .0006595F
51848b8605Smrg#define FOG_INCR (FOG_MAX/FOG_EXP_TABLE_SIZE)
52848b8605Smrgstatic GLfloat exp_table[FOG_EXP_TABLE_SIZE];
53848b8605Smrgstatic GLfloat inited = 0;
54848b8605Smrg
55848b8605Smrg#if 1
56848b8605Smrg#define NEG_EXP( result, narg )						\
57848b8605Smrgdo {									\
58b8e80941Smrg   GLfloat f = (GLfloat) (narg * (1.0F / FOG_INCR));			\
59848b8605Smrg   GLint k = (GLint) f;							\
60848b8605Smrg   if (k > FOG_EXP_TABLE_SIZE-2) 					\
61848b8605Smrg      result = (GLfloat) EXP_FOG_MAX;					\
62848b8605Smrg   else									\
63848b8605Smrg      result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]);	\
64848b8605Smrg} while (0)
65848b8605Smrg#else
66848b8605Smrg#define NEG_EXP( result, narg )					\
67848b8605Smrgdo {								\
68848b8605Smrg   result = exp(-narg);						\
69848b8605Smrg} while (0)
70848b8605Smrg#endif
71848b8605Smrg
72848b8605Smrg
73848b8605Smrg/**
74848b8605Smrg * Initialize the exp_table[] lookup table for approximating exp().
75848b8605Smrg */
76848b8605Smrgstatic void
77848b8605Smrginit_static_data( void )
78848b8605Smrg{
79848b8605Smrg   GLfloat f = 0.0F;
80848b8605Smrg   GLint i = 0;
81848b8605Smrg   for ( ; i < FOG_EXP_TABLE_SIZE ; i++, f += FOG_INCR) {
82b8e80941Smrg      exp_table[i] = expf(-f);
83848b8605Smrg   }
84848b8605Smrg   inited = 1;
85848b8605Smrg}
86848b8605Smrg
87848b8605Smrg
88848b8605Smrg/**
89848b8605Smrg * Compute per-vertex fog blend factors from fog coordinates by
90848b8605Smrg * evaluating the GL_LINEAR, GL_EXP or GL_EXP2 fog function.
91848b8605Smrg * Fog coordinates are distances from the eye (typically between the
92848b8605Smrg * near and far clip plane distances).
93848b8605Smrg * Note that fogcoords may be negative, if eye z is source absolute
94848b8605Smrg * value must be taken earlier.
95848b8605Smrg * Fog blend factors are in the range [0,1].
96848b8605Smrg */
97848b8605Smrgstatic void
98848b8605Smrgcompute_fog_blend_factors(struct gl_context *ctx, GLvector4f *out, const GLvector4f *in)
99848b8605Smrg{
100848b8605Smrg   GLfloat end  = ctx->Fog.End;
101848b8605Smrg   GLfloat *v = in->start;
102848b8605Smrg   GLuint stride = in->stride;
103848b8605Smrg   GLuint n = in->count;
104848b8605Smrg   GLfloat (*data)[4] = out->data;
105848b8605Smrg   GLfloat d;
106848b8605Smrg   GLuint i;
107848b8605Smrg
108848b8605Smrg   out->count = in->count;
109848b8605Smrg
110848b8605Smrg   switch (ctx->Fog.Mode) {
111848b8605Smrg   case GL_LINEAR:
112848b8605Smrg      if (ctx->Fog.Start == ctx->Fog.End)
113848b8605Smrg         d = 1.0F;
114848b8605Smrg      else
115848b8605Smrg         d = 1.0F / (ctx->Fog.End - ctx->Fog.Start);
116848b8605Smrg      for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) {
117848b8605Smrg         const GLfloat z = *v;
118848b8605Smrg         GLfloat f = (end - z) * d;
119848b8605Smrg	 data[i][0] = CLAMP(f, 0.0F, 1.0F);
120848b8605Smrg      }
121848b8605Smrg      break;
122848b8605Smrg   case GL_EXP:
123848b8605Smrg      d = ctx->Fog.Density;
124848b8605Smrg      for ( i = 0 ; i < n ; i++, STRIDE_F(v,stride)) {
125848b8605Smrg         const GLfloat z = *v;
126848b8605Smrg         NEG_EXP( data[i][0], d * z );
127848b8605Smrg      }
128848b8605Smrg      break;
129848b8605Smrg   case GL_EXP2:
130848b8605Smrg      d = ctx->Fog.Density*ctx->Fog.Density;
131848b8605Smrg      for ( i = 0 ; i < n ; i++, STRIDE_F(v, stride)) {
132848b8605Smrg         const GLfloat z = *v;
133848b8605Smrg         NEG_EXP( data[i][0], d * z * z );
134848b8605Smrg      }
135848b8605Smrg      break;
136848b8605Smrg   default:
137848b8605Smrg      _mesa_problem(ctx, "Bad fog mode in make_fog_coord");
138848b8605Smrg      return;
139848b8605Smrg   }
140848b8605Smrg}
141848b8605Smrg
142848b8605Smrg
143848b8605Smrgstatic GLboolean
144848b8605Smrgrun_fog_stage(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
145848b8605Smrg{
146848b8605Smrg   TNLcontext *tnl = TNL_CONTEXT(ctx);
147848b8605Smrg   struct vertex_buffer *VB = &tnl->vb;
148848b8605Smrg   struct fog_stage_data *store = FOG_STAGE_DATA(stage);
149848b8605Smrg   GLvector4f *input;
150848b8605Smrg
151848b8605Smrg
152848b8605Smrg   if (!ctx->Fog.Enabled)
153848b8605Smrg      return GL_TRUE;
154848b8605Smrg
155848b8605Smrg   if (ctx->Fog.FogCoordinateSource == GL_FRAGMENT_DEPTH_EXT && !ctx->VertexProgram._Current) {
156848b8605Smrg      GLuint i;
157848b8605Smrg      GLfloat *coord;
158848b8605Smrg      /* Fog is computed from vertex or fragment Z values */
159848b8605Smrg      /* source = VB->AttribPtr[_TNL_ATTRIB_POS] or VB->EyePtr coords */
160848b8605Smrg      /* dest = VB->AttribPtr[_TNL_ATTRIB_FOG] = fog stage private storage */
161848b8605Smrg      VB->AttribPtr[_TNL_ATTRIB_FOG] = &store->fogcoord;
162848b8605Smrg
163848b8605Smrg      if (!ctx->_NeedEyeCoords) {
164848b8605Smrg         /* compute fog coords from object coords */
165848b8605Smrg	 const GLfloat *m = ctx->ModelviewMatrixStack.Top->m;
166848b8605Smrg	 GLfloat plane[4];
167848b8605Smrg
168848b8605Smrg	 /* Use this to store calculated eye z values:
169848b8605Smrg	  */
170848b8605Smrg	 input = &store->fogcoord;
171848b8605Smrg
172848b8605Smrg	 plane[0] = m[2];
173848b8605Smrg	 plane[1] = m[6];
174848b8605Smrg	 plane[2] = m[10];
175848b8605Smrg	 plane[3] = m[14];
176848b8605Smrg	 /* Full eye coords weren't required, just calculate the
177848b8605Smrg	  * eye Z values.
178848b8605Smrg	  */
179848b8605Smrg	 _mesa_dotprod_tab[VB->AttribPtr[_TNL_ATTRIB_POS]->size]
180848b8605Smrg	    ( (GLfloat *) input->data,
181848b8605Smrg	      4 * sizeof(GLfloat),
182848b8605Smrg	      VB->AttribPtr[_TNL_ATTRIB_POS], plane );
183848b8605Smrg
184848b8605Smrg	 input->count = VB->AttribPtr[_TNL_ATTRIB_POS]->count;
185848b8605Smrg
186848b8605Smrg	 /* make sure coords are really positive
187848b8605Smrg	    NOTE should avoid going through array twice */
188848b8605Smrg	 coord = input->start;
189848b8605Smrg	 for (i = 0; i < input->count; i++) {
190b8e80941Smrg	    *coord = fabsf(*coord);
191848b8605Smrg	    STRIDE_F(coord, input->stride);
192848b8605Smrg	 }
193848b8605Smrg      }
194848b8605Smrg      else {
195848b8605Smrg         /* fog coordinates = eye Z coordinates - need to copy for ABS */
196848b8605Smrg	 input = &store->fogcoord;
197848b8605Smrg
198848b8605Smrg	 if (VB->EyePtr->size < 2)
199848b8605Smrg	    _mesa_vector4f_clean_elem( VB->EyePtr, VB->Count, 2 );
200848b8605Smrg
201848b8605Smrg	 input->stride = 4 * sizeof(GLfloat);
202848b8605Smrg	 input->count = VB->EyePtr->count;
203848b8605Smrg	 coord = VB->EyePtr->start;
204848b8605Smrg	 for (i = 0 ; i < VB->EyePtr->count; i++) {
205b8e80941Smrg	    input->data[i][0] = fabsf(coord[2]);
206848b8605Smrg	    STRIDE_F(coord, VB->EyePtr->stride);
207848b8605Smrg	 }
208848b8605Smrg      }
209848b8605Smrg   }
210848b8605Smrg   else {
211848b8605Smrg      /* use glFogCoord() coordinates */
212848b8605Smrg      input = VB->AttribPtr[_TNL_ATTRIB_FOG];  /* source data */
213848b8605Smrg
214848b8605Smrg      /* input->count may be one if glFogCoord was only called once
215848b8605Smrg       * before glBegin.  But we need to compute fog for all vertices.
216848b8605Smrg       */
217848b8605Smrg      input->count = VB->AttribPtr[_TNL_ATTRIB_POS]->count;
218848b8605Smrg
219848b8605Smrg      VB->AttribPtr[_TNL_ATTRIB_FOG] = &store->fogcoord;  /* dest data */
220848b8605Smrg   }
221848b8605Smrg
222848b8605Smrg   if (tnl->_DoVertexFog) {
223848b8605Smrg      /* compute blend factors from fog coordinates */
224848b8605Smrg      compute_fog_blend_factors( ctx, VB->AttribPtr[_TNL_ATTRIB_FOG], input );
225848b8605Smrg   }
226848b8605Smrg   else {
227848b8605Smrg      /* results = incoming fog coords (compute fog per-fragment later) */
228848b8605Smrg      VB->AttribPtr[_TNL_ATTRIB_FOG] = input;
229848b8605Smrg   }
230848b8605Smrg
231848b8605Smrg   return GL_TRUE;
232848b8605Smrg}
233848b8605Smrg
234848b8605Smrg
235848b8605Smrg
236848b8605Smrg/* Called the first time stage->run() is invoked.
237848b8605Smrg */
238848b8605Smrgstatic GLboolean
239848b8605Smrgalloc_fog_data(struct gl_context *ctx, struct tnl_pipeline_stage *stage)
240848b8605Smrg{
241848b8605Smrg   TNLcontext *tnl = TNL_CONTEXT(ctx);
242848b8605Smrg   struct fog_stage_data *store;
243848b8605Smrg   stage->privatePtr = malloc(sizeof(*store));
244848b8605Smrg   store = FOG_STAGE_DATA(stage);
245848b8605Smrg   if (!store)
246848b8605Smrg      return GL_FALSE;
247848b8605Smrg
248848b8605Smrg   _mesa_vector4f_alloc( &store->fogcoord, 0, tnl->vb.Size, 32 );
249848b8605Smrg
250848b8605Smrg   if (!inited)
251848b8605Smrg      init_static_data();
252848b8605Smrg
253848b8605Smrg   return GL_TRUE;
254848b8605Smrg}
255848b8605Smrg
256848b8605Smrg
257848b8605Smrgstatic void
258848b8605Smrgfree_fog_data(struct tnl_pipeline_stage *stage)
259848b8605Smrg{
260848b8605Smrg   struct fog_stage_data *store = FOG_STAGE_DATA(stage);
261848b8605Smrg   if (store) {
262848b8605Smrg      _mesa_vector4f_free( &store->fogcoord );
263848b8605Smrg      free( store );
264848b8605Smrg      stage->privatePtr = NULL;
265848b8605Smrg   }
266848b8605Smrg}
267848b8605Smrg
268848b8605Smrg
269848b8605Smrgconst struct tnl_pipeline_stage _tnl_fog_coordinate_stage =
270848b8605Smrg{
271848b8605Smrg   "build fog coordinates",	/* name */
272848b8605Smrg   NULL,			/* private_data */
273848b8605Smrg   alloc_fog_data,		/* dtr */
274848b8605Smrg   free_fog_data,		/* dtr */
275848b8605Smrg   NULL,		/* check */
276848b8605Smrg   run_fog_stage		/* run -- initially set to init. */
277848b8605Smrg};
278