Clipper.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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_triangle_against_frustum(Vector<GPU::Vertex>& input_verts)
  80. {
  81. // FIXME C++23. Static reflection will provide looping over all enum values.
  82. clip_plane<ClipPlane::LEFT>(input_verts, m_vertex_buffer);
  83. clip_plane<ClipPlane::RIGHT>(m_vertex_buffer, input_verts);
  84. clip_plane<ClipPlane::TOP>(input_verts, m_vertex_buffer);
  85. clip_plane<ClipPlane::BOTTOM>(m_vertex_buffer, input_verts);
  86. clip_plane<ClipPlane::NEAR>(input_verts, m_vertex_buffer);
  87. clip_plane<ClipPlane::FAR>(m_vertex_buffer, input_verts);
  88. }
  89. }