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 5, 2025
1 parent 69a939b commit d1c963d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
32 changes: 32 additions & 0 deletions src/engine/qcommon/q_shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,38 @@ inline vec_t VectorNormalize2( const vec3_t v, vec3_t out )
MatrixFromAngles( m, angles[ PITCH ], angles[ YAW ], angles[ ROLL ] );
}

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

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;
}

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;
}

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;
}

//=============================================

// RB: XreaL quaternion math functions required by the renderer
Expand Down
58 changes: 29 additions & 29 deletions src/engine/renderer/tr_shade_calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,24 +737,24 @@ 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
vec2_t modifier;
vec2_t scrollPeriod = { 1.0f / x, 1.0f / y };
ComputeTextureWrapModifer( matrix, texMod->scrollPeriod, modifier );
ComputeTextureWrapModifer( matrix, scrollPeriod, modifier );

matrix[12] += matrix[0] * texMod->scroll[0] * modifier[0] + matrix[4] * texMod->scroll[1] * modifier[0];
matrix[13] += matrix[1] * texMod->scroll[0] * modifier[1] + matrix[5] * texMod->scroll[1] * modifier[1];
matrix[12] += matrix[0] * x * modifier[0] + matrix[4] * y * modifier[0];
matrix[13] += matrix[1] * x * modifier[1] + matrix[5] * y * modifier[1];
break;
}

Expand All @@ -775,17 +775,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 +799,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 @@ -814,10 +814,10 @@ void RB_CalcTexMatrix( const textureBundle_t *bundle, matrix_t matrix )
// with hardware limits
vec2_t modifier;
vec2_t scrollPeriod = { 1.0f / x, 1.0f / y };
ComputeTextureWrapModifer( matrix, texMod->scrollPeriod, modifier );
ComputeTextureWrapModifer( matrix, scrollPeriod, modifier );

matrix[12] += matrix[0] * texMod->scroll[0] * modifier[0] + matrix[4] * texMod->scroll[1] * modifier[0];
matrix[13] += matrix[1] * texMod->scroll[0] * modifier[1] + matrix[5] * texMod->scroll[1] * modifier[1];
matrix[12] += matrix[0] * x * modifier[0] + matrix[4] * y * modifier[0];
matrix[13] += matrix[1] * x * modifier[1] + matrix[5] * y * modifier[1];
break;
}

Expand All @@ -826,7 +826,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 +835,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 d1c963d

Please sign in to comment.