-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
71 lines (59 loc) · 2.12 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
#include <glm/gtc/matrix_transform.hpp>
#include "Camera.h"
Camera::Camera(glm::vec3 globalUpDirection, glm::vec3 position) :
globalUpDirection(globalUpDirection), position(position), frontDirection(), upDirection(), rightDirection() {
yaw = -90.0f;
pitch = 0.0f;
aspectRatio = 1.0f;
fieldOfView = 45.0f;
movementSpeed = 1.0f;
mouseSensitivity = 1.0f;
updateVectors();
}
glm::mat4 Camera::getViewMatrix() const {
return glm::lookAt(position, position + frontDirection, upDirection);
}
glm::mat4 Camera::getProjectionMatrix() const {
return glm::perspective(glm::radians(fieldOfView), aspectRatio, 0.1f, 100.0f);
}
void Camera::processKeyboardInput(const glm::vec3 &axes, float deltaTime) {
const glm::vec3 movement = axes * movementSpeed * deltaTime;
position += movement.x * rightDirection;
position += movement.y * upDirection;
position += movement.z * frontDirection;
}
void Camera::processMouseInput(const glm::vec2 &offset) {
const glm::vec2 rotation = offset * mouseSensitivity;
yaw += rotation.x;
pitch = glm::clamp(pitch + rotation.y, -89.0f, 89.0f);
updateVectors();
}
void Camera::processMouseScroll(float offset) {
fieldOfView = glm::clamp(fieldOfView - offset, 1.0f, 90.0f);
}
void Camera::updateAspectRatio(int width, int height) {
aspectRatio = (float) width / (float) height;
}
void Camera::reset() {
yaw = -90.0f;
pitch = 0.0f;
fieldOfView = 45.0f;
updateVectors();
}
void Camera::setMovementSpeed(float speed) {
movementSpeed = speed;
}
void Camera::setMouseSensitivity(float sensitivity) {
mouseSensitivity = sensitivity;
}
void Camera::updateVectors() {
float yawInRadians = glm::radians(yaw);
float pitchInRadians = glm::radians(pitch);
frontDirection = glm::normalize(glm::vec3(
glm::cos(yawInRadians) * glm::cos(pitchInRadians),
glm::sin(pitchInRadians),
glm::sin(yawInRadians) * glm::cos(pitchInRadians)
));
rightDirection = glm::normalize(glm::cross(frontDirection, globalUpDirection));
upDirection = glm::normalize(glm::cross(rightDirection, frontDirection));
}