summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGL/Clipper.h
diff options
context:
space:
mode:
authorJesse Buhagiar <jooster669@gmail.com>2021-05-04 23:00:45 +1000
committerAndreas Kling <kling@serenityos.org>2021-05-08 10:13:22 +0200
commite8cd89a53818190ce23694401bff261209583336 (patch)
tree8571af6b91da9931d1e7faafb4b5d421c2e1e56f /Userland/Libraries/LibGL/Clipper.h
parent834f3c64f0b54d14d8d5a39d670934d3a64a7f80 (diff)
downloadserenity-e8cd89a53818190ce23694401bff261209583336.zip
LibGL: Move polygon clipping to `Clipper` class
This code has also been optimised to be much more memory friendly by removing a _lot_ of extraneous copies. The result is that, when profiled, it's around 8x faster than the previous implementation. Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
Diffstat (limited to 'Userland/Libraries/LibGL/Clipper.h')
-rw-r--r--Userland/Libraries/LibGL/Clipper.h59
1 files changed, 59 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGL/Clipper.h b/Userland/Libraries/LibGL/Clipper.h
new file mode 100644
index 0000000000..5c2e5bb276
--- /dev/null
+++ b/Userland/Libraries/LibGL/Clipper.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Vector.h>
+#include <LibGfx/Vector4.h>
+
+namespace GL {
+
+class Clipper final {
+ enum ClipPlane : u8 {
+ LEFT = 0,
+ RIGHT,
+ TOP,
+ BOTTOM,
+ NEAR,
+ FAR
+ };
+
+ static constexpr u8 NUMBER_OF_CLIPPING_PLANES = 6;
+ static constexpr u8 MAX_CLIPPED_VERTS = 6;
+
+ static constexpr FloatVector4 clip_planes[] = {
+ { -1, 0, 0, 1 }, // Left Plane
+ { 1, 0, 0, 1 }, // Right Plane
+ { 0, 1, 0, 1 }, // Top Plane
+ { 0, -1, 0, 1 }, // Bottom plane
+ { 0, 0, 1, 1 }, // Near Plane
+ { 0, 0, -1, 1 } // Far Plane
+ };
+
+ static constexpr FloatVector4 clip_plane_normals[] = {
+ { 1, 0, 0, 1 }, // Left Plane
+ { -1, 0, 0, 1 }, // Right Plane
+ { 0, -1, 0, 1 }, // Top Plane
+ { 0, 1, 0, 1 }, // Bottom plane
+ { 0, 0, -1, 1 }, // Near Plane
+ { 0, 0, 1, 1 } // Far Plane
+ };
+
+public:
+ Clipper() { }
+
+ void clip_triangle_against_frustum(Vector<FloatVector4>& input_vecs);
+ const Vector<FloatVector4, MAX_CLIPPED_VERTS>& clipped_verts() const;
+
+private:
+ bool point_within_clip_plane(const FloatVector4& vertex, ClipPlane plane);
+ FloatVector4 clip_intersection_point(const FloatVector4& vec, const FloatVector4& prev_vec, ClipPlane plane_index);
+
+private:
+ Vector<FloatVector4, MAX_CLIPPED_VERTS> m_clipped_verts;
+};
+
+}