Clipper.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Vector.h>
  8. #include <LibGfx/Vector4.h>
  9. namespace GL {
  10. class Clipper final {
  11. enum ClipPlane : u8 {
  12. LEFT = 0,
  13. RIGHT,
  14. TOP,
  15. BOTTOM,
  16. NEAR,
  17. FAR
  18. };
  19. static constexpr u8 NUMBER_OF_CLIPPING_PLANES = 6;
  20. static constexpr u8 MAX_CLIPPED_VERTS = 6;
  21. static constexpr FloatVector4 clip_planes[] = {
  22. { -1, 0, 0, 1 }, // Left Plane
  23. { 1, 0, 0, 1 }, // Right Plane
  24. { 0, 1, 0, 1 }, // Top Plane
  25. { 0, -1, 0, 1 }, // Bottom plane
  26. { 0, 0, 1, 1 }, // Near Plane
  27. { 0, 0, -1, 1 } // Far Plane
  28. };
  29. static constexpr FloatVector4 clip_plane_normals[] = {
  30. { 1, 0, 0, 1 }, // Left Plane
  31. { -1, 0, 0, 1 }, // Right Plane
  32. { 0, -1, 0, 1 }, // Top Plane
  33. { 0, 1, 0, 1 }, // Bottom plane
  34. { 0, 0, -1, 1 }, // Near Plane
  35. { 0, 0, 1, 1 } // Far Plane
  36. };
  37. public:
  38. Clipper() { }
  39. void clip_triangle_against_frustum(Vector<FloatVector4>& input_vecs);
  40. const Vector<FloatVector4, MAX_CLIPPED_VERTS>& clipped_verts() const;
  41. private:
  42. bool point_within_clip_plane(const FloatVector4& vertex, ClipPlane plane);
  43. FloatVector4 clip_intersection_point(const FloatVector4& vec, const FloatVector4& prev_vec, ClipPlane plane_index);
  44. private:
  45. Vector<FloatVector4, MAX_CLIPPED_VERTS> m_clipped_verts;
  46. };
  47. }