Clipper.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. * Copyright (c) 2022, Jelle Raaijmakers <jelle@gmta.nl>
  5. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/StdLibExtras.h>
  10. #include <AK/Vector.h>
  11. #include <LibGPU/Vertex.h>
  12. #include <LibGfx/Vector4.h>
  13. #include <LibSoftGPU/Clipper.h>
  14. namespace SoftGPU {
  15. template<Clipper::ClipPlane plane>
  16. static constexpr bool point_within_clip_plane(FloatVector4 const& vertex)
  17. {
  18. if constexpr (plane == Clipper::ClipPlane::Left)
  19. return vertex.x() >= -vertex.w();
  20. else if constexpr (plane == Clipper::ClipPlane::Right)
  21. return vertex.x() <= vertex.w();
  22. else if constexpr (plane == Clipper::ClipPlane::Top)
  23. return vertex.y() <= vertex.w();
  24. else if constexpr (plane == Clipper::ClipPlane::Bottom)
  25. return vertex.y() >= -vertex.w();
  26. else if constexpr (plane == Clipper::ClipPlane::Near)
  27. return vertex.z() >= -vertex.w();
  28. else if constexpr (plane == Clipper::ClipPlane::Far)
  29. return vertex.z() <= vertex.w();
  30. return false;
  31. }
  32. static bool point_within_user_plane(FloatVector4 const& vertex, FloatVector4 const& user_plane)
  33. {
  34. return vertex.dot(user_plane) >= 0;
  35. }
  36. template<Clipper::ClipPlane plane>
  37. bool point_within_plane(GPU::Vertex const& vertex, FloatVector4 const& user_plane)
  38. {
  39. if constexpr (plane == Clipper::ClipPlane::User)
  40. return point_within_user_plane(vertex.eye_coordinates, user_plane);
  41. else
  42. return point_within_clip_plane<plane>(vertex.clip_coordinates);
  43. }
  44. template<Clipper::ClipPlane plane>
  45. static GPU::Vertex clip_intersection_point(GPU::Vertex const& p1, GPU::Vertex const& p2, FloatVector4 const& plane_normal)
  46. {
  47. auto p1_coordinates = (plane == Clipper::ClipPlane::User) ? p1.eye_coordinates : p1.clip_coordinates;
  48. auto p2_coordinates = (plane == Clipper::ClipPlane::User) ? p2.eye_coordinates : p2.clip_coordinates;
  49. auto x1 = plane_normal.dot(p1_coordinates);
  50. auto x2 = plane_normal.dot(p2_coordinates);
  51. auto const a = x1 / (x1 - x2);
  52. GPU::Vertex out;
  53. out.position = mix(p1.position, p2.position, a);
  54. out.eye_coordinates = mix(p1.eye_coordinates, p2.eye_coordinates, a);
  55. out.clip_coordinates = mix(p1.clip_coordinates, p2.clip_coordinates, a);
  56. out.color = mix(p1.color, p2.color, a);
  57. for (size_t i = 0; i < GPU::NUM_TEXTURE_UNITS; ++i)
  58. out.tex_coords[i] = mix(p1.tex_coords[i], p2.tex_coords[i], a);
  59. out.normal = mix(p1.normal, p2.normal, a);
  60. return out;
  61. }
  62. template<Clipper::ClipPlane plane>
  63. FLATTEN static void clip_plane(Vector<GPU::Vertex>& input_list, Vector<GPU::Vertex>& output_list, FloatVector4 const& clip_plane)
  64. {
  65. output_list.clear_with_capacity();
  66. auto input_list_size = input_list.size();
  67. if (input_list_size == 0)
  68. return;
  69. // Ensure we can perform unchecked appends in the loop below
  70. if (input_list_size * 2 > output_list.capacity())
  71. output_list.ensure_capacity(input_list_size * 2);
  72. auto const* prev_vec = &input_list.data()[0];
  73. auto is_prev_point_within_plane = point_within_plane<plane>(*prev_vec, clip_plane);
  74. for (size_t i = 1; i <= input_list_size; i++) {
  75. auto const& curr_vec = input_list[i % input_list_size];
  76. auto const is_curr_point_within_plane = point_within_plane<plane>(curr_vec, clip_plane);
  77. if (is_curr_point_within_plane != is_prev_point_within_plane)
  78. output_list.unchecked_append(clip_intersection_point<plane>(*prev_vec, curr_vec, clip_plane));
  79. if (is_curr_point_within_plane)
  80. output_list.unchecked_append(curr_vec);
  81. prev_vec = &curr_vec;
  82. is_prev_point_within_plane = is_curr_point_within_plane;
  83. }
  84. }
  85. void Clipper::clip_points_against_frustum(Vector<GPU::Vertex>& vertices)
  86. {
  87. m_vertex_buffer.clear_with_capacity();
  88. for (auto& vertex : vertices) {
  89. auto const coords = vertex.clip_coordinates;
  90. if (point_within_clip_plane<ClipPlane::Left>(coords) && point_within_clip_plane<ClipPlane::Right>(coords)
  91. && point_within_clip_plane<ClipPlane::Top>(coords) && point_within_clip_plane<ClipPlane::Bottom>(coords)
  92. && point_within_clip_plane<ClipPlane::Near>(coords) && point_within_clip_plane<ClipPlane::Far>(coords))
  93. m_vertex_buffer.append(vertex);
  94. }
  95. vertices.clear_with_capacity();
  96. vertices.extend(m_vertex_buffer);
  97. }
  98. constexpr FloatVector4 clip_plane_eqns[] = {
  99. { 1, 0, 0, 1 }, // Left Plane
  100. { -1, 0, 0, 1 }, // Right Plane
  101. { 0, -1, 0, 1 }, // Top Plane
  102. { 0, 1, 0, 1 }, // Bottom plane
  103. { 0, 0, 1, 1 }, // Near Plane
  104. { 0, 0, -1, 1 } // Far Plane
  105. };
  106. template<Clipper::ClipPlane plane>
  107. static constexpr bool constrain_line_within_plane(GPU::Vertex& from, GPU::Vertex& to)
  108. {
  109. constexpr auto clip_plane_eqn = clip_plane_eqns[to_underlying(plane)];
  110. auto from_within_plane = point_within_clip_plane<plane>(from.clip_coordinates);
  111. auto to_within_plane = point_within_clip_plane<plane>(to.clip_coordinates);
  112. if (!from_within_plane && !to_within_plane)
  113. return false;
  114. if (!from_within_plane)
  115. from = clip_intersection_point<plane>(from, to, clip_plane_eqn);
  116. else if (!to_within_plane)
  117. to = clip_intersection_point<plane>(from, to, clip_plane_eqn);
  118. return true;
  119. }
  120. bool Clipper::clip_line_against_frustum(GPU::Vertex& from, GPU::Vertex& to)
  121. {
  122. return constrain_line_within_plane<ClipPlane::Left>(from, to)
  123. && constrain_line_within_plane<ClipPlane::Right>(from, to)
  124. && constrain_line_within_plane<ClipPlane::Top>(from, to)
  125. && constrain_line_within_plane<ClipPlane::Bottom>(from, to)
  126. && constrain_line_within_plane<ClipPlane::Near>(from, to)
  127. && constrain_line_within_plane<ClipPlane::Far>(from, to);
  128. }
  129. void Clipper::clip_triangle_against_frustum(Vector<GPU::Vertex>& input_verts)
  130. {
  131. // FIXME C++23. Static reflection will provide looping over all enum values.
  132. clip_plane<ClipPlane::Left>(input_verts, m_vertex_buffer, clip_plane_eqns[0]);
  133. clip_plane<ClipPlane::Right>(m_vertex_buffer, input_verts, clip_plane_eqns[1]);
  134. clip_plane<ClipPlane::Top>(input_verts, m_vertex_buffer, clip_plane_eqns[2]);
  135. clip_plane<ClipPlane::Bottom>(m_vertex_buffer, input_verts, clip_plane_eqns[3]);
  136. clip_plane<ClipPlane::Near>(input_verts, m_vertex_buffer, clip_plane_eqns[4]);
  137. clip_plane<ClipPlane::Far>(m_vertex_buffer, input_verts, clip_plane_eqns[5]);
  138. }
  139. void Clipper::clip_triangle_against_user_defined(Vector<GPU::Vertex>& input_verts, Vector<FloatVector4>& user_planes)
  140. {
  141. // FIXME: Also implement user plane support for points and lines
  142. auto& in = input_verts;
  143. auto& out = m_vertex_buffer;
  144. for (auto const& plane : user_planes) {
  145. clip_plane<ClipPlane::User>(in, out, plane);
  146. swap(in, out);
  147. }
  148. }
  149. }