Skip to content

Commit 1b416fb

Browse files
committed
Fix moronic light grid array size calculation
First it calculated the number of points along the negative and positive axes of a dimension of the light grid. Then, instead of just adding those two together to determine the total number of points on the axis, it calculated the coordinates of the minimum and maximum points, subtracted them, and divided by the cell size to get the number of points on the axis. This division was floored, not rounded, so if the difference in coordinates had an error of -0.000001 it would get the wrong answer. Surprising that this didn't break earlier. Fixes #1518.
1 parent 98b3dbb commit 1b416fb

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

src/engine/renderer/tr_bsp.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4026,7 +4026,6 @@ void R_LoadLightGrid( lump_t *l )
40264026
}
40274027

40284028
int i, j, k;
4029-
vec3_t maxs;
40304029
world_t *w;
40314030
float *wMins, *wMaxs;
40324031
dgridPoint_t *in;
@@ -4065,9 +4064,20 @@ void R_LoadLightGrid( lump_t *l )
40654064

40664065
for ( i = 0; i < 3; i++ )
40674066
{
4068-
w->lightGridOrigin[ i ] = w->lightGridSize[ i ] * ceil( wMins[ i ] / w->lightGridSize[ i ] );
4069-
maxs[ i ] = w->lightGridSize[ i ] * floor( wMaxs[ i ] / w->lightGridSize[ i ] );
4070-
w->lightGridBounds[ i ] = ( maxs[ i ] - w->lightGridOrigin[ i ] ) / w->lightGridSize[ i ] + 1;
4067+
float numNegativePoints = ceil( wMins[ i ] / w->lightGridSize[ i ] );
4068+
float numPositivePoints = floor( wMaxs[ i ] / w->lightGridSize[ i ] );
4069+
w->lightGridBounds[ i ] = static_cast<int>( numPositivePoints - numNegativePoints ) + 1;
4070+
4071+
if ( w->lightGridBounds[ i ] <= 0 || w->lightGridBounds[ i ] > 5000 )
4072+
{
4073+
// sanity check to avoid integer overflows etc.
4074+
Log::Warn( "invalid light grid parameters, default light grid used" );
4075+
const byte color[ 3 ]{ 64, 64, 64 };
4076+
R_SetConstantColorLightGrid( color );
4077+
return;
4078+
}
4079+
4080+
w->lightGridOrigin[ i ] = w->lightGridSize[ i ] * numNegativePoints;
40714081
}
40724082

40734083
VectorMA( w->lightGridOrigin, -0.5f, w->lightGridSize,

0 commit comments

Comments
 (0)