Skip to content

Commit c3ad12a

Browse files
authored
[merge] Merge pull request #408 from inexorgame/yeetari/fov-fix
[camera] Make sure to convert horizontal fov to vertical fov before passing to glm
2 parents dd539ca + 01fee2c commit c3ad12a

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

configuration/renderer.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ name = "Inexor-Engine"
1313

1414
[application.window]
1515
mode = "windowed"
16-
width = 800
17-
height = 800
16+
width = 1280
17+
height = 720
1818
name = "Inexor-Vulkan-Renderer"
1919

2020
[shaders]

include/inexor/vulkan-renderer/camera.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Camera {
5353
/// The camera's maximum pitch angle.
5454
/// Looking straight upwards is the maximum pitch angle.
5555
float m_pitch_max{+89.0f};
56-
/// The camera's field of view.
56+
/// The camera's horizontal field of view.
5757
float m_fov{90.0f};
5858
/// The camera's maximum field of view.
5959
float m_fov_max{90.0f};
@@ -76,8 +76,12 @@ class Camera {
7676

7777
/// The keys for the movement FORWARD, BACKWARD, LEFT, RIGHT.
7878
std::array<bool, 4> m_keys{false, false, false, false};
79-
/// Will be set to ``true`` if the matrices need to be recalculated.
80-
bool m_update_needed{true};
79+
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};
8185

8286
void update_vectors();
8387

@@ -212,16 +216,12 @@ class Camera {
212216
void update(float delta_time);
213217

214218
[[nodiscard]] const glm::mat4 &view_matrix() {
215-
if (m_update_needed) {
216-
update_matrices();
217-
}
219+
update_matrices();
218220
return m_view_matrix;
219221
}
220222

221223
[[nodiscard]] const glm::mat4 &perspective_matrix() {
222-
if (m_update_needed) {
223-
update_matrices();
224-
}
224+
update_matrices();
225225
return m_perspective_matrix;
226226
}
227227
};

src/vulkan-renderer/camera.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +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-
m_update_needed = false;
25+
26+
m_update_view_matrix = true;
2627
}
2728
}
2829

2930
void Camera::update_matrices() {
3031
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+
}
3444
}
3545
}
3646

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

6272
void Camera::set_position(const glm::vec3 position) {
6373
m_position = position;
74+
m_update_view_matrix = true;
6475
}
6576

6677
void Camera::set_aspect_ratio(const float width, const float height) {
6778
m_aspect_ratio = width / height;
79+
m_update_perspective_matrix = true;
80+
m_update_vertical_fov = true;
6881
}
6982

7083
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)
92105

93106
void Camera::set_near_plane(const float near_plane) {
94107
m_near_plane = near_plane;
108+
m_update_perspective_matrix = true;
95109
}
96110

97111
void Camera::set_far_plane(const float far_plane) {
98112
m_far_plane = far_plane;
113+
m_update_perspective_matrix = true;
99114
}
100115

101116
void Camera::change_zoom(const float offset) {
102117
m_fov -= offset * m_zoom_step;
103118

104119
// Make sure field of view is in range between specified minimum and maximum value.
105120
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;
106124
}
107125

108126
void Camera::update(const float delta_time) {
109-
m_update_needed = true;
110-
111127
if (m_type == CameraType::LOOK_AT && is_moving()) {
112128
const float move_speed = delta_time * m_movement_speed;
113129

@@ -123,6 +139,8 @@ void Camera::update(const float delta_time) {
123139
if (m_keys[3] && !m_keys[1]) {
124140
m_position += m_right * move_speed;
125141
}
142+
143+
m_update_view_matrix = true;
126144
}
127145
}
128146

0 commit comments

Comments
 (0)