diff options
| author | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-12 18:05:18 +1100 |
|---|---|---|
| committer | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-12 18:05:18 +1100 |
| commit | 1cc08c51eb4b0f95c30c0a98ad1fc5ad3459b2df (patch) | |
| tree | 222dfcd07a1e40716127a347bbfd7119ce3d0984 /src/client/math.cc | |
initial commit
Diffstat (limited to 'src/client/math.cc')
| -rw-r--r-- | src/client/math.cc | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/client/math.cc b/src/client/math.cc new file mode 100644 index 0000000..01fc68b --- /dev/null +++ b/src/client/math.cc @@ -0,0 +1,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 |
