diff --git a/fractional/src/easel.rs b/fractional/src/easel.rs index 75d5b9e..cb87ea0 100644 --- a/fractional/src/easel.rs +++ b/fractional/src/easel.rs @@ -39,6 +39,7 @@ pub trait Canvas { fn clear(&mut self); fn draw(&mut self, c :&dyn Drawable, ofs :Coordinate, color :u32); fn put_text(&self, ofs :Coordinate, s :&str); + fn set_pixel(&mut self, c :Coordinate, color :u32); fn show(&self); } @@ -46,8 +47,10 @@ pub trait Drawable { fn plot(&self) -> Coordinates; } -pub trait Fillable { - fn fill(&self) -> Coordinates; +pub trait Fillable +where T: Add + Sub + Div + + Debug + Copy + From { + fn fill(&self, canvas :&mut dyn Canvas, color :u32); } #[derive(Debug, Clone, Copy)] @@ -440,17 +443,12 @@ where T: Add + Sub + Div impl Fillable for Polygon where T: Add + Sub + Div + Debug + Clone + Copy + From { - fn fill(&self) -> Coordinates { + fn fill(&self, canvas :&mut dyn Canvas, color :u32) { let scanlines = self.left_vertices().zip(self.right_vertices()); - // vertices is an iterator over all vertices making this polygon. - let vertices = scanlines.flat_map(|(l, r)| l.line_iter(&r)); - - // for debug only… - //let vertices :Vec<(Coordinate)> = vertices.collect(); - //println!("== [{}] {:?}", vertices.len(), vertices); - - Coordinates(Vec::>::new()) + for l in scanlines.flat_map(|(l, r)| l.line_iter(&r)) { + canvas.set_pixel(l, color); + } } } diff --git a/fractional/src/geometry.rs b/fractional/src/geometry.rs index 74fca4d..0f9b882 100644 --- a/fractional/src/geometry.rs +++ b/fractional/src/geometry.rs @@ -22,7 +22,7 @@ use std::convert::{From, Into}; use std::ops::{Add,Sub,Neg,Mul,Div}; use std::fmt::Debug; -use crate::easel::{Canvas,Coordinate,Coordinates,Polygon,Fillable}; +use crate::easel::{Canvas, Coordinate, Coordinates, Polygon}; use crate::transform::{TMatrix, Transformable}; use crate::trigonometry::Trig; use crate::vector::Vector; @@ -361,7 +361,7 @@ where T: Add + Sub + Neg / (n.mag() * light.dir().mag()), }; - // this if represents a first simple backface culling + // this "if" represents a first simple backface culling // approach. We only return face that face towards us. if lf < 0.into() { r = r * -lf; @@ -372,8 +372,6 @@ where T: Add + Sub + Neg | (g.round() as u32) << 8 | (b.round() as u32); - (&pg).fill(); - Some((pg, c)) } else { None diff --git a/fractional/src/main.rs b/fractional/src/main.rs index 903f259..afe19e9 100644 --- a/fractional/src/main.rs +++ b/fractional/src/main.rs @@ -30,14 +30,13 @@ use std::time::{Duration, Instant}; use fractional::continuous::Continuous; use fractional::easel::{ Coordinate, Coordinates, Drawable, Line, Polyline - , Polygon}; + , Polygon, Canvas, Fillable }; use fractional::fractional::{Fractional, from_vector}; use fractional::trigonometry::Trig; use fractional::vector::Vector; use fractional::transform::{TMatrix, Transformable}; use fractional::xcb::{XcbEasel, XcbCanvas}; -use fractional::easel::Canvas; use fractional::geometry::{Camera,DirectLight,Polyeder,Primitives}; @@ -351,7 +350,9 @@ fn _democanvas( xcb :&XcbEasel for (o, color) in objects { for (pg, c) in o.project(&camera, &light, color) { - canvas.draw(&pg, Coordinate(0, 0, 0.into()), c); + //canvas.draw(&pg, Coordinate(0, 0, 0.into()), c); + (&pg).fill(&mut canvas, c); + //println!("\n"); } } diff --git a/fractional/src/xcb.rs b/fractional/src/xcb.rs index 6b33fac..9c09458 100644 --- a/fractional/src/xcb.rs +++ b/fractional/src/xcb.rs @@ -240,6 +240,14 @@ impl<'a,T> Canvas for XcbCanvas<'a> { self.conn.flush(); } + fn set_pixel(&mut self, c :Coordinate, color :u32) { + let Coordinate(x, y, _) = c; + let idx :usize = (y * (self.width as i32) + x) as usize; + + //print!("({}, {})", idx, color); + self.shm[idx] = color; + } + fn show(&self) { xcb::copy_area( &self.conn, self.pixmap, self.window, self.gc , 0, 0, 0, 0