forked from GabrielRosenberg/Shader-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camera.cpp
75 lines (65 loc) · 2.4 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
72
73
74
75
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
Camera::Camera(glm::vec3 position, glm::vec3 up, float yaw, float pitch)
: front(glm::vec3(0.0f, 0.0f, -1.0f)), movementSpeed(SPEED), mouseSensitivity(SENSITIVITY), zoom(ZOOM) {
this->position = position;
this->worldUp = up;
this->yaw = yaw;
this->pitch = pitch;
updateCameraVectors();
}
Camera::Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) {
this->position = glm::vec3(posX, posY, posZ);
this->worldUp = glm::vec3(upX, upY, upZ);
this->yaw = yaw;
this->pitch = pitch;
updateCameraVectors();
}
glm::mat4 Camera::GetViewMatrix() {
return glm::lookAt(position, position + front, up);
}
void Camera::ProcessKeyboard(CameraMovement direction, float deltaTime) {
float velocity = movementSpeed * deltaTime;
if (direction == FORWARD)
position += front * velocity;
if (direction == BACKWARD)
position -= front * velocity;
if (direction == LEFT)
position -= right * velocity;
if (direction == RIGHT)
position += right * velocity;
}
void Camera::ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch) {
xoffset *= mouseSensitivity;
yoffset *= mouseSensitivity;
yaw += xoffset;
pitch += yoffset;
// make sure that when pitch is out of bounds, screen doesn't get flipped
if (constrainPitch)
{
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
}
// update Front, Right and Up Vectors using the updated Euler angles
updateCameraVectors();
}
void Camera::ProcessMouseScroll(float yoffset) {
zoom -= (float)yoffset;
if (zoom < 1.0f)
zoom = 1.0f;
if (zoom > 45.0f)
zoom = 45.0f;
}
void Camera::updateCameraVectors() {
// calculate the new Front vector
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
this->front = glm::normalize(front);
// also re-calculate the Right and Up vector
right = glm::normalize(glm::cross(this->front, worldUp)); // normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement.
up = glm::normalize(glm::cross(right, this->front));
}