forked from vixorien/D3D11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
134 lines (109 loc) · 3.15 KB
/
Copy pathCamera.cpp
File metadata and controls
134 lines (109 loc) · 3.15 KB
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
#include "Camera.h"
using namespace DirectX;
Camera::Camera(float x, float y, float z, float aspectRatio, float fieldOfView, float movementSpeed = 1, float mouseLookSpeed = 1)
: transform()
{
transform.SetPosition(x, y, z);
this->fieldOfView = fieldOfView;
this->aspectRatio = aspectRatio;
this->movementSpeed = movementSpeed;
this->mouseLookSpeed = mouseLookSpeed;
// Z positive goes INTO the screen
// Z positive goes INTO the screen with Left Hand
UpdateProjectionMatrix(aspectRatio);
UpdateViewMatrix();
}
Camera::~Camera()
{
}
void Camera::Update(float deltaTime)
{
Input& input = Input::GetInstance();
if (input.KeyDown('W'))
{
transform.MoveRelative(0, 0, movementSpeed * deltaTime);
}
if (input.KeyDown('S'))
{
transform.MoveRelative(0, 0, -1 * movementSpeed * deltaTime);
}
if (input.KeyDown('A'))
{
transform.MoveRelative(-1 * movementSpeed * deltaTime, 0, 0);
}
if (input.KeyDown('D'))
{
transform.MoveRelative(movementSpeed * deltaTime, 0, 0);
}
if (input.KeyDown(VK_SPACE)) {
transform.MoveAbsolute(0, movementSpeed * deltaTime, 0);
}
if (input.KeyDown('X')) {
transform.MoveAbsolute(0, -1 * movementSpeed * deltaTime, 0);
}
// Mouse input!
if (input.MouseLeftDown())
{
int cursorMoveX = input.GetMouseXDelta();
int cursorMoveY = input.GetMouseYDelta();
// X-axis rotation yaws around the Y-axis
transform.Rotate(0, cursorMoveX * mouseLookSpeed * deltaTime, 0);
// Y-axis rotation pitches around the X-axis
transform.Rotate(cursorMoveY * mouseLookSpeed * deltaTime, 0, 0);
// Get X rotation for clamp
DirectX::XMFLOAT3 newRotation = transform.GetPitchYawRoll();
if (newRotation.x >= DirectX::XM_PIDIV2)
{
transform.SetPitchYawRoll(DirectX::XM_PIDIV2, newRotation.y, newRotation.z);
}
if (newRotation.x <= DirectX::XM_PIDIV2 * -1)
{
transform.SetPitchYawRoll(DirectX::XM_PIDIV2 * -1, newRotation.y, newRotation.z);
}
}
// Update the view matrix because ewe have probably changed the transform
UpdateViewMatrix();
}
void Camera::UpdateViewMatrix()
{
XMFLOAT3 pos = transform.GetPosition();
XMFLOAT3 forward = transform.GetForward();
// Z positive goes INTO the screen with Left Hand
XMMATRIX view = XMMatrixLookToLH(
XMLoadFloat3(&pos),
XMLoadFloat3(&forward),
XMVectorSet(0, 1, 0, 0)
);
XMStoreFloat4x4(&viewMatrix, view);
}
void Camera::UpdateProjectionMatrix(float aspectRatio)
{
// Z positive goes INTO the screen with Left Hand
XMMATRIX proj = XMMatrixPerspectiveFovLH(
fieldOfView,
aspectRatio,
0.01f,
100.0f
);
XMStoreFloat4x4(&projMatrix, proj);
}
DirectX::XMFLOAT4X4 Camera::GetView()
{
return viewMatrix;
}
DirectX::XMFLOAT4X4 Camera::GetProjection()
{
return projMatrix;
}
Transform Camera::GetTransform()
{
return transform;
}
float* Camera::GetFOV()
{
return &fieldOfView;
}
float* Camera::GetAspectRatio()
{
return &aspectRatio;
}