-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
99 lines (84 loc) · 2.24 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
#include "Camera.h"
Camera::Camera()
{
_defPosition = XMFLOAT3(0.0f, 0.0f, -400.0f);
_pos = _defPosition;
_posVector = XMLoadFloat3(&_pos);
_rot = XMFLOAT3(0.0f, 0.0f, 0.0f);
_rotVector = XMLoadFloat3(&_rot);
this->UpdateViewMatrix();
}
void Camera::SetProjectionValues(float fovDegrees, float aspectRatio, float nearZ, float farZ)
{
float fovRadians = (fovDegrees / 360.0f) * XM_2PI;
_projectionMatrix = XMMatrixPerspectiveFovLH(fovRadians, aspectRatio, nearZ, farZ);
}
const XMMATRIX& Camera::GetViewMatrix() const
{
return _viewMatrix;
}
const XMMATRIX& Camera::GetProjectionMatrix() const
{
return _projectionMatrix;
}
const XMVECTOR& Camera::GetPositionVector() const
{
return _posVector;
}
const XMFLOAT3& Camera::GetPositionFloat3() const
{
return _pos;
}
const XMFLOAT3& Camera::GetDefPosition() const
{
return _defPosition;
}
void Camera::SetPosition(const XMVECTOR& pos)
{
XMStoreFloat3(&_pos, pos);
_posVector = pos;
this->UpdateViewMatrix();
}
void Camera::SetPosition(const XMFLOAT3& pos)
{
_pos = pos;
_posVector = XMLoadFloat3(&_pos);
this->UpdateViewMatrix();
}
void Camera::SetPosition(float x, float y, float z)
{
_pos = XMFLOAT3(x, y, z);
_posVector = XMLoadFloat3(&_pos);
this->UpdateViewMatrix();
}
void Camera::AdjustPosition(const XMVECTOR& pos)
{
_posVector += pos;
XMStoreFloat3(&_pos, _posVector);
this->UpdateViewMatrix();
}
void Camera::AdjustPosition(float x, float y, float z)
{
_pos.x += x;
_pos.y += y;
_pos.z += z;
_posVector = XMLoadFloat3(&_pos);
this->UpdateViewMatrix();
}
void Camera::Track(float x, float y, float z)
{
SetPosition(x, y, z);
}
void Camera::UpdateViewMatrix()
{
//Calculate camera rotation matrix
XMMATRIX camRotationMatrix = XMMatrixRotationRollPitchYaw(_rot.x, _rot.y, _rot.z);
//Calculate unit vector of cam target based off camera forward value transformed by cam rotation matrix
XMVECTOR camTarget = XMVector3TransformCoord(_DEFAULT_FORWARD_VECTOR, camRotationMatrix);
//Adjust cam target to be offset by the camera's current position
camTarget += _posVector;
//Calculate up direction based on current rotation
XMVECTOR upDir = XMVector3TransformCoord(_DEFAULT_UP_VECTOR, camRotationMatrix);
//Rebuild view matrix
_viewMatrix = XMMatrixLookAtLH(_posVector, camTarget, upDir);
}