Commit e5318389212d0e00584b0430c2f8242e81aa62b0
1 parent
42c575a9
First not fully correct filled polygons
Showing
4 changed files
with
23 additions
and
18 deletions
@@ -39,6 +39,7 @@ pub trait Canvas<T> { | @@ -39,6 +39,7 @@ pub trait Canvas<T> { | ||
39 | fn clear(&mut self); | 39 | fn clear(&mut self); |
40 | fn draw(&mut self, c :&dyn Drawable<T>, ofs :Coordinate<T>, color :u32); | 40 | fn draw(&mut self, c :&dyn Drawable<T>, ofs :Coordinate<T>, color :u32); |
41 | fn put_text(&self, ofs :Coordinate<T>, s :&str); | 41 | fn put_text(&self, ofs :Coordinate<T>, s :&str); |
42 | + fn set_pixel(&mut self, c :Coordinate<T>, color :u32); | ||
42 | fn show(&self); | 43 | fn show(&self); |
43 | } | 44 | } |
44 | 45 | ||
@@ -46,8 +47,10 @@ pub trait Drawable<T> { | @@ -46,8 +47,10 @@ pub trait Drawable<T> { | ||
46 | fn plot(&self) -> Coordinates<T>; | 47 | fn plot(&self) -> Coordinates<T>; |
47 | } | 48 | } |
48 | 49 | ||
49 | -pub trait Fillable<T> { | ||
50 | - fn fill(&self) -> Coordinates<T>; | 50 | +pub trait Fillable<T> |
51 | +where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> | ||
52 | + + Debug + Copy + From<i32> { | ||
53 | + fn fill(&self, canvas :&mut dyn Canvas<T>, color :u32); | ||
51 | } | 54 | } |
52 | 55 | ||
53 | #[derive(Debug, Clone, Copy)] | 56 | #[derive(Debug, Clone, Copy)] |
@@ -440,17 +443,12 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> | @@ -440,17 +443,12 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> | ||
440 | impl<T> Fillable<T> for Polygon<T> | 443 | impl<T> Fillable<T> for Polygon<T> |
441 | where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> | 444 | where T: Add<Output = T> + Sub<Output = T> + Div<Output = T> |
442 | + Debug + Clone + Copy + From<i32> { | 445 | + Debug + Clone + Copy + From<i32> { |
443 | - fn fill(&self) -> Coordinates<T> { | 446 | + fn fill(&self, canvas :&mut dyn Canvas<T>, color :u32) { |
444 | let scanlines = self.left_vertices().zip(self.right_vertices()); | 447 | let scanlines = self.left_vertices().zip(self.right_vertices()); |
445 | 448 | ||
446 | - // vertices is an iterator over all vertices making this polygon. | ||
447 | - let vertices = scanlines.flat_map(|(l, r)| l.line_iter(&r)); | ||
448 | - | ||
449 | - // for debug only… | ||
450 | - //let vertices :Vec<(Coordinate<T>)> = vertices.collect(); | ||
451 | - //println!("== [{}] {:?}", vertices.len(), vertices); | ||
452 | - | ||
453 | - Coordinates(Vec::<Coordinate<T>>::new()) | 449 | + for l in scanlines.flat_map(|(l, r)| l.line_iter(&r)) { |
450 | + canvas.set_pixel(l, color); | ||
451 | + } | ||
454 | } | 452 | } |
455 | } | 453 | } |
456 | 454 |
@@ -22,7 +22,7 @@ use std::convert::{From, Into}; | @@ -22,7 +22,7 @@ use std::convert::{From, Into}; | ||
22 | use std::ops::{Add,Sub,Neg,Mul,Div}; | 22 | use std::ops::{Add,Sub,Neg,Mul,Div}; |
23 | use std::fmt::Debug; | 23 | use std::fmt::Debug; |
24 | 24 | ||
25 | -use crate::easel::{Canvas,Coordinate,Coordinates,Polygon,Fillable}; | 25 | +use crate::easel::{Canvas, Coordinate, Coordinates, Polygon}; |
26 | use crate::transform::{TMatrix, Transformable}; | 26 | use crate::transform::{TMatrix, Transformable}; |
27 | use crate::trigonometry::Trig; | 27 | use crate::trigonometry::Trig; |
28 | use crate::vector::Vector; | 28 | use crate::vector::Vector; |
@@ -361,7 +361,7 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | @@ -361,7 +361,7 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | ||
361 | / (n.mag() * light.dir().mag()), | 361 | / (n.mag() * light.dir().mag()), |
362 | }; | 362 | }; |
363 | 363 | ||
364 | - // this if represents a first simple backface culling | 364 | + // this "if" represents a first simple backface culling |
365 | // approach. We only return face that face towards us. | 365 | // approach. We only return face that face towards us. |
366 | if lf < 0.into() { | 366 | if lf < 0.into() { |
367 | r = r * -lf; | 367 | r = r * -lf; |
@@ -372,8 +372,6 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | @@ -372,8 +372,6 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T> | ||
372 | | (g.round() as u32) << 8 | 372 | | (g.round() as u32) << 8 |
373 | | (b.round() as u32); | 373 | | (b.round() as u32); |
374 | 374 | ||
375 | - (&pg).fill(); | ||
376 | - | ||
377 | Some((pg, c)) | 375 | Some((pg, c)) |
378 | } else { | 376 | } else { |
379 | None | 377 | None |
@@ -30,14 +30,13 @@ use std::time::{Duration, Instant}; | @@ -30,14 +30,13 @@ use std::time::{Duration, Instant}; | ||
30 | 30 | ||
31 | use fractional::continuous::Continuous; | 31 | use fractional::continuous::Continuous; |
32 | use fractional::easel::{ Coordinate, Coordinates, Drawable, Line, Polyline | 32 | use fractional::easel::{ Coordinate, Coordinates, Drawable, Line, Polyline |
33 | - , Polygon}; | 33 | + , Polygon, Canvas, Fillable }; |
34 | use fractional::fractional::{Fractional, from_vector}; | 34 | use fractional::fractional::{Fractional, from_vector}; |
35 | use fractional::trigonometry::Trig; | 35 | use fractional::trigonometry::Trig; |
36 | use fractional::vector::Vector; | 36 | use fractional::vector::Vector; |
37 | use fractional::transform::{TMatrix, Transformable}; | 37 | use fractional::transform::{TMatrix, Transformable}; |
38 | 38 | ||
39 | use fractional::xcb::{XcbEasel, XcbCanvas}; | 39 | use fractional::xcb::{XcbEasel, XcbCanvas}; |
40 | -use fractional::easel::Canvas; | ||
41 | 40 | ||
42 | use fractional::geometry::{Camera,DirectLight,Polyeder,Primitives}; | 41 | use fractional::geometry::{Camera,DirectLight,Polyeder,Primitives}; |
43 | 42 | ||
@@ -351,7 +350,9 @@ fn _democanvas<T>( xcb :&XcbEasel | @@ -351,7 +350,9 @@ fn _democanvas<T>( xcb :&XcbEasel | ||
351 | 350 | ||
352 | for (o, color) in objects { | 351 | for (o, color) in objects { |
353 | for (pg, c) in o.project(&camera, &light, color) { | 352 | for (pg, c) in o.project(&camera, &light, color) { |
354 | - canvas.draw(&pg, Coordinate(0, 0, 0.into()), c); | 353 | + //canvas.draw(&pg, Coordinate(0, 0, 0.into()), c); |
354 | + (&pg).fill(&mut canvas, c); | ||
355 | + //println!("\n"); | ||
355 | } | 356 | } |
356 | } | 357 | } |
357 | 358 |
@@ -240,6 +240,14 @@ impl<'a,T> Canvas<T> for XcbCanvas<'a> { | @@ -240,6 +240,14 @@ impl<'a,T> Canvas<T> for XcbCanvas<'a> { | ||
240 | self.conn.flush(); | 240 | self.conn.flush(); |
241 | } | 241 | } |
242 | 242 | ||
243 | + fn set_pixel(&mut self, c :Coordinate<T>, color :u32) { | ||
244 | + let Coordinate(x, y, _) = c; | ||
245 | + let idx :usize = (y * (self.width as i32) + x) as usize; | ||
246 | + | ||
247 | + //print!("({}, {})", idx, color); | ||
248 | + self.shm[idx] = color; | ||
249 | + } | ||
250 | + | ||
243 | fn show(&self) { | 251 | fn show(&self) { |
244 | xcb::copy_area( &self.conn, self.pixmap, self.window, self.gc | 252 | xcb::copy_area( &self.conn, self.pixmap, self.window, self.gc |
245 | , 0, 0, 0, 0 | 253 | , 0, 0, 0, 0 |
Please
register
or
login
to post a comment