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
92
93
94
95
96
|
#include "client/render/camera.hh"
namespace client {
namespace render {
namespace camera {
float& get_xfov() noexcept {
static float xfov = 100.0f;
return xfov;
}
glm::vec3& get_pos() noexcept {
static glm::vec3 pos{0.0f, 0.0f, 0.0f};
return pos;
}
glm::vec3& get_front() noexcept {
static glm::vec3 front{0.0f, 0.0f, 0.0f};
return front;
}
glm::vec3& get_up() noexcept {
static glm::vec3 front{0.0f, 1.0f, 0.0f};
return front;
}
static glm::mat4 make_view() noexcept {
const glm::vec3& pos = get_pos();
return glm::lookAt(pos, pos + get_front(), get_up());
}
glm::mat4& get_view() noexcept {
static glm::mat4 view = make_view();
return view;
}
static glm::mat4 make_proj() noexcept {
const glm::vec2& window = client::render::get_window_size();
return glm::perspective(glm::radians(get_yfov()), window.x / window.y,
get_znear(), get_zfar());
}
glm::mat4& get_proj() noexcept {
static glm::mat4 proj = make_proj();
return proj;
}
static frustum make_frustum() noexcept {
const glm::mat4 matrix = get_proj() * get_view();
const glm::vec4 row_x = glm::row(matrix, 0);
const glm::vec4 row_y = glm::row(matrix, 1);
const glm::vec4 row_z = glm::row(matrix, 2);
const glm::vec4 row_w = glm::row(matrix, 3);
frustum ret{row_w + row_x, row_w - row_x, row_w + row_y,
row_w - row_y, row_w + row_z, row_w - row_z};
std::ranges::transform(ret, std::begin(ret), [](const auto& matrix) {
return glm::normalize(matrix);
});
return ret;
}
frustum& get_frustum() noexcept {
static frustum ret = make_frustum();
return ret;
}
float& get_znear() noexcept {
static float znear = 0.1f;
return znear;
}
float& get_zfar() noexcept {
static float zfar = 1024.0f;
return zfar;
}
float get_yfov() noexcept {
const glm::vec2& window = client::render::get_window_size();
const float yfov =
2.0f * std::atan((window.y / window.x) *
std::tan(glm::radians(get_xfov()) / 2.0f));
return glm::degrees(yfov);
}
void update() noexcept {
get_view() = make_view(); // specific order here!
get_proj() = make_proj();
get_frustum() = make_frustum();
}
} // namespace camera
} // namespace render
} // namespace client
|