Clipper.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Vector.h>
  9. #include <LibGPU/Vertex.h>
  10. #include <LibGfx/Vector4.h>
  11. #include <LibSoftGPU/Clipper.h>
  12. namespace SoftGPU {
  13. template<Clipper::ClipPlane plane>
  14. static constexpr bool point_within_clip_plane(FloatVector4 const& vertex)
  15. {
  16. if constexpr (plane == Clipper::ClipPlane::LEFT)
  17. return vertex.x() >= -vertex.w();
  18. else if constexpr (plane == Clipper::ClipPlane::RIGHT)
  19. return vertex.x() <= vertex.w();
  20. else if constexpr (plane == Clipper::ClipPlane::TOP)
  21. return vertex.y() <= vertex.w();
  22. else if constexpr (plane == Clipper::ClipPlane::BOTTOM)
  23. return vertex.y() >= -vertex.w();
  24. else if constexpr (plane == Clipper::ClipPlane::NEAR)
  25. return vertex.z() >= -vertex.w();
  26. else if constexpr (plane == Clipper::ClipPlane::FAR)
  27. return vertex.z() <= vertex.w();
  28. return false;
  29. }
  30. template<Clipper::ClipPlane plane>
  31. static constexpr GPU::Vertex clip_intersection_point(GPU::Vertex const& p1, GPU::Vertex const& p2)
  32. {
  33. constexpr FloatVector4 clip_plane_normals[] = {
  34. { 1, 0, 0, 0 }, // Left Plane
  35. { -1, 0, 0, 0 }, // Right Plane
  36. { 0, -1, 0, 0 }, // Top Plane
  37. { 0, 1, 0, 0 }, // Bottom plane
  38. { 0, 0, 1, 0 }, // Near Plane
  39. { 0, 0, -1, 0 } // Far Plane
  40. };
  41. constexpr auto clip_plane_normal = clip_plane_normals[to_underlying(plane)];
  42. // See https://www.microsoft.com/en-us/research/wp-content/uploads/1978/01/p245-blinn.pdf
  43. // "Clipping Using Homogeneous Coordinates" Blinn/Newell, 1978
  44. float const w1 = p1.clip_coordinates.w();
  45. float const w2 = p2.clip_coordinates.w();
  46. float const x1 = clip_plane_normal.dot(p1.clip_coordinates);
  47. float const x2 = clip_plane_normal.dot(p2.clip_coordinates);
  48. float const a = (w1 + x1) / ((w1 + x1) - (w2 + x2));
  49. GPU::Vertex out;
  50. out.position = mix(p1.position, p2.position, a);
  51. out.eye_coordinates = mix(p1.eye_coordinates, p2.eye_coordinates, a);
  52. out.clip_coordinates = mix(p1.clip_coordinates, p2.clip_coordinates, a);
  53. out.color = mix(p1.color, p2.color, a);
  54. for (size_t i = 0; i < GPU::NUM_SAMPLERS; ++i)
  55. out.tex_coords[i] = mix(p1.tex_coords[i], p2.tex_coords[i], a);
  56. out.normal = mix(p1.normal, p2.normal, a);
  57. return out;
  58. }
  59. template<Clipper::ClipPlane plane>
  60. FLATTEN static constexpr void clip_plane(Vector<GPU::Vertex>& read_list, Vector<GPU::Vertex>& write_list)
  61. {
  62. auto read_from = &read_list;
  63. auto write_to = &write_list;
  64. write_to->clear_with_capacity();
  65. for (size_t i = 0; i < read_from->size(); i++) {
  66. auto const& curr_vec = read_from->at((i + 1) % read_from->size());
  67. auto const& prev_vec = read_from->at(i);
  68. bool const is_curr_point_within_clip_plane = point_within_clip_plane<plane>(curr_vec.clip_coordinates);
  69. bool const is_prev_point_within_clip_plane = point_within_clip_plane<plane>(prev_vec.clip_coordinates);
  70. if (is_curr_point_within_clip_plane != is_prev_point_within_clip_plane) {
  71. auto const intersect = clip_intersection_point<plane>(prev_vec, curr_vec);
  72. write_to->append(intersect);
  73. }
  74. if (is_curr_point_within_clip_plane)
  75. write_to->append(curr_vec);
  76. }
  77. swap(write_list, read_list);
  78. }
  79. void Clipper::clip_triangle_against_frustum(Vector<GPU::Vertex>& input_verts)
  80. {
  81. list_a = input_verts;
  82. list_b.clear_with_capacity();
  83. // FIXME C++23. Static reflection will provide looping over all enum values.
  84. clip_plane<ClipPlane::LEFT>(list_a, list_b);
  85. clip_plane<ClipPlane::RIGHT>(list_a, list_b);
  86. clip_plane<ClipPlane::TOP>(list_a, list_b);
  87. clip_plane<ClipPlane::BOTTOM>(list_a, list_b);
  88. clip_plane<ClipPlane::NEAR>(list_a, list_b);
  89. clip_plane<ClipPlane::FAR>(list_a, list_b);
  90. input_verts = list_a;
  91. }
  92. }