Skip to content

Commit

Permalink
Optimise 3D skybox rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
samisalreadytaken committed Nov 17, 2022
1 parent be42ffb commit a1c8dba
Showing 1 changed file with 18 additions and 59 deletions.
77 changes: 18 additions & 59 deletions sp/src/game/client/viewrender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,6 @@ class CSkyboxView : public CRendering3dView
void Enable3dSkyboxFog( void );
void DrawInternal( view_id_t iSkyBoxViewID, bool bInvokePreAndPostRender, ITexture *pRenderTarget, ITexture *pDepthTarget );

#ifdef MAPBASE
void CalculateSkyAngles( const QAngle &angAngles );
#endif

sky3dparams_t * PreRender3dSkyboxWorld( SkyboxVisibility_t nSkyboxVisible );

sky3dparams_t *m_pSky3dParams;
Expand Down Expand Up @@ -5400,21 +5396,33 @@ void CSkyboxView::DrawInternal( view_id_t iSkyBoxViewID, bool bInvokePreAndPostR
// Re-use the x coordinate to determine if we shuld do this with angles
if (m_pSky3dParams->angles.GetX() != 0)
{
CalculateSkyAngles( m_pSky3dParams->skycamera->GetAbsAngles() );
const matrix3x4_t &matSky = m_pSky3dParams->skycamera->EntityToWorldTransform();
matrix3x4_t matView;
AngleMatrix( angles, origin, matView );
ConcatTransforms( matSky, matView, matView );
MatrixAngles( matView, angles, origin );
}
else
{
VectorAdd( origin, m_pSky3dParams->skycamera->GetAbsOrigin(), origin );
}

VectorAdd( origin, m_pSky3dParams->skycamera->GetAbsOrigin(), origin );
}
else
{
if (m_pSky3dParams->angles.GetX() != 0 ||
m_pSky3dParams->angles.GetY() != 0 ||
m_pSky3dParams->angles.GetZ() != 0)
{
CalculateSkyAngles( m_pSky3dParams->angles.Get() );
matrix3x4_t matSky, matView;
AngleMatrix( m_pSky3dParams->angles, m_pSky3dParams->origin, matSky );
AngleMatrix( angles, origin, matView );
ConcatTransforms( matSky, matView, matView );
MatrixAngles( matView, angles, origin );
}
else
{
VectorAdd( origin, m_pSky3dParams->origin, origin );
}

VectorAdd( origin, m_pSky3dParams->origin, origin );
}
#else
VectorAdd( origin, m_pSky3dParams->origin, origin );
Expand Down Expand Up @@ -5531,55 +5539,6 @@ void CSkyboxView::DrawInternal( view_id_t iSkyBoxViewID, bool bInvokePreAndPostR
#endif
}

#ifdef MAPBASE
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
void CSkyboxView::CalculateSkyAngles( const QAngle &angAngles )
{
// Unfortunately, it's not as simple as "angles += m_pSky3dParams->angles".
// This stuff took a long time to figure out. I'm glad I got it working.

// First, create a matrix for the sky's angles.
matrix3x4_t matSkyAngles;
AngleMatrix( angAngles, matSkyAngles );

// The code in between the lines below was mostly lifted from projected texture screenspace code and was a huge lifesaver.
// The comments are my attempt at explaining the little I understand of what's going on here.
// ----------------------------------------------------------------------

// These are the vectors that would eventually become our final angle directions.
Vector vecSkyForward, vecSkyRight, vecSkyUp;

// Get vectors from our original angles.
Vector vPlayerForward, vPlayerRight, vPlayerUp;
AngleVectors( angles, &vPlayerForward, &vPlayerRight, &vPlayerUp );

// Transform them from our sky angles matrix and put the results in those vectors we declared earlier.
VectorTransform( vPlayerForward, matSkyAngles, vecSkyForward );
VectorTransform( vPlayerRight, matSkyAngles, vecSkyRight );
VectorTransform( vPlayerUp, matSkyAngles, vecSkyUp );

// Normalize them.
VectorNormalize( vecSkyForward );
VectorNormalize( vecSkyRight );
VectorNormalize( vecSkyUp );

// Now do a bit of quaternion magic and apply that to our original angles.
// This works perfectly, so I'm not gonna touch it.
Quaternion quat;
BasisToQuaternion( vecSkyForward, vecSkyRight, vecSkyUp, quat );
QuaternionAngles( quat, angles );

// End of code mostly lifted from projected texture screenspace stuff
// ----------------------------------------------------------------------

// Now just rotate our origin with that matrix.
// We create a copy of the origin since VectorRotate doesn't want in1 to be the same variable as the destination.
VectorRotate(Vector(origin), matSkyAngles, origin);
}
#endif

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
Expand Down

0 comments on commit a1c8dba

Please sign in to comment.