forked from XutaxKamay/css_enhanced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrendermanager.cpp
255 lines (201 loc) · 7.51 KB
/
rendermanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: The main manager of the rendering
//
// $Revision: $
// $NoKeywords: $
//===========================================================================//
#include "rendermanager.h"
#include "legion.h"
#include "uimanager.h"
#include "worldmanager.h"
#include "materialsystem/imaterialsystem.h"
#include "tier2/tier2.h"
//-----------------------------------------------------------------------------
// Camera property
//-----------------------------------------------------------------------------
DEFINE_FIXEDSIZE_ALLOCATOR( CCameraProperty, 1, CMemoryPool::GROW_SLOW );
CCameraProperty::CCameraProperty()
{
m_Origin.Init();
m_Angles.Init();
m_Velocity.Init();
m_AngVelocity.Init();
}
void CCameraProperty::GetForward( Vector *pForward )
{
AngleVectors( m_Angles, pForward );
}
//-----------------------------------------------------------------------------
// Singleton accessor
//-----------------------------------------------------------------------------
static CRenderManager s_RenderManager;
extern CRenderManager *g_pRenderManager = &s_RenderManager;
//-----------------------------------------------------------------------------
// Game initialization
//-----------------------------------------------------------------------------
bool CRenderManager::Init()
{
m_bRenderWorldFullscreen = true;
return true;
}
void CRenderManager::Shutdown()
{
}
//-----------------------------------------------------------------------------
// Level initialization
//-----------------------------------------------------------------------------
LevelRetVal_t CRenderManager::LevelInit( bool bFirstCall )
{
return FINISHED;
}
LevelRetVal_t CRenderManager::LevelShutdown( bool bFirstCall )
{
return FINISHED;
}
//-----------------------------------------------------------------------------
// Property allocation
//-----------------------------------------------------------------------------
CCameraProperty *CRenderManager::CreateCameraProperty()
{
return new CCameraProperty;
}
void CRenderManager::DestroyCameraProperty( CCameraProperty *pProperty )
{
delete pProperty;
}
//-----------------------------------------------------------------------------
// Sets the rectangle to draw into
//-----------------------------------------------------------------------------
void CRenderManager::RenderWorldFullscreen()
{
m_bRenderWorldFullscreen = true;
}
void CRenderManager::RenderWorldInRect( int x, int y, int nWidth, int nHeight )
{
m_bRenderWorldFullscreen = false;
m_nRenderX = x;
m_nRenderY = y;
m_nRenderWidth = nWidth;
m_nRenderHeight = nHeight;
}
//-----------------------------------------------------------------------------
// Done completely client-side, want total smoothness, so simulate at render interval
//-----------------------------------------------------------------------------
void CRenderManager::UpdateLocalPlayerCamera()
{
float dt = IGameManager::DeltaTime();
CCameraProperty *pCamera = g_pWorldManager->GetLocalPlayer()->m_pCameraProperty;
VectorMA( pCamera->m_Origin, dt, pCamera->m_Velocity, pCamera->m_Origin );
VectorMA( pCamera->m_Angles, dt, pCamera->m_AngVelocity, pCamera->m_Angles );
}
//-----------------------------------------------------------------------------
// Per-frame update
//-----------------------------------------------------------------------------
void CRenderManager::Update( )
{
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
if ( GetLevelState() == NOT_IN_LEVEL )
{
g_pMaterialSystem->BeginFrame( 0 );
pRenderContext->ClearColor4ub( 76, 88, 68, 255 );
pRenderContext->ClearBuffers( true, true );
g_pUIManager->DrawUI();
g_pMaterialSystem->EndFrame();
g_pMaterialSystem->SwapBuffers();
return;
}
UpdateLocalPlayerCamera();
g_pMaterialSystem->BeginFrame( 0 );
pRenderContext->ClearColor4ub( 0, 0, 0, 255 );
pRenderContext->ClearBuffers( true, true );
RenderWorld();
g_pUIManager->DrawUI();
g_pMaterialSystem->EndFrame();
g_pMaterialSystem->SwapBuffers();
}
//-----------------------------------------------------------------------------
// Sets up the camera
//-----------------------------------------------------------------------------
void CRenderManager::SetupCameraRenderState( )
{
CCameraProperty *pCamera = g_pWorldManager->GetLocalPlayer()->m_pCameraProperty;
matrix3x4_t cameraToWorld;
AngleMatrix( pCamera->m_Angles, pCamera->m_Origin, cameraToWorld );
matrix3x4_t matRotate;
matrix3x4_t matRotateZ;
MatrixBuildRotationAboutAxis( Vector(0,0,1), -90, matRotateZ );
MatrixMultiply( cameraToWorld, matRotateZ, matRotate );
matrix3x4_t matRotateX;
MatrixBuildRotationAboutAxis( Vector(1,0,0), 90, matRotateX );
MatrixMultiply( matRotate, matRotateX, matRotate );
matrix3x4_t view;
MatrixInvert( matRotate, view );
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->LoadMatrix( view );
}
//-----------------------------------------------------------------------------
// Set up a projection matrix for a 90 degree fov
//-----------------------------------------------------------------------------
// FIXME: Better control over Z range
#define ZNEAR 0.1f
#define ZFAR 10000.0f
void CRenderManager::SetupProjectionMatrix( int nWidth, int nHeight, float flFOV )
{
VMatrix proj;
float flZNear = ZNEAR;
float flZFar = ZFAR;
float flApsectRatio = (nHeight != 0.0f) ? (float)nWidth / (float)nHeight : 100.0f;
float halfWidth = tan( flFOV * M_PI / 360.0 );
float halfHeight = halfWidth / flApsectRatio;
memset( proj.Base(), 0, sizeof( proj ) );
proj[0][0] = 1.0f / halfWidth;
proj[1][1] = 1.0f / halfHeight;
proj[2][2] = flZFar / ( flZNear - flZFar );
proj[3][2] = -1.0f;
proj[2][3] = flZNear * flZFar / ( flZNear - flZFar );
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->LoadMatrix( proj );
}
//-----------------------------------------------------------------------------
// Set up a orthographic projection matrix
//-----------------------------------------------------------------------------
void CRenderManager::SetupOrthoMatrix( int nWidth, int nHeight )
{
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->LoadIdentity();
pRenderContext->Ortho( 0, 0, nWidth, nHeight, -1.0f, 1.0f );
}
//-----------------------------------------------------------------------------
// Renders the world
//-----------------------------------------------------------------------------
void CRenderManager::RenderWorld()
{
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PushMatrix();
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->PushMatrix();
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PushMatrix();
pRenderContext->LoadIdentity();
if ( m_bRenderWorldFullscreen )
{
m_nRenderX = m_nRenderY = 0;
pRenderContext->GetRenderTargetDimensions( m_nRenderWidth, m_nRenderHeight );
}
pRenderContext->DepthRange( 0, 1 );
pRenderContext->Viewport( m_nRenderX, m_nRenderY, m_nRenderWidth, m_nRenderHeight );
SetupProjectionMatrix( m_nRenderWidth, m_nRenderHeight, 90 );
SetupCameraRenderState();
g_pWorldManager->DrawWorld();
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
pRenderContext->PopMatrix();
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->PopMatrix();
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->PopMatrix();
}