-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
186 lines (137 loc) · 6.1 KB
/
Copy pathCamera.cpp
File metadata and controls
186 lines (137 loc) · 6.1 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
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
#include "stdafx.h"
#include "BoundingVolume.h"
#include "Camera.h"
#include "Keyboard.h"
Renderer* Camera::p_renderer_;
BoundingVolume Camera::m_bounding_camera_ = BoundingVolume(0.15f); // Radius = 0.15 -> Breite = 0.3
BoundingVolume* Camera::m_bounding_frustum_;
// X,Y,Z Coordinates to set the direction we are looking at. Always Z-1 from m_cameraPosition Z
const XMFLOAT3 c_forwardVector = {0.0f, 0.0f, -1.0f};
const XMFLOAT4 c_cameraUp = XMFLOAT4(0.0f, 1.0f, 0.0f, 0.0f); // the worlds up vector
const float c_cameraSpeed = 0.005f;
const float c_playerHeight = 1.8f;
const float c_fovAngleY = 45.0f * (XM_PI / 180.0f);
const float c_nearZ = 0.1f;
const float c_farZ = 200.0f;
XMFLOAT3 m_cameraTarget = XMFLOAT3();
float m_pitch = 0;
float m_yaw = 0;
float aspectRatio = 1;
// X,Y,Z Coordinates for the position of the camera
XMFLOAT3 m_cameraPosition = XMFLOAT3(10.0f, c_playerHeight, 14.0f);
// X,Y,Z Coordinates of the camera target describing the point in space our camera is looking at. Needs to be X,Y,Z of m_cameraPosition - X,Y,Z of forwardVector
XMFLOAT3 getCameraTarget(XMVECTOR& rotatedForwardVector) {
XMFLOAT3 forward;
XMStoreFloat3(&forward, rotatedForwardVector);
m_cameraTarget = XMFLOAT3(m_cameraPosition.x + forward.x, m_cameraPosition.y + forward.y,
m_cameraPosition.z + forward.z);
return m_cameraTarget;
}
void Camera::SetAspectRatio(int width, int height) {
aspectRatio = (float)width / (float)height;
}
void Camera::SetCameraPosition(XMFLOAT3 position) {
m_cameraPosition = position;
Rotate(1, 1);
}
XMFLOAT3 Camera::GetCameraPosition() {
return XMFLOAT3(m_cameraPosition);
}
float Camera::GetPitch() {
return m_pitch;
}
float Camera::GetYaw() {
return m_yaw;
}
void Camera::Setup(Renderer* renderer) {
p_renderer_ = renderer;
}
XMFLOAT4X4 Camera::GetViewMatrix() {
XMVECTOR cPos = XMLoadFloat3(&m_cameraPosition);
XMVECTOR quaternionRotation = XMQuaternionMultiply(XMQuaternionRotationRollPitchYaw(m_pitch, m_yaw, 0),
XMQuaternionIdentity());
XMVECTOR forwardVector = XMVector3Rotate(XMLoadFloat3(&c_forwardVector), quaternionRotation);
XMVECTOR cameraTarget = XMLoadFloat3(&getCameraTarget(forwardVector));
XMVECTOR cUp = XMLoadFloat4(&c_cameraUp);
XMFLOAT4X4 m_cameraViewMat = {}; // this will store our view matrix
XMStoreFloat4x4(&m_cameraViewMat, XMMatrixLookAtRH(cPos, cameraTarget, cUp));
return m_cameraViewMat;
}
XMFLOAT4X4 Camera::GetProjectionMatrix() {
XMFLOAT4X4 m_cameraProjMat = {}; // this will store our projection matrix
XMStoreFloat4x4(&m_cameraProjMat, XMMatrixPerspectiveFovRH(c_fovAngleY, aspectRatio, c_nearZ, c_farZ));
return m_cameraProjMat;
}
void Camera::GetFrustum(BoundingVolume*& bounding_volume) {
if (m_bounding_frustum_ == nullptr) {
float nearHeight = 2 * tan(c_fovAngleY) * c_nearZ;
float farHeight = 2 * tan(c_fovAngleY) * c_farZ;
float nearWidth = nearHeight * aspectRatio;
float farWidth = farHeight * aspectRatio;
std::vector<Vertex> frustumModel;
//left down front
frustumModel.push_back(Vertex({XMFLOAT3(-nearWidth / 2, -nearHeight / 2, -c_nearZ), {0, 0, 0, 0}, {0, 0}}));
//right down front
frustumModel.push_back(Vertex({XMFLOAT3(nearWidth / 2, -nearHeight / 2, -c_nearZ), {0, 0, 0, 0}, {0, 0}}));
//right up front
frustumModel.push_back(Vertex({XMFLOAT3(nearWidth / 2, nearHeight / 2, -c_nearZ), {0, 0, 0, 0}, {0, 0}}));
//left up front
frustumModel.push_back(Vertex({XMFLOAT3(-nearWidth / 2, nearHeight / 2, -c_nearZ), {0, 0, 0, 0}, {0, 0}}));
//left down back
frustumModel.push_back(Vertex({XMFLOAT3(-farWidth / 2, -farHeight / 2, -c_farZ), {0, 0, 0, 0}, {0, 0}}));
//right down back
frustumModel.push_back(Vertex({XMFLOAT3(farWidth / 2, -farHeight / 2, -c_farZ), {0, 0, 0, 0}, {0, 0}}));
//right up back
frustumModel.push_back(Vertex({XMFLOAT3(farWidth / 2, farHeight / 2, -c_farZ), {0, 0, 0, 0}, {0, 0}}));
//left up back
frustumModel.push_back(Vertex({XMFLOAT3(-farWidth / 2, farHeight / 2, -c_farZ), {0, 0, 0, 0}, {0, 0}}));
m_bounding_frustum_ = new BoundingVolume(frustumModel);
}
bounding_volume = m_bounding_frustum_;
}
void Camera::Rotate(int yaw, int pitch) {
// Scale to slow the camera movement down
float scale = 0.003f;
m_pitch += -pitch * scale;
m_yaw += -yaw * scale;
//85°
float border = 85.0f / (180.0f / XM_PI);
if (m_pitch > border) {
m_pitch = border;
}
if (m_pitch < -border) {
m_pitch = -border;
}
}
void Camera::Update(float delta) {
XMFLOAT3 direction = {.0f, .0f, .0f};
if (Keyboard::IsPressed(0x41))
direction.x -= c_cameraSpeed * delta; // A-Key
if (Keyboard::IsPressed(0x44))
direction.x += c_cameraSpeed * delta; // D-Key
if (Keyboard::IsPressed(0x53))
direction.z += c_cameraSpeed * delta; // S-Key
if (Keyboard::IsPressed(0x57))
direction.z -= c_cameraSpeed * delta; // W-Key
if (direction.x == 0.0f && direction.y == 0.0f && direction.z == 0.0f)
return;
XMVECTOR quaternionRotation = XMQuaternionMultiply(XMQuaternionRotationRollPitchYaw(m_pitch, m_yaw, 0),
XMQuaternionIdentity());
XMVECTOR rotatedForwardVector = XMVector3Rotate(XMLoadFloat3(&c_forwardVector), quaternionRotation);
XMFLOAT3 cameraTarget = getCameraTarget(rotatedForwardVector);
XMVECTOR directionVector = XMLoadFloat3(&direction);
XMVECTOR rotatedDirection = XMVector3Rotate(directionVector, quaternionRotation);
XMFLOAT3 resolution;
XMFLOAT3 desiredPosition;
XMStoreFloat3(&desiredPosition, XMLoadFloat3(&m_cameraPosition) + rotatedDirection);
XMFLOAT4 desiredRotation;
XMStoreFloat4(&desiredRotation, quaternionRotation);
m_bounding_camera_.Update(&desiredPosition, &desiredRotation);
if (p_renderer_->Intersects(m_bounding_camera_, resolution)) {
rotatedDirection -= XMLoadFloat3(&resolution);
}
XMStoreFloat3(&m_cameraPosition, XMLoadFloat3(&m_cameraPosition) + rotatedDirection);
m_cameraPosition.y = c_playerHeight;
XMVECTOR camPosVector = XMLoadFloat3(&m_cameraPosition);
XMStoreFloat3(&m_cameraTarget, camPosVector + rotatedForwardVector);
}