1 /* $Id: t_vb_fog.c,v 1.7 2001/03/07 05:06:13 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999-2000 Brian Paul All Rights Reserved.
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:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
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.
27 * Keith Whitwell <keithw@valinux.com>
39 #include "math/m_xform.h"
41 #include "t_context.h"
42 #include "t_pipeline.h"
45 struct fog_stage_data
{
46 GLvector1f fogcoord
; /* has actual storage allocated */
47 GLvector1f input
; /* points into VB->EyePtr Z values */
50 #define FOG_STAGE_DATA(stage) ((struct fog_stage_data *)stage->privatePtr)
52 #define FOG_EXP_TABLE_SIZE 256
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;
60 #define NEG_EXP( result, narg ) \
62 float f = (narg * (1.0/FOG_INCR)); \
64 if (k > FOG_EXP_TABLE_SIZE-2) \
65 result = EXP_FOG_MAX; \
67 result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \
70 #define NEG_EXP( result, narg ) \
72 result = exp(-narg); \
77 static void init_static_data( void )
81 for ( ; i
< FOG_EXP_TABLE_SIZE
; i
++, f
+= FOG_INCR
) {
82 exp_table
[i
] = exp(-f
);
88 static void make_win_fog_coords( GLcontext
*ctx
, GLvector1f
*out
,
89 const GLvector1f
*in
)
91 GLfloat end
= ctx
->Fog
.End
;
92 GLfloat
*v
= in
->start
;
93 GLuint stride
= in
->stride
;
95 GLfloat
*data
= out
->data
;
99 out
->count
= in
->count
;
101 switch (ctx
->Fog
.Mode
) {
103 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
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
);
113 d
= ctx
->Fog
.Density
;
114 for ( i
= 0 ; i
< n
; i
++, STRIDE_F(v
,stride
))
115 NEG_EXP( data
[i
], d
*ABSF(*v
) );
118 d
= ctx
->Fog
.Density
*ctx
->Fog
.Density
;
119 for ( i
= 0 ; i
< n
; i
++, STRIDE_F(v
, stride
)) {
121 NEG_EXP( data
[i
], d
*z
*z
);
125 _mesa_problem(ctx
, "Bad fog mode in make_fog_coord");
131 static GLboolean
run_fog_stage( GLcontext
*ctx
,
132 struct gl_pipeline_stage
*stage
)
134 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
135 struct fog_stage_data
*store
= FOG_STAGE_DATA(stage
);
138 VB
->FogCoordPtr
= &store
->fogcoord
;
140 if (stage
->changed_inputs
== 0)
143 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
) {
144 if (!ctx
->_NeedEyeCoords
) {
145 GLfloat
*m
= ctx
->ModelView
.m
;
148 /* Use this to store calculated eye z values:
150 input
= &store
->fogcoord
;
157 /* Full eye coords weren't required, just calculate the
160 _mesa_dotprod_tab
[0][VB
->ObjPtr
->size
](input
->data
, sizeof(GLfloat
),
161 VB
->ObjPtr
, plane
, 0 );
163 input
->count
= VB
->ObjPtr
->count
;
167 input
= &store
->input
;
169 if (VB
->EyePtr
->size
< 2)
170 _mesa_vector4f_clean_elem( VB
->EyePtr
, VB
->Count
, 2 );
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
;
178 input
= VB
->FogCoordPtr
;
180 make_win_fog_coords( ctx
, VB
->FogCoordPtr
, input
);
184 static void check_fog_stage( GLcontext
*ctx
, struct gl_pipeline_stage
*stage
)
186 stage
->active
= ctx
->Fog
.Enabled
;
188 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
189 stage
->inputs
= VERT_EYE
;
191 stage
->inputs
= VERT_FOG_COORD
;
195 /* Called the first time stage->run() is invoked.
197 static GLboolean
alloc_fog_data( GLcontext
*ctx
,
198 struct gl_pipeline_stage
*stage
)
200 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
201 struct fog_stage_data
*store
;
202 stage
->privatePtr
= MALLOC(sizeof(*store
));
203 store
= FOG_STAGE_DATA(stage
);
207 _mesa_vector1f_alloc( &store
->fogcoord
, 0, tnl
->vb
.Size
, 32 );
208 _mesa_vector1f_init( &store
->input
, 0, 0 );
213 /* Now run the stage.
215 stage
->run
= run_fog_stage
;
216 return stage
->run( ctx
, stage
);
220 static void free_fog_data( struct gl_pipeline_stage
*stage
)
222 struct fog_stage_data
*store
= FOG_STAGE_DATA(stage
);
224 _mesa_vector1f_free( &store
->fogcoord
);
226 stage
->privatePtr
= NULL
;
231 const struct gl_pipeline_stage _tnl_fog_coordinate_stage
=
233 "build fog coordinates",
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. */