Clipper.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/Vector.h>
  10. #include <LibGPU/Vertex.h>
  11. #include <LibGfx/Vector4.h>
  12. #include <LibSoftGPU/Clipper.h>
  13. namespace SoftGPU {
  14. template<Clipper::ClipPlane plane>
  15. static constexpr bool point_within_clip_plane(FloatVector4 const& vertex)
  16. {
  17. if constexpr (plane == Clipper::ClipPlane::LEFT)
  18. return vertex.x() >= -vertex.w();
  19. else if constexpr (plane == Clipper::ClipPlane::RIGHT)
  20. return vertex.x() <= vertex.w();
  21. else if constexpr (plane == Clipper::ClipPlane::TOP)
  22. return vertex.y() <= vertex.w();
  23. else if constexpr (plane == Clipper::ClipPlane::BOTTOM)
  24. return vertex.y() >= -vertex.w();
  25. else if constexpr (plane == Clipper::ClipPlane::NEAR)
  26. return vertex.z() >= -vertex.w();
  27. else if constexpr (plane == Clipper::ClipPlane::FAR)
  28. return vertex.z() <= vertex.w();
  29. return false;
  30. }
  31. template<Clipper::ClipPlane plane>
  32. static constexpr GPU::Vertex clip_intersection_point(GPU::Vertex const& p1, GPU::Vertex const& p2)
  33. {
  34. constexpr FloatVector4 clip_plane_normals[] = {
  35. { 1, 0, 0, 1 }, // Left Plane
  36. { -1, 0, 0, 1 }, // Right Plane
  37. { 0, -1, 0, 1 }, // Top Plane
  38. { 0, 1, 0, 1 }, // Bottom plane
  39. { 0, 0, 1, 1 }, // Near Plane
  40. { 0, 0, -1, 1 } // Far Plane
  41. };
  42. constexpr auto clip_plane_normal = clip_plane_normals[to_underlying(plane)];
  43. // See https://www.microsoft.com/en-us/research/wp-content/uploads/1978/01/p245-blinn.pdf
  44. // "Clipping Using Homogeneous Coordinates" Blinn/Newell, 1978
  45. // Clip plane normals have W=1 so the vertices' W coordinates are included in x1 and x2.
  46. auto const x1 = clip_plane_normal.dot(p1.clip_coordinates);
  47. auto const x2 = clip_plane_normal.dot(p2.clip_coordinates);
  48. auto const a = x1 / (x1 - 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>& input_list, Vector<GPU::Vertex>& output_list)
  61. {
  62. output_list.clear_with_capacity();
  63. auto input_list_size = input_list.size();
  64. if (input_list_size == 0)
  65. return;
  66. auto const* prev_vec = &input_list.data()[0];
  67. auto is_prev_point_within_clip_plane = point_within_clip_plane<plane>(prev_vec->clip_coordinates);
  68. for (size_t i = 1; i <= input_list_size; i++) {
  69. auto const& curr_vec = input_list[i % input_list_size];
  70. auto const is_curr_point_within_clip_plane = point_within_clip_plane<plane>(curr_vec.clip_coordinates);
  71. if (is_curr_point_within_clip_plane != is_prev_point_within_clip_plane)
  72. output_list.append(clip_intersection_point<plane>(*prev_vec, curr_vec));
  73. if (is_curr_point_within_clip_plane)
  74. output_list.append(curr_vec);
  75. prev_vec = &curr_vec;
  76. is_prev_point_within_clip_plane = is_curr_point_within_clip_plane;
  77. }
  78. }
  79. void Clipper::clip_points_against_frustum(Vector<GPU::Vertex>& vertices)
  80. {
  81. m_vertex_buffer.clear_with_capacity();
  82. for (auto& vertex : vertices) {
  83. auto const coords = vertex.clip_coordinates;
  84. if (point_within_clip_plane<ClipPlane::LEFT>(coords) && point_within_clip_plane<ClipPlane::RIGHT>(coords)
  85. && point_within_clip_plane<ClipPlane::TOP>(coords) && point_within_clip_plane<ClipPlane::BOTTOM>(coords)
  86. && point_within_clip_plane<ClipPlane::NEAR>(coords) && point_within_clip_plane<ClipPlane::FAR>(coords))
  87. m_vertex_buffer.append(vertex);
  88. }
  89. vertices.clear_with_capacity();
  90. vertices.extend(m_vertex_buffer);
  91. }
  92. template<Clipper::ClipPlane plane>
  93. static constexpr bool constrain_line_within_plane(GPU::Vertex& from, GPU::Vertex& to)
  94. {
  95. auto from_within_plane = point_within_clip_plane<plane>(from.clip_coordinates);
  96. auto to_within_plane = point_within_clip_plane<plane>(to.clip_coordinates);
  97. if (!from_within_plane && !to_within_plane)
  98. return false;
  99. if (!from_within_plane)
  100. from = clip_intersection_point<plane>(from, to);
  101. else if (!to_within_plane)
  102. to = clip_intersection_point<plane>(from, to);
  103. return true;
  104. }
  105. bool Clipper::clip_line_against_frustum(GPU::Vertex& from, GPU::Vertex& to)
  106. {
  107. return constrain_line_within_plane<ClipPlane::LEFT>(from, to)
  108. && constrain_line_within_plane<ClipPlane::RIGHT>(from, to)
  109. && constrain_line_within_plane<ClipPlane::TOP>(from, to)
  110. && constrain_line_within_plane<ClipPlane::BOTTOM>(from, to)
  111. && constrain_line_within_plane<ClipPlane::NEAR>(from, to)
  112. && constrain_line_within_plane<ClipPlane::FAR>(from, to);
  113. }
  114. void Clipper::clip_triangle_against_frustum(Vector<GPU::Vertex>& input_verts)
  115. {
  116. // FIXME C++23. Static reflection will provide looping over all enum values.
  117. clip_plane<ClipPlane::LEFT>(input_verts, m_vertex_buffer);
  118. clip_plane<ClipPlane::RIGHT>(m_vertex_buffer, input_verts);
  119. clip_plane<ClipPlane::TOP>(input_verts, m_vertex_buffer);
  120. clip_plane<ClipPlane::BOTTOM>(m_vertex_buffer, input_verts);
  121. clip_plane<ClipPlane::NEAR>(input_verts, m_vertex_buffer);
  122. clip_plane<ClipPlane::FAR>(m_vertex_buffer, input_verts);
  123. }
  124. }