-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmecamera.cpp
500 lines (432 loc) · 15.6 KB
/
dmecamera.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//======= Copyright (c) 1996-2009, Valve Corporation, All rights reserved. ======
//
// A class representing a camera
//
//===============================================================================
#include "movieobjects/dmecamera.h"
#include "tier0/dbg.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "mathlib/vector.h"
#include "movieobjects/dmetransform.h"
#include "materialsystem/imaterialsystem.h"
#include "movieobjects_interfaces.h"
#include "tier2/tier2.h"
// FIXME: REMOVE
#include "istudiorender.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeCamera, CDmeCamera );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDmeCamera::OnConstruction()
{
m_flFieldOfView.InitAndSet( this, "fieldOfView", 30.0f );
// FIXME: This currently matches the client DLL for HL2
// but we probably need a way of getting this state from the client DLL
m_zNear.InitAndSet( this, "znear", 3.0f );
m_zFar.InitAndSet( this, "zfar", 16384.0f * 1.73205080757f );
m_flFocalDistance.InitAndSet( this, "focalDistance", 72.0f);
m_flZeroParallaxDistance.InitAndSet( this, "zeroParallaxDistance", 75.0f );
m_flEyeSeparation.InitAndSet( this, "eyeSeparation", 0.75f );
m_flAperture.InitAndSet( this, "aperture", 0.2f);
m_shutterSpeed.InitAndSet( this, "shutterSpeed", DmeTime_t( 0.5f / 24.0f ) );
m_flToneMapScale.InitAndSet( this, "toneMapScale", 1.0f );
m_flAOBias.InitAndSet( this, "SSAOBias", 0.0005f );
m_flAOStrength.InitAndSet( this, "SSAOStrength", 1.0f );
m_flAORadius.InitAndSet( this, "SSAORadius", 15.0f );
m_flBloomScale.InitAndSet( this, "bloomScale", 0.28f );
m_nDoFQuality.InitAndSet( this, "depthOfFieldQuality", 0 );
m_nMotionBlurQuality.InitAndSet( this, "motionBlurQuality", 0 );
m_flBloomWidth.InitAndSet( this, "bloomWidth", 9.0f );
m_bOrtho.InitAndSet( this, "ortho", false );
// Ortho
m_nAxis.InitAndSet( this, "axis", 0 );
m_bWasBehindFrustum.InitAndSet( this, "behindfrustum", false );
m_flDistance.InitAndSet( this, "distance", 32.0f );
for ( int i = 0; i < AXIS_COUNT; ++i )
{
char sz[ 32 ];
Q_snprintf( sz, sizeof( sz ), "scale%d", i );
m_flScale[ i ].InitAndSet( this, sz, 1.0f );
Q_snprintf( sz, sizeof( sz ), "lookat%d", i );
m_vecLookAt[ i ].Init( this, sz );
}
m_vecAxis.Init();
m_vecOrigin.Init();
m_angRotation.Init();
SetIdentityMatrix( m_Transform );
}
void CDmeCamera::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Loads the material system view matrix based on the transform
//-----------------------------------------------------------------------------
void CDmeCamera::LoadViewMatrix( bool bUseEngineCoordinateSystem )
{
if ( !g_pMaterialSystem )
return;
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
VMatrix view;
GetViewMatrix( view, bUseEngineCoordinateSystem );
pRenderContext->MatrixMode( MATERIAL_VIEW );
pRenderContext->LoadMatrix( view );
}
//-----------------------------------------------------------------------------
// Loads the material system projection matrix based on the fov, etc.
//-----------------------------------------------------------------------------
void CDmeCamera::LoadProjectionMatrix( int nDisplayWidth, int nDisplayHeight )
{
if ( !g_pMaterialSystem )
return;
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->MatrixMode( MATERIAL_PROJECTION );
VMatrix proj;
GetProjectionMatrix( proj, nDisplayWidth, nDisplayHeight );
pRenderContext->LoadMatrix( proj );
}
//-----------------------------------------------------------------------------
// Sets up studiorender camera state
//-----------------------------------------------------------------------------
void CDmeCamera::LoadStudioRenderCameraState()
{
// FIXME: Remove this! This should automatically happen in DrawModel
// in studiorender.
if ( !g_pStudioRender )
return;
matrix3x4_t transform;
GetTransform()->GetTransform( transform );
Vector vecOrigin, vecRight, vecUp, vecForward;
MatrixGetColumn( transform, 0, vecRight );
MatrixGetColumn( transform, 1, vecUp );
MatrixGetColumn( transform, 2, vecForward );
MatrixGetColumn( transform, 3, vecOrigin );
g_pStudioRender->SetViewState( vecOrigin, vecRight, vecUp, vecForward );
}
//-----------------------------------------------------------------------------
// Returns the x FOV (the full angle)
//-----------------------------------------------------------------------------
float CDmeCamera::GetFOVx() const
{
return m_flFieldOfView;
}
//-----------------------------------------------------------------------------
// Returns the near Z in inches
//-----------------------------------------------------------------------------
float CDmeCamera::GetNearZ() const
{
return m_zNear;
}
//-----------------------------------------------------------------------------
// Sets the near Z in inches
//-----------------------------------------------------------------------------
void CDmeCamera::SetNearZ( float zNear )
{
m_zNear = zNear;
}
//-----------------------------------------------------------------------------
// Returns the far Z in inches
//-----------------------------------------------------------------------------
float CDmeCamera::GetFarZ() const
{
return m_zFar;
}
//-----------------------------------------------------------------------------
// Sets the far Z in inches
//-----------------------------------------------------------------------------
void CDmeCamera::SetFarZ( float zFar )
{
m_zFar = zFar;
}
void CDmeCamera::SetFOVx( float fov )
{
m_flFieldOfView = fov;
}
//-----------------------------------------------------------------------------
// Returns the focal distance in inches
//-----------------------------------------------------------------------------
float CDmeCamera::GetFocalDistance() const
{
return m_flFocalDistance;
}
//-----------------------------------------------------------------------------
// Sets the focal distance in inches
//-----------------------------------------------------------------------------
void CDmeCamera::SetFocalDistance( const float &flFocalDistance )
{
m_flFocalDistance = flFocalDistance;
}
//-----------------------------------------------------------------------------
// Returns the zero-parallax distance in inches
//-----------------------------------------------------------------------------
float CDmeCamera::GetZeroParallaxDistance() const
{
return m_flZeroParallaxDistance;
}
//-----------------------------------------------------------------------------
// Sets the zero-parallax distance in inches
//-----------------------------------------------------------------------------
void CDmeCamera::SetZeroParallaxDistance( const float &flZeroParallaxDistance )
{
m_flZeroParallaxDistance = flZeroParallaxDistance;
}
//-----------------------------------------------------------------------------
// Returns the eye separation distance in inches
//-----------------------------------------------------------------------------
float CDmeCamera::GetEyeSeparation() const
{
return m_flEyeSeparation;
}
//-----------------------------------------------------------------------------
// Sets the eye separation distance in inches
//-----------------------------------------------------------------------------
void CDmeCamera::SetEyeSeparation( const float &flEyeSeparation )
{
m_flEyeSeparation = flEyeSeparation;
}
//-----------------------------------------------------------------------------
// Returns the camera aperture in inches
//-----------------------------------------------------------------------------
float CDmeCamera::GetAperture() const
{
return m_flAperture;
}
//-----------------------------------------------------------------------------
// Returns the camera aperture in inches
//-----------------------------------------------------------------------------
DmeTime_t CDmeCamera::GetShutterSpeed() const
{
return m_shutterSpeed;
}
//-----------------------------------------------------------------------------
// Returns the tone map scale
//-----------------------------------------------------------------------------
float CDmeCamera::GetToneMapScale() const
{
return m_flToneMapScale;
}
//-----------------------------------------------------------------------------
// Returns the camera's Ambient occlusion bias
//-----------------------------------------------------------------------------
float CDmeCamera::GetAOBias() const
{
return m_flAOBias;
}
//-----------------------------------------------------------------------------
// Returns the camera's Ambient occlusion strength
//-----------------------------------------------------------------------------
float CDmeCamera::GetAOStrength() const
{
return m_flAOStrength;
}
//-----------------------------------------------------------------------------
// Returns the camera's Ambient occlusion radius
//-----------------------------------------------------------------------------
float CDmeCamera::GetAORadius() const
{
return m_flAORadius;
}
//-----------------------------------------------------------------------------
// Returns the bloom scale
//-----------------------------------------------------------------------------
float CDmeCamera::GetBloomScale() const
{
return m_flBloomScale;
}
//-----------------------------------------------------------------------------
// Returns the bloom width
//-----------------------------------------------------------------------------
float CDmeCamera::GetBloomWidth() const
{
return m_flBloomWidth;
}
//-----------------------------------------------------------------------------
// Returns the number of Depth of Field samples
//-----------------------------------------------------------------------------
int CDmeCamera::GetDepthOfFieldQuality() const
{
return m_nDoFQuality;
}
//-----------------------------------------------------------------------------
// Returns the number of Motion Blur samples
//-----------------------------------------------------------------------------
int CDmeCamera::GetMotionBlurQuality() const
{
return m_nMotionBlurQuality;
}
//-----------------------------------------------------------------------------
// Returns the view direction
//-----------------------------------------------------------------------------
void CDmeCamera::GetViewDirection( Vector *pDirection )
{
matrix3x4_t transform;
GetTransform()->GetTransform( transform );
MatrixGetColumn( transform, 2, *pDirection );
// We look down the -z axis
*pDirection *= -1.0f;
}
//-----------------------------------------------------------------------------
// Sets up render state in the material system for rendering
//-----------------------------------------------------------------------------
void CDmeCamera::SetupRenderState( int nDisplayWidth, int nDisplayHeight, bool bUseEngineCoordinateSystem /* = false */ )
{
LoadViewMatrix( bUseEngineCoordinateSystem );
LoadProjectionMatrix( nDisplayWidth, nDisplayHeight );
LoadStudioRenderCameraState( );
}
//-----------------------------------------------------------------------------
// accessors for generated matrices
//-----------------------------------------------------------------------------
void CDmeCamera::GetViewMatrix( VMatrix &view, bool bUseEngineCoordinateSystem /* = false */ )
{
matrix3x4_t transform, invTransform;
CDmeTransform *pTransform = GetTransform();
pTransform->GetTransform( transform );
if ( bUseEngineCoordinateSystem )
{
VMatrix matRotate( transform );
VMatrix matRotateZ;
MatrixBuildRotationAboutAxis( matRotateZ, Vector(0,0,1), -90 );
MatrixMultiply( matRotate, matRotateZ, matRotate );
VMatrix matRotateX;
MatrixBuildRotationAboutAxis( matRotateX, Vector(1,0,0), 90 );
MatrixMultiply( matRotate, matRotateX, matRotate );
transform = matRotate.As3x4();
}
MatrixInvert( transform, invTransform );
view.Init( invTransform );
}
void CDmeCamera::GetProjectionMatrix( VMatrix &proj, int width, int height )
{
float flFOV = m_flFieldOfView.Get();
float flZNear = m_zNear.Get();
float flZFar = m_zFar.Get();
float flApsectRatio = (float)width / (float)height;
// MatrixBuildPerspective( proj, flFOV, flFOV * flApsectRatio, flZNear, flZFar );
#if 1
float halfWidth = tan( flFOV * M_PI / 360.0 );
float halfHeight = halfWidth / flApsectRatio;
#else
float halfHeight = tan( flFOV * M_PI / 360.0 );
float halfWidth = flApsectRatio * halfHeight;
#endif
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 );
}
void CDmeCamera::GetViewProjectionInverse( VMatrix &viewprojinv, int width, int height )
{
VMatrix view, proj;
GetViewMatrix( view );
GetProjectionMatrix( proj, width, height );
VMatrix viewproj;
MatrixMultiply( proj, view, viewproj );
bool success = MatrixInverseGeneral( viewproj, viewprojinv );
if ( !success )
{
Assert( 0 );
MatrixInverseTR( viewproj, viewprojinv );
}
}
//-----------------------------------------------------------------------------
// Computes the screen space position given a screen size
//-----------------------------------------------------------------------------
void CDmeCamera::ComputeScreenSpacePosition( const Vector &vecWorldPosition, int width, int height, Vector2D *pScreenPosition )
{
VMatrix view, proj, viewproj;
GetViewMatrix( view );
GetProjectionMatrix( proj, width, height );
MatrixMultiply( proj, view, viewproj );
Vector vecScreenPos;
Vector3DMultiplyPositionProjective( viewproj, vecWorldPosition, vecScreenPos );
pScreenPosition->x = ( vecScreenPos.x + 1.0f ) * width / 2.0f;
pScreenPosition->y = ( -vecScreenPos.y + 1.0f ) * height / 2.0f;
}
const matrix3x4_t &CDmeCamera::GetOrthoTransform() const
{
return m_Transform;
}
const Vector &CDmeCamera::GetOrthoAbsOrigin() const
{
return m_vecOrigin;
}
const QAngle &CDmeCamera::GetOrthoAbsAngles() const
{
return m_angRotation;
}
void CDmeCamera::OrthoUpdate()
{
m_vecAxis.Init( 0, 0, 0 );
switch ( m_nAxis )
{
default:
Assert( 0 );
case AXIS_X:
m_vecAxis[ 1 ] = 1;
break;
case AXIS_Y:
m_vecAxis[ 2 ] = -1;
break;
case AXIS_Z:
m_vecAxis[ 0 ] = 1;
break;
case AXIS_X_NEG:
m_vecAxis[ 1 ] = -1;
break;
case AXIS_Y_NEG:
m_vecAxis[ 2 ] = 1;
break;
case AXIS_Z_NEG:
m_vecAxis[ 0 ] = -1;
break;
}
m_vecOrigin = m_vecLookAt[ m_nAxis ].Get() - m_flDistance * m_vecAxis;
Assert( m_vecOrigin.IsValid() );
VectorAngles( m_vecAxis,m_angRotation );
AngleMatrix( m_angRotation, m_vecOrigin, m_Transform );
}
void CDmeCamera::FromCamera( CDmeCamera *pCamera )
{
m_bOrtho = pCamera->m_bOrtho;
matrix3x4_t mat;
pCamera->GetAbsTransform( mat );
MatrixCopy( mat, m_Transform );
m_flFieldOfView = pCamera->GetFOVx();
for ( int i = 0; i < AXIS_COUNT; ++i )
{
m_vecLookAt[ i ] =pCamera->m_vecLookAt[ i ];
m_flScale[ i ] = pCamera->m_flScale[ i ];
}
m_flDistance = pCamera->m_flDistance;
m_nAxis = pCamera->m_nAxis;
m_bWasBehindFrustum = pCamera->m_bWasBehindFrustum;
if ( m_bOrtho )
{
OrthoUpdate();
}
}
void CDmeCamera::ToCamera( CDmeCamera *pCamera )
{
pCamera->m_bOrtho = m_bOrtho;
pCamera->SetFOVx( m_flFieldOfView );
pCamera->SetAbsTransform( m_Transform );
for ( int i = 0; i < AXIS_COUNT; ++i )
{
pCamera->m_vecLookAt[ i ] = m_vecLookAt[ i ];
pCamera->m_flScale[ i ] = m_flScale[ i ];
}
pCamera->m_flDistance = m_flDistance;
pCamera->m_nAxis = m_nAxis;
pCamera->m_bWasBehindFrustum = m_bWasBehindFrustum;
if ( m_bOrtho )
{
pCamera->OrthoUpdate();
}
}