From af197f5f8f182c04ab29cd3c7a18a0cb95a1d9b7 Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Mon, 18 Jun 2001 23:55:18 +0000 Subject: [PATCH] fix glDraw/CopyPixels w/ fog bug. minor fog code clean-ups. --- src/mesa/swrast/s_bitmap.c | 10 +++--- src/mesa/swrast/s_copypix.c | 47 +++++++++++++++++++++------ src/mesa/swrast/s_drawpix.c | 25 ++++++++++++--- src/mesa/swrast/s_fog.c | 78 ++++++++++++++++++++++++++++++++------------- src/mesa/swrast/s_fog.h | 6 +++- src/mesa/swrast/s_span.c | 4 ++- 6 files changed, 128 insertions(+), 42 deletions(-) diff --git a/src/mesa/swrast/s_bitmap.c b/src/mesa/swrast/s_bitmap.c index 0c4592b..17387ba 100644 --- a/src/mesa/swrast/s_bitmap.c +++ b/src/mesa/swrast/s_bitmap.c @@ -1,4 +1,4 @@ -/* $Id: s_bitmap.c,v 1.10 2001/05/30 15:22:05 brianp Exp $ */ +/* $Id: s_bitmap.c,v 1.11 2001/06/18 23:55:18 brianp Exp $ */ /* * Mesa 3-D graphics library @@ -75,10 +75,12 @@ _swrast_Bitmap( GLcontext *ctx, GLint px, GLint py, fragZ = (GLdepth) ( ctx->Current.RasterPos[2] * ctx->DepthMaxF); if (ctx->Fog.Enabled) { - if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) + if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) { fogCoord = ctx->Current.FogCoord; - else - fogCoord = ctx->Current.RasterDistance; + } + else { + fogCoord = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance); + } } else { fogCoord = 0.0; diff --git a/src/mesa/swrast/s_copypix.c b/src/mesa/swrast/s_copypix.c index f9d8497..3c7ee26 100644 --- a/src/mesa/swrast/s_copypix.c +++ b/src/mesa/swrast/s_copypix.c @@ -1,4 +1,4 @@ -/* $Id: s_copypix.c,v 1.19 2001/05/30 15:22:05 brianp Exp $ */ +/* $Id: s_copypix.c,v 1.20 2001/06/18 23:55:18 brianp Exp $ */ /* * Mesa 3-D graphics library @@ -37,6 +37,7 @@ #include "s_context.h" #include "s_depth.h" +#include "s_fog.h" #include "s_histogram.h" #include "s_pixeltex.h" #include "s_span.h" @@ -110,9 +111,16 @@ copy_conv_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy, if (ctx->Depth.Test || ctx->Fog.Enabled) { /* fill in array of z values */ GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->DepthMax); - GLfloat fog = (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) ? - ctx->Current.RasterFogCoord : ctx->Current.RasterDistance; + GLfloat fog; GLint i; + + if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) { + fog = ctx->Current.RasterFogCoord; + } + else { + fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance); + } + for (i = 0; i < width; i++) { zspan[i] = z; fogSpan[i] = fog; @@ -344,8 +352,15 @@ copy_rgba_pixels(GLcontext *ctx, GLint srcx, GLint srcy, if (ctx->Depth.Test || ctx->Fog.Enabled) { /* fill in array of z values */ GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->DepthMax); - GLfloat fog = (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) ? - ctx->Current.RasterFogCoord : ctx->Current.RasterDistance; + GLfloat fog; + + if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) { + fog = ctx->Current.RasterFogCoord; + } + else { + fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance); + } + for (i=0;iDepth.Test || ctx->Fog.Enabled) { /* fill in array of z values */ GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->DepthMax); - GLfloat fog = (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) ? - ctx->Current.RasterFogCoord : ctx->Current.RasterDistance; + GLfloat fog; + + if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) { + fog = ctx->Current.RasterFogCoord; + } + else { + fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance); + } + for (i=0;iFog.Enabled) { - GLfloat fog = (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) ? - ctx->Current.RasterFogCoord : ctx->Current.RasterDistance; + GLfloat fog; + + if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) { + fog = ctx->Current.RasterFogCoord; + } + else { + fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance); + } + for (i = 0; i < width; i++) { fogSpan[i] = fog; } diff --git a/src/mesa/swrast/s_drawpix.c b/src/mesa/swrast/s_drawpix.c index 4ab5a77..fb11af5 100644 --- a/src/mesa/swrast/s_drawpix.c +++ b/src/mesa/swrast/s_drawpix.c @@ -1,4 +1,4 @@ -/* $Id: s_drawpix.c,v 1.20 2001/05/30 15:22:05 brianp Exp $ */ +/* $Id: s_drawpix.c,v 1.21 2001/06/18 23:55:18 brianp Exp $ */ /* * Mesa 3-D graphics library @@ -37,6 +37,7 @@ #include "s_context.h" #include "s_drawpix.h" +#include "s_fog.h" #include "s_pixeltex.h" #include "s_span.h" #include "s_stencil.h" @@ -497,9 +498,16 @@ draw_index_pixels( GLcontext *ctx, GLint x, GLint y, /* Fragment depth values */ if (ctx->Depth.Test || ctx->Fog.Enabled) { GLdepth zval = (GLdepth) (ctx->Current.RasterPos[2] * ctx->DepthMaxF); - GLfloat fog = (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) ? - ctx->Current.RasterFogCoord : ctx->Current.RasterDistance; + GLfloat fog; GLint i; + + if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) { + fog = ctx->Current.RasterFogCoord; + } + else { + fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance); + } + for (i = 0; i < drawWidth; i++) { zspan[i] = zval; fogSpan[i] = fog; @@ -736,9 +744,16 @@ draw_rgba_pixels( GLcontext *ctx, GLint x, GLint y, if (ctx->Depth.Test || ctx->Fog.Enabled) { /* fill in array of z values */ GLdepth z = (GLdepth) (ctx->Current.RasterPos[2] * ctx->DepthMaxF); - GLfloat fog = (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) ? - ctx->Current.RasterFogCoord : ctx->Current.RasterDistance; + GLfloat fog; GLint i; + + if (ctx->Fog.FogCoordinateSource == GL_FOG_COORDINATE_EXT) { + fog = ctx->Current.RasterFogCoord; + } + else { + fog = _mesa_z_to_fogfactor(ctx, ctx->Current.RasterDistance); + } + for (i=0;iFog.Mode) { + case GL_LINEAR: + if (ctx->Fog.Start == ctx->Fog.End) + d = 1.0F; + else + d = 1.0F / (ctx->Fog.End - ctx->Fog.Start); + f = (ctx->Fog.End - z) * d; + return CLAMP(f, 0.0F, 1.0F); + case GL_EXP: + d = ctx->Fog.Density; + f = exp(-d * z); + return f; + case GL_EXP2: + d = ctx->Fog.Density; + f = exp(-(d * d * z * z)); + return f; + default: + _mesa_problem(ctx, "Bad fog mode in make_fog_coord"); + return 0.0; + } +} + + + /* * Apply fog to an array of RGBA pixels. * Input: n - number of pixels - * fog - array of interpolated screen-space fog coordinates in [0..1] + * fog - array of fog factors in [0,1] * red, green, blue, alpha - pixel colors * Output: red, green, blue, alpha - fogged pixel colors */ @@ -70,7 +104,7 @@ _mesa_fog_rgba_pixels( const GLcontext *ctx, /* * Apply fog to an array of color index pixels. * Input: n - number of pixels - * z - array of integer depth values + * fog - array of fog factors in [0,1] * index - pixel color indexes * Output: index - fogged pixel color indexes */ @@ -90,7 +124,7 @@ _mesa_fog_ci_pixels( const GLcontext *ctx, /* - * Calculate fog coords from window z values + * Calculate fog factors (in [0,1]) from window z values * Input: n - number of pixels * z - array of integer depth values * red, green, blue, alpha - pixel colors @@ -98,11 +132,11 @@ _mesa_fog_ci_pixels( const GLcontext *ctx, * * Use lookup table & interpolation? */ -void -_mesa_win_fog_coords_from_z( const GLcontext *ctx, - GLuint n, - const GLdepth z[], - GLfloat fogcoord[] ) +static void +compute_fog_factors_from_z( const GLcontext *ctx, + GLuint n, + const GLdepth z[], + GLfloat fogFact[] ) { const GLboolean ortho = (ctx->ProjectionMatrix.m[15] != 0.0F); const GLfloat p10 = ctx->ProjectionMatrix.m[10]; @@ -154,7 +188,7 @@ _mesa_win_fog_coords_from_z( const GLcontext *ctx, GLfloat eyez = (ndcz - p14) / p10; if (eyez < 0.0) eyez = -eyez; - fogcoord[i] = (fogEnd - eyez) * fogScale; + fogFact[i] = (fogEnd - eyez) * fogScale; } } else { @@ -164,7 +198,7 @@ _mesa_win_fog_coords_from_z( const GLcontext *ctx, GLfloat eyez = p14 / (ndcz + p10); if (eyez < 0.0) eyez = -eyez; - fogcoord[i] = (fogEnd - eyez) * fogScale; + fogFact[i] = (fogEnd - eyez) * fogScale; } } } @@ -176,7 +210,7 @@ _mesa_win_fog_coords_from_z( const GLcontext *ctx, GLfloat eyez = (ndcz - p14) / p10; if (eyez < 0.0) eyez = -eyez; - fogcoord[i] = exp( -ctx->Fog.Density * eyez ); + fogFact[i] = exp( -ctx->Fog.Density * eyez ); } } else { @@ -186,7 +220,7 @@ _mesa_win_fog_coords_from_z( const GLcontext *ctx, GLfloat eyez = p14 / (ndcz + p10); if (eyez < 0.0) eyez = -eyez; - fogcoord[i] = exp( -ctx->Fog.Density * eyez ); + fogFact[i] = exp( -ctx->Fog.Density * eyez ); } } break; @@ -203,7 +237,7 @@ _mesa_win_fog_coords_from_z( const GLcontext *ctx, if (tmp < FLT_MIN_10_EXP) tmp = FLT_MIN_10_EXP; #endif - fogcoord[i] = exp( tmp ); + fogFact[i] = exp( tmp ); } } else { @@ -217,13 +251,13 @@ _mesa_win_fog_coords_from_z( const GLcontext *ctx, if (tmp < FLT_MIN_10_EXP) tmp = FLT_MIN_10_EXP; #endif - fogcoord[i] = exp( tmp ); + fogFact[i] = exp( tmp ); } } } break; default: - _mesa_problem(ctx, "Bad fog mode in _mesa_win_fog_coords_from_z"); + _mesa_problem(ctx, "Bad fog mode in compute_fog_factors_from_z"); return; } } @@ -240,10 +274,10 @@ void _mesa_depth_fog_rgba_pixels( const GLcontext *ctx, GLuint n, const GLdepth z[], GLchan rgba[][4] ) { - GLfloat fog[PB_SIZE]; + GLfloat fogFact[PB_SIZE]; ASSERT(n <= PB_SIZE); - _mesa_win_fog_coords_from_z( ctx, n, z, fog ); - _mesa_fog_rgba_pixels( ctx, n, fog, rgba ); + compute_fog_factors_from_z( ctx, n, z, fogFact ); + _mesa_fog_rgba_pixels( ctx, n, fogFact, rgba ); } @@ -258,8 +292,8 @@ void _mesa_depth_fog_ci_pixels( const GLcontext *ctx, GLuint n, const GLdepth z[], GLuint index[] ) { - GLfloat fog[PB_SIZE]; + GLfloat fogFact[PB_SIZE]; ASSERT(n <= PB_SIZE); - _mesa_win_fog_coords_from_z( ctx, n, z, fog ); - _mesa_fog_ci_pixels( ctx, n, fog, index ); + compute_fog_factors_from_z( ctx, n, z, fogFact ); + _mesa_fog_ci_pixels( ctx, n, fogFact, index ); } diff --git a/src/mesa/swrast/s_fog.h b/src/mesa/swrast/s_fog.h index 88f9964..497583d 100644 --- a/src/mesa/swrast/s_fog.h +++ b/src/mesa/swrast/s_fog.h @@ -1,4 +1,4 @@ -/* $Id: s_fog.h,v 1.4 2001/05/03 22:13:32 brianp Exp $ */ +/* $Id: s_fog.h,v 1.5 2001/06/18 23:55:18 brianp Exp $ */ /* * Mesa 3-D graphics library @@ -33,6 +33,10 @@ #include "swrast.h" +extern GLfloat +_mesa_z_to_fogfactor(GLcontext *ctx, GLfloat z); + + extern void _mesa_fog_rgba_pixels( const GLcontext *ctx, GLuint n, const GLfloat fog[], diff --git a/src/mesa/swrast/s_span.c b/src/mesa/swrast/s_span.c index e161ff4..a3e4e70 100644 --- a/src/mesa/swrast/s_span.c +++ b/src/mesa/swrast/s_span.c @@ -1,4 +1,4 @@ -/* $Id: s_span.c,v 1.14 2001/05/15 21:30:27 brianp Exp $ */ +/* $Id: s_span.c,v 1.15 2001/06/18 23:55:18 brianp Exp $ */ /* * Mesa 3-D graphics library @@ -175,6 +175,7 @@ multi_write_index_span( GLcontext *ctx, GLuint n, GLint x, GLint y, * Input: n - number of pixels in the span * x, y - location of leftmost pixel in the span * z - array of [n] z-values + * fog - array of fog factor values in [0,1] * index - array of [n] color indexes * primitive - either GL_POINT, GL_LINE, GL_POLYGON, or GL_BITMAP */ @@ -603,6 +604,7 @@ _mesa_write_rgba_span( GLcontext *ctx, GLuint n, GLint x, GLint y, * Input: n - number of pixels in the span * x, y - location of leftmost pixel in the span * z - array of [n] z-values + * fog - array of fog factor values in [0,1] * r, g, b, a - the color of the pixels * primitive - either GL_POINT, GL_LINE, GL_POLYGON or GL_BITMAP. */ -- 2.7.4