@@ -22,15 +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
- m_update_needed = false ;
25
+
26
+ m_update_view_matrix = true ;
26
27
}
27
28
}
28
29
29
30
void Camera::update_matrices () {
30
31
if (m_type == CameraType::LOOK_AT) {
31
- m_view_matrix = glm::lookAt (m_position, m_position + m_front, m_up);
32
- m_perspective_matrix = glm::perspective (glm::radians (m_fov), m_aspect_ratio, m_near_plane, m_far_plane);
33
- m_update_needed = false ;
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
+ }
34
44
}
35
45
}
36
46
@@ -61,10 +71,13 @@ void Camera::set_movement_state(const CameraMovement key, const bool pressed) {
61
71
62
72
void Camera::set_position (const glm::vec3 position) {
63
73
m_position = position;
74
+ m_update_view_matrix = true ;
64
75
}
65
76
66
77
void Camera::set_aspect_ratio (const float width, const float height) {
67
78
m_aspect_ratio = width / height;
79
+ m_update_perspective_matrix = true ;
80
+ m_update_vertical_fov = true ;
68
81
}
69
82
70
83
void Camera::set_movement_speed (const float speed) {
@@ -92,22 +105,25 @@ void Camera::set_rotation(const float yaw, const float pitch, const float roll)
92
105
93
106
void Camera::set_near_plane (const float near_plane) {
94
107
m_near_plane = near_plane;
108
+ m_update_perspective_matrix = true ;
95
109
}
96
110
97
111
void Camera::set_far_plane (const float far_plane) {
98
112
m_far_plane = far_plane;
113
+ m_update_perspective_matrix = true ;
99
114
}
100
115
101
116
void Camera::change_zoom (const float offset) {
102
117
m_fov -= offset * m_zoom_step;
103
118
104
119
// Make sure field of view is in range between specified minimum and maximum value.
105
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 ;
106
124
}
107
125
108
126
void Camera::update (const float delta_time) {
109
- m_update_needed = true ;
110
-
111
127
if (m_type == CameraType::LOOK_AT && is_moving ()) {
112
128
const float move_speed = delta_time * m_movement_speed;
113
129
@@ -123,6 +139,8 @@ void Camera::update(const float delta_time) {
123
139
if (m_keys[3 ] && !m_keys[1 ]) {
124
140
m_position += m_right * move_speed;
125
141
}
142
+
143
+ m_update_view_matrix = true ;
126
144
}
127
145
}
128
146
0 commit comments