aboutsummaryrefslogtreecommitdiff
path: root/src/client/math.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/math.cc')
-rw-r--r--src/client/math.cc26
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