forked from perilouswithadollarsign/cstrike15_src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.cpp
723 lines (594 loc) · 20.8 KB
/
camera.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Implements a camera for the 3D view.
//
// $NoKeywords: $
//=============================================================================//
#include <windows.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "Camera.h"
#include "hammer_mathlib.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//
// Indices of camera axes.
//
#define CAMERA_RIGHT 0
#define CAMERA_UP 1
#define CAMERA_FORWARD 2
#define CAMERA_ORIGIN 3
#define MIN_PITCH -90.0f
#define MAX_PITCH 90.0f
static void DBG(char *fmt, ...)
{
char ach[128];
va_list va;
va_start(va, fmt);
vsprintf(ach, fmt, va);
va_end(va);
OutputDebugString(ach);
}
//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CCamera::CCamera(void)
{
m_ViewPoint.Init();
m_fPitch = 0.0;
m_fRoll = 0.0;
m_fYaw = 0.0;
m_fHorizontalFOV = 90;
m_fNearClip = 1.0;
m_fFarClip = 5000;
m_fZoom = 1.0f;
m_bIsOrthographic = false;
m_fScaleHorz = m_fScaleVert = 1;
m_nViewWidth = m_nViewHeight = 100;
BuildViewMatrix();
BuildProjMatrix();
}
//-----------------------------------------------------------------------------
// Purpose: Destructor.
//-----------------------------------------------------------------------------
CCamera::~CCamera(void)
{
}
void CCamera::SetViewPort( int width, int height )
{
if ( m_nViewWidth != width || m_nViewHeight != height )
{
m_nViewWidth = width;
m_nViewHeight = height;
BuildProjMatrix();
}
}
void CCamera::GetViewPort( int &width, int &height )
{
width = m_nViewWidth;
height = m_nViewHeight;
}
//-----------------------------------------------------------------------------
// Purpose: Returns the current value of the camera's pitch.
//-----------------------------------------------------------------------------
float CCamera::GetPitch(void)
{
return(m_fPitch);
}
//-----------------------------------------------------------------------------
// Purpose: Returns the current value of the camera's roll.
//-----------------------------------------------------------------------------
float CCamera::GetRoll(void)
{
return(m_fRoll);
}
//-----------------------------------------------------------------------------
// Purpose: Returns the current value of the camera's yaw.
//-----------------------------------------------------------------------------
float CCamera::GetYaw(void)
{
return(m_fYaw);
}
//-----------------------------------------------------------------------------
// Purpose: returns the camera angles
// Output : returns the camera angles
//-----------------------------------------------------------------------------
QAngle CCamera::GetAngles()
{
return QAngle( m_fPitch, m_fYaw, m_fRoll );
}
//-----------------------------------------------------------------------------
// Purpose: Moves the camera along the camera's right axis.
// Input : fUnits - World units to move the camera.
//-----------------------------------------------------------------------------
void CCamera::MoveRight(float fUnits)
{
if (fUnits != 0)
{
m_ViewPoint[0] += m_ViewMatrix[CAMERA_RIGHT][0] * fUnits;
m_ViewPoint[1] += m_ViewMatrix[CAMERA_RIGHT][1] * fUnits;
m_ViewPoint[2] += m_ViewMatrix[CAMERA_RIGHT][2] * fUnits;
BuildViewMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: Moves the camera along the camera's up axis.
// Input : fUnits - World units to move the camera.
//-----------------------------------------------------------------------------
void CCamera::MoveUp(float fUnits)
{
if (fUnits != 0)
{
m_ViewPoint[0] += m_ViewMatrix[CAMERA_UP][0] * fUnits;
m_ViewPoint[1] += m_ViewMatrix[CAMERA_UP][1] * fUnits;
m_ViewPoint[2] += m_ViewMatrix[CAMERA_UP][2] * fUnits;
BuildViewMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: Moves the camera along the camera's forward axis.
// Input : fUnits - World units to move the camera.
//-----------------------------------------------------------------------------
void CCamera::MoveForward(float fUnits)
{
if (fUnits != 0)
{
m_ViewPoint[0] -= m_ViewMatrix[CAMERA_FORWARD][0] * fUnits;
m_ViewPoint[1] -= m_ViewMatrix[CAMERA_FORWARD][1] * fUnits;
m_ViewPoint[2] -= m_ViewMatrix[CAMERA_FORWARD][2] * fUnits;
BuildViewMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns the camera's viewpoint.
//-----------------------------------------------------------------------------
void CCamera::GetViewPoint(Vector& ViewPoint) const
{
ViewPoint = m_ViewPoint;
}
//-----------------------------------------------------------------------------
// Purpose: Returns a vector indicating the camera's forward axis.
//-----------------------------------------------------------------------------
void CCamera::GetViewForward(Vector& ViewForward) const
{
ViewForward[0] = -m_ViewMatrix[CAMERA_FORWARD][0];
ViewForward[1] = -m_ViewMatrix[CAMERA_FORWARD][1];
ViewForward[2] = -m_ViewMatrix[CAMERA_FORWARD][2];
}
//-----------------------------------------------------------------------------
// Purpose: Returns a vector indicating the camera's up axis.
//-----------------------------------------------------------------------------
void CCamera::GetViewUp(Vector& ViewUp) const
{
ViewUp[0] = m_ViewMatrix[CAMERA_UP][0];
ViewUp[1] = m_ViewMatrix[CAMERA_UP][1];
ViewUp[2] = m_ViewMatrix[CAMERA_UP][2];
}
//-----------------------------------------------------------------------------
// Purpose: Returns a vector indicating the camera's right axis.
//-----------------------------------------------------------------------------
void CCamera::GetViewRight(Vector& ViewRight) const
{
ViewRight[0] = m_ViewMatrix[CAMERA_RIGHT][0];
ViewRight[1] = m_ViewMatrix[CAMERA_RIGHT][1];
ViewRight[2] = m_ViewMatrix[CAMERA_RIGHT][2];
}
//-----------------------------------------------------------------------------
// Purpose: Returns the horizontal field of view in degrees.
//-----------------------------------------------------------------------------
float CCamera::GetFOV(void)
{
return(m_fHorizontalFOV);
}
//-----------------------------------------------------------------------------
// Purpose: Returns the distance from the camera to the near clipping plane in world units.
//-----------------------------------------------------------------------------
float CCamera::GetNearClip(void)
{
return(m_fNearClip);
}
//-----------------------------------------------------------------------------
// Purpose: Returns the distance from the camera to the far clipping plane in world units.
//-----------------------------------------------------------------------------
float CCamera::GetFarClip(void)
{
return(m_fFarClip);
}
//-----------------------------------------------------------------------------
// Purpose: Sets up fields of view & clip plane distances for the view frustum.
// Input : fHorizontalFOV -
// fVerticalFOV -
// fNearClip -
// fFarClip -
//-----------------------------------------------------------------------------
void CCamera::SetPerspective(float fHorizontalFOV, float fNearClip, float fFarClip)
{
m_bIsOrthographic = false;
m_fHorizontalFOV = fHorizontalFOV;
m_fNearClip = fNearClip;
m_fFarClip = fFarClip;
BuildProjMatrix();
}
void CCamera::SetOrthographic(float fZoom, float fNearClip, float fFarClip)
{
m_fZoom = fZoom;
m_fNearClip = fNearClip;
m_fFarClip = fFarClip;
m_bIsOrthographic = true;
BuildProjMatrix();
}
void CCamera::GetFrustumPlanes( Vector4D Planes[6] )
{
// TODO check for FrustumPlanesFromMatrix, maybe we can use that
Vector ViewForward;
GetViewForward(ViewForward);
VMatrix CameraMatrix = m_ProjMatrix * m_ViewMatrix;
//
// Now the plane coefficients can be pulled directly out of the the camera
// matrix as follows:
//
// Right : first_column - fourth_column
// Left : -first_column - fourth_column
// Top : second_column - fourth_column
// Bottom: -second_column - fourth_column
// Front : -third_column - fourth_column
// Back : third_column + fourth_column
//
// dvs: My plane constants should be coming directly from the matrices,
// but they aren't (for some reason). Instead I calculate the plane
// constants myself. Sigh.
//
Planes[0][0] = CameraMatrix[0][0] - CameraMatrix[3][0];
Planes[0][1] = CameraMatrix[0][1] - CameraMatrix[3][1];
Planes[0][2] = CameraMatrix[0][2] - CameraMatrix[3][2];
VectorNormalize(Planes[0].AsVector3D());
Planes[0][3] = DotProduct(m_ViewPoint, Planes[0].AsVector3D());
Planes[1][0] = -CameraMatrix[0][0] - CameraMatrix[3][0];
Planes[1][1] = -CameraMatrix[0][1] - CameraMatrix[3][1];
Planes[1][2] = -CameraMatrix[0][2] - CameraMatrix[3][2];
VectorNormalize(Planes[1].AsVector3D());
Planes[1][3] = DotProduct(m_ViewPoint, Planes[1].AsVector3D());
Planes[2][0] = CameraMatrix[1][0] - CameraMatrix[3][0];
Planes[2][1] = CameraMatrix[1][1] - CameraMatrix[3][1];
Planes[2][2] = CameraMatrix[1][2] - CameraMatrix[3][2];
VectorNormalize(Planes[2].AsVector3D());
Planes[2][3] = DotProduct(m_ViewPoint, Planes[2].AsVector3D());
Planes[3][0] = -CameraMatrix[1][0] - CameraMatrix[3][0];
Planes[3][1] = -CameraMatrix[1][1] - CameraMatrix[3][1];
Planes[3][2] = -CameraMatrix[1][2] - CameraMatrix[3][2];
VectorNormalize(Planes[3].AsVector3D());
Planes[3][3] = DotProduct(m_ViewPoint, Planes[3].AsVector3D());
Planes[4][0] = -CameraMatrix[2][0] - CameraMatrix[3][0];
Planes[4][1] = -CameraMatrix[2][1] - CameraMatrix[3][1];
Planes[4][2] = -CameraMatrix[2][2] - CameraMatrix[3][2];
VectorNormalize(Planes[4].AsVector3D());
Planes[4][3] = DotProduct(m_ViewPoint + ViewForward * m_fNearClip, Planes[4].AsVector3D());
Planes[5][0] = CameraMatrix[2][0] + CameraMatrix[3][0];
Planes[5][1] = CameraMatrix[2][1] + CameraMatrix[3][1];
Planes[5][2] = CameraMatrix[2][2] + CameraMatrix[3][2];
VectorNormalize(Planes[5].AsVector3D());
Planes[5][3] = DotProduct(m_ViewPoint + ViewForward * m_fFarClip, Planes[5].AsVector3D());
}
bool CCamera::IsOrthographic()
{
return m_bIsOrthographic;
}
void CCamera::BuildProjMatrix()
{
memset( &m_ProjMatrix,0,sizeof(m_ProjMatrix) );
VMatrix &m = m_ProjMatrix;
if ( m_bIsOrthographic )
{
// same as D3DXMatrixOrthoRH
float w = (float)m_nViewWidth / m_fZoom;
float h = (float)m_nViewHeight / m_fZoom;
m[0][0] = 2/w;
m[1][1] = 2/h;
m[2][2] = 1/(m_fNearClip-m_fFarClip);
m[2][3] = m_fNearClip/(m_fNearClip-m_fFarClip);
m[3][3] = 1;
}
else
{
// same as D3DXMatrixPerspectiveRH
float w = 2 * m_fNearClip * tan( m_fHorizontalFOV * M_PI / 360.0 );
float h = ( w * float(m_nViewHeight) ) / float(m_nViewWidth);
m[0][0] = 2*m_fNearClip/w;
m[1][1] = 2*m_fNearClip/h;
m[2][2] = m_fFarClip/(m_fNearClip-m_fFarClip);
m[2][3] = (m_fNearClip*m_fFarClip)/(m_fNearClip-m_fFarClip);
m[3][2] = -1;
}
m_ViewProjMatrix = m_ProjMatrix * m_ViewMatrix;
m_ViewProjMatrix.InverseGeneral( m_InvViewProjMatrix );
}
//-----------------------------------------------------------------------------
// Purpose: Sets the distance from the camera to the near clipping plane in world units.
//-----------------------------------------------------------------------------
void CCamera::SetNearClip(float fNearClip)
{
if ( m_fNearClip != fNearClip )
{
m_fNearClip = fNearClip;
BuildProjMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets the distance from the camera to the far clipping plane in world units.
//-----------------------------------------------------------------------------
void CCamera::SetFarClip(float fFarClip)
{
if ( m_fFarClip != fFarClip )
{
m_fFarClip = fFarClip;
BuildProjMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets the pitch in degrees, from [MIN_PITCH..MAX_PITCH]
//-----------------------------------------------------------------------------
void CCamera::SetPitch(float fDegrees)
{
if (m_fPitch != fDegrees)
{
m_fPitch = fDegrees;
if (m_fPitch > MAX_PITCH)
{
m_fPitch = MAX_PITCH;
}
else if (m_fPitch < MIN_PITCH)
{
m_fPitch = MIN_PITCH;
}
BuildViewMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets the roll in degrees, from [0..360)
//-----------------------------------------------------------------------------
void CCamera::SetRoll(float fDegrees)
{
while (fDegrees >= 360)
{
fDegrees -= 360;
}
while (fDegrees < 0)
{
fDegrees += 360;
}
if (m_fRoll != fDegrees)
{
m_fRoll = fDegrees;
BuildViewMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets the yaw in degrees, from [0..360)
//-----------------------------------------------------------------------------
void CCamera::SetYaw(float fDegrees)
{
while (fDegrees >= 360)
{
fDegrees -= 360;
}
while (fDegrees < 0)
{
fDegrees += 360;
}
if (m_fYaw != fDegrees)
{
m_fYaw = fDegrees;
BuildViewMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets the view point.
//-----------------------------------------------------------------------------
void CCamera::SetViewPoint(const Vector &ViewPoint)
{
if ( m_ViewPoint != ViewPoint )
{
m_ViewPoint = ViewPoint;
BuildViewMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets the camera target, rebuilding the view matrix.
// Input : ViewTarget - the point in world space that the camera should look at.
//-----------------------------------------------------------------------------
void CCamera::SetViewTarget(const Vector &ViewTarget)
{
Vector ViewOrigin;
Vector ViewForward;
GetViewPoint( ViewOrigin );
VectorSubtract(ViewTarget, ViewOrigin, ViewForward);
VectorNormalize(ViewForward);
//
// Ideally we could replace the math below with standard VectorAngles stuff, but Hammer
// camera matrices use a different convention from QAngle (sadly).
//
float fYaw = RAD2DEG(atan2(ViewForward[0], ViewForward[1]));
SetYaw(fYaw);
float fPitch = -RAD2DEG(asin(ViewForward[2]));
SetPitch(fPitch);
}
//-----------------------------------------------------------------------------
// Purpose: Move the camera along a worldspace vector.
//-----------------------------------------------------------------------------
void CCamera::Move(Vector &vDelta)
{
m_ViewPoint += vDelta;
BuildViewMatrix();
}
//-----------------------------------------------------------------------------
// Purpose: Pitches the camera forward axis toward the camera's down axis a
// given number of degrees.
//-----------------------------------------------------------------------------
void CCamera::Pitch(float fDegrees)
{
if (fDegrees != 0)
{
float fPitch = GetPitch();
fPitch += fDegrees;
SetPitch(fPitch);
}
}
//-----------------------------------------------------------------------------
// Purpose: Rolls the camera's right axis toward the camera's up axis a given
// number of degrees.
//-----------------------------------------------------------------------------
void CCamera::Roll(float fDegrees)
{
if (fDegrees != 0)
{
float fRoll = GetRoll();
fRoll += fDegrees;
SetRoll(fRoll);
}
}
//-----------------------------------------------------------------------------
// Purpose: Yaws the camera's forward axis toward the camera's right axis a
// given number of degrees.
//-----------------------------------------------------------------------------
void CCamera::Yaw(float fDegrees)
{
if (fDegrees != 0)
{
float fYaw = GetYaw();
fYaw += fDegrees;
SetYaw(fYaw);
}
}
//-----------------------------------------------------------------------------
// Purpose: Loads the given matrix with an identity matrix.
// Input : Matrix - 4 x 4 matrix to be loaded with the identity matrix.
//-----------------------------------------------------------------------------
void CCamera::CameraIdentityMatrix(VMatrix& Matrix)
{
// This function produces a transform which transforms from
// material system camera space to quake camera space
// Camera right axis lies along the world X axis.
Matrix[CAMERA_RIGHT][0] = 1;
Matrix[CAMERA_RIGHT][1] = 0;
Matrix[CAMERA_RIGHT][2] = 0;
Matrix[CAMERA_RIGHT][3] = 0;
// Camera up axis lies along the world Z axis.
Matrix[CAMERA_UP][0] = 0;
Matrix[CAMERA_UP][1] = 0;
Matrix[CAMERA_UP][2] = 1;
Matrix[CAMERA_UP][3] = 0;
// Camera forward axis lies along the negative world Y axis.
Matrix[CAMERA_FORWARD][0] = 0;
Matrix[CAMERA_FORWARD][1] = -1;
Matrix[CAMERA_FORWARD][2] = 0;
Matrix[CAMERA_FORWARD][3] = 0;
Matrix[CAMERA_ORIGIN][0] = 0;
Matrix[CAMERA_ORIGIN][1] = 0;
Matrix[CAMERA_ORIGIN][2] = 0;
Matrix[CAMERA_ORIGIN][3] = 1;
}
//-----------------------------------------------------------------------------
// Purpose: Generates a view matrix based on our current yaw, pitch, and roll.
// The view matrix does not consider FOV or clip plane distances.
//-----------------------------------------------------------------------------
void CCamera::BuildViewMatrix()
{
// The camera transformation is produced by multiplying roll * yaw * pitch.
// This will transform a point from world space into quake camera space,
// which is exactly what we want for our view matrix. However, quake
// camera space isn't the same as material system camera space, so
// we're going to have to apply a transformation that goes from quake
// camera space to material system camera space.
CameraIdentityMatrix( m_ViewMatrix );
RotateAroundAxis(m_ViewMatrix, m_fPitch, 0 );
RotateAroundAxis(m_ViewMatrix, m_fRoll, 1);
RotateAroundAxis(m_ViewMatrix, m_fYaw, 2);
// Translate the viewpoint to the world origin.
VMatrix TempMatrix;
TempMatrix.Identity();
TempMatrix.SetTranslation( -m_ViewPoint );
m_ViewMatrix = m_ViewMatrix * TempMatrix;
m_ViewProjMatrix = m_ProjMatrix * m_ViewMatrix;
m_ViewProjMatrix.InverseGeneral( m_InvViewProjMatrix );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : Matrix -
//-----------------------------------------------------------------------------
void CCamera::GetViewMatrix(VMatrix& Matrix)
{
Matrix = m_ViewMatrix;
}
void CCamera::GetProjMatrix(VMatrix& Matrix)
{
Matrix = m_ProjMatrix;
}
//-----------------------------------------------------------------------------
// Purpose: Sets the view matrix of the current projection
// Output : Matrix - the matrix to store the current projection matrix
//-----------------------------------------------------------------------------
void CCamera::GetViewProjMatrix( VMatrix &Matrix )
{
Matrix = m_ViewProjMatrix;
}
//-----------------------------------------------------------------------------
// Purpose: to set the zoom in the orthographic gl view
// Input: fScale - the zoom scale
//-----------------------------------------------------------------------------
void CCamera::SetZoom( float fScale )
{
if ( m_fZoom != fScale )
{
m_fZoom = fScale;
BuildProjMatrix();
}
}
//-----------------------------------------------------------------------------
// Purpose: to accumulate the zoom in the orthographic gl view
// Input: fScale - the zoom scale
//-----------------------------------------------------------------------------
void CCamera::Zoom( float fScale )
{
m_fZoom += fScale;
// don't zoom negative
if( m_fZoom < 0.00001f )
m_fZoom = 0.00001f;
}
//-----------------------------------------------------------------------------
// Purpose: to get the zoom in the orthographic gl view
// Output: return the zoom scale
//-----------------------------------------------------------------------------
float CCamera::GetZoom( void )
{
return m_fZoom;
}
void CCamera::WorldToView( const Vector& vWorld, Vector2D &vView )
{
Vector vView3D;
Vector3DMultiplyPositionProjective( m_ViewProjMatrix, vWorld, vView3D );
// NOTE: The negative sign on y is because wc wants to think about screen
// coordinates in a different way than the material system
vView.x = 0.5 * (vView3D.x + 1.0) * m_nViewWidth;
vView.y = 0.5 * (-vView3D.y + 1.0) * m_nViewHeight;
}
void CCamera::ViewToWorld( const Vector2D &vView, Vector& vWorld)
{
Vector vView3D;
vView3D.x = 2.0 * vView.x / m_nViewWidth - 1;
vView3D.y = -2.0 * vView.y / m_nViewHeight + 1;
vView3D.z = 0;
Vector3DMultiplyPositionProjective( m_InvViewProjMatrix, vView3D, vWorld );
}
void CCamera::BuildRay( const Vector2D &vView, Vector& vStart, Vector& vEnd )
{
// Find the point they clicked on in world coordinates. It lies on the near
// clipping plane.
Vector vClickPoint;
ViewToWorld(vView, vClickPoint );
// Build a ray from the viewpoint through the point on the near clipping plane.
Vector vRay = vClickPoint - m_ViewPoint;
VectorNormalize( vRay );
vStart = m_ViewPoint;
vEnd = vStart + vRay * 99999;
}