-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfps_camera.h
50 lines (43 loc) · 1.3 KB
/
fps_camera.h
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
#include "camera.h"
class FPSCamera : public Camera {
public:
FPSCamera(float fov, float width, float height) : Camera(fov, width, height) {
up = glm::vec3(0.0f, 1.0f, 0.0f);
yaw = -90.0f;
pitch = 0.0f;
onMouseMoved(0.0f, 0.0f);
update();
}
void onMouseMoved(float xRel, float yRel) {
yaw += xRel * mouseSensitivity;
pitch -= yRel * mouseSensitivity;
if(pitch > 89.0f)
pitch = 89.0f;
if(pitch < -89.0f)
pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(pitch)) * cos(glm::radians(yaw));
front.y = sin(glm::radians(pitch));
front.z = cos(glm::radians(pitch)) * sin(glm::radians(yaw));
lookAt = glm::normalize(front);
update();
}
void update() override {
view = glm::lookAt(position, position+lookAt, up);
viewProj = projection * view;
}
void moveFront(float amount) {
translate(glm::normalize(glm::vec3(1.0f, 0.0f, 1.0f)*lookAt) * amount);
update();
}
void moveSideways(float amount) {
translate(glm::normalize(glm::cross(lookAt, up)) * amount);
update();
}
protected:
float yaw;
float pitch;
glm::vec3 lookAt;
const float mouseSensitivity = 0.3f;
glm::vec3 up;
};