Skip to content

Commit 56a70a6

Browse files
committed
renderer: implement not-clamped mapOverBright in GLSL
We cannot overBright separate stage as stage are clamped within [0.0, 1.0] when blending multiple stages together, meaning the result of the multiplication of a separate light stage with a light factor will be clamped before blending it with the color map. This implementation implements overBright in the camera shader, but to only apply this overBright to surfaces having received lights, surfaces that do not receive lights gets divided by the light factor in a way re-multiplying them cancels the division. Stages and surfaces to be blended over other stages or surfaces are also divided to avoid multiplying multiple time the same surface, in order to have the final result being (m * a) + b instead of m * (a + b) wich would be (m * a) + (m * b) and then sump up the factor, something we don't want to do.
1 parent ae3a862 commit 56a70a6

16 files changed

+210
-50
lines changed

src/engine/renderer/gl_shader.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,7 @@ GLShader_generic::GLShader_generic( GLShaderManager *manager ) :
15801580
u_ModelMatrix( this ),
15811581
u_ProjectionMatrixTranspose( this ),
15821582
u_ModelViewProjectionMatrix( this ),
1583+
u_LightFactor( this ),
15831584
u_ColorModulate( this ),
15841585
u_Color( this ),
15851586
u_Bones( this ),
@@ -1618,6 +1619,7 @@ GLShader_lightMapping::GLShader_lightMapping( GLShaderManager *manager ) :
16181619
u_ViewOrigin( this ),
16191620
u_ModelMatrix( this ),
16201621
u_ModelViewProjectionMatrix( this ),
1622+
u_LightFactor( this ),
16211623
u_Bones( this ),
16221624
u_VertexInterpolation( this ),
16231625
u_ReliefDepthScale( this ),
@@ -1938,6 +1940,7 @@ GLShader_skybox::GLShader_skybox( GLShaderManager *manager ) :
19381940
u_AlphaThreshold( this ),
19391941
u_ModelMatrix( this ),
19401942
u_ModelViewProjectionMatrix( this ),
1943+
u_LightFactor( this ),
19411944
u_VertexInterpolation( this ),
19421945
GLDeformStage( this ),
19431946
GLCompileMacro_USE_ALPHA_TESTING( this )
@@ -1954,6 +1957,7 @@ GLShader_fogQuake3::GLShader_fogQuake3( GLShaderManager *manager ) :
19541957
GLShader( "fogQuake3", ATTR_POSITION | ATTR_QTANGENT, manager ),
19551958
u_ModelMatrix( this ),
19561959
u_ModelViewProjectionMatrix( this ),
1960+
u_LightFactor( this ),
19571961
u_Color( this ),
19581962
u_Bones( this ),
19591963
u_VertexInterpolation( this ),
@@ -1982,6 +1986,7 @@ GLShader_fogGlobal::GLShader_fogGlobal( GLShaderManager *manager ) :
19821986
u_ViewMatrix( this ),
19831987
u_ModelViewProjectionMatrix( this ),
19841988
u_UnprojectMatrix( this ),
1989+
u_LightFactor( this ),
19851990
u_Color( this ),
19861991
u_FogDistanceVector( this ),
19871992
u_FogDepthVector( this )
@@ -2073,6 +2078,7 @@ GLShader_cameraEffects::GLShader_cameraEffects( GLShaderManager *manager ) :
20732078
u_ColorModulate( this ),
20742079
u_TextureMatrix( this ),
20752080
u_ModelViewProjectionMatrix( this ),
2081+
u_LightFactor( this ),
20762082
u_DeformMagnitude( this ),
20772083
u_InverseGamma( this )
20782084
{

src/engine/renderer/gl_shader.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,21 @@ class GLCompileMacro_USE_ALPHA_TESTING :
13041304
}
13051305
};
13061306

