3 * Mesa 3-D graphics library
6 * Copyright (C) 1999-2003 Brian Paul All Rights Reserved.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 * Keith Whitwell <keith@tungstengraphics.com>
37 #include "math/m_xform.h"
39 #include "t_context.h"
40 #include "t_pipeline.h"
43 struct fog_stage_data
{
44 GLvector4f fogcoord
; /* has actual storage allocated */
45 GLvector4f input
; /* points into VB->EyePtr Z values */
48 #define FOG_STAGE_DATA(stage) ((struct fog_stage_data *)stage->privatePtr)
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 static GLfloat inited
= 0;
58 #define NEG_EXP( result, narg ) \
60 GLfloat f = (GLfloat) (narg * (1.0/FOG_INCR)); \
61 GLint k = (GLint) f; \
62 if (k > FOG_EXP_TABLE_SIZE-2) \
63 result = (GLfloat) EXP_FOG_MAX; \
65 result = exp_table[k] + (f-k)*(exp_table[k+1]-exp_table[k]); \
68 #define NEG_EXP( result, narg ) \
70 result = exp(-narg); \
75 static void init_static_data( void )
79 for ( ; i
< FOG_EXP_TABLE_SIZE
; i
++, f
+= FOG_INCR
) {
80 exp_table
[i
] = (GLfloat
) exp(-f
);
86 static void make_win_fog_coords( GLcontext
*ctx
, GLvector4f
*out
,
87 const GLvector4f
*in
)
89 GLfloat end
= ctx
->Fog
.End
;
90 GLfloat
*v
= in
->start
;
91 GLuint stride
= in
->stride
;
93 GLfloat (*data
)[4] = out
->data
;
97 out
->count
= in
->count
;
99 switch (ctx
->Fog
.Mode
) {
101 if (ctx
->Fog
.Start
== ctx
->Fog
.End
)
104 d
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
105 for ( i
= 0 ; i
< n
; i
++, STRIDE_F(v
, stride
)) {
106 GLfloat f
= (end
- FABSF(*v
)) * d
;
107 data
[i
][0] = CLAMP(f
, 0.0F
, 1.0F
);
111 d
= ctx
->Fog
.Density
;
112 for ( i
= 0 ; i
< n
; i
++, STRIDE_F(v
,stride
))
113 NEG_EXP( data
[i
][0], d
* FABSF(*v
) );
116 d
= ctx
->Fog
.Density
*ctx
->Fog
.Density
;
117 for ( i
= 0 ; i
< n
; i
++, STRIDE_F(v
, stride
)) {
119 NEG_EXP( data
[i
][0], d
* z
* z
);
123 _mesa_problem(ctx
, "Bad fog mode in make_fog_coord");
129 static GLboolean
run_fog_stage( GLcontext
*ctx
,
130 struct tnl_pipeline_stage
*stage
)
132 struct vertex_buffer
*VB
= &TNL_CONTEXT(ctx
)->vb
;
133 struct fog_stage_data
*store
= FOG_STAGE_DATA(stage
);
136 if (stage
->changed_inputs
== 0)
139 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
) {
140 /* fog computed from Z depth */
141 /* source = VB->ObjPtr or VB->EyePtr coords */
142 /* dest = VB->FogCoordPtr = fog stage private storage */
143 VB
->FogCoordPtr
= &store
->fogcoord
;
145 if (!ctx
->_NeedEyeCoords
) {
146 const GLfloat
*m
= ctx
->ModelviewMatrixStack
.Top
->m
;
149 /* Use this to store calculated eye z values:
151 input
= &store
->fogcoord
;
158 /* Full eye coords weren't required, just calculate the
161 _mesa_dotprod_tab
[VB
->ObjPtr
->size
]( (GLfloat
*) input
->data
,
165 input
->count
= VB
->ObjPtr
->count
;
168 input
= &store
->input
;
170 if (VB
->EyePtr
->size
< 2)
171 _mesa_vector4f_clean_elem( VB
->EyePtr
, VB
->Count
, 2 );
173 input
->data
= (GLfloat (*)[4]) &(VB
->EyePtr
->data
[0][2]);
174 input
->start
= VB
->EyePtr
->start
+2;
175 input
->stride
= VB
->EyePtr
->stride
;
176 input
->count
= VB
->EyePtr
->count
;
180 /* use glFogCoord() coordinates */
181 /* source = VB->FogCoordPtr */
182 input
= VB
->FogCoordPtr
;
183 /* dest = fog stage private storage */
184 VB
->FogCoordPtr
= &store
->fogcoord
;
187 make_win_fog_coords( ctx
, VB
->FogCoordPtr
, input
);
189 VB
->AttribPtr
[_TNL_ATTRIB_FOG
] = VB
->FogCoordPtr
;
194 static void check_fog_stage( GLcontext
*ctx
, struct tnl_pipeline_stage
*stage
)
196 stage
->active
= ctx
->Fog
.Enabled
&& !ctx
->VertexProgram
.Enabled
;
198 if (ctx
->Fog
.FogCoordinateSource
== GL_FRAGMENT_DEPTH_EXT
)
199 stage
->inputs
= _TNL_BIT_POS
;
201 stage
->inputs
= _TNL_BIT_FOG
;
205 /* Called the first time stage->run() is invoked.
207 static GLboolean
alloc_fog_data( GLcontext
*ctx
,
208 struct tnl_pipeline_stage
*stage
)
210 TNLcontext
*tnl
= TNL_CONTEXT(ctx
);
211 struct fog_stage_data
*store
;
212 stage
->privatePtr
= MALLOC(sizeof(*store
));
213 store
= FOG_STAGE_DATA(stage
);
217 _mesa_vector4f_alloc( &store
->fogcoord
, 0, tnl
->vb
.Size
, 32 );
218 _mesa_vector4f_init( &store
->input
, 0, 0 );
223 /* Now run the stage.
225 stage
->run
= run_fog_stage
;
226 return stage
->run( ctx
, stage
);
230 static void free_fog_data( struct tnl_pipeline_stage
*stage
)
232 struct fog_stage_data
*store
= FOG_STAGE_DATA(stage
);
234 _mesa_vector4f_free( &store
->fogcoord
);
236 stage
->privatePtr
= NULL
;
241 const struct tnl_pipeline_stage _tnl_fog_coordinate_stage
=
243 "build fog coordinates", /* name */
244 _NEW_FOG
, /* check_state */
245 _NEW_FOG
, /* run_state */
246 GL_FALSE
, /* active? */
248 _TNL_BIT_FOG
, /* outputs */
249 0, /* changed_inputs */
250 NULL
, /* private_data */
251 free_fog_data
, /* dtr */
252 check_fog_stage
, /* check */
253 alloc_fog_data
/* run -- initially set to init. */