From bfab14040b2f65e02572960f8dc4b1b346f8c151 Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Mon, 28 Apr 2014 06:10:12 +0000 Subject: [PATCH] gl_model.c (CalcSurfaceExtents): fix a lighting glitch due to floating point precision. patch from Eric Wasylishen. git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@908 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/gl_model.c | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Quake/gl_model.c b/Quake/gl_model.c index ea07d850..2163e03a 100644 --- a/Quake/gl_model.c +++ b/Quake/gl_model.c @@ -943,10 +943,26 @@ void CalcSurfaceExtents (msurface_t *s) for (j=0 ; j<2 ; j++) { - val = v->position[0] * tex->vecs[j][0] + - v->position[1] * tex->vecs[j][1] + - v->position[2] * tex->vecs[j][2] + - tex->vecs[j][3]; + /* The following calculation is sensitive to floating-point + * precision. It needs to produce the same result that the + * light compiler does, because R_BuildLightMap uses surf-> + * extents to know the width/height of a surface's lightmap, + * and incorrect rounding here manifests itself as patches + * of "corrupted" looking lightmaps. + * Most light compilers are win32 executables, so they use + * x87 floating point. This means the multiplies and adds + * are done at 80-bit precision, and the result is rounded + * down to 32-bits and stored in val. + * Adding the casts to double seems to be good enough to fix + * lighting glitches when Quakespasm is compiled as x86_64 + * and using SSE2 floating-point. A potential trouble spot + * is the hallway at the beginning of mfxsp17. -- ericw + */ + val = ((double)v->position[0] * (double)tex->vecs[j][0]) + + ((double)v->position[1] * (double)tex->vecs[j][1]) + + ((double)v->position[2] * (double)tex->vecs[j][2]) + + (double)tex->vecs[j][3]; + if (val < mins[j]) mins[j] = val; if (val > maxs[j])