From e82ce8032c71ca8ccce1f7efdd67b2f28a34242b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Molinari?= Date: Mon, 4 Sep 2023 22:47:31 +0200 Subject: [PATCH] gl-renderer: Fix quad clipper non-zero area check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The non-zero area check of clipper_quad_clip() is incorrect for quads initialized with a polygon starting with a vertical edge. In order to handle polygons starting with an horizontal edge and polygons starting with a vertical one, it must check opposite vertices for equality. The test previously described as "Box intersects entire smaller aligned quad" is now described as "Clockwise winding and top/left initial vertex". This test keeps the same values as before but all combinations of winding order and first edge orientations are also tested. The QUAD() macro isn't used anymore to do so. Signed-off-by: Loïc Molinari --- libweston/vertex-clipping.c | 2 +- tests/vertex-clip-test.c | 92 +++++++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/libweston/vertex-clipping.c b/libweston/vertex-clipping.c index 7f659e189..4de172e4e 100644 --- a/libweston/vertex-clipping.c +++ b/libweston/vertex-clipping.c @@ -374,7 +374,7 @@ clipper_quad_clip(struct clipper_quad *quad, vertices[i].y = CLIP(quad->polygon[i].y, box[0].y, box[1].y); } - if ((vertices[0].x != vertices[1].x) && + if ((vertices[0].x != vertices[2].x) && (vertices[0].y != vertices[2].y)) return 4; else diff --git a/tests/vertex-clip-test.c b/tests/vertex-clip-test.c index 543143eb7..0c0be6769 100644 --- a/tests/vertex-clip-test.c +++ b/tests/vertex-clip-test.c @@ -583,17 +583,99 @@ static const struct vertex_clip_test_data quad_clip_expected_data[] = { .clipped_n = 5, }, - /* Miscellaneous cases: */ + /* Box intersects entire smaller aligned quad of different winding + * orders and first edge orientations: */ - /* Box intersects entire smaller aligned quad. */ + /* Clockwise winding and top/left initial vertex. */ { .aligned = true, - .box = BOX (-0.50f, -0.50f, 0.50f, 0.50f), - .polygon = QUAD(-0.25f, -0.25f, 0.25f, 0.25f), - .clipped = QUAD(-0.25f, -0.25f, 0.25f, 0.25f), + .box = BOX(-0.50f, -0.50f, 0.50f, 0.50f), + .polygon = {{-0.25f, -0.25f}, { 0.25f, -0.25f}, + { 0.25f, 0.25f}, {-0.25f, 0.25f}}, + .clipped = {{-0.25f, -0.25f}, { 0.25f, -0.25f}, + { 0.25f, 0.25f}, {-0.25f, 0.25f}}, .clipped_n = 4, }, + /* Clockwise winding and top/right initial vertex. */ + { + .aligned = true, + .box = BOX(-0.50f, -0.50f, 0.50f, 0.50f), + .polygon = {{ 0.25f, -0.25f}, { 0.25f, 0.25f}, + {-0.25f, 0.25f}, {-0.25f, -0.25f}}, + .clipped = {{ 0.25f, -0.25f}, { 0.25f, 0.25f}, + {-0.25f, 0.25f}, {-0.25f, -0.25f}}, + .clipped_n = 4, + }, + + /* Clockwise winding and bottom/right initial vertex. */ + { + .aligned = true, + .box = BOX(-0.50f, -0.50f, 0.50f, 0.50f), + .polygon = {{ 0.25f, 0.25f}, {-0.25f, 0.25f}, + {-0.25f, -0.25f}, { 0.25f, -0.25f}}, + .clipped = {{ 0.25f, 0.25f}, {-0.25f, 0.25f}, + {-0.25f, -0.25f}, { 0.25f, -0.25f}}, + .clipped_n = 4, + }, + + /* Clockwise winding and bottom/left initial vertex. */ + { + .aligned = true, + .box = BOX(-0.50f, -0.50f, 0.50f, 0.50f), + .polygon = {{-0.25f, 0.25f}, {-0.25f, -0.25f}, + { 0.25f, -0.25f}, { 0.25f, 0.25f}}, + .clipped = {{-0.25f, 0.25f}, {-0.25f, -0.25f}, + { 0.25f, -0.25f}, { 0.25f, 0.25f}}, + .clipped_n = 4, + }, + + /* Counter-clockwise winding and top/left initial vertex. */ + { + .aligned = true, + .box = BOX(-0.50f, -0.50f, 0.50f, 0.50f), + .polygon = {{-0.25f, -0.25f}, {-0.25f, 0.25f}, + { 0.25f, 0.25f}, { 0.25f, -0.25f}}, + .clipped = {{-0.25f, -0.25f}, {-0.25f, 0.25f}, + { 0.25f, 0.25f}, { 0.25f, -0.25f}}, + .clipped_n = 4, + }, + + /* Counter-clockwise winding and top/right initial vertex. */ + { + .aligned = true, + .box = BOX(-0.50f, -0.50f, 0.50f, 0.50f), + .polygon = {{ 0.25f, -0.25f}, {-0.25f, -0.25f}, + {-0.25f, 0.25f}, { 0.25f, 0.25f}}, + .clipped = {{ 0.25f, -0.25f}, {-0.25f, -0.25f}, + {-0.25f, 0.25f}, { 0.25f, 0.25f}}, + .clipped_n = 4, + }, + + /* Counter-clockwise winding and bottom/right initial vertex. */ + { + .aligned = true, + .box = BOX(-0.50f, -0.50f, 0.50f, 0.50f), + .polygon = {{ 0.25f, 0.25f}, { 0.25f, -0.25f}, + {-0.25f, -0.25f}, {-0.25f, 0.25f}}, + .clipped = {{ 0.25f, 0.25f}, { 0.25f, -0.25f}, + {-0.25f, -0.25f}, {-0.25f, 0.25f}}, + .clipped_n = 4, + }, + + /* Counter-clockwise winding and bottom/left initial vertex. */ + { + .aligned = true, + .box = BOX(-0.50f, -0.50f, 0.50f, 0.50f), + .polygon = {{-0.25f, 0.25f}, { 0.25f, 0.25f}, + { 0.25f, -0.25f}, {-0.25f, -0.25f}}, + .clipped = {{-0.25f, 0.25f}, { 0.25f, 0.25f}, + { 0.25f, -0.25f}, {-0.25f, -0.25f}}, + .clipped_n = 4, + }, + + /* Miscellaneous cases: */ + /* Box intersects entire same size aligned quad. */ { .aligned = true,