-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.cpp
More file actions
153 lines (124 loc) · 2.8 KB
/
camera.cpp
File metadata and controls
153 lines (124 loc) · 2.8 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
#include "camera.hpp"
void Camera::Init(float curX, float curY) {
lastX = curX;
lastY = curY;
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
projection = glm::perspective(glm::radians(fov), DISPLAY_WIDTH / DISPLAY_HEIGHT, 0.1f, 100.0f);
}
void Camera::Update(float deltaTime) {
if (!enabled) {
return;
}
_Zoom();
_Tilt();
_Move(deltaTime);
}
void Camera::Disable() {
enabled = false;
}
void Camera::Enable() {
enabled = true;
}
void Camera::_Move(float deltaTime) {
if (upKeyDown) {
cameraPos += cameraFront * MOVE_SPEED * deltaTime;
}
if (downKeyDown) {
cameraPos -= cameraFront * MOVE_SPEED * deltaTime;
}
if (leftKeyDown) {
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * MOVE_SPEED * deltaTime;
}
if (rightKeyDown) {
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * MOVE_SPEED * deltaTime;
}
}
void Camera::_Tilt() {
// Change camera direction
glm::vec3 direction;
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(direction);
view = glm::lookAt(
cameraPos,
cameraPos + cameraFront,
cameraUp
);
}
void Camera::_Zoom() {
// Update FOV (zoom)
projection = glm::perspective(glm::radians(fov), DISPLAY_WIDTH / DISPLAY_HEIGHT, 0.1f, 100.0f);
}
void Camera::OnKeyPress(int key, int action) {
if (!enabled) {
return;
}
if (key == GLFW_KEY_W) {
if (action == GLFW_PRESS) {
upKeyDown = true;
} else if (action == GLFW_RELEASE) {
upKeyDown = false;
}
}
if (key == GLFW_KEY_S) {
if (action == GLFW_PRESS) {
downKeyDown = true;
} else if (action == GLFW_RELEASE) {
downKeyDown = false;
}
}
if (key == GLFW_KEY_A) {
if (action == GLFW_PRESS) {
leftKeyDown = true;
} else if (action == GLFW_RELEASE) {
leftKeyDown = false;
}
}
if (key == GLFW_KEY_D) {
if (action == GLFW_PRESS) {
rightKeyDown = true;
} else if (action == GLFW_RELEASE) {
rightKeyDown = false;
}
}
}
void Camera::OnMouseMove(float xPos, float yPos) {
if (!enabled) {
return;
}
float xOffset = (xPos - lastX) * MOUSE_SENSITIVITY;
float yOffset = (lastY - yPos) * MOUSE_SENSITIVITY;
lastX = xPos;
lastY = yPos;
yaw += xOffset;
pitch += yOffset;
if (pitch > 89.0f) {
pitch = 89.0f;
} else if (pitch < -89.0f) {
pitch = -89.0f;
}
}
void Camera::OnScroll(float yOffset) {
if (!enabled) {
return;
}
fov -= yOffset;
if (fov < 1.0f) {
fov = 1.0f;
} else if (fov > 45.0f) {
fov = 45.0f;
}
}
glm::mat4 Camera::GetViewMatrix() {
return glm::mat4(view);
}
glm::mat4 Camera::GetProjectionMatrix() {
return glm::mat4(projection);
}
glm::vec3 Camera::GetPosition() {
return glm::vec3(cameraPos);
}
glm::vec3 Camera::GetDirection() {
return glm::vec3(cameraFront);
}