blob: 01fc68bc6dfd49ac2f775ad4b4719b381793dc86 (
plain)
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
|
#include "client/math.hh"
namespace client {
namespace math {
std::optional<glm::vec2> world_to_screen(const glm::vec3& pos,
const glm::mat4& m,
const glm::vec2& window) noexcept {
// clang-format off
const float w = m[0][3] * pos.x + m[1][3] * pos.y + m[2][3] * pos.z + m[3][3];
if (w < 0.0f) {
return std::nullopt;
}
const float inv_w = std::fabs(1.0f / w);
const float x = window.x * 0.5f + (0.5f * (m[0][0] * pos.x + m[1][0] * pos.y + m[2][0] * pos.z + m[3][0]) * inv_w * window.x + 0.5f);
const float y = window.y * 0.5f + (0.5f * (m[0][1] * pos.x + m[1][1] * pos.y + m[2][1] * pos.z + m[3][1]) * inv_w * window.y + 0.5f);
// clang-format on
return glm::vec2{x, y};
}
} // namespace math
} // namespace client
|