@@ -22,14 +22,25 @@ void Camera::update_vectors() {
22
22
m_front = glm::normalize (front);
23
23
m_right = glm::normalize (glm::cross (m_front, m_world_up));
24
24
m_up = glm::normalize (glm::cross (m_right, m_front));
25
+
26
+ m_update_view_matrix = true ;
25
27
}
26
28
}
27
29
28
30
void Camera::update_matrices () {
29
31
if (m_type == CameraType::LOOK_AT) {
30
- const float vertical_fov = 2 .0f * glm::atan (glm::tan (glm::radians (m_fov) / 2 .0f ) / m_aspect_ratio);
31
- m_view_matrix = glm::lookAt (m_position, m_position + m_front, m_up);
32
- m_perspective_matrix = glm::perspective (vertical_fov, m_aspect_ratio, m_near_plane, m_far_plane);
32
+ if (m_update_vertical_fov) {
33
+ m_vertical_fov = 2 .0f * glm::atan (glm::tan (glm::radians (m_fov) / 2 .0f ) / m_aspect_ratio);
34
+ m_update_vertical_fov = false ;
35
+ }
36
+ if (m_update_view_matrix) {
37
+ m_view_matrix = glm::lookAt (m_position, m_position + m_front, m_up);
38
+ m_update_view_matrix = false ;
39
+ }
40
+ if (m_update_perspective_matrix) {
41
+ m_perspective_matrix = glm::perspective (m_vertical_fov, m_aspect_ratio, m_near_plane, m_far_plane);
42
+ m_update_perspective_matrix = false ;
43
+ }
33
44
}
34
45
}
35
46
@@ -60,10 +71,13 @@ void Camera::set_movement_state(const CameraMovement key, const bool pressed) {
60
71
61
72
void Camera::set_position (const glm::vec3 position) {
62
73
m_position = position;
74
+ m_update_view_matrix = true ;
63
75
}
64
76
65
77
void Camera::set_aspect_ratio (const float width, const float height) {
66
78
m_aspect_ratio = width / height;
79
+ m_update_perspective_matrix = true ;
80
+ m_update_vertical_fov = true ;
67
81
}
68
82
69
83
void Camera::set_movement_speed (const float speed) {
@@ -91,17 +105,22 @@ void Camera::set_rotation(const float yaw, const float pitch, const float roll)
91
105
92
106
void Camera::set_near_plane (const float near_plane) {
93
107
m_near_plane = near_plane;
108
+ m_update_perspective_matrix = true ;
94
109
}
95
110
96
111
void Camera::set_far_plane (const float far_plane) {
97
112
m_far_plane = far_plane;
113
+ m_update_perspective_matrix = true ;
98
114
}
99
115
100
116
void Camera::change_zoom (const float offset) {
101
117
m_fov -= offset * m_zoom_step;
102
118
103
119
// Make sure field of view is in range between specified minimum and maximum value.
104
120
m_fov = std::clamp (m_fov, m_fov_min, m_fov_max);
121
+
122
+ m_update_vertical_fov = true ;
123
+ m_update_perspective_matrix = true ;
105
124
}
106
125
107
126
void Camera::update (const float delta_time) {
@@ -120,6 +139,8 @@ void Camera::update(const float delta_time) {
120
139
if (m_keys[3 ] && !m_keys[1 ]) {
121
140
m_position += m_right * move_speed;
122
141
}
142
+
143
+ m_update_view_matrix = true ;
123
144
}
124
145
}
125
146
0 commit comments