OpenBarnyard
 
Loading...
Searching...
No Matches
T2RenderContext_GL.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
3#include "Math/TMatrix44.h"
4
5//-----------------------------------------------------------------------------
6// Enables memory debugging.
7// Note: Should be the last include!
8//-----------------------------------------------------------------------------
10
12
13T2RenderContext::T2RenderContext()
14{
15 m_bDepthTest = TFALSE;
16 glDisable( GL_DEPTH_TEST );
17
18 m_bBlend = TFALSE;
19 glDisable( GL_BLEND );
20}
21
22T2RenderContext::~T2RenderContext()
23{
24}
25
26void T2RenderContext::ComputePerspectiveProjection( TMatrix44& a_rOutProjection, const T2Viewport& a_rViewportParams, const Projection& a_rProjParams )
27{
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;
44}
45
46void T2RenderContext::ComputeOrthographicProjection( TMatrix44& a_rOutProjection, const T2Viewport& a_rViewportParams, const Projection& a_rProjParams )
47{
48 TASSERT( a_rProjParams.vecProj.x != 0.0f );
49 TASSERT( a_rProjParams.vecProj.y != 0.0f );
50 TASSERT( a_rViewportParams.GetWidth() != 0.0f );
51 TASSERT( a_rViewportParams.GetHeight() != 0.0f );
52
53 TASSERT( TMath::IsFinite( a_rProjParams.vecProj.x ) && ( !TMath::IsNaN( a_rProjParams.vecProj.x ) ) );
54 TASSERT( TMath::IsFinite( a_rProjParams.vecProj.y ) && ( !TMath::IsNaN( a_rProjParams.vecProj.y ) ) );
55 TASSERT( TMath::IsFinite( a_rProjParams.vecCenter.x ) && ( !TMath::IsNaN( a_rProjParams.vecCenter.x ) ) );
56 TASSERT( TMath::IsFinite( a_rProjParams.vecCenter.y ) && ( !TMath::IsNaN( a_rProjParams.vecCenter.y ) ) );
57
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;
74}
75
76void T2RenderContext::ComputePerspectiveFrustum( Frustum& a_rcFrustum, const T2Viewport& a_rViewportParams, const Projection& a_rProjParams )
77{
78 TASSERT( a_rProjParams.vecProj.x != 0.0f );
79 TASSERT( a_rProjParams.vecProj.y != 0.0f );
80
81 TASSERT( TMath::IsFinite( a_rProjParams.vecProj.x ) && ( !TMath::IsNaN( a_rProjParams.vecProj.x ) ) );
82 TASSERT( TMath::IsFinite( a_rProjParams.vecProj.y ) && ( !TMath::IsNaN( a_rProjParams.vecProj.y ) ) );
83 TASSERT( TMath::IsFinite( a_rProjParams.vecCenter.x ) && ( !TMath::IsNaN( a_rProjParams.vecCenter.x ) ) );
84 TASSERT( TMath::IsFinite( a_rProjParams.vecCenter.y ) && ( !TMath::IsNaN( a_rProjParams.vecCenter.y ) ) );
85
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 );
89 TVector4 vec2 = TVector4( ( a_rViewportParams.GetWidth() - a_rProjParams.vecCenter.x ) * fVal1, vec1.y, 1.0f, 0.0f );
90 TVector4 vec3 = TVector4( vec2.x, -a_rProjParams.vecCenter.y * fVal2, 1.0f, 0.0f );
91 TVector4 vec4 = TVector4( vec1.x, vec3.y, 1.0f, 0.0f );
92
93 // Setup planes
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 );
98
99 // Normalize each plane
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();
104
105 // Setup near and far planes
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 );
108}
109
110void T2RenderContext::ComputeOrthographicFrustum( Frustum& a_rcFrustum, const T2Viewport& a_rViewportParams, const Projection& a_rProjParams )
111{
112 TASSERT( a_rProjParams.vecProj.x != 0.0f );
113 TASSERT( a_rProjParams.vecProj.y != 0.0f );
114 TASSERT( a_rViewportParams.GetWidth() != 0.0f );
115 TASSERT( a_rViewportParams.GetHeight() != 0.0f );
116
117 TASSERT( TMath::IsFinite( a_rProjParams.vecProj.x ) && ( !TMath::IsNaN( a_rProjParams.vecProj.x ) ) );
118 TASSERT( TMath::IsFinite( a_rProjParams.vecProj.y ) && ( !TMath::IsNaN( a_rProjParams.vecProj.y ) ) );
119 TASSERT( TMath::IsFinite( a_rProjParams.vecCenter.x ) && ( !TMath::IsNaN( a_rProjParams.vecCenter.x ) ) );
120 TASSERT( TMath::IsFinite( a_rProjParams.vecCenter.y ) && ( !TMath::IsNaN( a_rProjParams.vecCenter.y ) ) );
121
122 TFLOAT fWidth = a_rViewportParams.GetWidth();
123 TFLOAT fHeight = a_rViewportParams.GetHeight();
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;
128
129 // Setup planes
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 );
136}
137
138TBOOL T2RenderContext::CullSphereToFrustumSimple( const TSphere& a_rSphere, const TPlane* a_pPlanes, TINT a_iNumPlanes )
139{
140 for ( TSIZE i = 0; i < FRUSTUMPLANE_NUMOF; i++ )
141 {
142 TFLOAT fDist = TVector4::DotProduct3( a_rSphere.AsVector4(), a_pPlanes[ i ].AsVector4() );
143
144 if ( a_rSphere.GetRadius() < fDist - a_pPlanes[ i ].GetD() )
145 return TFALSE;
146 }
147
148 return TTRUE;
149}
150
151TINT T2RenderContext::CullSphereToFrustum( const TSphere& a_rSphere, const TPlane* a_pPlanes, TINT a_iClipFlags, TINT a_iClipFlagsMask )
152{
153 TINT iLeftPlanes = a_iClipFlags & a_iClipFlagsMask;
154 TINT iPlaneFlag = 1;
155
156 do {
157 if ( iLeftPlanes == 0 )
158 {
159 return a_iClipFlags;
160 }
161
162 if ( iLeftPlanes & iPlaneFlag )
163 {
164 TFLOAT fDist = TVector4::DotProduct3( a_rSphere.AsVector4(), a_pPlanes->AsVector4() ) - a_pPlanes->GetD();
165
166 if ( a_rSphere.GetRadius() < fDist )
167 {
168 return -1;
169 }
170
171 if ( fDist < -a_rSphere.GetRadius() )
172 {
173 a_iClipFlags &= ~iPlaneFlag;
174 }
175
176 iLeftPlanes &= ~iPlaneFlag;
177 }
178
179 iPlaneFlag = iPlaneFlag << 1;
180 a_pPlanes++;
181
182 } while ( TTRUE );
183}
184
185TBOOL T2RenderContext::SetShaderProgram( const T2Shader& a_rcShaderProgram )
186{
187 if ( m_uiCurrentShaderProgram != a_rcShaderProgram.GetProgram() )
188 {
189 a_rcShaderProgram.Use();
190 m_uiCurrentShaderProgram = a_rcShaderProgram.GetProgram();
191
192 return TTRUE;
193 }
194
195 return TFALSE;
196}
197
198GLuint T2RenderContext::GetTexture2D( TINT a_iTextureIndex )
199{
200 return m_aCurrentTextures[ a_iTextureIndex ];
201}
202
203void T2RenderContext::SetTexture2D( TINT a_iTextureIndex, GLuint a_uiTexture )
204{
205 if ( m_aCurrentTextures[ a_iTextureIndex ] != a_uiTexture )
206 {
207 if ( m_iCurrentTextureUnit != a_iTextureIndex )
208 {
209 glActiveTexture( GL_TEXTURE0 + a_iTextureIndex );
210 m_iCurrentTextureUnit = a_iTextureIndex;
211 }
212
213 glBindTexture( GL_TEXTURE_2D, a_uiTexture );
214 m_aCurrentTextures[ a_iTextureIndex ] = a_uiTexture;
215 }
216}
217
218void T2RenderContext::SetTexture2D( TINT a_iTextureIndex, const T2GLTexture& a_rcTexture )
219{
220 SetTexture2D( a_iTextureIndex, a_rcTexture.GetHandle() );
221}
222
223void T2RenderContext::ResetTexture2D( TINT a_iTextureIndex )
224{
225 if ( m_iCurrentTextureUnit != a_iTextureIndex )
226 {
227 glActiveTexture( GL_TEXTURE0 + a_iTextureIndex );
228 glBindTexture( GL_TEXTURE_2D, NULL );
229
230 m_iCurrentTextureUnit = a_iTextureIndex;
231 m_aCurrentTextures[ a_iTextureIndex ] = -1;
232 }
233}
234
235void T2RenderContext::EnableDepthTest( TBOOL a_bEnable )
236{
237 if ( m_bDepthTest != a_bEnable )
238 {
239 if ( a_bEnable )
240 glEnable( GL_DEPTH_TEST );
241 else
242 glDisable( GL_DEPTH_TEST );
243
244 m_bDepthTest = a_bEnable;
245 }
246}
247
248void T2RenderContext::EnableBlend( TBOOL a_bEnable )
249{
250 if ( m_bBlend != a_bEnable )
251 {
252 if ( a_bEnable )
253 glEnable( GL_BLEND );
254 else
255 glDisable( GL_BLEND );
256
257 m_bBlend = a_bEnable;
258 }
259}
260
4x4 matrix implementation for the Toshi engine
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
size_t TSIZE
Definition Typedefs.h:9
float TFLOAT
Definition Typedefs.h:4
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
TFORCEINLINE TBOOL IsFinite(TFLOAT fVal)
Definition TMathInline.h:41
TFORCEINLINE TBOOL IsNaN(TFLOAT fVal)
TFLOAT m_f21
Definition TMatrix44.h:361
TFLOAT m_f12
Definition TMatrix44.h:360
TFLOAT m_f44
Definition TMatrix44.h:363
TFLOAT m_f43
Definition TMatrix44.h:363
TFLOAT m_f32
Definition TMatrix44.h:362
TFLOAT m_f13
Definition TMatrix44.h:360
TFLOAT m_f14
Definition TMatrix44.h:360
TFLOAT m_f11
Definition TMatrix44.h:360
TFLOAT m_f23
Definition TMatrix44.h:361
TFLOAT m_f22
Definition TMatrix44.h:361
TFLOAT m_f41
Definition TMatrix44.h:363
TFLOAT m_f31
Definition TMatrix44.h:362
TFLOAT m_f24
Definition TMatrix44.h:361
TFLOAT m_f34
Definition TMatrix44.h:362
TFLOAT m_f42
Definition TMatrix44.h:363
TFLOAT m_f33
Definition TMatrix44.h:362
Definition TPlane.h:7
TFORCEINLINE TVector4 & AsVector4()
Definition TPlane.h:124
TFORCEINLINE constexpr TFLOAT GetD() const
Definition TPlane.h:69
TFORCEINLINE TVector4 & AsVector4()
Definition TSphere.h:111
constexpr TFORCEINLINE TFLOAT GetRadius() const
Definition TSphere.h:106
TFLOAT x
Definition TVector4.h:367
TFLOAT y
Definition TVector4.h:367
TFLOAT constexpr DotProduct3(const TVector4 &vec) const
Definition TVector4.h:339
GLuint GetHandle() const
TFLOAT GetWidth() const
TFLOAT GetHeight() const