primitive back-culling
This commit is contained in:
parent
90420bdc72
commit
10882983fe
1 changed files with 16 additions and 10 deletions
26
src/main.rs
26
src/main.rs
|
@ -39,13 +39,11 @@ fn main() {
|
|||
let w = 800;
|
||||
let h = 800;
|
||||
let mut fb = TGAImage::new(w, h, TGAFormat::RGB);
|
||||
// fill_triangles(&mut fb);
|
||||
// fb.write_file("triangles.tga", true, true).unwrap();
|
||||
|
||||
_render_head(&mut fb);
|
||||
}
|
||||
|
||||
fn fill_triangles(fb: &mut TGAImage) {
|
||||
fn _fill_triangles(fb: &mut TGAImage) {
|
||||
let t1a = Point2i::new(7, 45);
|
||||
let t1b = Point2i::new(35, 100);
|
||||
let t1c = Point2i::new(45, 60);
|
||||
|
@ -175,17 +173,25 @@ impl Triangle {
|
|||
pub fn render_filled(&self, color: TGAColor, fb: &mut TGAImage) {
|
||||
let fb = Arc::new(Mutex::new(fb));
|
||||
let bb = self.bb();
|
||||
let total_area_sign = self.signed_area().signum();
|
||||
let total_area = self.signed_area();
|
||||
if total_area < 1.0 {
|
||||
return;
|
||||
}
|
||||
(bb.ymin()..=bb.ymax()).into_par_iter().for_each(|y| {
|
||||
(bb.xmin()..bb.xmax()).into_par_iter().for_each(|x| {
|
||||
let p = Point2i::new(x, y);
|
||||
let a = Triangle::new(p, self.a, self.b).signed_area().signum() * total_area_sign;
|
||||
let b = Triangle::new(p, self.b, self.c).signed_area().signum() * total_area_sign;
|
||||
let c = Triangle::new(p, self.c, self.a).signed_area().signum() * total_area_sign;
|
||||
let a = Triangle::new(p, self.a, self.b)
|
||||
.signed_area()
|
||||
.is_sign_positive();
|
||||
let b = Triangle::new(p, self.b, self.c)
|
||||
.signed_area()
|
||||
.is_sign_positive();
|
||||
let c = Triangle::new(p, self.c, self.a)
|
||||
.signed_area()
|
||||
.is_sign_positive();
|
||||
|
||||
if a.is_sign_positive()
|
||||
&& b.is_sign_positive()
|
||||
&& c.is_sign_positive()
|
||||
if a && b
|
||||
&& c
|
||||
&& let Ok(mut fb) = fb.lock()
|
||||
{
|
||||
fb.set(x as u32, y as u32, color);
|
||||
|
|
Loading…
Reference in a new issue