1 /* $Id: fog.c,v 1.5 2000/02/02 22:21:39 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.
42 _mesa_Fogf(GLenum pname
, GLfloat param
)
44 _mesa_Fogfv(pname
, ¶m
);
49 _mesa_Fogi(GLenum pname
, GLint param
)
51 GLfloat fparam
= (GLfloat
) param
;
52 _mesa_Fogfv(pname
, &fparam
);
57 _mesa_Fogiv(GLenum pname
, const GLint
*params
)
66 p
[0] = (GLfloat
) *params
;
69 p
[0] = INT_TO_FLOAT( params
[0] );
70 p
[1] = INT_TO_FLOAT( params
[1] );
71 p
[2] = INT_TO_FLOAT( params
[2] );
72 p
[3] = INT_TO_FLOAT( params
[3] );
75 /* Error will be caught later in gl_Fogfv */
78 _mesa_Fogfv(pname
, p
);
83 _mesa_Fogfv( GLenum pname
, const GLfloat
*params
)
85 GET_CURRENT_CONTEXT(ctx
);
90 m
= (GLenum
) (GLint
) *params
;
91 if (m
==GL_LINEAR
|| m
==GL_EXP
|| m
==GL_EXP2
) {
95 gl_error( ctx
, GL_INVALID_ENUM
, "glFog" );
101 gl_error( ctx
, GL_INVALID_VALUE
, "glFog" );
105 ctx
->Fog
.Density
= *params
;
110 /* Prior to OpenGL 1.1, this was an error */
112 gl_error( ctx
, GL_INVALID_VALUE
, "glFog(GL_FOG_START)" );
116 ctx
->Fog
.Start
= *params
;
120 /* Prior to OpenGL 1.1, this was an error */
122 gl_error( ctx
, GL_INVALID_VALUE
, "glFog(GL_FOG_END)" );
126 ctx
->Fog
.End
= *params
;
129 ctx
->Fog
.Index
= *params
;
132 ctx
->Fog
.Color
[0] = params
[0];
133 ctx
->Fog
.Color
[1] = params
[1];
134 ctx
->Fog
.Color
[2] = params
[2];
135 ctx
->Fog
.Color
[3] = params
[3];
138 gl_error( ctx
, GL_INVALID_ENUM
, "glFog" );
142 if (ctx
->Driver
.Fogfv
) {
143 (*ctx
->Driver
.Fogfv
)( ctx
, pname
, params
);
146 ctx
->NewState
|= NEW_FOG
;
150 typedef void (*fog_func
)( struct vertex_buffer
*VB
, GLuint side
,
154 static fog_func fog_ci_tab
[2];
155 static fog_func fog_rgba_tab
[2];
158 * Compute the fogged color for an array of vertices.
159 * Input: n - number of vertices
160 * v - array of vertices
161 * color - the original vertex colors
162 * Output: color - the fogged colors
165 #define TAG(x) x##_raw
170 #define TAG(x) x##_masked
171 #define CULLCHECK if (cullmask[i]&flag)
177 _mesa_init_fog( void )
179 init_fog_tab_masked();
185 * Compute fog for the vertices in the vertex buffer.
188 _mesa_fog_vertices( struct vertex_buffer
*VB
)
190 GLcontext
*ctx
= VB
->ctx
;
191 GLuint i
= VB
->CullMode
& 1;
193 if (ctx
->Visual
->RGBAflag
) {
195 if (ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) {
196 fog_rgba_tab
[i
]( VB
, 0, VERT_FACE_FRONT
);
197 fog_rgba_tab
[i
]( VB
, 1, VERT_FACE_REAR
);
199 fog_rgba_tab
[i
]( VB
, 0, VERT_FACE_FRONT
|VERT_FACE_REAR
);
203 /* Fog color indexes */
204 if (ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) {
205 fog_ci_tab
[i
]( VB
, 0, VERT_FACE_FRONT
);
206 fog_ci_tab
[i
]( VB
, 1, VERT_FACE_REAR
);
208 fog_ci_tab
[i
]( VB
, 0, VERT_FACE_FRONT
|VERT_FACE_REAR
);
214 * Apply fog to an array of RGBA pixels.
215 * Input: n - number of pixels
216 * z - array of integer depth values
217 * red, green, blue, alpha - pixel colors
218 * Output: red, green, blue, alpha - fogged pixel colors
221 _mesa_fog_rgba_pixels( const GLcontext
*ctx
,
222 GLuint n
, const GLdepth z
[], GLubyte rgba
[][4] )
224 GLfloat c
= ctx
->ProjectionMatrix
.m
[10];
225 GLfloat d
= ctx
->ProjectionMatrix
.m
[14];
228 GLfloat rFog
= ctx
->Fog
.Color
[0] * 255.0F
;
229 GLfloat gFog
= ctx
->Fog
.Color
[1] * 255.0F
;
230 GLfloat bFog
= ctx
->Fog
.Color
[2] * 255.0F
;
232 GLfloat tz
= ctx
->Viewport
.WindowMap
.m
[MAT_TZ
];
233 GLfloat szInv
= 1.0F
/ ctx
->Viewport
.WindowMap
.m
[MAT_SZ
];
235 switch (ctx
->Fog
.Mode
) {
238 GLfloat fogEnd
= ctx
->Fog
.End
;
239 GLfloat fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
241 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
242 GLfloat eyez
= -d
/ (c
+ndcz
);
244 if (eyez
< 0.0) eyez
= -eyez
;
245 f
= (fogEnd
- eyez
) * fogScale
;
246 f
= CLAMP( f
, 0.0F
, 1.0F
);
248 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
249 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
250 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
256 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
257 GLfloat eyez
= d
/ (c
+ndcz
);
261 f
= exp( -ctx
->Fog
.Density
* eyez
);
263 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
264 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
265 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
270 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
272 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
273 GLfloat eyez
= d
/ (c
+ndcz
);
275 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
277 /* XXX this underflow check may be needed for other systems */
278 if (tmp
< FLT_MIN_10_EXP
)
279 f
= exp( FLT_MIN_10_EXP
);
284 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
285 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
286 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
291 gl_problem(ctx
, "Bad fog mode in gl_fog_rgba_pixels");
300 * Apply fog to an array of color index pixels.
301 * Input: n - number of pixels
302 * z - array of integer depth values
303 * index - pixel color indexes
304 * Output: index - fogged pixel color indexes
307 _mesa_fog_ci_pixels( const GLcontext
*ctx
,
308 GLuint n
, const GLdepth z
[], GLuint index
[] )
310 GLfloat c
= ctx
->ProjectionMatrix
.m
[10];
311 GLfloat d
= ctx
->ProjectionMatrix
.m
[14];
314 GLfloat tz
= ctx
->Viewport
.WindowMap
.m
[MAT_TZ
];
315 GLfloat szInv
= 1.0F
/ ctx
->Viewport
.WindowMap
.m
[MAT_SZ
];
317 switch (ctx
->Fog
.Mode
) {
320 GLfloat fogEnd
= ctx
->Fog
.End
;
321 GLfloat fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
323 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
324 GLfloat eyez
= -d
/ (c
+ndcz
);
326 if (eyez
< 0.0) eyez
= -eyez
;
327 f
= (fogEnd
- eyez
) * fogScale
;
328 f
= CLAMP( f
, 0.0F
, 1.0F
);
329 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
335 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
336 GLfloat eyez
= -d
/ (c
+ndcz
);
340 f
= exp( -ctx
->Fog
.Density
* eyez
);
341 f
= CLAMP( f
, 0.0F
, 1.0F
);
342 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
347 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
349 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
350 GLfloat eyez
= -d
/ (c
+ndcz
);
354 tmp
= negDensitySquared
* eyez
* eyez
;
356 /* XXX this underflow check may be needed for other systems */
357 if (tmp
< FLT_MIN_10_EXP
)
358 f
= exp( FLT_MIN_10_EXP
);
362 f
= CLAMP( f
, 0.0F
, 1.0F
);
363 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
368 gl_problem(ctx
, "Bad fog mode in gl_fog_ci_pixels");