Skip to content

Commit 368629e

Browse files
committed
Fix texture scroll
Previous code assumed that a texture with a scroll textureMod would always wrap around when `scroll * backEnd.refdef.floatTime == floor( scroll * backEnd.refdef.floatTime )`. This is incorrect if the texture matrix had already been modified by another texMod, so the relevant values are no longer 1.0f and 0.0f. Fix this by using `scrollPeriod[0] / ( matrix[0] + matrix[4] )` and `scrollPeriod[0] / ( matrix[0] + matrix[4] )` to get the actual time at which wrap-around for `x` and `y` will occur, and use `fmodf( backEnd.refdef.floatTime, period )` to get the part that is equivalent to just using `backEnd.refdef.floatTime`, but without the value getting continuously larger. Also NUKED some useless matrix computations from texMod scroll.
1 parent 2a3ebd8 commit 368629e

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

src/engine/renderer/tr_shade_calc.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,16 @@ TEX COORDS
701701
====================================================================
702702
*/
703703

704+
static inline void ComputeTextureWrapModifer( const matrix_t matrix, const vec2_t scroll, vec2_t modifier ) {
705+
const float xDiv = ( matrix[0] * scroll[0] + matrix[4] * scroll[1] );
706+
const float yDiv = ( matrix[1] * scroll[0] + matrix[5] * scroll[1] );
707+
const float xPeriod = xDiv ? 1.0f / xDiv : 1.0f;
708+
const float yPeriod = yDiv ? 1.0f / yDiv : 1.0f;
709+
710+
modifier[0] = xPeriod ? fmodf( backEnd.refdef.floatTime, xPeriod ) : 0.0f;
711+
modifier[1] = yPeriod ? fmodf( backEnd.refdef.floatTime, yPeriod ) : 0.0f;
712+
}
713+
704714
/*
705715
===============
706716
RB_CalcTexMatrix
@@ -741,24 +751,24 @@ void RB_CalcTexMatrix( const textureBundle_t *bundle, matrix_t matrix )
741751

742752
// clamp so coordinates don't continuously get larger, causing problems
743753
// with hardware limits
744-
x = x - floor( x );
745-
y = y - floor( y );
754+
vec2_t modifier;
755+
vec2_t scroll { x, y };
756+
ComputeTextureWrapModifer( matrix, scroll, modifier );
746757

747-
MatrixMultiplyTranslation( matrix, x, y, 0.0 );
758+
matrix[12] += matrix[0] * x * modifier[0] + matrix[4] * x * modifier[0];
759+
matrix[13] += matrix[1] * y * modifier[1] + matrix[5] * y * modifier[1];
748760
break;
749761
}
750762

751763
case texMod_t::TMOD_SCROLL:
752764
{
753-
float x = texMod->scroll[ 0 ] * backEnd.refdef.floatTime;
754-
float y = texMod->scroll[ 1 ] * backEnd.refdef.floatTime;
755-
756765
// clamp so coordinates don't continuously get larger, causing problems
757766
// with hardware limits
758-
x = x - floor( x );
759-
y = y - floor( y );
767+
vec2_t modifier;
768+
ComputeTextureWrapModifer( matrix, texMod->scroll, modifier );
760769

761-
MatrixMultiplyTranslation( matrix, x, y, 0.0 );
770+
matrix[12] += matrix[0] * texMod->scroll[0] * modifier[0] + matrix[4] * texMod->scroll[1] * modifier[0];
771+
matrix[13] += matrix[1] * texMod->scroll[0] * modifier[1] + matrix[5] * texMod->scroll[1] * modifier[1];
762772
break;
763773
}
764774

@@ -804,10 +814,12 @@ void RB_CalcTexMatrix( const textureBundle_t *bundle, matrix_t matrix )
804814

805815
// clamp so coordinates don't continuously get larger, causing problems
806816
// with hardware limits
807-
x = x - floor( x );
808-
y = y - floor( y );
817+
vec2_t modifier;
818+
vec2_t scroll{ x, y };
819+
ComputeTextureWrapModifer( matrix, scroll, modifier );
809820

810-
MatrixMultiplyTranslation( matrix, x, y, 0.0 );
821+
matrix[12] += matrix[0] * x * modifier[0] + matrix[4] * x * modifier[0];
822+
matrix[13] += matrix[1] * y * modifier[1] + matrix[5] * y * modifier[1];
811823
break;
812824
}
813825

0 commit comments

Comments
 (0)