-
Notifications
You must be signed in to change notification settings - Fork 13
/
Camera.cpp
194 lines (161 loc) · 4.82 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
#include "Camera.h"
#include "Input.h"
using namespace DirectX;
Camera::Camera(
float x,
float y,
float z,
float moveSpeed,
float mouseLookSpeed,
float fieldOfView,
float aspectRatio,
float nearClip,
float farClip,
CameraProjectionType projType)
:
movementSpeed(moveSpeed),
mouseLookSpeed(mouseLookSpeed),
fieldOfView(fieldOfView),
aspectRatio(aspectRatio),
nearClip(nearClip),
farClip(farClip),
projectionType(projType),
orthographicWidth(2.0f)
{
transform.SetPosition(x, y, z);
UpdateViewMatrix();
UpdateProjectionMatrix(aspectRatio);
}
Camera::Camera(
DirectX::XMFLOAT3 position,
float moveSpeed,
float mouseLookSpeed,
float fieldOfView,
float aspectRatio,
float nearClip,
float farClip,
CameraProjectionType projType)
:
movementSpeed(moveSpeed),
mouseLookSpeed(mouseLookSpeed),
fieldOfView(fieldOfView),
aspectRatio(aspectRatio),
nearClip(nearClip),
farClip(farClip),
projectionType(projType)
{
transform.SetPosition(position);
UpdateViewMatrix();
UpdateProjectionMatrix(aspectRatio);
}
// Nothing to really do
Camera::~Camera()
{ }
// Camera's update, which looks for key presses
void Camera::Update(float dt)
{
// Current speed
float speed = dt * movementSpeed;
// Get the input manager instance
Input& input = Input::GetInstance();
// Speed up or down as necessary
if (input.KeyDown(VK_SHIFT)) { speed *= 5; }
if (input.KeyDown(VK_CONTROL)) { speed *= 0.1f; }
// Movement
if (input.KeyDown('W')) { transform.MoveRelative(0, 0, speed); }
if (input.KeyDown('S')) { transform.MoveRelative(0, 0, -speed); }
if (input.KeyDown('A')) { transform.MoveRelative(-speed, 0, 0); }
if (input.KeyDown('D')) { transform.MoveRelative(speed, 0, 0); }
if (input.KeyDown('X')) { transform.MoveAbsolute(0, -speed, 0); }
if (input.KeyDown(' ')) { transform.MoveAbsolute(0, speed, 0); }
// Handle mouse movement only when button is down
if (input.MouseLeftDown())
{
// Calculate cursor change
float xDiff = mouseLookSpeed * input.GetMouseXDelta();
float yDiff = mouseLookSpeed * input.GetMouseYDelta();
transform.Rotate(yDiff, xDiff, 0);
// Clamp the X rotation
XMFLOAT3 rot = transform.GetPitchYawRoll();
if (rot.x > XM_PIDIV2) rot.x = XM_PIDIV2;
if (rot.x < -XM_PIDIV2) rot.x = -XM_PIDIV2;
transform.SetRotation(rot);
}
// Update the view every frame - could be optimized
UpdateViewMatrix();
}
// Creates a new view matrix based on current position and orientation
void Camera::UpdateViewMatrix()
{
// Get the camera's forward vector and position
XMFLOAT3 forward = transform.GetForward();
XMFLOAT3 pos = transform.GetPosition();
// Make the view matrix and save
XMMATRIX view = XMMatrixLookToLH(
XMLoadFloat3(&pos),
XMLoadFloat3(&forward),
XMVectorSet(0, 1, 0, 0)); // World up axis
XMStoreFloat4x4(&viewMatrix, view);
}
// Updates the projection matrix
void Camera::UpdateProjectionMatrix(float aspectRatio)
{
this->aspectRatio = aspectRatio;
XMMATRIX P;
// Which type?
if (projectionType == CameraProjectionType::Perspective)
{
P = XMMatrixPerspectiveFovLH(
fieldOfView, // Field of View Angle
aspectRatio, // Aspect ratio
nearClip, // Near clip plane distance
farClip); // Far clip plane distance
}
else // CameraProjectionType::ORTHOGRAPHIC
{
P = XMMatrixOrthographicLH(
orthographicWidth, // Projection width (in world units)
orthographicWidth / aspectRatio,// Projection height (in world units)
nearClip, // Near clip plane distance
farClip); // Far clip plane distance
}
XMStoreFloat4x4(&projMatrix, P);
}
DirectX::XMFLOAT4X4 Camera::GetView() { return viewMatrix; }
DirectX::XMFLOAT4X4 Camera::GetProjection() { return projMatrix; }
Transform* Camera::GetTransform() { return &transform; }
float Camera::GetAspectRatio() { return aspectRatio; }
float Camera::GetFieldOfView() { return fieldOfView; }
void Camera::SetFieldOfView(float fov)
{
fieldOfView = fov;
UpdateProjectionMatrix(aspectRatio);
}
float Camera::GetMovementSpeed() { return movementSpeed; }
void Camera::SetMovementSpeed(float speed) { movementSpeed = speed; }
float Camera::GetMouseLookSpeed() { return mouseLookSpeed; }
void Camera::SetMouseLookSpeed(float speed) { mouseLookSpeed = speed; }
float Camera::GetNearClip() { return nearClip; }
void Camera::SetNearClip(float distance)
{
nearClip = distance;
UpdateProjectionMatrix(aspectRatio);
}
float Camera::GetFarClip() { return farClip; }
void Camera::SetFarClip(float distance)
{
farClip = distance;
UpdateProjectionMatrix(aspectRatio);
}
float Camera::GetOrthographicWidth() { return orthographicWidth; }
void Camera::SetOrthographicWidth(float width)
{
orthographicWidth = width;
UpdateProjectionMatrix(aspectRatio);
}
CameraProjectionType Camera::GetProjectionType() { return projectionType; }
void Camera::SetProjectionType(CameraProjectionType type)
{
projectionType = type;
UpdateProjectionMatrix(aspectRatio);
}