Skip to content

Commit 01fee2c

Browse files
committed
[camera] Add cache for camera data update
1 parent 1e8c012 commit 01fee2c

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

include/inexor/vulkan-renderer/camera.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ class Camera {
7777
/// The keys for the movement FORWARD, BACKWARD, LEFT, RIGHT.
7878
std::array<bool, 4> m_keys{false, false, false, false};
7979

80+
float m_vertical_fov{0.0f};
81+
82+
bool m_update_vertical_fov{false};
83+
bool m_update_view_matrix{false};
84+
bool m_update_perspective_matrix{false};
85+
8086
void update_vectors();
8187

8288
void update_matrices();

src/vulkan-renderer/camera.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,25 @@ void Camera::update_vectors() {
2222
m_front = glm::normalize(front);
2323
m_right = glm::normalize(glm::cross(m_front, m_world_up));
2424
m_up = glm::normalize(glm::cross(m_right, m_front));
25+
26+
m_update_view_matrix = true;
2527
}
2628
}
2729

2830
void Camera::update_matrices() {
2931
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+
}
3344
}
3445
}
3546

@@ -60,10 +71,13 @@ void Camera::set_movement_state(const CameraMovement key, const bool pressed) {
6071

6172
void Camera::set_position(const glm::vec3 position) {
6273
m_position = position;
74+
m_update_view_matrix = true;
6375
}
6476

6577
void Camera::set_aspect_ratio(const float width, const float height) {
6678
m_aspect_ratio = width / height;
79+
m_update_perspective_matrix = true;
80+
m_update_vertical_fov = true;
6781
}
6882

6983
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)
91105

92106
void Camera::set_near_plane(const float near_plane) {
93107
m_near_plane = near_plane;
108+
m_update_perspective_matrix = true;
94109
}
95110

96111
void Camera::set_far_plane(const float far_plane) {
97112
m_far_plane = far_plane;
113+
m_update_perspective_matrix = true;
98114
}
99115

100116
void Camera::change_zoom(const float offset) {
101117
m_fov -= offset * m_zoom_step;
102118

103119
// Make sure field of view is in range between specified minimum and maximum value.
104120
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;
105124
}
106125

107126
void Camera::update(const float delta_time) {
@@ -120,6 +139,8 @@ void Camera::update(const float delta_time) {
120139
if (m_keys[3] && !m_keys[1]) {
121140
m_position += m_right * move_speed;
122141
}
142+
143+
m_update_view_matrix = true;
123144
}
124145
}
125146

0 commit comments

Comments
 (0)