1307+
class u_LightFactor :
1308+
GLUniform1f
1309+
{
1310+
public:
1311+
u_LightFactor( GLShader *shader ) :
1312+
GLUniform1f( shader, "u_LightFactor" )
1313+
{
1314+
}
1315+
1316+
void SetUniform_LightFactor( const float lightFactor )
1317+
{
1318+
this->SetValue( lightFactor );
1319+
}
1320+
};
1321+
13071322
class u_TextureMatrix :
13081323
GLUniformMatrix4f
13091324
{
@@ -2270,6 +2285,7 @@ class GLShader_generic :
22702285
public u_ModelMatrix,
22712286
public u_ProjectionMatrixTranspose,
22722287
public u_ModelViewProjectionMatrix,
2288+
public u_LightFactor,
22732289
public u_ColorModulate,
22742290
public u_Color,
22752291
public u_Bones,
@@ -2300,6 +2316,7 @@ class GLShader_lightMapping :
23002316
public u_ViewOrigin,
23012317
public u_ModelMatrix,
23022318
public u_ModelViewProjectionMatrix,
2319+
public u_LightFactor,
23032320
public u_Bones,
23042321
public u_VertexInterpolation,
23052322
public u_ReliefDepthScale,
@@ -2502,6 +2519,7 @@ class GLShader_skybox :
25022519
public u_AlphaThreshold,
25032520
public u_ModelMatrix,
25042521
public u_ModelViewProjectionMatrix,
2522+
public u_LightFactor,
25052523
public u_VertexInterpolation,
25062524
public GLDeformStage,
25072525
public GLCompileMacro_USE_ALPHA_TESTING
@@ -2515,6 +2533,7 @@ class GLShader_fogQuake3 :
25152533
public GLShader,
25162534
public u_ModelMatrix,
25172535
public u_ModelViewProjectionMatrix,
2536+
public u_LightFactor,
25182537
public u_Color,
25192538
public u_Bones,
25202539
public u_VertexInterpolation,
@@ -2537,6 +2556,7 @@ class GLShader_fogGlobal :
25372556
public u_ViewMatrix,
25382557
public u_ModelViewProjectionMatrix,
25392558
public u_UnprojectMatrix,
2559+
public u_LightFactor,
25402560
public u_Color,
25412561
public u_FogDistanceVector,
25422562
public u_FogDepthVector
@@ -2607,6 +2627,7 @@ class GLShader_cameraEffects :
26072627
public u_ColorModulate,
26082628
public u_TextureMatrix,
26092629
public u_ModelViewProjectionMatrix,
2630+
public u_LightFactor,
26102631
public u_DeformMagnitude,
26112632
public u_InverseGamma
26122633
{

src/engine/renderer/glsl_source/cameraEffects_fp.glsl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2424

2525
uniform sampler2D u_CurrentMap;
2626
uniform sampler3D u_ColorMap;
27+
28+
uniform float u_LightFactor;
2729
uniform vec4 u_ColorModulate;
2830
uniform float u_InverseGamma;
2931

@@ -36,9 +38,11 @@ void main()
3638
// calculate the screen texcoord in the 0.0 to 1.0 range
3739
vec2 st = gl_FragCoord.st / r_FBufSize;
3840

39-
vec4 original = clamp(texture2D(u_CurrentMap, st), 0.0, 1.0);
41+
vec4 color = clamp(texture2D(u_CurrentMap, st), 0.0, 1.0);
42+
43+
color.rgb *= u_LightFactor;
4044

41-
vec4 color = original;
45+
color.rgb = clamp(color.rgb, 0, 1);
4246

4347
// apply color grading
4448
vec3 colCoord = color.rgb * 15.0 / 16.0 + 0.5 / 16.0;

src/engine/renderer/glsl_source/fogGlobal_fp.glsl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2424

2525
uniform sampler2D u_ColorMap; // fog texture
2626
uniform sampler2D u_DepthMap;
27+
28+
uniform float u_LightFactor;
2729
uniform vec3 u_ViewOrigin;
2830
uniform vec4 u_FogDistanceVector;
2931
uniform vec4 u_FogDepthVector;
@@ -52,5 +54,9 @@ void main()
5254
// st.s = vertexDistanceToCamera;
5355
st.t = 1.0;
5456

55-
outputColor = u_Color * texture2D(u_ColorMap, st);
57+
vec4 color = texture2D(u_ColorMap, st);
58+
59+
color.rgb /= u_LightFactor;
60+
61+
outputColor = u_Color * color;
5662
}

src/engine/renderer/glsl_source/fogQuake3_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2424

2525
uniform sampler2D u_ColorMap;
2626

27+
uniform float u_LightFactor;
2728
IN(smooth) vec3 var_Position;
2829
IN(smooth) vec2 var_TexCoords;
2930
IN(smooth) vec4 var_Color;
@@ -35,6 +36,9 @@ void main()
3536
vec4 color = texture2D(u_ColorMap, var_TexCoords);
3637

3738
color *= var_Color;
39+
40+
color.rgb /= u_LightFactor;
41+
3842
outputColor = color;
3943

4044
#if 0

src/engine/renderer/glsl_source/generic_fp.glsl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2525
uniform sampler2D u_ColorMap;
2626
uniform float u_AlphaThreshold;
2727

28+
#if !defined(GENERIC_2D)
29+
uniform float u_LightFactor;
30+
#endif
31+
2832
IN(smooth) vec2 var_TexCoords;
2933
IN(smooth) vec4 var_Color;
3034

@@ -54,6 +58,11 @@ void main()
5458
#endif
5559

5660
color *= var_Color;
61+
62+
#if !defined(GENERIC_2D) && !defined(USE_DEPTH_FADE)
63+
color.rgb /= u_LightFactor;
64+
#endif
65+
5766
outputColor = color;
5867

5968
#if defined(GENERIC_2D)

src/engine/renderer/glsl_source/lightMapping_fp.glsl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ uniform sampler2D u_MaterialMap;
2727
uniform sampler2D u_GlowMap;
2828

2929
uniform float u_AlphaThreshold;
30+
uniform float u_LightFactor;
3031
uniform vec3 u_ViewOrigin;
3132

3233
IN(smooth) vec3 var_Position;
@@ -177,6 +178,8 @@ void main()
177178
color.rgb += texture2D(u_GlowMap, texCoords).rgb;
178179
#endif
179180

181+
color.rgb /= u_LightFactor;
182+
180183
outputColor = color;
181184

182185
// Debugging.

src/engine/renderer/glsl_source/skybox_fp.glsl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2525
const float radiusWorld = 4096.0; // Value used by quake 3 skybox code
2626

2727
uniform samplerCube u_ColorMap;
28+
29+
uniform float u_LightFactor;
2830
uniform sampler2D u_CloudMap;
2931

3032
uniform bool u_UseCloudMap;
@@ -73,5 +75,7 @@ void main()
7375
}
7476
#endif
7577

78+
color.rgb /= u_LightFactor;
79+
7680
outputColor = color;
7781
}

src/engine/renderer/tr_backend.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2960,6 +2960,9 @@ void RB_RenderGlobalFog()
29602960

29612961
gl_fogGlobalShader->SetUniform_ViewOrigin( backEnd.viewParms.orientation.origin ); // world space
29622962

2963+
// u_LightFactor
2964+
gl_fogGlobalShader->SetUniform_LightFactor( tr.mapLightFactor );
2965+
29632966
{
29642967
fog_t *fog;
29652968

@@ -3281,6 +3284,9 @@ void RB_CameraPostFX()
32813284
// enable shader, set arrays
32823285
gl_cameraEffectsShader->BindProgram( 0 );
32833286

3287+
// u_LightFactor
3288+
gl_cameraEffectsShader->SetUniform_LightFactor( tr.mapLightFactor );
3289+
32843290
gl_cameraEffectsShader->SetUniform_ColorModulate( backEnd.viewParms.gradingWeights );
32853291
gl_cameraEffectsShader->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[ glState.stackIndex ] );
32863292

0 commit comments

Comments
 (0)