1 /* $Id: fog.c,v 1.1 1999/08/19 00:55:41 jtg 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.
39 #include "GL/xf86glx.h"
45 void gl_Fogfv( GLcontext
*ctx
, GLenum pname
, const GLfloat
*params
)
51 m
= (GLenum
) (GLint
) *params
;
52 if (m
==GL_LINEAR
|| m
==GL_EXP
|| m
==GL_EXP2
) {
56 gl_error( ctx
, GL_INVALID_ENUM
, "glFog" );
62 gl_error( ctx
, GL_INVALID_VALUE
, "glFog" );
66 ctx
->Fog
.Density
= *params
;
71 /* Prior to OpenGL 1.1, this was an error */
73 gl_error( ctx
, GL_INVALID_VALUE
, "glFog(GL_FOG_START)" );
77 ctx
->Fog
.Start
= *params
;
81 /* Prior to OpenGL 1.1, this was an error */
83 gl_error( ctx
, GL_INVALID_VALUE
, "glFog(GL_FOG_END)" );
87 ctx
->Fog
.End
= *params
;
90 ctx
->Fog
.Index
= *params
;
93 ctx
->Fog
.Color
[0] = params
[0];
94 ctx
->Fog
.Color
[1] = params
[1];
95 ctx
->Fog
.Color
[2] = params
[2];
96 ctx
->Fog
.Color
[3] = params
[3];
99 gl_error( ctx
, GL_INVALID_ENUM
, "glFog" );
103 if (ctx
->Driver
.Fogfv
) {
104 (*ctx
->Driver
.Fogfv
)( ctx
, pname
, params
);
107 ctx
->NewState
|= NEW_FOG
;
111 typedef void (*fog_func
)( struct vertex_buffer
*VB
, GLuint side
,
115 static fog_func fog_ci_tab
[2];
116 static fog_func fog_rgba_tab
[2];
119 * Compute the fogged color for an array of vertices.
120 * Input: n - number of vertices
121 * v - array of vertices
122 * color - the original vertex colors
123 * Output: color - the fogged colors
126 #define TAG(x) x##_raw
131 #define TAG(x) x##_masked
132 #define CULLCHECK if (cullmask[i]&flag)
136 void gl_init_fog( void )
138 init_fog_tab_masked();
143 * Compute fog for the vertices in the vertex buffer.
145 void gl_fog_vertices( struct vertex_buffer
*VB
)
147 GLcontext
*ctx
= VB
->ctx
;
148 GLuint i
= VB
->CullMode
& 1;
150 if (ctx
->Visual
->RGBAflag
) {
152 if (ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) {
153 fog_rgba_tab
[i
]( VB
, 0, VERT_FACE_FRONT
);
154 fog_rgba_tab
[i
]( VB
, 1, VERT_FACE_REAR
);
156 fog_rgba_tab
[i
]( VB
, 0, VERT_FACE_FRONT
|VERT_FACE_REAR
);
160 /* Fog color indexes */
161 if (ctx
->TriangleCaps
& DD_TRI_LIGHT_TWOSIDE
) {
162 fog_ci_tab
[i
]( VB
, 0, VERT_FACE_FRONT
);
163 fog_ci_tab
[i
]( VB
, 1, VERT_FACE_REAR
);
165 fog_ci_tab
[i
]( VB
, 0, VERT_FACE_FRONT
|VERT_FACE_REAR
);
171 * Apply fog to an array of RGBA pixels.
172 * Input: n - number of pixels
173 * z - array of integer depth values
174 * red, green, blue, alpha - pixel colors
175 * Output: red, green, blue, alpha - fogged pixel colors
177 void gl_fog_rgba_pixels( const GLcontext
*ctx
,
178 GLuint n
, const GLdepth z
[], GLubyte rgba
[][4] )
180 GLfloat c
= ctx
->ProjectionMatrix
.m
[10];
181 GLfloat d
= ctx
->ProjectionMatrix
.m
[14];
184 GLfloat rFog
= ctx
->Fog
.Color
[0] * 255.0F
;
185 GLfloat gFog
= ctx
->Fog
.Color
[1] * 255.0F
;
186 GLfloat bFog
= ctx
->Fog
.Color
[2] * 255.0F
;
188 GLfloat tz
= ctx
->Viewport
.WindowMap
.m
[MAT_TZ
];
189 GLfloat szInv
= 1.0F
/ ctx
->Viewport
.WindowMap
.m
[MAT_SZ
];
191 switch (ctx
->Fog
.Mode
) {
194 GLfloat fogEnd
= ctx
->Fog
.End
;
195 GLfloat fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
197 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
198 GLfloat eyez
= -d
/ (c
+ndcz
);
200 if (eyez
< 0.0) eyez
= -eyez
;
201 f
= (fogEnd
- eyez
) * fogScale
;
202 f
= CLAMP( f
, 0.0F
, 1.0F
);
204 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
205 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
206 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
212 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
213 GLfloat eyez
= d
/ (c
+ndcz
);
217 f
= exp( -ctx
->Fog
.Density
* eyez
);
219 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
220 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
221 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
226 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
228 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
229 GLfloat eyez
= d
/ (c
+ndcz
);
231 GLfloat tmp
= negDensitySquared
* eyez
* eyez
;
233 /* XXX this underflow check may be needed for other systems */
234 if (tmp
< FLT_MIN_10_EXP
)
235 f
= exp( FLT_MIN_10_EXP
);
240 rgba
[i
][RCOMP
] = (GLint
) (f
* rgba
[i
][RCOMP
] + g
* rFog
);
241 rgba
[i
][GCOMP
] = (GLint
) (f
* rgba
[i
][GCOMP
] + g
* gFog
);
242 rgba
[i
][BCOMP
] = (GLint
) (f
* rgba
[i
][BCOMP
] + g
* bFog
);
247 gl_problem(ctx
, "Bad fog mode in gl_fog_rgba_pixels");
256 * Apply fog to an array of color index pixels.
257 * Input: n - number of pixels
258 * z - array of integer depth values
259 * index - pixel color indexes
260 * Output: index - fogged pixel color indexes
262 void gl_fog_ci_pixels( const GLcontext
*ctx
,
263 GLuint n
, const GLdepth z
[], GLuint index
[] )
265 GLfloat c
= ctx
->ProjectionMatrix
.m
[10];
266 GLfloat d
= ctx
->ProjectionMatrix
.m
[14];
269 GLfloat tz
= ctx
->Viewport
.WindowMap
.m
[MAT_TZ
];
270 GLfloat szInv
= 1.0F
/ ctx
->Viewport
.WindowMap
.m
[MAT_SZ
];
272 switch (ctx
->Fog
.Mode
) {
275 GLfloat fogEnd
= ctx
->Fog
.End
;
276 GLfloat fogScale
= 1.0F
/ (ctx
->Fog
.End
- ctx
->Fog
.Start
);
278 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
279 GLfloat eyez
= -d
/ (c
+ndcz
);
281 if (eyez
< 0.0) eyez
= -eyez
;
282 f
= (fogEnd
- eyez
) * fogScale
;
283 f
= CLAMP( f
, 0.0F
, 1.0F
);
284 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
290 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
291 GLfloat eyez
= -d
/ (c
+ndcz
);
295 f
= exp( -ctx
->Fog
.Density
* eyez
);
296 f
= CLAMP( f
, 0.0F
, 1.0F
);
297 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
302 GLfloat negDensitySquared
= -ctx
->Fog
.Density
* ctx
->Fog
.Density
;
304 GLfloat ndcz
= ((GLfloat
) z
[i
] - tz
) * szInv
;
305 GLfloat eyez
= -d
/ (c
+ndcz
);
309 tmp
= negDensitySquared
* eyez
* eyez
;
311 /* XXX this underflow check may be needed for other systems */
312 if (tmp
< FLT_MIN_10_EXP
)
313 f
= exp( FLT_MIN_10_EXP
);
317 f
= CLAMP( f
, 0.0F
, 1.0F
);
318 index
[i
] = (GLuint
) ((GLfloat
) index
[i
] + (1.0F
-f
) * ctx
->Fog
.Index
);
323 gl_problem(ctx
, "Bad fog mode in gl_fog_ci_pixels");