From 30971cd098d147a4363df0dec0c338587dc1478f Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Wed, 15 Nov 2000 00:26:01 +0000 Subject: rewrite of _mesa_win_fog_coords_from_z() so that both perspective and orthographic projection are handled correctly --- src/mesa/swrast/s_fog.c | 109 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 85 insertions(+), 24 deletions(-) (limited to 'src/mesa') diff --git a/src/mesa/swrast/s_fog.c b/src/mesa/swrast/s_fog.c index 4cfea675d6..dfdc1821f2 100644 --- a/src/mesa/swrast/s_fog.c +++ b/src/mesa/swrast/s_fog.c @@ -1,4 +1,4 @@ -/* $Id: s_fog.c,v 1.2 2000/11/05 18:24:40 keithw Exp $ */ +/* $Id: s_fog.c,v 1.3 2000/11/15 00:26:01 brianp Exp $ */ /* * Mesa 3-D graphics library @@ -102,12 +102,35 @@ _mesa_win_fog_coords_from_z( const GLcontext *ctx, const GLdepth z[], GLfixed fogcoord[] ) { - GLfloat c = ctx->ProjectionMatrix.m[10]; - GLfloat d = ctx->ProjectionMatrix.m[14]; + const GLboolean ortho = (ctx->ProjectionMatrix.m[15] != 0.0F); + const GLfloat p10 = ctx->ProjectionMatrix.m[10]; + const GLfloat p14 = ctx->ProjectionMatrix.m[14]; + const GLfloat tz = ctx->Viewport._WindowMap.m[MAT_TZ]; + const GLfloat szInv = 1.0F / ctx->Viewport._WindowMap.m[MAT_SZ]; GLuint i; - GLfloat tz = ctx->Viewport._WindowMap.m[MAT_TZ]; - GLfloat szInv = 1.0F / ctx->Viewport._WindowMap.m[MAT_SZ]; + /* + * Note: to compute eyeZ from the ndcZ we have to solve the following: + * + * p[10] * eyeZ + p[14] * eyeW + * ndcZ = --------------------------- + * p[11] * eyeZ + p[15] * eyeW + * + * Thus: + * + * p[14] * eyeW - p[15] * eyeW * ndcZ + * eyeZ = ---------------------------------- + * p[11] * ndcZ - p[10] + * + * If we note: + * a) if using an orthographic projection, p[11] = 0 and p[15] = 1. + * b) if using a perspective projection, p[11] = -1 and p[15] = 0. + * c) we assume eyeW = 1 (not always true- glVertex4) + * + * Then we can simplify the calculation of eyeZ quite a bit. We do + * separate calculations for the orthographic and perspective cases below. + * Note that we drop a negative sign or two since they don't matter. + */ switch (ctx->Fog.Mode) { case GL_LINEAR: @@ -115,35 +138,73 @@ _mesa_win_fog_coords_from_z( const GLcontext *ctx, GLfloat fogEnd = ctx->Fog.End; GLfloat fogScale = (GLfloat) FIXED_ONE / (ctx->Fog.End - ctx->Fog.Start); - for (i=0;iFog.Density * eyez )); - } + if (ortho) { + for (i=0;iFog.Density * eyez )); + } + } + else { + /* perspective */ + for (i=0;iFog.Density * eyez )); + } + } break; case GL_EXP2: { GLfloat negDensitySquared = -ctx->Fog.Density * ctx->Fog.Density; - for (i=0;i