OpenBarnyard
 
Loading...
Searching...
No Matches
TRenderContext.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "TRenderContext.h"
3#include "TRenderInterface.h"
4
5//-----------------------------------------------------------------------------
6// Enables memory debugging.
7// Note: Should be the last include!
8//-----------------------------------------------------------------------------
10
12
14{
15 if ( a_bDirty ) m_eFlags |= FLAG_DIRTY;
16 else
18}
19
20void TRenderContext::SetFlag( FLAG a_eFlag, TBOOL a_bEnable )
21{
22 if ( a_bEnable ) m_eFlags |= a_eFlag;
23 else
24 m_eFlags &= ~a_eFlag;
25}
26
28{
29 if ( a_bEnable ) m_eFlags |= FLAG_FOG;
30 else
32}
33
41
43 : m_pRenderer( a_pRenderer ), m_eFlags( FLAG_DIRTY ), m_eClipFlags( 0x3F ), m_eCameraMode( CameraMode_Perspective ), m_pCurrentSkeletonInstance( TNULL ), m_pCurrentCameraObject( TNULL ), m_AmbientColor( 1.0f, 1.0f, 1.0f, 0.0f ), m_FogColor( 1.0f, 1.0f, 1.0f, 0.0f ), m_fFogDistanceStart( 10.0f ), m_fFogDistanceEnd( 1000.0f ), m_fAlphaBlend( 1.0f ), m_fShadeCoeff( 0.0f )
44{
45 TIMPLEMENT();
46
47 // Setup viewport parameters
48 if ( m_pRenderer->GetCurrentDevice() != TNULL )
49 {
50 auto pDevice = m_pRenderer->GetCurrentDevice();
51 auto pMode = pDevice->GetMode();
52 m_oViewportParams.fWidth = TFLOAT( pMode->GetWidth() );
53 m_oViewportParams.fHeight = TFLOAT( pMode->GetHeight() );
54 }
55 else
56 {
57 m_oViewportParams.fWidth = 640.0f;
58 m_oViewportParams.fHeight = 480.0f;
59 }
60
61 m_oViewportParams.fX = 0;
62 m_oViewportParams.fY = 0;
63 m_oViewportParams.fMaxZ = 1.0f;
64 m_oViewportParams.fMinZ = 1.0f;
65
66 // Setup projection parameters
67 m_oProjParams.SetFromFOV( m_oViewportParams.fWidth, m_oViewportParams.fHeight, TMath::DegToRad( 45.0f ), 1.0f, 1000.0f );
68
69 m_oModelViewMatrix.Identity();
70 m_oWorldViewMatrix.Identity();
72
73 m_AmbientColor.Set( 0.0f, 0.0f, 0.0f, 0.0f );
75}
76
80
87
94
96{
97 TASSERT( params.m_Proj.x != 0.0f );
98 TASSERT( params.m_Proj.y != 0.0f );
99 TASSERT( TMath::IsFinite( params.m_Proj.x ) && ( !TMath::IsNaN( params.m_Proj.x ) ) );
100 TASSERT( TMath::IsFinite( params.m_Proj.y ) && ( !TMath::IsNaN( params.m_Proj.y ) ) );
101 TASSERT( TMath::IsFinite( params.m_Centre.x ) && ( !TMath::IsNaN( params.m_Centre.x ) ) );
102 TASSERT( TMath::IsFinite( params.m_Centre.y ) && ( !TMath::IsNaN( params.m_Centre.y ) ) );
103
104 m_oProjParams = params;
106}
107
113
115{
116 m_eCameraMode = cameraMode;
118}
119
120void TRenderContext::ComputePerspectiveProjection( TMatrix44& a_rOutProjection, const VIEWPORTPARAMS& a_rViewportParams, const PROJECTIONPARAMS& a_rProjParams )
121{
122 a_rOutProjection.m_f11 = ( a_rProjParams.m_Proj.x * 2.0f ) / a_rViewportParams.fWidth;
123 a_rOutProjection.m_f12 = 0.0f;
124 a_rOutProjection.m_f13 = 0.0f;
125 a_rOutProjection.m_f14 = 0.0f;
126 a_rOutProjection.m_f21 = 0.0f;
127 a_rOutProjection.m_f22 = -( ( a_rProjParams.m_Proj.y * 2.0f ) / a_rViewportParams.fHeight );
128 a_rOutProjection.m_f23 = 0.0f;
129 a_rOutProjection.m_f24 = 0.0f;
130 a_rOutProjection.m_f31 = ( a_rProjParams.m_Centre.x * 2.0f ) / a_rViewportParams.fWidth - 1.0f;
131 a_rOutProjection.m_f32 = -( ( a_rProjParams.m_Centre.y * 2.0f ) / a_rViewportParams.fHeight - 1.0f );
132 a_rOutProjection.m_f33 = a_rProjParams.m_fFarClip / ( a_rProjParams.m_fFarClip - a_rProjParams.m_fNearClip );
133 a_rOutProjection.m_f34 = 1.0f;
134 a_rOutProjection.m_f41 = 0.0f;
135 a_rOutProjection.m_f42 = 0.0f;
136 a_rOutProjection.m_f43 = -( ( a_rProjParams.m_fNearClip * a_rProjParams.m_fFarClip ) / ( a_rProjParams.m_fFarClip - a_rProjParams.m_fNearClip ) );
137 a_rOutProjection.m_f44 = 0.0f;
138}
139
140void TRenderContext::ComputeOrthographicProjection( TMatrix44& a_rOutProjection, const VIEWPORTPARAMS& a_rViewportParams, const PROJECTIONPARAMS& a_rProjParams )
141{
142 TASSERT( a_rProjParams.m_Proj.x != 0.0f );
143 TASSERT( a_rProjParams.m_Proj.y != 0.0f );
144 TASSERT( a_rViewportParams.fWidth != 0.0f );
145 TASSERT( a_rViewportParams.fHeight != 0.0f );
146
147 TASSERT( TMath::IsFinite( a_rProjParams.m_Proj.x ) && ( !TMath::IsNaN( a_rProjParams.m_Proj.x ) ) );
148 TASSERT( TMath::IsFinite( a_rProjParams.m_Proj.y ) && ( !TMath::IsNaN( a_rProjParams.m_Proj.y ) ) );
149 TASSERT( TMath::IsFinite( a_rProjParams.m_Centre.x ) && ( !TMath::IsNaN( a_rProjParams.m_Centre.x ) ) );
150 TASSERT( TMath::IsFinite( a_rProjParams.m_Centre.y ) && ( !TMath::IsNaN( a_rProjParams.m_Centre.y ) ) );
151
152 a_rOutProjection.m_f11 = ( a_rProjParams.m_Proj.x * 2.0f ) / a_rViewportParams.fWidth;
153 a_rOutProjection.m_f12 = 0.0f;
154 a_rOutProjection.m_f13 = 0.0f;
155 a_rOutProjection.m_f14 = 0.0f;
156 a_rOutProjection.m_f21 = 0.0f;
157 a_rOutProjection.m_f22 = -( ( a_rProjParams.m_Proj.y * 2.0f ) / a_rViewportParams.fHeight );
158 a_rOutProjection.m_f23 = 0.0f;
159 a_rOutProjection.m_f24 = 0.0f;
160 a_rOutProjection.m_f31 = 0.0f;
161 a_rOutProjection.m_f32 = 0.0f;
162 a_rOutProjection.m_f33 = 1.0f / ( a_rProjParams.m_fFarClip - a_rProjParams.m_fNearClip );
163 a_rOutProjection.m_f34 = 0.0f;
164 a_rOutProjection.m_f41 = ( a_rProjParams.m_Centre.x * 2.0f ) / a_rViewportParams.fWidth - 1.0f;
165 a_rOutProjection.m_f42 = -( ( a_rProjParams.m_Centre.y * 2.0f ) / a_rViewportParams.fHeight - 1.0f );
166 a_rOutProjection.m_f43 = -( a_rProjParams.m_fNearClip / ( a_rProjParams.m_fFarClip - a_rProjParams.m_fNearClip ) );
167 a_rOutProjection.m_f44 = 1.0f;
168}
169
171{
172 return m_eClipFlags;
173}
174
176{
177 return std::exchange( m_eClipFlags, a_uiClipFlags );
178}
179
181{
183 {
184 auto& viewWorld = GetViewWorldMatrix();
185
186 for ( TSIZE i = 0; i < WORLDPLANE_NUMOF; i++ )
187 {
189 }
190
192 }
193
194 return m_aWorldPlanes;
195}
196
197TBOOL TRenderContext::CullSphereToFrustumSimple( const TSphere& a_rSphere, const TPlane* a_pPlanes, TINT a_iNumPlanes )
198{
199 for ( TSIZE i = 0; i < WORLDPLANE_NUMOF; i++ )
200 {
201 TFLOAT fDist = TVector4::DotProduct3( a_rSphere.AsVector4(), a_pPlanes[ i ].AsVector4() );
202
203 if ( a_rSphere.GetRadius() < fDist - a_pPlanes[ i ].GetD() )
204 return TFALSE;
205 }
206
207 return TTRUE;
208}
209
210TINT TRenderContext::CullSphereToFrustum( const TSphere& a_rSphere, const TPlane* a_pPlanes, TINT a_iClipFlags, TINT a_iClipFlagsMask )
211{
212 TINT iLeftPlanes = a_iClipFlags & a_iClipFlagsMask;
213 TINT iPlaneFlag = 1;
214
215 do {
216 if ( iLeftPlanes == 0 )
217 {
218 return a_iClipFlags;
219 }
220
221 if ( iLeftPlanes & iPlaneFlag )
222 {
223 TFLOAT fDist = TVector4::DotProduct3( a_rSphere.AsVector4(), a_pPlanes->AsVector4() ) - a_pPlanes->GetD();
224
225 if ( a_rSphere.GetRadius() < fDist )
226 {
227 return -1;
228 }
229
230 if ( fDist < -a_rSphere.GetRadius() )
231 {
232 a_iClipFlags &= ~iPlaneFlag;
233 }
234
235 iLeftPlanes &= ~iPlaneFlag;
236 }
237
238 iPlaneFlag = iPlaneFlag << 1;
239 a_pPlanes++;
240
241 } while ( TTRUE );
242}
243
254
265
276
287
289{
290 m_oLightIds.Reset();
291}
292
294{
295 m_oLightIds.Add( a_iLightId );
296}
297
298void TRenderContext::ComputePerspectiveFrustum( TFrustum& a_rcFrustum, const VIEWPORTPARAMS& a_rViewportParams, const PROJECTIONPARAMS& a_rProjParams )
299{
300 TASSERT( a_rProjParams.m_Proj.x != 0.0f );
301 TASSERT( a_rProjParams.m_Proj.y != 0.0f );
302
303 TASSERT( TMath::IsFinite( a_rProjParams.m_Proj.x ) && ( !TMath::IsNaN( a_rProjParams.m_Proj.x ) ) );
304 TASSERT( TMath::IsFinite( a_rProjParams.m_Proj.y ) && ( !TMath::IsNaN( a_rProjParams.m_Proj.y ) ) );
305 TASSERT( TMath::IsFinite( a_rProjParams.m_Centre.x ) && ( !TMath::IsNaN( a_rProjParams.m_Centre.x ) ) );
306 TASSERT( TMath::IsFinite( a_rProjParams.m_Centre.y ) && ( !TMath::IsNaN( a_rProjParams.m_Centre.y ) ) );
307
308 TFLOAT fVal1 = 1.0f / a_rProjParams.m_Proj.x;
309 TFLOAT fVal2 = 1.0f / a_rProjParams.m_Proj.y;
310 TVector4 vec1 = TVector4( -a_rProjParams.m_Centre.x * fVal1, ( a_rViewportParams.fHeight - a_rProjParams.m_Centre.y ) * fVal2, 1.0f, 0.0f );
311 TVector4 vec2 = TVector4( ( a_rViewportParams.fWidth - a_rProjParams.m_Centre.x ) * fVal1, vec1.y, 1.0f, 0.0f );
312 TVector4 vec3 = TVector4( vec2.x, -a_rProjParams.m_Centre.y * fVal2, 1.0f, 0.0f );
313 TVector4 vec4 = TVector4( vec1.x, vec3.y, 1.0f, 0.0f );
314
315 // Setup planes
316 a_rcFrustum[ WORLDPLANE_LEFT ].AsVector4().CrossProduct( vec4, vec1 );
317 a_rcFrustum[ WORLDPLANE_RIGHT ].AsVector4().CrossProduct( vec2, vec3 );
318 a_rcFrustum[ WORLDPLANE_BOTTOM ].AsVector4().CrossProduct( vec3, vec4 );
319 a_rcFrustum[ WORLDPLANE_TOP ].AsVector4().CrossProduct( vec1, vec2 );
320
321 // Normalize each plane
322 a_rcFrustum[ WORLDPLANE_LEFT ].AsNormal().Normalize();
323 a_rcFrustum[ WORLDPLANE_RIGHT ].AsNormal().Normalize();
324 a_rcFrustum[ WORLDPLANE_BOTTOM ].AsNormal().Normalize();
325 a_rcFrustum[ WORLDPLANE_TOP ].AsNormal().Normalize();
326
327 // Setup near and far planes
328 a_rcFrustum[ WORLDPLANE_NEAR ].Set( 0.0f, 0.0f, -1.0f, -a_rProjParams.m_fNearClip );
329 a_rcFrustum[ WORLDPLANE_FAR ].Set( 0.0f, 0.0f, 1.0f, a_rProjParams.m_fFarClip );
330}
331
332void TRenderContext::ComputeOrthographicFrustum( TFrustum& a_rcFrustum, const VIEWPORTPARAMS& a_rViewportParams, const PROJECTIONPARAMS& a_rProjParams )
333{
334 TASSERT( a_rProjParams.m_Proj.x != 0.0f );
335 TASSERT( a_rProjParams.m_Proj.y != 0.0f );
336 TASSERT( a_rViewportParams.fWidth != 0.0f );
337 TASSERT( a_rViewportParams.fHeight != 0.0f );
338
339 TASSERT( TMath::IsFinite( a_rProjParams.m_Proj.x ) && ( !TMath::IsNaN( a_rProjParams.m_Proj.x ) ) );
340 TASSERT( TMath::IsFinite( a_rProjParams.m_Proj.y ) && ( !TMath::IsNaN( a_rProjParams.m_Proj.y ) ) );
341 TASSERT( TMath::IsFinite( a_rProjParams.m_Centre.x ) && ( !TMath::IsNaN( a_rProjParams.m_Centre.x ) ) );
342 TASSERT( TMath::IsFinite( a_rProjParams.m_Centre.y ) && ( !TMath::IsNaN( a_rProjParams.m_Centre.y ) ) );
343
344 TFLOAT fWidth = a_rViewportParams.fWidth;
345 TFLOAT fHeight = a_rViewportParams.fHeight;
346 TFLOAT fCentreX = a_rProjParams.m_Centre.x;
347 TFLOAT fCentreY = a_rProjParams.m_Centre.y;
348 TFLOAT fProjXOverOne = 1.0f / a_rProjParams.m_Proj.x;
349 TFLOAT fProjYOverOne = 1.0f / a_rProjParams.m_Proj.y;
350
351 // Setup planes
352 a_rcFrustum[ WORLDPLANE_LEFT ].Set( -1.0f, 0.0f, 0.0f, fCentreX * fProjXOverOne );
353 a_rcFrustum[ WORLDPLANE_RIGHT ].Set( 1.0f, 0.0f, 0.0f, ( fWidth - fCentreX ) * fProjXOverOne );
354 a_rcFrustum[ WORLDPLANE_BOTTOM ].Set( 0.0f, -1.0f, 0.0f, fCentreY * fProjYOverOne );
355 a_rcFrustum[ WORLDPLANE_TOP ].Set( 0.0f, 1.0f, 0.0f, ( fHeight - fCentreY ) * fProjYOverOne );
356 a_rcFrustum[ WORLDPLANE_NEAR ].Set( 0.0f, 0.0f, -1.0f, -a_rProjParams.m_fNearClip );
357 a_rcFrustum[ WORLDPLANE_FAR ].Set( 0.0f, 0.0f, 1.0f, a_rProjParams.m_fFarClip );
358}
359
360void TRenderContext::PROJECTIONPARAMS::SetFromFOV( TFLOAT a_fViewportWidth, TFLOAT a_fViewportHeight, TFLOAT a_fFOV, TFLOAT a_fNearPlane, TFLOAT a_fFarPlane )
361{
362 TFLOAT fHalfWidth = a_fViewportWidth * 0.5f;
363 TFLOAT fHalfHeight = a_fViewportHeight * 0.5f;
364 TFLOAT fProj = fHalfHeight / TMath::Tan( a_fFOV );
365
366 m_fNearClip = a_fNearPlane;
367 m_fFarClip = a_fFarPlane;
368 m_Centre.x = fHalfWidth;
369 m_Centre.y = fHalfHeight;
370 m_Proj.x = fProj;
371 m_Proj.y = fProj;
372}
373
374void TLightIDList::Add( TLightID a_iLightId )
375{
376 for ( TINT i = 0; i < 4; i++ )
377 {
378 if ( aIDs[ i ] == -1 )
379 {
380 aIDs[ i ] = a_iLightId;
381 return;
382 }
383 }
384}
385
387{
388 aIDs[ 3 ] = -1;
389 aIDs[ 2 ] = -1;
390 aIDs[ 1 ] = -1;
391 aIDs[ 0 ] = -1;
392}
393
395{
396 TASSERT( a_iIndex < MAX_NUM_LIGHTS );
397 return aIDs[ a_iIndex ];
398}
399
TPlane[WORLDPLANE_NUMOF] TFrustum
TINT8 TLightID
@ WORLDPLANE_BOTTOM
@ WORLDPLANE_NUMOF
@ WORLDPLANE_TOP
@ WORLDPLANE_RIGHT
@ WORLDPLANE_FAR
@ WORLDPLANE_NEAR
@ WORLDPLANE_LEFT
Rendering system interface for the Toshi engine.
#define TIMPLEMENT()
Definition Defines.h:136
#define TASSERT(X,...)
Definition Defines.h:138
#define HASANYFLAG(STATE, FLAG)
Definition Defines.h:5
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
unsigned int TUINT
Definition Typedefs.h:8
size_t TSIZE
Definition Typedefs.h:9
float TFLOAT
Definition Typedefs.h:4
#define TNULL
Definition Typedefs.h:23
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 TFLOAT Tan(TFLOAT fVal)
Definition TMathInline.h:44
TFORCEINLINE TBOOL IsFinite(TFLOAT fVal)
Definition TMathInline.h:41
TFORCEINLINE TBOOL IsNaN(TFLOAT fVal)
TFORCEINLINE constexpr TFLOAT DegToRad(TFLOAT fDeg)
Definition TMathInline.h:64
TFLOAT m_f21
Definition TMatrix44.h:361
TFLOAT m_f12
Definition TMatrix44.h:360
static void TransformPlaneOrthogonal(TPlane &a_rOutPlane, const TMatrix44 &a_rMatrix, const TPlane &a_rcSourcePlane)
Definition TMatrix44.h:239
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 y
Definition TVector2.h:139
TFLOAT x
Definition TVector2.h:139
TFLOAT x
Definition TVector4.h:367
TFLOAT y
Definition TVector4.h:367
TFLOAT constexpr DotProduct3(const TVector4 &vec) const
Definition TVector4.h:339
TLightID aIDs[MAX_NUM_LIGHTS]
TLightID & operator[](TINT a_iIndex)
void Add(TLightID a_iLightId)
static constexpr TINT MAX_NUM_LIGHTS
PROJECTIONPARAMS m_oProjParams
static TBOOL CullSphereToFrustumSimple(const TSphere &a_rSphere, const TPlane *a_pPlanes, TINT a_iNumPlanes)
TRenderContext(TRenderInterface *pRender)
TMatrix44 m_oViewWorldMatrix
void SetProjectionParams(const PROJECTIONPARAMS &a_rParams)
TVector4 m_AmbientColor
virtual ~TRenderContext()
CameraMode m_eCameraMode
static void ComputeOrthographicFrustum(TFrustum &a_rcFrustum, const VIEWPORTPARAMS &a_rViewportParams, const PROJECTIONPARAMS &a_rProjParams)
TUINT SetClipFlags(TUINT a_uiClipFlags)
void SetFlag(FLAG a_eFlag, TBOOL a_bEnable)
virtual void SetWorldViewMatrix(const TMatrix44 &a_rMatrix)
TFrustum m_aFrustumPlanes1
TFLOAT m_fFogDistanceStart
TFrustum m_aWorldPlanes
const TPlane * GetWorldPlanes()
const TMatrix44 & GetModelWorldMatrix()
void SetViewportParameters(const VIEWPORTPARAMS &a_rParams)
VIEWPORTPARAMS m_oViewportParams
static void ComputeOrthographicProjection(TMatrix44 &a_rOutProjection, const VIEWPORTPARAMS &a_rViewportParams, const PROJECTIONPARAMS &a_rProjParams)
TMatrix44 m_oModelWorldMatrix
TMatrix44 m_oWorldViewMatrix
static TINT CullSphereToFrustum(const TSphere &a_rSphere, const TPlane *a_pPlanes, TINT a_iClipFlags, TINT a_iClipFlagsMask)
TMatrix44 m_oModelViewMatrix
void AddLight(TLightID a_iLightId)
void SetFogDistance(TFLOAT a_fStart, TFLOAT a_fEnd)
TLightIDList m_oLightIds
TUINT GetClipFlags() const
void SetDirty(TBOOL a_bDirty)
virtual void SetModelViewMatrix(const TMatrix44 &a_rMatrix)
TRenderInterface * m_pRenderer
TSkeletonInstance * m_pCurrentSkeletonInstance
static void ComputePerspectiveProjection(TMatrix44 &a_rOutProjection, const VIEWPORTPARAMS &a_rViewportParams, const PROJECTIONPARAMS &a_rProjParams)
static void ComputePerspectiveFrustum(TFrustum &a_rcFrustum, const VIEWPORTPARAMS &a_rViewportParams, const PROJECTIONPARAMS &a_rProjParams)
const TMatrix44 & GetViewWorldMatrix()
const TMatrix44 & GetWorldModelMatrix()
void EnableFog(TBOOL a_bEnable)
const TMatrix44 & GetViewModelMatrix()
TMatrix44 m_oWorldModelMatrix
TCameraObject * m_pCurrentCameraObject
void SetCameraMode(CameraMode a_eCameraMode)
TMatrix44 m_oViewModelMatrix
void SetFromFOV(TFLOAT a_fViewportWidth, TFLOAT a_fViewportHeight, TFLOAT a_fFOV, TFLOAT a_fNearPlane, TFLOAT a_fFarPlane)