2021-05-04 23:00:45 +10:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
2021-08-16 19:22:08 +02:00
|
|
|
* Copyright (c) 2021, Stephan Unverwerth <s.unverwerth@serenityos.org>
|
2022-01-27 17:18:07 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2021-05-04 23:00:45 +10:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-27 17:18:07 -07:00
|
|
|
#include <AK/Vector.h>
|
2022-03-27 15:43:51 +02:00
|
|
|
#include <LibGPU/Vertex.h>
|
2022-01-27 17:18:07 -07:00
|
|
|
#include <LibGfx/Vector4.h>
|
2021-12-16 20:32:38 +01:00
|
|
|
#include <LibSoftGPU/Clipper.h>
|
2021-05-04 23:00:45 +10:00
|
|
|
|
2021-12-16 20:32:38 +01:00
|
|
|
namespace SoftGPU {
|
2021-08-16 19:22:08 +02:00
|
|
|
|
2022-01-27 17:18:07 -07:00
|
|
|
template<Clipper::ClipPlane plane>
|
|
|
|
|
static constexpr bool point_within_clip_plane(FloatVector4 const& vertex)
|
2021-05-04 23:00:45 +10:00
|
|
|
{
|
2022-04-10 02:45:41 +02:00
|
|
|
if constexpr (plane == Clipper::ClipPlane::LEFT)
|
2021-12-02 00:46:32 +01:00
|
|
|
return vertex.x() >= -vertex.w();
|
2022-04-10 02:45:41 +02:00
|
|
|
else if constexpr (plane == Clipper::ClipPlane::RIGHT)
|
2021-12-02 00:46:32 +01:00
|
|
|
return vertex.x() <= vertex.w();
|
2022-04-10 02:45:41 +02:00
|
|
|
else if constexpr (plane == Clipper::ClipPlane::TOP)
|
2021-12-02 00:46:32 +01:00
|
|
|
return vertex.y() <= vertex.w();
|
2022-04-10 02:45:41 +02:00
|
|
|
else if constexpr (plane == Clipper::ClipPlane::BOTTOM)
|
2021-12-02 00:46:32 +01:00
|
|
|
return vertex.y() >= -vertex.w();
|
2022-04-10 02:45:41 +02:00
|
|
|
else if constexpr (plane == Clipper::ClipPlane::NEAR)
|
2021-12-02 00:46:32 +01:00
|
|
|
return vertex.z() >= -vertex.w();
|
2022-04-10 02:45:41 +02:00
|
|
|
else if constexpr (plane == Clipper::ClipPlane::FAR)
|
2021-12-02 00:46:32 +01:00
|
|
|
return vertex.z() <= vertex.w();
|
2021-05-04 23:00:45 +10:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-27 17:18:07 -07:00
|
|
|
template<Clipper::ClipPlane plane>
|
2022-03-27 15:43:51 +02:00
|
|
|
static constexpr GPU::Vertex clip_intersection_point(GPU::Vertex const& p1, GPU::Vertex const& p2)
|
2021-05-04 23:00:45 +10:00
|
|
|
{
|
2022-01-27 17:18:07 -07:00
|
|
|
constexpr FloatVector4 clip_plane_normals[] = {
|
|
|
|
|
{ 1, 0, 0, 0 }, // Left Plane
|
|
|
|
|
{ -1, 0, 0, 0 }, // Right Plane
|
|
|
|
|
{ 0, -1, 0, 0 }, // Top Plane
|
|
|
|
|
{ 0, 1, 0, 0 }, // Bottom plane
|
|
|
|
|
{ 0, 0, 1, 0 }, // Near Plane
|
|
|
|
|
{ 0, 0, -1, 0 } // Far Plane
|
|
|
|
|
};
|
|
|
|
|
constexpr auto clip_plane_normal = clip_plane_normals[to_underlying(plane)];
|
|
|
|
|
|
2021-08-16 19:22:08 +02:00
|
|
|
// See https://www.microsoft.com/en-us/research/wp-content/uploads/1978/01/p245-blinn.pdf
|
2021-09-30 20:03:41 -04:00
|
|
|
// "Clipping Using Homogeneous Coordinates" Blinn/Newell, 1978
|
2021-05-04 23:00:45 +10:00
|
|
|
|
2022-01-23 12:39:32 -07:00
|
|
|
float const w1 = p1.clip_coordinates.w();
|
|
|
|
|
float const w2 = p2.clip_coordinates.w();
|
2022-01-27 17:18:07 -07:00
|
|
|
float const x1 = clip_plane_normal.dot(p1.clip_coordinates);
|
|
|
|
|
float const x2 = clip_plane_normal.dot(p2.clip_coordinates);
|
2022-01-23 12:39:32 -07:00
|
|
|
float const a = (w1 + x1) / ((w1 + x1) - (w2 + x2));
|
2021-05-04 23:00:45 +10:00
|
|
|
|
2022-03-27 15:43:51 +02:00
|
|
|
GPU::Vertex out;
|
2022-01-04 15:02:30 +01:00
|
|
|
out.position = mix(p1.position, p2.position, a);
|
|
|
|
|
out.eye_coordinates = mix(p1.eye_coordinates, p2.eye_coordinates, a);
|
|
|
|
|
out.clip_coordinates = mix(p1.clip_coordinates, p2.clip_coordinates, a);
|
|
|
|
|
out.color = mix(p1.color, p2.color, a);
|
2022-03-27 15:43:51 +02:00
|
|
|
for (size_t i = 0; i < GPU::NUM_SAMPLERS; ++i)
|
2022-01-15 18:33:55 +01:00
|
|
|
out.tex_coords[i] = mix(p1.tex_coords[i], p2.tex_coords[i], a);
|
2022-01-06 21:45:02 +01:00
|
|
|
out.normal = mix(p1.normal, p2.normal, a);
|
2021-08-16 19:22:08 +02:00
|
|
|
return out;
|
2021-05-04 23:00:45 +10:00
|
|
|
}
|
|
|
|
|
|
2022-01-27 17:18:07 -07:00
|
|
|
template<Clipper::ClipPlane plane>
|
2022-04-10 02:50:26 +02:00
|
|
|
FLATTEN static constexpr void clip_plane(Vector<GPU::Vertex>& input_list, Vector<GPU::Vertex>& output_list)
|
2021-05-04 23:00:45 +10:00
|
|
|
{
|
2022-04-10 02:50:26 +02:00
|
|
|
output_list.clear_with_capacity();
|
2021-05-04 23:00:45 +10:00
|
|
|
|
2022-04-10 02:50:26 +02:00
|
|
|
auto input_list_size = input_list.size();
|
|
|
|
|
if (input_list_size == 0)
|
|
|
|
|
return;
|
2021-05-04 23:00:45 +10:00
|
|
|
|
2022-04-10 02:50:26 +02:00
|
|
|
auto const* prev_vec = &input_list.data()[0];
|
|
|
|
|
auto is_prev_point_within_clip_plane = point_within_clip_plane<plane>(prev_vec->clip_coordinates);
|
|
|
|
|
|
|
|
|
|
for (size_t i = 1; i <= input_list_size; i++) {
|
|
|
|
|
auto const& curr_vec = input_list[i % input_list_size];
|
|
|
|
|
auto const is_curr_point_within_clip_plane = point_within_clip_plane<plane>(curr_vec.clip_coordinates);
|
|
|
|
|
|
|
|
|
|
if (is_curr_point_within_clip_plane != is_prev_point_within_clip_plane)
|
|
|
|
|
output_list.append(clip_intersection_point<plane>(*prev_vec, curr_vec));
|
2022-01-27 17:18:49 -07:00
|
|
|
|
|
|
|
|
if (is_curr_point_within_clip_plane)
|
2022-04-10 02:50:26 +02:00
|
|
|
output_list.append(curr_vec);
|
|
|
|
|
|
|
|
|
|
prev_vec = &curr_vec;
|
|
|
|
|
is_prev_point_within_clip_plane = is_curr_point_within_clip_plane;
|
2022-01-08 13:55:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-27 15:43:51 +02:00
|
|
|
void Clipper::clip_triangle_against_frustum(Vector<GPU::Vertex>& input_verts)
|
2022-01-08 13:55:23 -07:00
|
|
|
{
|
2022-01-27 17:18:07 -07:00
|
|
|
// FIXME C++23. Static reflection will provide looping over all enum values.
|
2022-04-10 02:50:26 +02:00
|
|
|
clip_plane<ClipPlane::LEFT>(input_verts, m_vertex_buffer);
|
|
|
|
|
clip_plane<ClipPlane::RIGHT>(m_vertex_buffer, input_verts);
|
|
|
|
|
clip_plane<ClipPlane::TOP>(input_verts, m_vertex_buffer);
|
|
|
|
|
clip_plane<ClipPlane::BOTTOM>(m_vertex_buffer, input_verts);
|
|
|
|
|
clip_plane<ClipPlane::NEAR>(input_verts, m_vertex_buffer);
|
|
|
|
|
clip_plane<ClipPlane::FAR>(m_vertex_buffer, input_verts);
|
2021-08-16 19:22:08 +02:00
|
|
|
}
|
2022-01-27 17:18:07 -07:00
|
|
|
|
2021-05-04 23:00:45 +10:00
|
|
|
}
|