feat: port polygon fill with Sutherland-Hodgman clipping, 26 snapshot tests
Browse filesPure Rust polygon fill pipeline: Sutherland-Hodgman 4-edge clipper +
scanline rasterizer using DDA edge stepping. Add fill_hline to
PixelWriter trait. All 26 snapshot tests pass (11 lines, 8 thick
lines, 5 polygons, 2 pixel grid basics).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- crates/openwa-core/src/display/dd_display.rs +4 -0
- crates/openwa-core/src/display/line_draw.rs +351 -1
- crates/openwa-dll/src/replacements/display.rs +91 -1
- testdata/snapshots/poly_diamond.bin +3 -0
- testdata/snapshots/poly_partially_outside.bin +3 -0
- testdata/snapshots/poly_restricted_clip.bin +3 -0
- testdata/snapshots/poly_square.bin +3 -0
- testdata/snapshots/poly_triangle.bin +3 -0
crates/openwa-core/src/display/dd_display.rs
CHANGED
|
@@ -654,6 +654,10 @@ impl line_draw::PixelWriter for BitGridWriter {
|
|
| 654 |
unsafe { DisplayBitGrid::put_pixel_clipped_raw(self.0, x, y, color) }
|
| 655 |
}
|
| 656 |
#[inline]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 657 |
fn clip_left(&self) -> i32 {
|
| 658 |
unsafe { (*self.0).clip_left as i32 }
|
| 659 |
}
|
|
|
|
| 654 |
unsafe { DisplayBitGrid::put_pixel_clipped_raw(self.0, x, y, color) }
|
| 655 |
}
|
| 656 |
#[inline]
|
| 657 |
+
fn fill_hline(&mut self, x1: i32, x2: i32, y: i32, color: u8) {
|
| 658 |
+
unsafe { DisplayBitGrid::fill_hline_raw(self.0, x1, x2, y, color) }
|
| 659 |
+
}
|
| 660 |
+
#[inline]
|
| 661 |
fn clip_left(&self) -> i32 {
|
| 662 |
unsafe { (*self.0).clip_left as i32 }
|
| 663 |
}
|
crates/openwa-core/src/display/line_draw.rs
CHANGED
|
@@ -17,12 +17,14 @@
|
|
| 17 |
|
| 18 |
use crate::fixed::Fixed;
|
| 19 |
|
| 20 |
-
/// Trait for pixel-addressable surfaces used by
|
| 21 |
///
|
| 22 |
/// Implemented for `PixelGrid` (test-only, pure Rust) and can be implemented
|
| 23 |
/// for `DisplayBitGrid` (runtime, via vtable dispatch).
|
| 24 |
pub trait PixelWriter {
|
| 25 |
fn put_pixel_clipped(&mut self, x: i32, y: i32, color: u8);
|
|
|
|
|
|
|
| 26 |
fn clip_left(&self) -> i32;
|
| 27 |
fn clip_top(&self) -> i32;
|
| 28 |
fn clip_right(&self) -> i32;
|
|
@@ -98,6 +100,13 @@ impl PixelWriter for PixelGrid {
|
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
#[inline]
|
| 102 |
fn clip_left(&self) -> i32 {
|
| 103 |
self.clip_left as i32
|
|
@@ -530,6 +539,285 @@ pub fn draw_line_two_color(
|
|
| 530 |
}
|
| 531 |
}
|
| 532 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
// =========================================================================
|
| 534 |
// Tests
|
| 535 |
// =========================================================================
|
|
@@ -819,4 +1107,66 @@ mod tests {
|
|
| 819 |
draw_line_two_color(&mut grid, f(10), f(10), f(118), f(118), 9, 10);
|
| 820 |
assert_matches_snapshot(&grid, "twocol_restricted_clip");
|
| 821 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 822 |
}
|
|
|
|
| 17 |
|
| 18 |
use crate::fixed::Fixed;
|
| 19 |
|
| 20 |
+
/// Trait for pixel-addressable surfaces used by drawing algorithms.
|
| 21 |
///
|
| 22 |
/// Implemented for `PixelGrid` (test-only, pure Rust) and can be implemented
|
| 23 |
/// for `DisplayBitGrid` (runtime, via vtable dispatch).
|
| 24 |
pub trait PixelWriter {
|
| 25 |
fn put_pixel_clipped(&mut self, x: i32, y: i32, color: u8);
|
| 26 |
+
/// Fill a horizontal span from x1 (inclusive) to x2 (exclusive) at row y.
|
| 27 |
+
fn fill_hline(&mut self, x1: i32, x2: i32, y: i32, color: u8);
|
| 28 |
fn clip_left(&self) -> i32;
|
| 29 |
fn clip_top(&self) -> i32;
|
| 30 |
fn clip_right(&self) -> i32;
|
|
|
|
| 100 |
}
|
| 101 |
}
|
| 102 |
|
| 103 |
+
#[inline]
|
| 104 |
+
fn fill_hline(&mut self, x1: i32, x2: i32, y: i32, color: u8) {
|
| 105 |
+
let offset = (y as u32 * self.row_stride + x1 as u32) as usize;
|
| 106 |
+
let len = (x2 - x1) as usize;
|
| 107 |
+
self.data[offset..offset + len].fill(color);
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
#[inline]
|
| 111 |
fn clip_left(&self) -> i32 {
|
| 112 |
self.clip_left as i32
|
|
|
|
| 539 |
}
|
| 540 |
}
|
| 541 |
|
| 542 |
+
// =========================================================================
|
| 543 |
+
// Polygon fill (port of 0x4F7BA0 + 0x4F7D00 + 0x4F7E90)
|
| 544 |
+
// =========================================================================
|
| 545 |
+
|
| 546 |
+
/// Maximum vertices after clipping. Sutherland-Hodgman can at most double
|
| 547 |
+
/// the vertex count per clip edge, but in practice WA's global buffers are
|
| 548 |
+
/// ~256 entries. We use a generous limit.
|
| 549 |
+
const MAX_CLIP_VERTS: usize = 512;
|
| 550 |
+
|
| 551 |
+
/// A Fixed-point vertex (x, y).
|
| 552 |
+
#[derive(Clone, Copy)]
|
| 553 |
+
struct Vertex {
|
| 554 |
+
x: Fixed,
|
| 555 |
+
y: Fixed,
|
| 556 |
+
}
|
| 557 |
+
|
| 558 |
+
/// Clip polygon edges against a single axis boundary.
|
| 559 |
+
///
|
| 560 |
+
/// Sutherland-Hodgman one-edge clip. For each consecutive edge (prev → curr),
|
| 561 |
+
/// outputs 0, 1, or 2 vertices depending on inside/outside transitions.
|
| 562 |
+
///
|
| 563 |
+
/// `inside` returns true if the coordinate is inside the boundary.
|
| 564 |
+
/// `intersect` computes the intersection point on the boundary.
|
| 565 |
+
fn clip_polygon_edge(
|
| 566 |
+
input: &[Vertex],
|
| 567 |
+
output: &mut [Vertex; MAX_CLIP_VERTS],
|
| 568 |
+
inside: impl Fn(Fixed) -> bool,
|
| 569 |
+
coord: impl Fn(&Vertex) -> Fixed,
|
| 570 |
+
other: impl Fn(&Vertex) -> Fixed,
|
| 571 |
+
make_vertex: impl Fn(Fixed, Fixed) -> Vertex,
|
| 572 |
+
boundary: Fixed,
|
| 573 |
+
) -> usize {
|
| 574 |
+
if input.len() < 2 {
|
| 575 |
+
return 0;
|
| 576 |
+
}
|
| 577 |
+
let mut out_count = 0;
|
| 578 |
+
|
| 579 |
+
let mut prev = input[input.len() - 1];
|
| 580 |
+
for &curr in input {
|
| 581 |
+
let prev_in = inside(coord(&prev));
|
| 582 |
+
let curr_in = inside(coord(&curr));
|
| 583 |
+
|
| 584 |
+
if prev_in && curr_in {
|
| 585 |
+
// Both inside → emit current
|
| 586 |
+
output[out_count] = curr;
|
| 587 |
+
out_count += 1;
|
| 588 |
+
} else if prev_in && !curr_in {
|
| 589 |
+
// Leaving → emit intersection
|
| 590 |
+
let ratio = (boundary - coord(&prev)).div_raw(coord(&curr) - coord(&prev));
|
| 591 |
+
let clipped_other = other(&prev) + (other(&curr) - other(&prev)).mul_raw(ratio);
|
| 592 |
+
output[out_count] = make_vertex(boundary, clipped_other);
|
| 593 |
+
out_count += 1;
|
| 594 |
+
} else if !prev_in && curr_in {
|
| 595 |
+
// Entering → emit intersection then current
|
| 596 |
+
let ratio = (boundary - coord(&prev)).div_raw(coord(&curr) - coord(&prev));
|
| 597 |
+
let clipped_other = other(&prev) + (other(&curr) - other(&prev)).mul_raw(ratio);
|
| 598 |
+
output[out_count] = make_vertex(boundary, clipped_other);
|
| 599 |
+
out_count += 1;
|
| 600 |
+
output[out_count] = curr;
|
| 601 |
+
out_count += 1;
|
| 602 |
+
}
|
| 603 |
+
// else both outside → emit nothing
|
| 604 |
+
|
| 605 |
+
if out_count >= MAX_CLIP_VERTS {
|
| 606 |
+
break;
|
| 607 |
+
}
|
| 608 |
+
prev = curr;
|
| 609 |
+
}
|
| 610 |
+
out_count
|
| 611 |
+
}
|
| 612 |
+
|
| 613 |
+
/// Clip polygon against the writer's clip rectangle.
|
| 614 |
+
///
|
| 615 |
+
/// Port of 0x4F7BA0 (X clip) + 0x4F7D00 (Y clip).
|
| 616 |
+
/// Returns the clipped vertex count. Vertices are stored in `out`.
|
| 617 |
+
fn clip_polygon(
|
| 618 |
+
verts: &[Vertex],
|
| 619 |
+
out: &mut [Vertex; MAX_CLIP_VERTS],
|
| 620 |
+
writer: &impl PixelWriter,
|
| 621 |
+
) -> usize {
|
| 622 |
+
let cl = Fixed::from_int(writer.clip_left());
|
| 623 |
+
let cr = Fixed::from_int(writer.clip_right());
|
| 624 |
+
let ct = Fixed::from_int(writer.clip_top());
|
| 625 |
+
let cb = Fixed::from_int(writer.clip_bottom());
|
| 626 |
+
|
| 627 |
+
// Clip against left X
|
| 628 |
+
let mut buf_a = [Vertex {
|
| 629 |
+
x: Fixed::ZERO,
|
| 630 |
+
y: Fixed::ZERO,
|
| 631 |
+
}; MAX_CLIP_VERTS];
|
| 632 |
+
let count = clip_polygon_edge(
|
| 633 |
+
verts,
|
| 634 |
+
&mut buf_a,
|
| 635 |
+
|x| x >= cl,
|
| 636 |
+
|v| v.x,
|
| 637 |
+
|v| v.y,
|
| 638 |
+
|x, y| Vertex { x, y },
|
| 639 |
+
cl,
|
| 640 |
+
);
|
| 641 |
+
if count < 3 {
|
| 642 |
+
return 0;
|
| 643 |
+
}
|
| 644 |
+
|
| 645 |
+
// Clip against right X
|
| 646 |
+
let mut buf_b = [Vertex {
|
| 647 |
+
x: Fixed::ZERO,
|
| 648 |
+
y: Fixed::ZERO,
|
| 649 |
+
}; MAX_CLIP_VERTS];
|
| 650 |
+
let count = clip_polygon_edge(
|
| 651 |
+
&buf_a[..count],
|
| 652 |
+
&mut buf_b,
|
| 653 |
+
|x| x <= cr,
|
| 654 |
+
|v| v.x,
|
| 655 |
+
|v| v.y,
|
| 656 |
+
|x, y| Vertex { x, y },
|
| 657 |
+
cr,
|
| 658 |
+
);
|
| 659 |
+
if count < 3 {
|
| 660 |
+
return 0;
|
| 661 |
+
}
|
| 662 |
+
|
| 663 |
+
// Clip against top Y
|
| 664 |
+
let count = clip_polygon_edge(
|
| 665 |
+
&buf_b[..count],
|
| 666 |
+
&mut buf_a,
|
| 667 |
+
|y| y >= ct,
|
| 668 |
+
|v| v.y,
|
| 669 |
+
|v| v.x,
|
| 670 |
+
|y, x| Vertex { x, y },
|
| 671 |
+
ct,
|
| 672 |
+
);
|
| 673 |
+
if count < 3 {
|
| 674 |
+
return 0;
|
| 675 |
+
}
|
| 676 |
+
|
| 677 |
+
// Clip against bottom Y
|
| 678 |
+
let count = clip_polygon_edge(
|
| 679 |
+
&buf_a[..count],
|
| 680 |
+
out,
|
| 681 |
+
|y| y <= cb,
|
| 682 |
+
|v| v.y,
|
| 683 |
+
|v| v.x,
|
| 684 |
+
|y, x| Vertex { x, y },
|
| 685 |
+
cb,
|
| 686 |
+
);
|
| 687 |
+
|
| 688 |
+
count
|
| 689 |
+
}
|
| 690 |
+
|
| 691 |
+
/// Maximum scanline height for the span table.
|
| 692 |
+
const MAX_SCANLINES: usize = 1024;
|
| 693 |
+
|
| 694 |
+
/// Rasterize a clipped convex polygon using scanline fill.
|
| 695 |
+
///
|
| 696 |
+
/// Port of 0x4F7E90. For each edge, computes a span table (X per scanline),
|
| 697 |
+
/// then fills horizontal lines between left and right span tables.
|
| 698 |
+
pub fn fill_polygon(writer: &mut impl PixelWriter, verts: &[Vertex], color: u8) {
|
| 699 |
+
if verts.len() < 3 {
|
| 700 |
+
return;
|
| 701 |
+
}
|
| 702 |
+
|
| 703 |
+
// Two span tables: one accumulates "left" edges, the other "right".
|
| 704 |
+
// WA uses two global i16 arrays at 0x8AD058 and 0x8AD858.
|
| 705 |
+
let mut span_a = [0i16; MAX_SCANLINES];
|
| 706 |
+
let mut span_b = [0i16; MAX_SCANLINES];
|
| 707 |
+
let mut y_min = i32::MAX;
|
| 708 |
+
let mut y_max = i32::MIN;
|
| 709 |
+
|
| 710 |
+
let n = verts.len();
|
| 711 |
+
for i in 0..n {
|
| 712 |
+
let prev = verts[(i + n - 1) % n];
|
| 713 |
+
let curr = verts[i];
|
| 714 |
+
|
| 715 |
+
let py_prev = prev.y.round_to_int();
|
| 716 |
+
let py_curr = curr.y.round_to_int();
|
| 717 |
+
|
| 718 |
+
if py_prev == py_curr {
|
| 719 |
+
continue;
|
| 720 |
+
}
|
| 721 |
+
|
| 722 |
+
// Determine direction: iterate from smaller Y to larger Y
|
| 723 |
+
let (start_x, start_y, end_x, end_y, py_start, py_end);
|
| 724 |
+
if py_curr < py_prev {
|
| 725 |
+
start_x = curr.x;
|
| 726 |
+
start_y = curr.y;
|
| 727 |
+
end_x = prev.x;
|
| 728 |
+
end_y = prev.y;
|
| 729 |
+
py_start = py_prev;
|
| 730 |
+
py_end = py_curr;
|
| 731 |
+
} else {
|
| 732 |
+
start_x = prev.x;
|
| 733 |
+
start_y = prev.y;
|
| 734 |
+
end_x = curr.x;
|
| 735 |
+
end_y = curr.y;
|
| 736 |
+
py_start = py_curr;
|
| 737 |
+
py_end = py_prev;
|
| 738 |
+
};
|
| 739 |
+
|
| 740 |
+
if py_end < y_min {
|
| 741 |
+
y_min = py_end;
|
| 742 |
+
}
|
| 743 |
+
if py_start > y_max {
|
| 744 |
+
y_max = py_start;
|
| 745 |
+
}
|
| 746 |
+
|
| 747 |
+
let scanline_count = py_start - py_end;
|
| 748 |
+
if scanline_count <= 0 {
|
| 749 |
+
continue;
|
| 750 |
+
}
|
| 751 |
+
|
| 752 |
+
// Compute DDA slope for X across scanlines (plain integer divide)
|
| 753 |
+
let dx = end_x.to_raw() - start_x.to_raw();
|
| 754 |
+
let dy = end_y.to_raw() - start_y.to_raw();
|
| 755 |
+
let slope = if dy != 0 { dx / dy } else { 0 };
|
| 756 |
+
|
| 757 |
+
// Starting X with sub-pixel correction
|
| 758 |
+
let x_start = start_x.to_raw() + ((py_end * 0x10000 - start_y.to_raw()) + 0x8000) * slope;
|
| 759 |
+
|
| 760 |
+
// Corrected step for the span table DDA
|
| 761 |
+
let x_end_correction =
|
| 762 |
+
((py_start * 0x10000 - end_y.to_raw()) + 0x8000) * slope - x_start + end_x.to_raw();
|
| 763 |
+
let step = if scanline_count != 0 {
|
| 764 |
+
x_end_correction / scanline_count
|
| 765 |
+
} else {
|
| 766 |
+
0
|
| 767 |
+
};
|
| 768 |
+
|
| 769 |
+
// Fill the appropriate span table
|
| 770 |
+
// Which table depends on edge direction (left vs right side of polygon).
|
| 771 |
+
// WA uses two fixed tables offset by 0x800 bytes (1024 i16 entries).
|
| 772 |
+
// We pick table based on the table pointer offset in the original:
|
| 773 |
+
// 0x8AD058 vs 0x8AD858 — determined by the scanline pointer base.
|
| 774 |
+
let table = if py_curr < py_prev {
|
| 775 |
+
&mut span_a
|
| 776 |
+
} else {
|
| 777 |
+
&mut span_b
|
| 778 |
+
};
|
| 779 |
+
|
| 780 |
+
let mut val = x_start + 0x8000; // round
|
| 781 |
+
for j in 0..scanline_count as usize {
|
| 782 |
+
let idx = py_end as usize + j;
|
| 783 |
+
if idx < MAX_SCANLINES {
|
| 784 |
+
table[idx] = (val >> 16) as i16;
|
| 785 |
+
}
|
| 786 |
+
val = val.wrapping_add(step);
|
| 787 |
+
}
|
| 788 |
+
}
|
| 789 |
+
|
| 790 |
+
// Fill scanlines between the two span tables
|
| 791 |
+
for y in y_min..y_max {
|
| 792 |
+
if y < 0 || y as usize >= MAX_SCANLINES {
|
| 793 |
+
continue;
|
| 794 |
+
}
|
| 795 |
+
let a = span_a[y as usize] as i32;
|
| 796 |
+
let b = span_b[y as usize] as i32;
|
| 797 |
+
if a != b {
|
| 798 |
+
let (x1, x2) = if a < b { (a, b) } else { (b, a) };
|
| 799 |
+
writer.fill_hline(x1, x2, y, color);
|
| 800 |
+
}
|
| 801 |
+
}
|
| 802 |
+
}
|
| 803 |
+
|
| 804 |
+
/// Draw a filled polygon. Clips against the writer's bounds, then scanline-fills.
|
| 805 |
+
///
|
| 806 |
+
/// Input vertices are Fixed-point (x, y) pairs.
|
| 807 |
+
pub fn draw_polygon_filled(writer: &mut impl PixelWriter, vertices: &[(Fixed, Fixed)], color: u8) {
|
| 808 |
+
let input: Vec<Vertex> = vertices.iter().map(|&(x, y)| Vertex { x, y }).collect();
|
| 809 |
+
|
| 810 |
+
let mut clipped = [Vertex {
|
| 811 |
+
x: Fixed::ZERO,
|
| 812 |
+
y: Fixed::ZERO,
|
| 813 |
+
}; MAX_CLIP_VERTS];
|
| 814 |
+
let count = clip_polygon(&input, &mut clipped, writer);
|
| 815 |
+
|
| 816 |
+
if count > 2 {
|
| 817 |
+
fill_polygon(writer, &clipped[..count], color);
|
| 818 |
+
}
|
| 819 |
+
}
|
| 820 |
+
|
| 821 |
// =========================================================================
|
| 822 |
// Tests
|
| 823 |
// =========================================================================
|
|
|
|
| 1107 |
draw_line_two_color(&mut grid, f(10), f(10), f(118), f(118), 9, 10);
|
| 1108 |
assert_matches_snapshot(&grid, "twocol_restricted_clip");
|
| 1109 |
}
|
| 1110 |
+
|
| 1111 |
+
// Polygon fill snapshot tests
|
| 1112 |
+
macro_rules! snapshot_test_poly {
|
| 1113 |
+
($name:ident, $snap:expr, $verts:expr, $color:expr) => {
|
| 1114 |
+
#[test]
|
| 1115 |
+
fn $name() {
|
| 1116 |
+
let mut grid = PixelGrid::new(128, 128);
|
| 1117 |
+
draw_polygon_filled(&mut grid, $verts, $color);
|
| 1118 |
+
assert_matches_snapshot(&grid, $snap);
|
| 1119 |
+
}
|
| 1120 |
+
};
|
| 1121 |
+
}
|
| 1122 |
+
|
| 1123 |
+
snapshot_test_poly!(
|
| 1124 |
+
snap_poly_triangle,
|
| 1125 |
+
"poly_triangle",
|
| 1126 |
+
&[(f(64), f(10)), (f(118), f(100)), (f(10), f(100))],
|
| 1127 |
+
1
|
| 1128 |
+
);
|
| 1129 |
+
snapshot_test_poly!(
|
| 1130 |
+
snap_poly_square,
|
| 1131 |
+
"poly_square",
|
| 1132 |
+
&[
|
| 1133 |
+
(f(20), f(20)),
|
| 1134 |
+
(f(100), f(20)),
|
| 1135 |
+
(f(100), f(100)),
|
| 1136 |
+
(f(20), f(100))
|
| 1137 |
+
],
|
| 1138 |
+
2
|
| 1139 |
+
);
|
| 1140 |
+
snapshot_test_poly!(
|
| 1141 |
+
snap_poly_diamond,
|
| 1142 |
+
"poly_diamond",
|
| 1143 |
+
&[
|
| 1144 |
+
(f(64), f(10)),
|
| 1145 |
+
(f(118), f(64)),
|
| 1146 |
+
(f(64), f(118)),
|
| 1147 |
+
(f(10), f(64))
|
| 1148 |
+
],
|
| 1149 |
+
3
|
| 1150 |
+
);
|
| 1151 |
+
snapshot_test_poly!(
|
| 1152 |
+
snap_poly_partially_outside,
|
| 1153 |
+
"poly_partially_outside",
|
| 1154 |
+
&[(f(64), f(-30)), (f(160), f(100)), (f(-30), f(100))],
|
| 1155 |
+
4
|
| 1156 |
+
);
|
| 1157 |
+
|
| 1158 |
+
#[test]
|
| 1159 |
+
fn snap_poly_restricted_clip() {
|
| 1160 |
+
let mut grid = PixelGrid::new(128, 128);
|
| 1161 |
+
grid.clip_left = 30;
|
| 1162 |
+
grid.clip_top = 30;
|
| 1163 |
+
grid.clip_right = 98;
|
| 1164 |
+
grid.clip_bottom = 98;
|
| 1165 |
+
draw_polygon_filled(
|
| 1166 |
+
&mut grid,
|
| 1167 |
+
&[(f(64), f(10)), (f(118), f(100)), (f(10), f(100))],
|
| 1168 |
+
5,
|
| 1169 |
+
);
|
| 1170 |
+
assert_matches_snapshot(&grid, "poly_restricted_clip");
|
| 1171 |
+
}
|
| 1172 |
}
|
crates/openwa-dll/src/replacements/display.rs
CHANGED
|
@@ -167,8 +167,98 @@ pub unsafe fn capture_line_snapshots() {
|
|
| 167 |
});
|
| 168 |
count += 1;
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
let _ = log_line(&format!(
|
| 171 |
-
"[Display] SNAPSHOT: Saved {count}
|
| 172 |
));
|
| 173 |
|
| 174 |
// Free the grid
|
|
|
|
| 167 |
});
|
| 168 |
count += 1;
|
| 169 |
|
| 170 |
+
// Polygon fill tests — call WA's polygon pipeline directly
|
| 171 |
+
let clip_x: unsafe extern "thiscall" fn(*mut DisplayBitGrid, *const i32, i32) -> i32 =
|
| 172 |
+
core::mem::transmute(rb(0x004F_7BA0) as usize);
|
| 173 |
+
let clip_y: unsafe extern "thiscall" fn(*mut DisplayBitGrid, i32) -> i32 =
|
| 174 |
+
core::mem::transmute(rb(0x004F_7D00) as usize);
|
| 175 |
+
let rasterize: unsafe extern "stdcall" fn(*mut DisplayBitGrid, i32, u32) =
|
| 176 |
+
core::mem::transmute(rb(0x004F_7E90) as usize);
|
| 177 |
+
|
| 178 |
+
// Helper: write vertices to the global vertex buffer (0x8B1370) and call pipeline
|
| 179 |
+
let vert_buf = rb(0x008B_1370) as *mut i32;
|
| 180 |
+
|
| 181 |
+
macro_rules! polygon_snap {
|
| 182 |
+
($name:expr, $verts:expr, $color:expr) => {{
|
| 183 |
+
let verts: &[(i32, i32)] = $verts;
|
| 184 |
+
for (i, &(x, y)) in verts.iter().enumerate() {
|
| 185 |
+
*vert_buf.add(i * 2) = x;
|
| 186 |
+
*vert_buf.add(i * 2 + 1) = y;
|
| 187 |
+
}
|
| 188 |
+
snap!($name, {
|
| 189 |
+
let n = clip_x(grid, vert_buf, verts.len() as i32);
|
| 190 |
+
if n > 2 {
|
| 191 |
+
let n = clip_y(grid, n);
|
| 192 |
+
if n > 2 {
|
| 193 |
+
rasterize(grid, n, $color);
|
| 194 |
+
}
|
| 195 |
+
}
|
| 196 |
+
});
|
| 197 |
+
count += 1;
|
| 198 |
+
}};
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
// Triangle
|
| 202 |
+
polygon_snap!(
|
| 203 |
+
"poly_triangle",
|
| 204 |
+
&[(f(64), f(10)), (f(118), f(100)), (f(10), f(100))],
|
| 205 |
+
1u32
|
| 206 |
+
);
|
| 207 |
+
|
| 208 |
+
// Square
|
| 209 |
+
polygon_snap!(
|
| 210 |
+
"poly_square",
|
| 211 |
+
&[
|
| 212 |
+
(f(20), f(20)),
|
| 213 |
+
(f(100), f(20)),
|
| 214 |
+
(f(100), f(100)),
|
| 215 |
+
(f(20), f(100))
|
| 216 |
+
],
|
| 217 |
+
2u32
|
| 218 |
+
);
|
| 219 |
+
|
| 220 |
+
// Diamond
|
| 221 |
+
polygon_snap!(
|
| 222 |
+
"poly_diamond",
|
| 223 |
+
&[
|
| 224 |
+
(f(64), f(10)),
|
| 225 |
+
(f(118), f(64)),
|
| 226 |
+
(f(64), f(118)),
|
| 227 |
+
(f(10), f(64))
|
| 228 |
+
],
|
| 229 |
+
3u32
|
| 230 |
+
);
|
| 231 |
+
|
| 232 |
+
// Partially outside (triangle extending beyond grid)
|
| 233 |
+
polygon_snap!(
|
| 234 |
+
"poly_partially_outside",
|
| 235 |
+
&[(f(64), f(-30)), (f(160), f(100)), (f(-30), f(100))],
|
| 236 |
+
4u32
|
| 237 |
+
);
|
| 238 |
+
|
| 239 |
+
// Restricted clip rect
|
| 240 |
+
snap!("poly_restricted_clip", {
|
| 241 |
+
(*grid).clip_left = 30;
|
| 242 |
+
(*grid).clip_top = 30;
|
| 243 |
+
(*grid).clip_right = 98;
|
| 244 |
+
(*grid).clip_bottom = 98;
|
| 245 |
+
let verts: &[(i32, i32)] = &[(f(64), f(10)), (f(118), f(100)), (f(10), f(100))];
|
| 246 |
+
for (i, &(x, y)) in verts.iter().enumerate() {
|
| 247 |
+
*vert_buf.add(i * 2) = x;
|
| 248 |
+
*vert_buf.add(i * 2 + 1) = y;
|
| 249 |
+
}
|
| 250 |
+
let n = clip_x(grid, vert_buf, verts.len() as i32);
|
| 251 |
+
if n > 2 {
|
| 252 |
+
let n = clip_y(grid, n);
|
| 253 |
+
if n > 2 {
|
| 254 |
+
rasterize(grid, n, 5u32);
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
});
|
| 258 |
+
count += 1;
|
| 259 |
+
|
| 260 |
let _ = log_line(&format!(
|
| 261 |
+
"[Display] SNAPSHOT: Saved {count} snapshots to {dir}/"
|
| 262 |
));
|
| 263 |
|
| 264 |
// Free the grid
|
testdata/snapshots/poly_diamond.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2c1449e300e29274421b3890891f8df3b64cf31ae48fcda4b351b17b82cf267b
|
| 3 |
+
size 16396
|
testdata/snapshots/poly_partially_outside.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:95b11d37791d5f7254c82f3181e2b49a483d5ee285b95fd917bd1719d32a1937
|
| 3 |
+
size 16396
|
testdata/snapshots/poly_restricted_clip.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:62821afb3e54143d3ffec6bbe05a9350f66abe86014bb0b0c66bea9472477c86
|
| 3 |
+
size 16396
|
testdata/snapshots/poly_square.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8e76ebd44d47dc1be0264cb377de9ab5ac92492b750a59b273ce06349bb64ac0
|
| 3 |
+
size 16396
|
testdata/snapshots/poly_triangle.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b91c75d60da9cd203faa066fa59ac38baa44835224b15d5d006c3b43c9297a97
|
| 3 |
+
size 16396
|