1 /* $Id: fog.c,v 1.8 2000/03/07 17:40:34 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.
43 _mesa_Fogf(GLenum pname
, GLfloat param
)
45 _mesa_Fogfv(pname
, ¶m
);
50 _mesa_Fogi(GLenum pname
, GLint param
)
52 GLfloat fparam
= (GLfloat
) param
;
53 _mesa_Fogfv(pname
, &fparam
);
58 _mesa_Fogiv(GLenum pname
, const GLint
*params
)
67 p
[0] = (GLfloat
) *params
;
70 p
[0] = INT_TO_FLOAT( params
[0] );
71 p
[1] = INT_TO_FLOAT( params
[1] );
72 p
[2] = INT_TO_FLOAT( params
[2] );
73 p
[3] = INT_TO_FLOAT( params
[3] );
76 /* Error will be caught later in _mesa_Fogfv */
79 _mesa_Fogfv(pname
, p
);
84 _mesa_Fogfv( GLenum pname
, const GLfloat
*params
)
86 GET_CURRENT_CONTEXT(ctx
);
91 m
= (GLenum
) (GLint
) *params
;
92 if (m
==GL_LINEAR
|| m
==GL_EXP
|| m
==GL_EXP2
) {
96 gl_error( ctx
, GL_INVALID_ENUM
, "glFog" );
102 gl_error( ctx
, GL_INVALID_VALUE
, "glFog" );
106 ctx
->Fog
.Density
= *params
;
110 ctx
->Fog
.Start
= *params
;
113 ctx
->Fog
.End
= *params
;
116 ctx
->Fog
.Index
= *params
;
119 ctx
->Fog
.Color
[0] = params
[0];
120 ctx
->Fog
.Color
[1] = params
[1];
121 ctx
->Fog
.Color
[2] = params
[2];
122 ctx
->Fog
.Color
[3] = params
[3];
125 gl_error( ctx
, GL_INVALID_ENUM
, "glFog" );
129 if (ctx
->Driver
.Fogfv
) {
130 (*ctx
->Driver
.Fogfv
)( ctx
, pname
, params
);
133 ctx
->NewState
|= NEW_FOG
;
137 typedef void (*fog_func
)( struct vertex_buffer
*VB
, GLuint side
,
140 typedef void (*fog_coord_func
)( struct vertex_buffer
*VB
,
141 const GLvector4f
*from
,
144 static fog_func fog_ci_tab
[2];
145 static fog_func fog_rgba_tab
[2];
146 static fog_coord_func make_fog_coord_tab
[2];
149 * Compute the fogged color for an array of vertices.
150 * Input: n - number of vertices
151 * v - array of vertices
152 * color - the original vertex colors
153 * Output: color - the fogged colors
156 #define TAG(x) x##_raw
161 #define TAG(x) x##_masked
162 #define CULLCHECK if (cullmask[i]&flag)
168 _mesa_init_fog( void )
170 init_fog_tab_masked();
176 * Compute fog for the vertices in the vertex buffer.
179 _mesa_fog_vertices( struct vertex_buffer
*VB
)
181 GLcontext
*ctx
= VB
->ctx
;
182 GLuint i
= VB
->CullMode
& 1;
184 if (ctx
->Visual
->RGBAflag
) {
186 if (ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) {
187 fog_rgba_tab
[i
]( VB
, 0, VERT_FACE_FRONT
);
188 fog_rgba_tab
[i
]( VB
, 1, VERT_FACE_REAR
);
190 fog_rgba_tab
[i
]( VB
, 0, VERT_FACE_FRONT
|VERT_FACE_REAR
);
194 /* Fog color indexes */
195 if (ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) {
196 fog_ci_tab
[i
]( VB
, 0, VERT_FACE_FRONT
);
197 fog_ci_tab
[i
]( VB
, 1, VERT_FACE_REAR
);
199 fog_ci_tab
[i
]( VB
, 0, VERT_FACE_FRONT
|VERT_FACE_REAR
);
205 static void check_fog_coords( GLcontext
*ctx
, struct gl_pipeline_stage
*d
)
209 if (ctx
->FogMode
==FOG_FRAGMENT
)
211 d
->type
= PIPE_IMMEDIATE
|PIPE_PRECALC
;
212 d
->inputs
= VERT_OBJ_ANY
;
213 d
->outputs
= VERT_FOG_COORD
;
218 static void gl_make_fog_coords( struct vertex_buffer
*VB
)
220 GLcontext
*ctx
= VB
->ctx
;
222 /* If full eye coords weren't required, just calculate the eye Z
225 if (!ctx
->NeedEyeCoords
) {
226 GLfloat
*m
= ctx
->ModelView
.m
;
234 gl_dotprod_tab
[0][VB
->ObjPtr
->size
](&VB
->Eye
,
235 2, /* fill z coordinates */
240 make_fog_coord_tab
[0]( VB
, &VB
->Eye
, 0 );
244 make_fog_coord_tab
[0]( VB
, VB
->EyePtr
, 0 );
249 /* Drivers that want fog coordinates in VB->Spec[0] alpha, can substitute this
250 * stage for the default PIPE_OP_FOG pipeline stage.
252 struct gl_pipeline_stage gl_fog_coord_stage
= {
253 "build fog coordinates",
255 PIPE_PRECALC
|PIPE_IMMEDIATE
,
258 NEW_LIGHTING
|NEW_RASTER_OPS
|NEW_FOG
|NEW_MODELVIEW
,
270 * Apply fog to an array of RGBA pixels.
271 * Input: n - number of pixels
272 * z - array of integer depth values
273 * red, green, blue, alpha - pixel colors
274 * Output: red, green, blue, alpha - fogged pixel colors
277 _mesa_fog_rgba_pixels( const GLcontext
*ctx
,
278 GLuint n
, const GLdepth z
[], GLubyte rgba
[][4] )
280 GLfloat c
= ctx
->ProjectionMatrix
.m
[10];
281 GLfloat d
= ctx
->ProjectionMatrix
.m
[14];
284 GLfloat rFog
= ctx
->Fog
.Color
[0] * 255.0F
;
285 GLfloat gFog
= ctx
->Fog
.Color
[1] * 255.0F
;
286 GLfloat bFog
= ctx
->Fog
.Color
[2] * 255.0F
;
288 GLfloat tz
= ctx
->Viewport
.WindowMap
.m
[MAT_TZ
];
289 GLfloat szInv
= 1.0F
/ ctx
->Viewport
.WindowMap
.m
[MAT_SZ
];
291 switch (ctx
->Fog
.Mode
) {
294 GLfloat fogEnd
= ctx
->Fog
.End
;
295 GLfloat fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
297 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
298 GLfloat eyez
= -d
/ (c
+ndcz
);
300 if (eyez
< 0.0) eyez
= -eyez
;
301 f
= (fogEnd
- eyez
) * fogScale
;
302 f
= CLAMP( f
, 0.0F
, 1.0F
);
304 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
305 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
306 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
312 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
313 GLfloat eyez
= d
/ (c
+ndcz
);
317 f
= exp( -ctx
->Fog
.Density
* eyez
);
319 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
320 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
321 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
326 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
328 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
329 GLfloat eyez
= d
/ (c
+ndcz
);
331 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
333 /* XXX this underflow check may be needed for other systems */
334 if (tmp
< FLT_MIN_10_EXP
)
335 f
= exp( FLT_MIN_10_EXP
);
340 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
341 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
342 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
347 gl_problem(ctx
, "Bad fog mode in _mesa_fog_rgba_pixels");
356 * Apply fog to an array of color index pixels.
357 * Input: n - number of pixels
358 * z - array of integer depth values
359 * index - pixel color indexes
360 * Output: index - fogged pixel color indexes
363 _mesa_fog_ci_pixels( const GLcontext
*ctx
,
364 GLuint n
, const GLdepth z
[], GLuint index
[] )
366 GLfloat c
= ctx
->ProjectionMatrix
.m
[10];
367 GLfloat d
= ctx
->ProjectionMatrix
.m
[14];
370 GLfloat tz
= ctx
->Viewport
.WindowMap
.m
[MAT_TZ
];
371 GLfloat szInv
= 1.0F
/ ctx
->Viewport
.WindowMap
.m
[MAT_SZ
];
373 switch (ctx
->Fog
.Mode
) {
376 GLfloat fogEnd
= ctx
->Fog
.End
;
377 GLfloat fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
379 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
380 GLfloat eyez
= -d
/ (c
+ndcz
);
382 if (eyez
< 0.0) eyez
= -eyez
;
383 f
= (fogEnd
- eyez
) * fogScale
;
384 f
= CLAMP( f
, 0.0F
, 1.0F
);
385 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
391 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
392 GLfloat eyez
= -d
/ (c
+ndcz
);
396 f
= exp( -ctx
->Fog
.Density
* eyez
);
397 f
= CLAMP( f
, 0.0F
, 1.0F
);
398 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
403 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
405 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
406 GLfloat eyez
= -d
/ (c
+ndcz
);
410 tmp
= negDensitySquared
* eyez
* eyez
;
412 /* XXX this underflow check may be needed for other systems */
413 if (tmp
< FLT_MIN_10_EXP
)
414 f
= exp( FLT_MIN_10_EXP
);
418 f
= CLAMP( f
, 0.0F
, 1.0F
);
419 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
424 gl_problem(ctx
, "Bad fog mode in _mesa_fog_ci_pixels");