-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCamera.cpp
More file actions
108 lines (105 loc) · 3.25 KB
/
Camera.cpp
File metadata and controls
108 lines (105 loc) · 3.25 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
#include "Camera.h"
#include <glm/gtx/rotate_vector.hpp>
/**
* @brief Method to translate the camera
*/
void Camera::translate(Camera::Direction direction) {
switch (direction) {
case Direction::FORWARD:
position += front * translationSensitivity;
break;
case Direction::BACKWARD:
position -= front * translationSensitivity;
break;
case Direction::RIGHT:
position += right * translationSensitivity;
break;
case Direction::LEFT:
position -= right * translationSensitivity;
break;
case Direction::UP:
position += up * translationSensitivity;
break;
case Direction::DOWN:
position -= up * translationSensitivity;
break;
}
updateVectors();
}
/**
* Method to update the vectors of the camera view
*/
void Camera::updateVectors() {
front = glm::normalize(glm::vec3(
cos(angles.yaw) * cos(angles.pitch),
sin(angles.pitch),
sin(angles.yaw) * cos(angles.pitch)
));
right = glm::normalize(glm::cross(front, worldUp));
auto rolledRight = glm::rotate(right, angles.roll, front);
up = glm::normalize(glm::cross(rolledRight, front));
}
/**
* @brief Method to set the position and axis of camera
*/
void Camera::lookAt() {
auto center = position + front;
gluLookAt(position.x, position.y, position.z, center.x, center.y, center.z, up.x, up.y, up.z);
}
/**
* @brief Parametrized constructor to initialise Camera class
*/
Camera::Camera(const glm::vec3 &position, const glm::vec3 &target, const glm::vec3 &worldUp) : position(position),
initPosition(position),
worldUp(worldUp) {
front = glm::normalize(target - position);
initAngles.yaw = angles.yaw = atan2f(front.z, front.x);
initAngles.pitch = angles.pitch = asinf(front.y);
initAngles.roll = angles.roll = 0;
updateVectors();
}
/**
* @brief Method to reset the camera position and axis to initial
*/
void Camera::reset() {
position = initPosition;
angles.yaw = initAngles.yaw;
angles.pitch = initAngles.pitch;
angles.roll = initAngles.roll;
zoomFactor = 1;
updateVectors();
}
/**
* @brief Method to pitch and yaw the camera
*/
void Camera::rotate(int x, int y) {
angles.yaw -= rotationSensitivity * (float) x;
angles.pitch += rotationSensitivity * (float) y;
angles.pitch = glm::clamp(angles.pitch, -HALF_PI + 0.001f, HALF_PI);
updateVectors();
}
/**
* @brief Method to roll the camera in xy-plane
*/
void Camera::rotate(int z) {
angles.roll += rotationSensitivity * (float) z;
if (angles.roll >= TWO_PI)
angles.roll -= TWO_PI;
else if (angles.roll <= -TWO_PI)
angles.roll += TWO_PI;
updateVectors();
}
/**
* @brief Method to zoom the camera
*/
void Camera::zoom(int y) {
zoomFactor -= (float) y * zoomSensitivity;
zoomFactor = std::fmax(zoomFactor, 0.05f);
zoomFactor = std::fmin(zoomFactor, 20.f);
}
/**
* @brief Method to get the zoom factor
*/
GLfloat Camera::getZoom() const {
return zoomFactor;
}