-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.cpp
More file actions
77 lines (60 loc) · 2.58 KB
/
Camera.cpp
File metadata and controls
77 lines (60 loc) · 2.58 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
#include "Camera.hpp"
namespace gps {
//Camera constructor
//Camera constructor
Camera::Camera(glm::vec3 cameraPosition, glm::vec3 cameraTarget, glm::vec3 cameraUp) {
this->cameraPosition = cameraPosition;
this->cameraTarget = cameraTarget;
// front direction = normalized (target - position)
this->cameraFrontDirection = glm::normalize(cameraTarget - cameraPosition);
// normalize up
this->cameraUpDirection = glm::normalize(cameraUp);
// right = front x up
this->cameraRightDirection = glm::normalize(glm::cross(cameraFrontDirection, cameraUpDirection));
}
//return the view matrix, using the glm::lookAt() function
glm::mat4 Camera::getViewMatrix() {
return glm::lookAt(cameraPosition, cameraTarget, cameraUpDirection);
}
//update the camera internal parameters following a camera move event
void Camera::move(MOVE_DIRECTION direction, float speed) {
switch (direction) {
case MOVE_FORWARD:
cameraPosition += cameraFrontDirection * speed;
break;
case MOVE_BACKWARD:
cameraPosition -= cameraFrontDirection * speed;
break;
case MOVE_RIGHT:
cameraPosition += cameraRightDirection * speed;
break;
case MOVE_LEFT:
cameraPosition -= cameraRightDirection * speed;
break;
}
// Update target = position + front
cameraTarget = cameraPosition + cameraFrontDirection;
}
//update the camera internal parameters following a camera rotate event
//yaw - camera rotation around the y axis
//pitch - camera rotation around the x axis
void Camera::rotate(float pitch, float yaw) {
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));
cameraFrontDirection = glm::normalize(front);
// Recalculate right and up
cameraRightDirection = glm::normalize(glm::cross(cameraFrontDirection, glm::vec3(0.0f, 1.0f, 0.0f)));
cameraUpDirection = glm::normalize(glm::cross(cameraRightDirection, cameraFrontDirection));
// Update target
cameraTarget = cameraPosition + cameraFrontDirection;
}
glm::vec3 Camera::getPosition() const {
return this->cameraPosition;
}
void Camera::setPosition(glm::vec3 newPosition) {
this->cameraPosition = newPosition;
this->cameraTarget = this->cameraPosition + this->cameraFrontDirection;
}
}