-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCamera.cpp
98 lines (86 loc) · 2.22 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#define GLM_ENABLE_EXPERIMENTAL
#include "Camera.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
#include <iostream>
#define PI 3.14159265f
Camera::Camera() {
pos = glm::vec3(1.5f, 0.5f, 0.5f);
forward = glm::vec3(0.0f, 0.0f, 1.0f);
up = glm::vec3(0.0f, 1.0f, 0.0f);
right = glm::normalize(glm::cross(forward, up));
}
glm::mat4 Camera::render() {
return glm::lookAt(pos, pos+forward, up);
}
// set the camera position
void Camera::setPosition(glm::vec3 pos) {
this->pos = pos;
}
void Camera::setForward(glm::vec3 forward) {
this->forward = forward;
}
void Camera::setUp(glm::vec3 up) {
this->up = up;
}
void Camera::move(glm::vec3 dir) {
this->setPosition(this->pos + dir);
}
// move the camera UP, DOWN, LEFT, RIGHT, FORWARD, BACKWARD
void Camera::move(Dir dir, float distance) {
switch (dir) {
case UP:
move(up * distance);
break;
case DOWN:
move((-up) * distance);
break;
case LEFT:
move((-right) * distance);
break;
case RIGHT:
move(right * distance);
break;
case FORWARD:
move(forward * distance);
break;
case BACKWARD:
move((-forward) * distance);
break;
default:
std::cout << "Available options are: UP, DOWN, LEFT, RIGHT, FORWARD, BACKWARDS.\n";
}
}
void Camera::rotateTowards(glm::vec3 v, float angle) {
glm::vec3 normal = glm::normalize(glm::cross(forward, v));
forward = glm::rotate(forward, angle, normal);
// update right and up vectors
right = glm::normalize(glm::cross(forward, up));
up = glm::normalize(glm::cross(right, forward));
forward = glm::normalize(forward);
}
// rotate the camera UP, DOWN, LEFT, RIGHT
void Camera::rotateTowards(Dir dir, float angle) {
switch (dir) {
case UP:
rotateTowards(up, angle);
break;
case DOWN:
rotateTowards(-up, angle);
break;
case LEFT:
rotateTowards(-right, angle);
break;
case RIGHT:
rotateTowards(right, angle);
break;
case FORWARD:
std::cout << "Cannot rotate camera towards FORWARD. Available options are: UP, DOWN, LEFT, RIGHT.\n";
break;
case BACKWARD:
std::cout << "Cannot rotate camera towards BACKWARDS. Available options are: UP, DOWN, LEFT, RIGHT.\n";
break;
default:
std::cout << "Available options are: UP, DOWN, LEFT, RIGHT.\n";
}
}