Skip to content

Commit

Permalink
Add functions for mat3x2 for texMods
Browse files Browse the repository at this point in the history
The texture matrices only use elements 0, 1, 4, 5, 12, and 13, so doing all the other computations there is wasteful.
  • Loading branch information
VReaperV committed Feb 8, 2025
1 parent 63647fd commit f1b8984
Showing 1 changed file with 55 additions and 23 deletions.
78 changes: 55 additions & 23 deletions src/engine/renderer/tr_shade_calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,38 @@ TEX COORDS
====================================================================
*/

static inline void Mat3x2MultiplyScale( matrix_t m, const float x, const float y ) {
m[0] *= x;
m[4] *= y;
m[1] *= x;
m[5] *= y;
}

static inline void Mat3x2MultiplyTranslation( matrix_t m, const float x, const float y ) {
m[12] += m[0] * x + m[4] * y;
m[13] += m[1] * x + m[5] * y;
}

static inline void Mat3x2MultiplyZRotation( matrix_t m, const float degrees ) {
float angle = DEG2RAD( degrees );
float s = sinf( angle );
float c = cosf( angle );

const float tmp[] = { m[0], m[1], m[4], m[5] };
m[0] = tmp[0] * c + tmp[2] * s;
m[1] = tmp[1] * c + tmp[3] * s;
m[4] = tmp[0] * -s + tmp[2] * c;
m[5] = tmp[1] * -s + tmp[3] * c;
}

static inline void Mat3x2MultiplyShear( matrix_t m, const float x, const float y ) {
const float tmp[] = { m[0], m[1] };
m[0] += m[4] * y;
m[1] += m[5] * y;
m[4] += tmp[0] * x;
m[5] += tmp[1] * x;
}

static inline void ComputeTextureWrapModifer( const matrix_t matrix, const vec2_t scrollPeriod, vec2_t modifier ) {
const float xPeriod = scrollPeriod[0] / ( matrix[0] + matrix[1] );
const float yPeriod = scrollPeriod[1] / ( matrix[4] + matrix[5] );
Expand Down Expand Up @@ -737,15 +769,15 @@ void RB_CalcTexMatrix( const textureBundle_t *bundle, matrix_t matrix )
float x = ( 1.0f / 4.0f );
float y = ( wf->phase + backEnd.refdef.floatTime * wf->frequency );

MatrixMultiplyScale( matrix, 1.0f + ( wf->amplitude * sinf( y ) + wf->base ) * x,
1.0f + ( wf->amplitude * sinf( y + 0.25f ) + wf->base ) * x, 0.0 );
Mat3x2MultiplyScale( matrix, 1.0f + ( wf->amplitude * sinf( y ) + wf->base ) * x,
1.0f + ( wf->amplitude * sinf( y + 0.25f ) + wf->base ) * x );
break;
}

case texMod_t::TMOD_ENTITY_TRANSLATE:
{
float x = backEnd.currentEntity->e.shaderTexCoord[ 0 ] * backEnd.refdef.floatTime;
float y = backEnd.currentEntity->e.shaderTexCoord[ 1 ] * backEnd.refdef.floatTime;
float x = backEnd.currentEntity->e.shaderTexCoord[ 0 ];
float y = backEnd.currentEntity->e.shaderTexCoord[ 1 ];

// clamp so coordinates don't continuously get larger, causing problems
// with hardware limits
Expand Down Expand Up @@ -775,17 +807,17 @@ void RB_CalcTexMatrix( const textureBundle_t *bundle, matrix_t matrix )
float x = texMod->scale[ 0 ];
float y = texMod->scale[ 1 ];

MatrixMultiplyScale( matrix, x, y, 0.0 );
Mat3x2MultiplyScale( matrix, x, y );
break;
}

case texMod_t::TMOD_STRETCH:
{
float p = 1.0f / RB_EvalWaveForm( &texMod->wave );

MatrixMultiplyTranslation( matrix, 0.5, 0.5, 0.0 );
MatrixMultiplyScale( matrix, p, p, 0.0 );
MatrixMultiplyTranslation( matrix, -0.5, -0.5, 0.0 );
Mat3x2MultiplyTranslation( matrix, 0.5, 0.5 );
Mat3x2MultiplyScale( matrix, p, p );
Mat3x2MultiplyTranslation( matrix, -0.5, -0.5 );
break;
}

Expand All @@ -799,9 +831,9 @@ void RB_CalcTexMatrix( const textureBundle_t *bundle, matrix_t matrix )
{
float x = -texMod->rotateSpeed * backEnd.refdef.floatTime;

MatrixMultiplyTranslation( matrix, 0.5, 0.5, 0.0 );
MatrixMultiplyZRotation( matrix, x );
MatrixMultiplyTranslation( matrix, -0.5, -0.5, 0.0 );
Mat3x2MultiplyTranslation( matrix, 0.5, 0.5 );
Mat3x2MultiplyZRotation( matrix, x );
Mat3x2MultiplyTranslation( matrix, -0.5, -0.5 );
break;
}

Expand All @@ -826,7 +858,7 @@ void RB_CalcTexMatrix( const textureBundle_t *bundle, matrix_t matrix )
float x = RB_EvalExpression( &texMod->sExp, 0 );
float y = RB_EvalExpression( &texMod->tExp, 0 );

MatrixMultiplyScale( matrix, x, y, 0.0 );
Mat3x2MultiplyScale( matrix, x, y );
break;
}

Expand All @@ -835,30 +867,30 @@ void RB_CalcTexMatrix( const textureBundle_t *bundle, matrix_t matrix )
float x = RB_EvalExpression( &texMod->sExp, 0 );
float y = RB_EvalExpression( &texMod->tExp, 0 );

MatrixMultiplyTranslation( matrix, 0.5, 0.5, 0.0 );
MatrixMultiplyScale( matrix, x, y, 0.0 );
MatrixMultiplyTranslation( matrix, -0.5, -0.5, 0.0 );
Mat3x2MultiplyTranslation( matrix, 0.5, 0.5 );
Mat3x2MultiplyScale( matrix, x, y );
Mat3x2MultiplyTranslation( matrix, -0.5, -0.5 );
break;
}

case texMod_t::TMOD_SHEAR:
{
float x = RB_EvalExpression( &texMod->sExp, 0 );
float y = RB_EvalExpression( &texMod->tExp, 0 );
const float x = RB_EvalExpression( &texMod->sExp, 0 );
const float y = RB_EvalExpression( &texMod->tExp, 0 );

MatrixMultiplyTranslation( matrix, 0.5, 0.5, 0.0 );
MatrixMultiplyShear( matrix, x, y );
MatrixMultiplyTranslation( matrix, -0.5, -0.5, 0.0 );
Mat3x2MultiplyTranslation( matrix, 0.5, 0.5 );
Mat3x2MultiplyShear( matrix, x, y );
Mat3x2MultiplyTranslation( matrix, -0.5, -0.5 );
break;
}

case texMod_t::TMOD_ROTATE2:
{
float x = RB_EvalExpression( &texMod->rExp, 0 );

MatrixMultiplyTranslation( matrix, 0.5, 0.5, 0.0 );
MatrixMultiplyZRotation( matrix, x );
MatrixMultiplyTranslation( matrix, -0.5, -0.5, 0.0 );
Mat3x2MultiplyTranslation( matrix, 0.5, 0.5 );
Mat3x2MultiplyZRotation( matrix, x );
Mat3x2MultiplyTranslation( matrix, -0.5, -0.5 );
break;
}

Expand Down

0 comments on commit f1b8984

Please sign in to comment.