-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
91 lines (76 loc) · 2.34 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
//
// Created by pavli on 16/10/2022.
//
#include "Camera.h"
glm::mat4 Camera::get_view() const {
return glm::lookAt(camera_pos, camera_pos + camera_front, camera_up);
}
void Camera::update_position(Window *window) {
const float cameraSpeed = 0.05f;
bool updated = false;
if (window->is_pressed(GLFW_KEY_W)){
camera_pos += cameraSpeed * camera_front;
updated = true;
}
if (window->is_pressed(GLFW_KEY_S)){
camera_pos -= cameraSpeed * camera_front;
updated = true;
}
if (window->is_pressed(GLFW_KEY_A)){
camera_pos -= glm::normalize(glm::cross(camera_front, camera_up)) * cameraSpeed;
updated = true;
}
if (window->is_pressed(GLFW_KEY_D)){
camera_pos += glm::normalize(glm::cross(camera_front, camera_up)) * cameraSpeed;
updated = true;
}
if (window->is_pressed(GLFW_KEY_E)){
camera_pos -= cameraSpeed * camera_up;
updated = true;
}
if (window->is_pressed(GLFW_KEY_Q)){
camera_pos += cameraSpeed * camera_up;
updated = true;
}
if(updated){
notify_observers(Event::VIEW_UPDATE);
}
}
void Camera::update(Subject* subject, Event event) {
if(event == Event::VIEW_UPDATE){
auto data = CallbackController::get_instance()->get_last_data();
mouse_callback((float) data[0], (float) data[1]);
}
}
void Camera::mouse_callback(float xpos, float ypos) {
if (first_mouse) {
last_x = xpos;
last_y = ypos;
first_mouse = false;
}
float x_offset = xpos - last_x;
float y_offset = last_y - ypos;
last_x = xpos;
last_y = ypos;
float sensitivity = 0.1f;
x_offset *= sensitivity;
y_offset *= sensitivity;
angle_to_sides += x_offset;
angle_updown += y_offset;
if (angle_updown > 89.0f)
angle_updown = 89.0f;
if (angle_updown < -89.0f)
angle_updown = -89.0f;
glm::vec3 direction;
direction.x = cos(glm::radians(angle_to_sides)) * cos(glm::radians(angle_updown));
direction.y = sin(glm::radians(angle_updown));
direction.z = sin(glm::radians(angle_to_sides)) * cos(glm::radians(angle_updown));
camera_front = glm::normalize(direction);
notify_observers(Event::VIEW_UPDATE);
}
glm::vec3 Camera::get_position() {
return camera_pos;
}
glm::vec3 Camera::get_direction() {
return camera_front;
}