Skip to content

Commit 49b275e

Browse files
committed
Fix colorModulate for lightFactor < 4
Also fixes extra bits beng added to the `lightFactor` part `SetUniform_ColorModulateColorGen` in some cases.
1 parent 8104069 commit 49b275e

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

src/engine/renderer/gl_shader.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3591,11 +3591,11 @@ class u_ColorModulate :
35913591
};
35923592

35933593
enum class ColorModulate {
3594-
COLOR_ADD = BIT( 0 ),
3595-
COLOR_NEGATE = BIT( 1 ),
3594+
COLOR_ONE = BIT( 0 ),
3595+
COLOR_MINUS_ONE = BIT( 1 ),
35963596
COLOR_LIGHTFACTOR = BIT( 2 ),
3597-
ALPHA_ADD = BIT( 3 ),
3598-
ALPHA_NEGATE = BIT( 4 )
3597+
ALPHA_ONE = BIT( 3 ),
3598+
ALPHA_MINUS_ONE = BIT( 4 )
35993599
};
36003600

36013601
class u_ColorModulateColorGen :
@@ -3617,22 +3617,23 @@ class u_ColorModulateColorGen :
36173617
);
36183618
}
36193619

3620+
uint32_t lightFactor = 0;
36203621
switch ( colorGen ) {
36213622
case colorGen_t::CGEN_VERTEX:
36223623
needAttrib = true;
36233624
if ( vertexOverbright ) {
36243625
// vertexOverbright is only needed for non-lightmapped cases. When there is a
36253626
// lightmap, this is done by multiplying with the overbright-scaled white image
36263627
colorModulate |= Util::ordinal( ColorModulate::COLOR_LIGHTFACTOR );
3627-
colorModulate |= uint32_t( tr.mapLightFactor / 4 ) << 5;
3628+
lightFactor = uint32_t( tr.mapLightFactor ) << 5;
36283629
} else {
3629-
colorModulate |= Util::ordinal( ColorModulate::COLOR_ADD );
3630+
colorModulate |= Util::ordinal( ColorModulate::COLOR_ONE );
36303631
}
36313632
break;
36323633

36333634
case colorGen_t::CGEN_ONE_MINUS_VERTEX:
36343635
needAttrib = true;
3635-
colorModulate |= Util::ordinal( ColorModulate::COLOR_ADD ) | Util::ordinal( ColorModulate::COLOR_NEGATE );
3636+
colorModulate |= Util::ordinal( ColorModulate::COLOR_MINUS_ONE );
36363637
break;
36373638

36383639
default:
@@ -3641,20 +3642,20 @@ class u_ColorModulateColorGen :
36413642

36423643
if ( useMapLightFactor ) {
36433644
ASSERT_EQ( vertexOverbright, false );
3644-
colorModulate |= uint32_t( tr.mapLightFactor ) << 5;
3645-
} else {
3646-
colorModulate |= 1 << 5;
3645+
lightFactor = uint32_t( tr.mapLightFactor ) << 5;
36473646
}
36483647

3648+
colorModulate |= lightFactor ? lightFactor : 1 << 5;
3649+
36493650
switch ( alphaGen ) {
36503651
case alphaGen_t::AGEN_VERTEX:
36513652
needAttrib = true;
3652-
colorModulate |= Util::ordinal( ColorModulate::ALPHA_ADD );
3653+
colorModulate |= Util::ordinal( ColorModulate::ALPHA_ONE );
36533654
break;
36543655

36553656
case alphaGen_t::AGEN_ONE_MINUS_VERTEX:
36563657
needAttrib = true;
3657-
colorModulate |= Util::ordinal( ColorModulate::ALPHA_ADD ) | Util::ordinal( ColorModulate::ALPHA_NEGATE );
3658+
colorModulate |= Util::ordinal( ColorModulate::ALPHA_MINUS_ONE );
36583659
break;
36593660

36603661
default:

src/engine/renderer/glsl_source/common.glsl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ array must be in the form of uvec4 array[] */
4343
#define UINT_FROM_UVEC4_ARRAY( array, id ) ( ( array )[( id ) / 4][( id ) % 4] )
4444
#define UVEC2_FROM_UVEC4_ARRAY( array, id ) ( ( id ) % 2 == 0 ? ( array )[( id ) / 2].xy : ( array )[( id ) / 2].zw )
4545

46-
/* Bit 0: color add
47-
Bit 1: color negate
48-
Bit 2: lightFactor add
49-
Bit 3: alpha add
50-
Bit 4: alpha negate
51-
Bit 5-12: lightFactor */
46+
/* Bit 0: color * 1
47+
Bit 1: color * ( -1 )
48+
Bit 2: color += lightFactor
49+
Bit 3: alpha * 1
50+
Bit 4: alpha * ( -1 )
51+
Bit 5-7: lightFactor */
5252

5353
float colorModArray[3] = float[3] ( 0.0f, 1.0f, -1.0f );
5454

@@ -59,11 +59,11 @@ vec4 ColorModulateToColor( const in uint colorMod ) {
5959
}
6060

6161
vec4 ColorModulateToColor( const in uint colorMod, const in float lightFactor ) {
62-
vec4 colorModulate = vec4( colorModArray[colorMod & 3] + ( colorMod & 4 ) * lightFactor );
62+
vec4 colorModulate = vec4( colorModArray[colorMod & 3] + ( ( colorMod & 4 ) >> 2 ) * lightFactor );
6363
colorModulate.a = ( colorModArray[( colorMod & 24 ) >> 3] );
6464
return colorModulate;
6565
}
6666

6767
float ColorModulateToLightFactor( const in uint colorMod ) {
68-
return ( colorMod >> 5 ) & 0xF;
68+
return ( colorMod >> 5 ) & 0x7;
6969
}

0 commit comments

Comments
 (0)