Skip to content

Commit 0e1b009

Browse files
committed
Make models brighter with deluxe mapping off
The previous commit fixed excessive brightness with grid lighting when deluxe mapping is disabled by using the average expected brightness (over all directions) instead of the maximum, but it went too far and made things dim. Boost the light a bit since light directions are positively correlated with normals in practice.
1 parent b6dc4ae commit 0e1b009

File tree

6 files changed

+29
-16
lines changed

6 files changed

+29
-16
lines changed

src/engine/renderer/gl_shader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@ static std::string GenEngineConstants() {
687687

688688
AddDefine( str, "r_tileStep", glState.tileStep[0], glState.tileStep[1] );
689689

690+
AddDefine( str, "LIGHTGRID_AVERAGE_COSINE", tr.lightGridAverageCosine );
691+
690692
if ( glConfig.realtimeLighting )
691693
{
692694
AddDefine( str, "r_realtimeLighting", 1 );

src/engine/renderer/glsl_source/computeLight_fp.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ vec4 EnvironmentalSpecularFactor( vec3 viewDir, vec3 normal )
5151

5252
#if defined(USE_GRID_LIGHTING) || defined(USE_GRID_DELUXE_MAPPING)
5353
void ReadLightGrid( in vec4 texel1, in vec4 texel2, in float lightFactor, out vec3 lightDir, out vec3 ambientColor, out vec3 lightColor ) {
54-
vec3 totalColor = /*LIGHTGRID_MAX_LIGHT*/ 1.25 * texel1.rgb;
54+
vec3 totalColor = (1.0 + LIGHTGRID_AVERAGE_COSINE) * texel1.rgb;
5555
float total1Norm = totalColor.r + totalColor.g + totalColor.b;
5656
vec3 scaledLightDir = 2.0 * (texel2.xyz - (128.0 / 255.0));
5757
float directed1Norm = 3.0 * length(scaledLightDir);
58-
float directedFraction = clamp((0.25 * directed1Norm) / total1Norm, 0.0, 1.0);
59-
float directedScale = 4.0 * directedFraction;
58+
float directedFraction = clamp((LIGHTGRID_AVERAGE_COSINE * directed1Norm) / total1Norm, 0.0, 1.0);
59+
float directedScale = directedFraction / LIGHTGRID_AVERAGE_COSINE;
6060
float ambientScale = 1.0 - directedFraction;
6161
ambientColor = ambientScale * totalColor;
6262
lightColor = directedScale * totalColor;

src/engine/renderer/tr_backend.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,11 +2227,11 @@ static void RB_RenderDebugUtils()
22272227
lightDir[ 1 ] = snorm8ToFloat( gp2->direction[ 1 ] - 128 );
22282228
lightDir[ 2 ] = snorm8ToFloat( gp2->direction[ 2 ] - 128 );
22292229
Color::Color totalColor = Color::Adapt( gp1->color );
2230-
totalColor *= LIGHTGRID_MAX_LIGHT;
2230+
totalColor *= 1.0f + tr.lightGridAverageCosine;
22312231
float total1Norm = totalColor.Red() + totalColor.Green() + totalColor.Blue();
22322232
float directed1Norm = 3.0f * VectorLength( lightDir );
2233-
float directedFraction = ( 0.25 * directed1Norm ) / total1Norm;
2234-
float directedScale = 4.0 * directedFraction;
2233+
float directedFraction = ( tr.lightGridAverageCosine * directed1Norm ) / total1Norm;
2234+
float directedScale = directedFraction / tr.lightGridAverageCosine;
22352235
float ambientScale = 1.0 - directedFraction;
22362236
Color::Color ambientColor = totalColor * ambientScale;
22372237
Color::Color directedColor = totalColor * directedScale;

src/engine/renderer/tr_bsp.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3636,13 +3636,11 @@ void R_LoadLightGrid( lump_t *l )
36363636
direction[ 2 ] = cosf( lat );
36373637

36383638
// Separate ambient and directed colors are not implemented, so this is the total average light
3639-
// (averaged over all direction vectors): ambient + 0.25 * directed. See TraceGrid in light.c in
3640-
// q3map2 for more information about this equation. Though if to take
3641-
// half-Lambert lighting's cosine modification into account, the 1/4 factor should be replaced
3642-
// by 1/3. The result is scaled down to fit in [0, 1].
3643-
gridPoint1->color[ 0 ] = floatToUnorm8( ( ambientColor[ 0 ] + 0.25f * directedColor[ 0 ] ) / LIGHTGRID_MAX_LIGHT );
3644-
gridPoint1->color[ 1 ] = floatToUnorm8( ( ambientColor[ 1 ] + 0.25f * directedColor[ 1 ] ) / LIGHTGRID_MAX_LIGHT );
3645-
gridPoint1->color[ 2 ] = floatToUnorm8( ( ambientColor[ 2 ] + 0.25f * directedColor[ 2 ] ) / LIGHTGRID_MAX_LIGHT );
3639+
// (averaged over all direction vectors). The result is scaled down to fit in [0, 1].
3640+
float colorScale = 1.0f / ( 1.0f + tr.lightGridAverageCosine );
3641+
gridPoint1->color[ 0 ] = floatToUnorm8( ( ambientColor[ 0 ] + tr.lightGridAverageCosine * directedColor[ 0 ] ) * colorScale );
3642+
gridPoint1->color[ 1 ] = floatToUnorm8( ( ambientColor[ 1 ] + tr.lightGridAverageCosine * directedColor[ 1 ] ) * colorScale );
3643+
gridPoint1->color[ 2 ] = floatToUnorm8( ( ambientColor[ 2 ] + tr.lightGridAverageCosine * directedColor[ 2 ] ) * colorScale );
36463644
gridPoint1->unused = 255;
36473645

36483646
// The length of the direction vector is used to determine how much directed light there is.

src/engine/renderer/tr_init.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,21 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
13501350
}
13511351
}
13521352

1353+
if ( glConfig.deluxeMapping )
1354+
{
1355+
// Average value of max(0, dot(random vector, light dir)) over the unit sphere, used to
1356+
// compare directed lighting's average contribution with ambient. See TraceGrid in light.c in
1357+
// q3map2 for more information about this calculation. Though if we took half-Lambert lighting's
1358+
// cosine modification into account it would be 1/3 instead of 1/4.
1359+
tr.lightGridAverageCosine = 0.25f;
1360+
}
1361+
else
1362+
{
1363+
// Boost it when deluxe is disabled because otherwise it's too dark. Normals tend to line up
1364+
// with the light direction better than chance.
1365+
tr.lightGridAverageCosine = 0.4f;
1366+
}
1367+
13531368
R_NoiseInit();
13541369

13551370
R_Register();

src/engine/renderer/tr_local.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,9 +1684,6 @@ enum
16841684
bspSurface_t *firstSurface;
16851685
};
16861686

1687-
// max combined ambient+directed contribution to one color channel
1688-
#define LIGHTGRID_MAX_LIGHT 1.25f
1689-
16901687
// The ambient and directional colors are packed into four bytes, the color[3] is the
16911688
// average of the ambient and directional colors and the ambientPart factor is the
16921689
// proportion of ambient light in the total light
@@ -2501,6 +2498,7 @@ enum
25012498
vec3_t ambientLight;
25022499
bool ambientLightSet = false;
25032500

2501+
float lightGridAverageCosine;
25042502
image_t *lightGrid1Image;
25052503
image_t *lightGrid2Image;
25062504

0 commit comments

Comments
 (0)