Browse Source

First not fully correct filled polygons

master
Georg Hopp 6 years ago
parent
commit
e531838921
Signed by: ghopp GPG Key ID: 4C5D226768784538
  1. 20
      fractional/src/easel.rs
  2. 6
      fractional/src/geometry.rs
  3. 7
      fractional/src/main.rs
  4. 8
      fractional/src/xcb.rs

20
fractional/src/easel.rs

@ -39,6 +39,7 @@ pub trait Canvas<T> {
fn clear(&mut self);
fn draw(&mut self, c :&dyn Drawable<T>, ofs :Coordinate<T>, color :u32);
fn put_text(&self, ofs :Coordinate<T>, s :&str);
fn set_pixel(&mut self, c :Coordinate<T>, color :u32);
fn show(&self);
}
@ -46,8 +47,10 @@ pub trait Drawable<T> {
fn plot(&self) -> Coordinates<T>;
}
pub trait Fillable<T> {
fn fill(&self) -> Coordinates<T>;
pub trait Fillable<T>
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
+ Debug + Copy + From<i32> {
fn fill(&self, canvas :&mut dyn Canvas<T>, color :u32);
}
#[derive(Debug, Clone, Copy)]
@ -440,17 +443,12 @@ where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
impl<T> Fillable<T> for Polygon<T>
where T: Add<Output = T> + Sub<Output = T> + Div<Output = T>
+ Debug + Clone + Copy + From<i32> {
fn fill(&self) -> Coordinates<T> {
fn fill(&self, canvas :&mut dyn Canvas<T>, 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<T>)> = vertices.collect();
//println!("== [{}] {:?}", vertices.len(), vertices);
Coordinates(Vec::<Coordinate<T>>::new())
for l in scanlines.flat_map(|(l, r)| l.line_iter(&r)) {
canvas.set_pixel(l, color);
}
}
}

6
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<Output = T> + Sub<Output = T> + Neg<Output = T>
/ (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<Output = T> + Sub<Output = T> + Neg<Output = T>
| (g.round() as u32) << 8
| (b.round() as u32);
(&pg).fill();
Some((pg, c))
} else {
None

7
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<T>( 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");
}
}

8
fractional/src/xcb.rs

@ -240,6 +240,14 @@ impl<'a,T> Canvas<T> for XcbCanvas<'a> {
self.conn.flush();
}
fn set_pixel(&mut self, c :Coordinate<T>, 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

Loading…
Cancel
Save