Commit e5318389212d0e00584b0430c2f8242e81aa62b0

Authored by Georg Hopp
1 parent 42c575a9

First not fully correct filled polygons

... ... @@ -39,6 +39,7 @@ pub trait Canvas<T> {
39 39 fn clear(&mut self);
40 40 fn draw(&mut self, c :&dyn Drawable<T>, ofs :Coordinate<T>, color :u32);
41 41 fn put_text(&self, ofs :Coordinate<T>, s :&str);
  42 + fn set_pixel(&mut self, c :Coordinate<T>, color :u32);
42 43 fn show(&self);
43 44 }
44 45
... ... @@ -46,8 +47,10 @@ pub trait Drawable<T> {
46 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 56 #[derive(Debug, Clone, Copy)]
... ... @@ -440,17 +443,12 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
440 443 impl<T> Fillable<T> for Polygon<T>
441 444 where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
442 445 + Debug + Clone + Copy + From<i32> {
443   - fn fill(&self) -> Coordinates<T> {
  446 + fn fill(&self, canvas :&mut dyn Canvas<T>, color :u32) {
444 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 22 use std::ops::{Add,Sub,Neg,Mul,Div};
23 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 26 use crate::transform::{TMatrix, Transformable};
27 27 use crate::trigonometry::Trig;
28 28 use crate::vector::Vector;
... ... @@ -361,7 +361,7 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
361 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 365 // approach. We only return face that face towards us.
366 366 if lf < 0.into() {
367 367 r = r * -lf;
... ... @@ -372,8 +372,6 @@ where T: Add<Output = T> + Sub<Output = T> + Neg<Output = T>
372 372 | (g.round() as u32) << 8
373 373 | (b.round() as u32);
374 374
375   - (&pg).fill();
376   -
377 375 Some((pg, c))
378 376 } else {
379 377 None
... ...
... ... @@ -30,14 +30,13 @@ use std::time::{Duration, Instant};
30 30
31 31 use fractional::continuous::Continuous;
32 32 use fractional::easel::{ Coordinate, Coordinates, Drawable, Line, Polyline
33   - , Polygon};
  33 + , Polygon, Canvas, Fillable };
34 34 use fractional::fractional::{Fractional, from_vector};
35 35 use fractional::trigonometry::Trig;
36 36 use fractional::vector::Vector;
37 37 use fractional::transform::{TMatrix, Transformable};
38 38
39 39 use fractional::xcb::{XcbEasel, XcbCanvas};
40   -use fractional::easel::Canvas;
41 40
42 41 use fractional::geometry::{Camera,DirectLight,Polyeder,Primitives};
43 42
... ... @@ -351,7 +350,9 @@ fn _democanvas<T>( xcb :&XcbEasel
351 350
352 351 for (o, color) in objects {
353 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 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 251 fn show(&self) {
244 252 xcb::copy_area( &self.conn, self.pixmap, self.window, self.gc
245 253 , 0, 0, 0, 0
... ...
Please register or login to post a comment