gpu-style filled triangles

This commit is contained in:
Joe Ardent 2025-09-07 14:23:39 -07:00
parent 88a406f84d
commit 10ac742f4f

View file

@ -156,10 +156,16 @@ struct Triangle {
impl Triangle { impl Triangle {
pub fn new(a: Point2i, b: Point2i, c: Point2i) -> Self { pub fn new(a: Point2i, b: Point2i, c: Point2i) -> Self {
let mut sorted = [a, b, c]; Self { a, b, c }
}
pub fn sort(&mut self) {
let mut sorted = [self.a, self.b, self.c];
sorted.sort_unstable_by(|a, b| a.y.cmp(&b.y)); sorted.sort_unstable_by(|a, b| a.y.cmp(&b.y));
let [a, b, c] = sorted; let [a, b, c] = sorted;
Self { a, b, c } self.a = a;
self.b = b;
self.c = c;
} }
pub fn signed_area(&self) -> f32 { pub fn signed_area(&self) -> f32 {
@ -179,13 +185,27 @@ impl Triangle {
upper_right: Point2i::new(xmax, ymax), upper_right: Point2i::new(xmax, ymax),
} }
} }
pub fn render_filled(&self, color: TGAColor, fb: &mut TGAImage) { pub fn render_filled(&self, color: TGAColor, fb: &mut TGAImage) {
let fb = Arc::new(Mutex::new(fb));
let bb = self.bb(); let bb = self.bb();
for y in bb.ymin()..=bb.ymax() { let total_area = self.signed_area();
let a = Point2i::new(bb.xmin(), y); (bb.ymin()..=bb.ymax()).into_par_iter().for_each(|y| {
let b = Point2i::new(bb.xmax(), y); (bb.xmin()..bb.xmax()).into_par_iter().for_each(|x| {
line(a, b, color, fb); let p = Point2i::new(x, y);
} let a = Triangle::new(p, self.a, self.b).signed_area() / total_area;
let b = Triangle::new(p, self.b, self.c).signed_area() / total_area;
let c = Triangle::new(p, self.c, self.a).signed_area() / total_area;
if a.is_sign_positive()
&& b.is_sign_positive()
&& c.is_sign_positive()
&& let Ok(mut fb) = fb.lock()
{
fb.set(x as u32, y as u32, color);
}
});
});
} }
pub fn render_lines(&self, color: TGAColor, fb: &mut TGAImage) { pub fn render_lines(&self, color: TGAColor, fb: &mut TGAImage) {