Clipper.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include <LibSoftGPU/Vertex.h>
  10. namespace SoftGPU {
  11. class Clipper final {
  12. enum ClipPlane : u8 {
  13. LEFT = 0,
  14. RIGHT,
  15. TOP,
  16. BOTTOM,
  17. NEAR,
  18. FAR
  19. };
  20. static constexpr u8 NUMBER_OF_CLIPPING_PLANES = 6;
  21. static constexpr u8 MAX_CLIPPED_VERTS = 6;
  22. static constexpr FloatVector4 clip_planes[] = {
  23. { -1, 0, 0, 1 }, // Left Plane
  24. { 1, 0, 0, 1 }, // Right Plane
  25. { 0, 1, 0, 1 }, // Top Plane
  26. { 0, -1, 0, 1 }, // Bottom plane
  27. { 0, 0, 1, 1 }, // Near Plane
  28. { 0, 0, -1, 1 } // Far Plane
  29. };
  30. static constexpr FloatVector4 clip_plane_normals[] = {
  31. { 1, 0, 0, 0 }, // Left Plane
  32. { -1, 0, 0, 0 }, // Right Plane
  33. { 0, -1, 0, 0 }, // Top Plane
  34. { 0, 1, 0, 0 }, // Bottom plane
  35. { 0, 0, 1, 0 }, // Near Plane
  36. { 0, 0, -1, 0 } // Far Plane
  37. };
  38. public:
  39. Clipper() { }
  40. void clip_triangle_against_frustum(Vector<Vertex>& input_vecs);
  41. private:
  42. bool point_within_clip_plane(const FloatVector4& vertex, ClipPlane plane);
  43. Vertex clip_intersection_point(const Vertex& vec, const Vertex& prev_vec, ClipPlane plane_index);
  44. Vector<Vertex> list_a;
  45. Vector<Vertex> list_b;
  46. };
  47. }