1 /* $Id: fog.c,v 1.3 1999/11/08 07:36:44 brianp Exp $ */
4 * Mesa 3-D graphics library
7 * Copyright (C) 1999 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.
28 /* $XFree86: xc/lib/GL/mesa/src/fog.c,v 1.4 1999/04/04 00:20:24 dawes Exp $ */
37 #include "GL/xf86glx.h"
48 void gl_Fogfv( GLcontext
*ctx
, GLenum pname
, const GLfloat
*params
)
54 m
= (GLenum
) (GLint
) *params
;
55 if (m
==GL_LINEAR
|| m
==GL_EXP
|| m
==GL_EXP2
) {
59 gl_error( ctx
, GL_INVALID_ENUM
, "glFog" );
65 gl_error( ctx
, GL_INVALID_VALUE
, "glFog" );
69 ctx
->Fog
.Density
= *params
;
74 /* Prior to OpenGL 1.1, this was an error */
76 gl_error( ctx
, GL_INVALID_VALUE
, "glFog(GL_FOG_START)" );
80 ctx
->Fog
.Start
= *params
;
84 /* Prior to OpenGL 1.1, this was an error */
86 gl_error( ctx
, GL_INVALID_VALUE
, "glFog(GL_FOG_END)" );
90 ctx
->Fog
.End
= *params
;
93 ctx
->Fog
.Index
= *params
;
96 ctx
->Fog
.Color
[0] = params
[0];
97 ctx
->Fog
.Color
[1] = params
[1];
98 ctx
->Fog
.Color
[2] = params
[2];
99 ctx
->Fog
.Color
[3] = params
[3];
102 gl_error( ctx
, GL_INVALID_ENUM
, "glFog" );
106 if (ctx
->Driver
.Fogfv
) {
107 (*ctx
->Driver
.Fogfv
)( ctx
, pname
, params
);
110 ctx
->NewState
|= NEW_FOG
;
114 typedef void (*fog_func
)( struct vertex_buffer
*VB
, GLuint side
,
118 static fog_func fog_ci_tab
[2];
119 static fog_func fog_rgba_tab
[2];
122 * Compute the fogged color for an array of vertices.
123 * Input: n - number of vertices
124 * v - array of vertices
125 * color - the original vertex colors
126 * Output: color - the fogged colors
129 #define TAG(x) x##_raw
134 #define TAG(x) x##_masked
135 #define CULLCHECK if (cullmask[i]&flag)
139 void gl_init_fog( void )
141 init_fog_tab_masked();
146 * Compute fog for the vertices in the vertex buffer.
148 void gl_fog_vertices( struct vertex_buffer
*VB
)
150 GLcontext
*ctx
= VB
->ctx
;
151 GLuint i
= VB
->CullMode
& 1;
153 if (ctx
->Visual
->RGBAflag
) {
155 if (ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) {
156 fog_rgba_tab
[i
]( VB
, 0, VERT_FACE_FRONT
);
157 fog_rgba_tab
[i
]( VB
, 1, VERT_FACE_REAR
);
159 fog_rgba_tab
[i
]( VB
, 0, VERT_FACE_FRONT
|VERT_FACE_REAR
);
163 /* Fog color indexes */
164 if (ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) {
165 fog_ci_tab
[i
]( VB
, 0, VERT_FACE_FRONT
);
166 fog_ci_tab
[i
]( VB
, 1, VERT_FACE_REAR
);
168 fog_ci_tab
[i
]( VB
, 0, VERT_FACE_FRONT
|VERT_FACE_REAR
);
174 * Apply fog to an array of RGBA pixels.
175 * Input: n - number of pixels
176 * z - array of integer depth values
177 * red, green, blue, alpha - pixel colors
178 * Output: red, green, blue, alpha - fogged pixel colors
180 void gl_fog_rgba_pixels( const GLcontext
*ctx
,
181 GLuint n
, const GLdepth z
[], GLubyte rgba
[][4] )
183 GLfloat c
= ctx
->ProjectionMatrix
.m
[10];
184 GLfloat d
= ctx
->ProjectionMatrix
.m
[14];
187 GLfloat rFog
= ctx
->Fog
.Color
[0] * 255.0F
;
188 GLfloat gFog
= ctx
->Fog
.Color
[1] * 255.0F
;
189 GLfloat bFog
= ctx
->Fog
.Color
[2] * 255.0F
;
191 GLfloat tz
= ctx
->Viewport
.WindowMap
.m
[MAT_TZ
];
192 GLfloat szInv
= 1.0F
/ ctx
->Viewport
.WindowMap
.m
[MAT_SZ
];
194 switch (ctx
->Fog
.Mode
) {
197 GLfloat fogEnd
= ctx
->Fog
.End
;
198 GLfloat fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
200 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
201 GLfloat eyez
= -d
/ (c
+ndcz
);
203 if (eyez
< 0.0) eyez
= -eyez
;
204 f
= (fogEnd
- eyez
) * fogScale
;
205 f
= CLAMP( f
, 0.0F
, 1.0F
);
207 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
208 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
209 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
215 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
216 GLfloat eyez
= d
/ (c
+ndcz
);
220 f
= exp( -ctx
->Fog
.Density
* eyez
);
222 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
223 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
224 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
229 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
231 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
232 GLfloat eyez
= d
/ (c
+ndcz
);
234 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
236 /* XXX this underflow check may be needed for other systems */
237 if (tmp
< FLT_MIN_10_EXP
)
238 f
= exp( FLT_MIN_10_EXP
);
243 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
244 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
245 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
250 gl_problem(ctx
, "Bad fog mode in gl_fog_rgba_pixels");
259 * Apply fog to an array of color index pixels.
260 * Input: n - number of pixels
261 * z - array of integer depth values
262 * index - pixel color indexes
263 * Output: index - fogged pixel color indexes
265 void gl_fog_ci_pixels( const GLcontext
*ctx
,
266 GLuint n
, const GLdepth z
[], GLuint index
[] )
268 GLfloat c
= ctx
->ProjectionMatrix
.m
[10];
269 GLfloat d
= ctx
->ProjectionMatrix
.m
[14];
272 GLfloat tz
= ctx
->Viewport
.WindowMap
.m
[MAT_TZ
];
273 GLfloat szInv
= 1.0F
/ ctx
->Viewport
.WindowMap
.m
[MAT_SZ
];
275 switch (ctx
->Fog
.Mode
) {
278 GLfloat fogEnd
= ctx
->Fog
.End
;
279 GLfloat fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
281 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
282 GLfloat eyez
= -d
/ (c
+ndcz
);
284 if (eyez
< 0.0) eyez
= -eyez
;
285 f
= (fogEnd
- eyez
) * fogScale
;
286 f
= CLAMP( f
, 0.0F
, 1.0F
);
287 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
293 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
294 GLfloat eyez
= -d
/ (c
+ndcz
);
298 f
= exp( -ctx
->Fog
.Density
* eyez
);
299 f
= CLAMP( f
, 0.0F
, 1.0F
);
300 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
305 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
307 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
308 GLfloat eyez
= -d
/ (c
+ndcz
);
312 tmp
= negDensitySquared
* eyez
* eyez
;
314 /* XXX this underflow check may be needed for other systems */
315 if (tmp
< FLT_MIN_10_EXP
)
316 f
= exp( FLT_MIN_10_EXP
);
320 f
= CLAMP( f
, 0.0F
, 1.0F
);
321 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
326 gl_problem(ctx
, "Bad fog mode in gl_fog_ci_pixels");