Clipper.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Format.h>
  8. #include <AK/ScopeGuard.h>
  9. #include <LibSoftGPU/Clipper.h>
  10. namespace SoftGPU {
  11. bool Clipper::point_within_clip_plane(const FloatVector4& vertex, ClipPlane plane)
  12. {
  13. switch (plane) {
  14. case ClipPlane::LEFT:
  15. return vertex.x() >= -vertex.w();
  16. case ClipPlane::RIGHT:
  17. return vertex.x() <= vertex.w();
  18. case ClipPlane::TOP:
  19. return vertex.y() <= vertex.w();
  20. case ClipPlane::BOTTOM:
  21. return vertex.y() >= -vertex.w();
  22. case ClipPlane::NEAR:
  23. return vertex.z() >= -vertex.w();
  24. case ClipPlane::FAR:
  25. return vertex.z() <= vertex.w();
  26. }
  27. return false;
  28. }
  29. Vertex Clipper::clip_intersection_point(const Vertex& p1, const Vertex& p2, ClipPlane plane_index)
  30. {
  31. // See https://www.microsoft.com/en-us/research/wp-content/uploads/1978/01/p245-blinn.pdf
  32. // "Clipping Using Homogeneous Coordinates" Blinn/Newell, 1978
  33. float w1 = p1.clip_coordinates.w();
  34. float w2 = p2.clip_coordinates.w();
  35. float x1 = clip_plane_normals[plane_index].dot(p1.clip_coordinates);
  36. float x2 = clip_plane_normals[plane_index].dot(p2.clip_coordinates);
  37. float a = (w1 + x1) / ((w1 + x1) - (w2 + x2));
  38. Vertex out;
  39. out.position = mix(p1.position, p2.position, a);
  40. out.eye_coordinates = mix(p1.eye_coordinates, p2.eye_coordinates, a);
  41. out.clip_coordinates = mix(p1.clip_coordinates, p2.clip_coordinates, a);
  42. out.color = mix(p1.color, p2.color, a);
  43. out.tex_coord = mix(p1.tex_coord, p2.tex_coord, a);
  44. out.normal = mix(p1.normal, p2.normal, a);
  45. return out;
  46. }
  47. void Clipper::clip_triangle_against_frustum(Vector<Vertex>& input_verts)
  48. {
  49. list_a = input_verts;
  50. list_b.clear_with_capacity();
  51. auto read_from = &list_a;
  52. auto write_to = &list_b;
  53. for (size_t plane = 0; plane < NUMBER_OF_CLIPPING_PLANES; plane++) {
  54. write_to->clear_with_capacity();
  55. // Save me, C++23
  56. for (size_t i = 0; i < read_from->size(); i++) {
  57. const auto& curr_vec = read_from->at((i + 1) % read_from->size());
  58. const auto& prev_vec = read_from->at(i);
  59. if (point_within_clip_plane(curr_vec.clip_coordinates, static_cast<ClipPlane>(plane))) {
  60. if (!point_within_clip_plane(prev_vec.clip_coordinates, static_cast<ClipPlane>(plane))) {
  61. auto intersect = clip_intersection_point(prev_vec, curr_vec, static_cast<ClipPlane>(plane));
  62. write_to->append(intersect);
  63. }
  64. write_to->append(curr_vec);
  65. } else if (point_within_clip_plane(prev_vec.clip_coordinates, static_cast<ClipPlane>(plane))) {
  66. auto intersect = clip_intersection_point(prev_vec, curr_vec, static_cast<ClipPlane>(plane));
  67. write_to->append(intersect);
  68. }
  69. }
  70. swap(write_to, read_from);
  71. }
  72. input_verts = *read_from;
  73. }
  74. }