13T2RenderContext::T2RenderContext()
16 glDisable( GL_DEPTH_TEST );
19 glDisable( GL_BLEND );
22T2RenderContext::~T2RenderContext()
26void T2RenderContext::ComputePerspectiveProjection(
TMatrix44& a_rOutProjection,
const T2Viewport& a_rViewportParams,
const Projection& a_rProjParams )
28 a_rOutProjection.
m_f11 = ( a_rProjParams.vecProj.x * 2.0f ) / a_rViewportParams.
GetWidth();
29 a_rOutProjection.
m_f12 = 0.0f;
30 a_rOutProjection.
m_f13 = 0.0f;
31 a_rOutProjection.
m_f14 = 0.0f;
32 a_rOutProjection.
m_f21 = 0.0f;
33 a_rOutProjection.
m_f22 = -( ( a_rProjParams.vecProj.y * 2.0f ) / a_rViewportParams.
GetHeight() );
34 a_rOutProjection.
m_f23 = 0.0f;
35 a_rOutProjection.
m_f24 = 0.0f;
36 a_rOutProjection.
m_f31 = ( a_rProjParams.vecCenter.x * 2.0f ) / a_rViewportParams.
GetWidth() - 1.0f;
37 a_rOutProjection.
m_f32 = -( ( a_rProjParams.vecCenter.y * 2.0f ) / a_rViewportParams.
GetHeight() - 1.0f );
38 a_rOutProjection.
m_f33 = a_rProjParams.fFarClip / ( a_rProjParams.fFarClip - a_rProjParams.fNearClip );
39 a_rOutProjection.
m_f34 = 1.0f;
40 a_rOutProjection.
m_f41 = 0.0f;
41 a_rOutProjection.
m_f42 = 0.0f;
42 a_rOutProjection.
m_f43 = -( ( a_rProjParams.fNearClip * a_rProjParams.fFarClip ) / ( a_rProjParams.fFarClip - a_rProjParams.fNearClip ) );
43 a_rOutProjection.
m_f44 = 0.0f;
46void T2RenderContext::ComputeOrthographicProjection(
TMatrix44& a_rOutProjection,
const T2Viewport& a_rViewportParams,
const Projection& a_rProjParams )
48 TASSERT( a_rProjParams.vecProj.x != 0.0f );
49 TASSERT( a_rProjParams.vecProj.y != 0.0f );
58 a_rOutProjection.
m_f11 = ( a_rProjParams.vecProj.x * 2.0f ) / a_rViewportParams.
GetWidth();
59 a_rOutProjection.
m_f12 = 0.0f;
60 a_rOutProjection.
m_f13 = 0.0f;
61 a_rOutProjection.
m_f14 = 0.0f;
62 a_rOutProjection.
m_f21 = 0.0f;
63 a_rOutProjection.
m_f22 = -( ( a_rProjParams.vecProj.y * 2.0f ) / a_rViewportParams.
GetHeight() );
64 a_rOutProjection.
m_f23 = 0.0f;
65 a_rOutProjection.
m_f24 = 0.0f;
66 a_rOutProjection.
m_f31 = 0.0f;
67 a_rOutProjection.
m_f32 = 0.0f;
68 a_rOutProjection.
m_f33 = 1.0f / ( a_rProjParams.fFarClip - a_rProjParams.fNearClip );
69 a_rOutProjection.
m_f34 = 0.0f;
70 a_rOutProjection.
m_f41 = ( a_rProjParams.vecCenter.x * 2.0f ) / a_rViewportParams.
GetWidth() - 1.0f;
71 a_rOutProjection.
m_f42 = -( ( a_rProjParams.vecCenter.y * 2.0f ) / a_rViewportParams.
GetHeight() - 1.0f );
72 a_rOutProjection.
m_f43 = -( a_rProjParams.fNearClip / ( a_rProjParams.fFarClip - a_rProjParams.fNearClip ) );
73 a_rOutProjection.
m_f44 = 1.0f;
76void T2RenderContext::ComputePerspectiveFrustum(
Frustum& a_rcFrustum,
const T2Viewport& a_rViewportParams,
const Projection& a_rProjParams )
78 TASSERT( a_rProjParams.vecProj.x != 0.0f );
79 TASSERT( a_rProjParams.vecProj.y != 0.0f );
86 TFLOAT fVal1 = 1.0f / a_rProjParams.vecProj.x;
87 TFLOAT fVal2 = 1.0f / a_rProjParams.vecProj.y;
88 TVector4 vec1 =
TVector4( -a_rProjParams.vecCenter.x * fVal1, ( a_rViewportParams.
GetHeight() - a_rProjParams.vecCenter.y ) * fVal2, 1.0f, 0.0f );
90 TVector4 vec3 =
TVector4( vec2.
x, -a_rProjParams.vecCenter.y * fVal2, 1.0f, 0.0f );
94 a_rcFrustum[ FRUSTUMPLANE_LEFT ].AsVector4().CrossProduct( vec4, vec1 );
95 a_rcFrustum[ FRUSTUMPLANE_RIGHT ].AsVector4().CrossProduct( vec2, vec3 );
96 a_rcFrustum[ FRUSTUMPLANE_BOTTOM ].AsVector4().CrossProduct( vec3, vec4 );
97 a_rcFrustum[ FRUSTUMPLANE_TOP ].AsVector4().CrossProduct( vec1, vec2 );
100 a_rcFrustum[ FRUSTUMPLANE_LEFT ].AsNormal().Normalize();
101 a_rcFrustum[ FRUSTUMPLANE_RIGHT ].AsNormal().Normalize();
102 a_rcFrustum[ FRUSTUMPLANE_BOTTOM ].AsNormal().Normalize();
103 a_rcFrustum[ FRUSTUMPLANE_TOP ].AsNormal().Normalize();
106 a_rcFrustum[ FRUSTUMPLANE_NEAR ].Set( 0.0f, 0.0f, -1.0f, -a_rProjParams.fNearClip );
107 a_rcFrustum[ FRUSTUMPLANE_FAR ].Set( 0.0f, 0.0f, 1.0f, a_rProjParams.fFarClip );
110void T2RenderContext::ComputeOrthographicFrustum(
Frustum& a_rcFrustum,
const T2Viewport& a_rViewportParams,
const Projection& a_rProjParams )
112 TASSERT( a_rProjParams.vecProj.x != 0.0f );
113 TASSERT( a_rProjParams.vecProj.y != 0.0f );
124 TFLOAT fCentreX = a_rProjParams.vecCenter.x;
125 TFLOAT fCentreY = a_rProjParams.vecCenter.y;
126 TFLOAT fProjXOverOne = 1.0f / a_rProjParams.vecProj.x;
127 TFLOAT fProjYOverOne = 1.0f / a_rProjParams.vecProj.y;
130 a_rcFrustum[ FRUSTUMPLANE_LEFT ].Set( -1.0f, 0.0f, 0.0f, fCentreX * fProjXOverOne );
131 a_rcFrustum[ FRUSTUMPLANE_RIGHT ].Set( 1.0f, 0.0f, 0.0f, ( fWidth - fCentreX ) * fProjXOverOne );
132 a_rcFrustum[ FRUSTUMPLANE_BOTTOM ].Set( 0.0f, -1.0f, 0.0f, fCentreY * fProjYOverOne );
133 a_rcFrustum[ FRUSTUMPLANE_TOP ].Set( 0.0f, 1.0f, 0.0f, ( fHeight - fCentreY ) * fProjYOverOne );
134 a_rcFrustum[ FRUSTUMPLANE_NEAR ].Set( 0.0f, 0.0f, -1.0f, -a_rProjParams.fNearClip );
135 a_rcFrustum[ FRUSTUMPLANE_FAR ].Set( 0.0f, 0.0f, 1.0f, a_rProjParams.fFarClip );
138TBOOL T2RenderContext::CullSphereToFrustumSimple(
const TSphere& a_rSphere,
const TPlane* a_pPlanes,
TINT a_iNumPlanes )
140 for (
TSIZE i = 0; i < FRUSTUMPLANE_NUMOF; i++ )
144 if ( a_rSphere.
GetRadius() < fDist - a_pPlanes[ i ].GetD() )
151TINT T2RenderContext::CullSphereToFrustum(
const TSphere& a_rSphere,
const TPlane* a_pPlanes,
TINT a_iClipFlags,
TINT a_iClipFlagsMask )
153 TINT iLeftPlanes = a_iClipFlags & a_iClipFlagsMask;
157 if ( iLeftPlanes == 0 )
162 if ( iLeftPlanes & iPlaneFlag )
173 a_iClipFlags &= ~iPlaneFlag;
176 iLeftPlanes &= ~iPlaneFlag;
179 iPlaneFlag = iPlaneFlag << 1;
185TBOOL T2RenderContext::SetShaderProgram(
const T2Shader& a_rcShaderProgram )
187 if ( m_uiCurrentShaderProgram != a_rcShaderProgram.GetProgram() )
189 a_rcShaderProgram.Use();
190 m_uiCurrentShaderProgram = a_rcShaderProgram.GetProgram();
198GLuint T2RenderContext::GetTexture2D(
TINT a_iTextureIndex )
200 return m_aCurrentTextures[ a_iTextureIndex ];
203void T2RenderContext::SetTexture2D(
TINT a_iTextureIndex, GLuint a_uiTexture )
205 if ( m_aCurrentTextures[ a_iTextureIndex ] != a_uiTexture )
207 if ( m_iCurrentTextureUnit != a_iTextureIndex )
209 glActiveTexture( GL_TEXTURE0 + a_iTextureIndex );
210 m_iCurrentTextureUnit = a_iTextureIndex;
213 glBindTexture( GL_TEXTURE_2D, a_uiTexture );
214 m_aCurrentTextures[ a_iTextureIndex ] = a_uiTexture;
218void T2RenderContext::SetTexture2D(
TINT a_iTextureIndex,
const T2GLTexture& a_rcTexture )
220 SetTexture2D( a_iTextureIndex, a_rcTexture.
GetHandle() );
223void T2RenderContext::ResetTexture2D(
TINT a_iTextureIndex )
225 if ( m_iCurrentTextureUnit != a_iTextureIndex )
227 glActiveTexture( GL_TEXTURE0 + a_iTextureIndex );
228 glBindTexture( GL_TEXTURE_2D, NULL );
230 m_iCurrentTextureUnit = a_iTextureIndex;
231 m_aCurrentTextures[ a_iTextureIndex ] = -1;
235void T2RenderContext::EnableDepthTest(
TBOOL a_bEnable )
237 if ( m_bDepthTest != a_bEnable )
240 glEnable( GL_DEPTH_TEST );
242 glDisable( GL_DEPTH_TEST );
244 m_bDepthTest = a_bEnable;
248void T2RenderContext::EnableBlend(
TBOOL a_bEnable )
250 if ( m_bBlend != a_bEnable )
253 glEnable( GL_BLEND );
255 glDisable( GL_BLEND );
257 m_bBlend = a_bEnable;
4x4 matrix implementation for the Toshi engine
#define TOSHI_NAMESPACE_START
#define TOSHI_NAMESPACE_END
TFORCEINLINE TBOOL IsFinite(TFLOAT fVal)
TFORCEINLINE TBOOL IsNaN(TFLOAT fVal)
TFORCEINLINE TVector4 & AsVector4()
TFORCEINLINE constexpr TFLOAT GetD() const
TFORCEINLINE TVector4 & AsVector4()
constexpr TFORCEINLINE TFLOAT GetRadius() const
TFLOAT constexpr DotProduct3(const TVector4 &vec) const