Compare commits
No commits in common. "20325e6a75b0824237b1f55c894501d16f1de247" and "06596a0f768a977b504852d04acc09dd9950bb94" have entirely different histories.
20325e6a75
...
06596a0f76
3 changed files with 128 additions and 238 deletions
136
src/main.rs
136
src/main.rs
|
@ -15,8 +15,6 @@ use point::*;
|
|||
mod model;
|
||||
use model::*;
|
||||
|
||||
const BLACK: TGAColor = TGAColor { bgra: [0u8; 4] };
|
||||
|
||||
const WHITE: TGAColor = TGAColor {
|
||||
bgra: [255, 255, 255, 255],
|
||||
};
|
||||
|
@ -38,18 +36,44 @@ const YELLOW: TGAColor = TGAColor {
|
|||
};
|
||||
|
||||
fn main() {
|
||||
let w = 640;
|
||||
let h = 640;
|
||||
let w = 800;
|
||||
let h = 800;
|
||||
let mut fb = TGAImage::new(w, h, TGAFormat::RGB);
|
||||
|
||||
let a = Point2i::new(170, 40).with_color(BLUE);
|
||||
let b = Point2i::new(550, 390).with_color(GREEN);
|
||||
let c = Point2i::new(230, 590).with_color(RED);
|
||||
render_head(&mut fb);
|
||||
}
|
||||
|
||||
let t = Triangle2i::new(a, b, c);
|
||||
t.render_lines(20, BLACK, &mut fb);
|
||||
fn _render_diablo(fb: &mut TGAImage) {
|
||||
let model = Model::from_obj("diablo3_pose.obj");
|
||||
model.render_wireframe(fb);
|
||||
fb.write_file("diablo.tga", true, true).unwrap();
|
||||
}
|
||||
|
||||
fb.write_file("triangle.tga", true).unwrap();
|
||||
fn render_head(fb: &mut TGAImage) {
|
||||
let model = Model::from_obj("head.obj");
|
||||
model.render_triangles(fb);
|
||||
fb.write_file("head.tga", true, true).unwrap();
|
||||
}
|
||||
fn _baseline(framebuffer: &mut TGAImage) {
|
||||
let ax = 7;
|
||||
let ay = 3;
|
||||
let bx = 12;
|
||||
let by = 37;
|
||||
let cx = 62;
|
||||
let cy = 53;
|
||||
|
||||
framebuffer.set(ax, ay, WHITE);
|
||||
framebuffer.set(bx, by, WHITE);
|
||||
framebuffer.set(cx, cy, WHITE);
|
||||
|
||||
let a = Point2i::newu(ax, ay);
|
||||
let b = Point2i::newu(bx, by);
|
||||
let c = Point2i::newu(cx, cy);
|
||||
|
||||
line(a, b, BLUE, framebuffer);
|
||||
line(c, b, GREEN, framebuffer);
|
||||
line(c, a, YELLOW, framebuffer);
|
||||
line(a, c, RED, framebuffer);
|
||||
}
|
||||
|
||||
fn line(mut a: Point2i, mut b: Point2i, color: TGAColor, mut fb: &mut TGAImage) {
|
||||
|
@ -113,96 +137,28 @@ impl Triangle2i {
|
|||
return;
|
||||
}
|
||||
let it = 1.0 / total_area;
|
||||
(bb.xmin()..=bb.xmax()).into_par_iter().for_each(|x| {
|
||||
(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 alpha = Triangle2i::new(p, self.b, self.c).signed_area() * it;
|
||||
let beta = Triangle2i::new(p, self.c, self.a).signed_area() * it;
|
||||
let gamma = Triangle2i::new(p, self.a, self.b).signed_area() * it;
|
||||
let a = Triangle2i::new(p, self.a, self.b).signed_area() * it;
|
||||
let b = Triangle2i::new(p, self.b, self.c).signed_area() * it;
|
||||
let c = Triangle2i::new(p, self.c, self.a).signed_area() * it;
|
||||
|
||||
if alpha.is_sign_positive() && beta.is_sign_positive() && gamma.is_sign_positive() {
|
||||
let color = if let Some(ca) = self.a.color
|
||||
&& let Some(cb) = self.b.color
|
||||
&& let Some(cc) = self.c.color
|
||||
if a.is_sign_positive()
|
||||
&& b.is_sign_positive()
|
||||
&& c.is_sign_positive()
|
||||
&& let Ok(mut fb) = fb.lock()
|
||||
{
|
||||
color + (ca * alpha) + (cb * beta) + (cc * gamma)
|
||||
} else {
|
||||
color
|
||||
};
|
||||
|
||||
if let Ok(mut fb) = fb.lock() {
|
||||
fb.set(x as u32, y as u32, color);
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn render_lines(&self, thickness: i32, color: TGAColor, fb: &mut TGAImage) {
|
||||
let fb = Arc::new(Mutex::new(fb));
|
||||
let bb = self.bb();
|
||||
let total_area = self.signed_area();
|
||||
if total_area < 1.0 {
|
||||
return;
|
||||
}
|
||||
let it = 1.0 / total_area;
|
||||
(bb.xmin()..=bb.xmax()).into_par_iter().for_each(|x| {
|
||||
(bb.ymin()..=bb.ymax()).into_par_iter().for_each(|y| {
|
||||
let p = Point2i::new(x, y);
|
||||
let alpha = Triangle2i::new(p, self.b, self.c).signed_area() * it;
|
||||
let beta = Triangle2i::new(p, self.c, self.a).signed_area() * it;
|
||||
let gamma = Triangle2i::new(p, self.a, self.b).signed_area() * it;
|
||||
|
||||
if alpha.is_sign_positive() && beta.is_sign_positive() && gamma.is_sign_positive() {
|
||||
let ad = ((self.b.y - self.a.y) * x - (self.b.x - self.a.x) * y
|
||||
+ self.b.x * self.a.y
|
||||
- self.b.y * self.a.x)
|
||||
.abs()
|
||||
/ ((self.b.y - self.a.y).pow(2) + (self.b.x - self.a.x).pow(2)).isqrt();
|
||||
let bd = ((self.c.y - self.b.y) * x - (self.c.x - self.b.x) * y
|
||||
+ self.c.x * self.b.y
|
||||
- self.c.y * self.b.x)
|
||||
.abs()
|
||||
/ ((self.c.y - self.b.y).pow(2) + (self.c.x - self.b.x).pow(2)).isqrt();
|
||||
let cd = ((self.a.y - self.c.y) * x - (self.a.x - self.c.x) * y
|
||||
+ self.a.x * self.c.y
|
||||
- self.a.y * self.c.x)
|
||||
.abs()
|
||||
/ ((self.a.y - self.c.y).pow(2) + (self.a.x - self.c.x).pow(2)).isqrt();
|
||||
|
||||
if ad <= thickness || bd <= thickness || cd <= thickness {
|
||||
let color = if let Some(ca) = self.a.color
|
||||
&& let Some(cb) = self.b.color
|
||||
&& let Some(cc) = self.c.color
|
||||
{
|
||||
color + (ca * alpha) + (cb * beta) + (cc * gamma)
|
||||
} else {
|
||||
color
|
||||
};
|
||||
|
||||
if let Ok(mut fb) = fb.lock() {
|
||||
fb.set(x as u32, y as u32, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn centroid(&self) -> Point2i {
|
||||
let x = self.a.x / 3 + self.b.x / 3 + self.c.x / 3;
|
||||
let y = self.a.y / 3 + self.b.y / 3 + self.c.y / 3;
|
||||
let has_color = self.a.color.is_some() || self.b.color.is_some() || self.c.color.is_some();
|
||||
let denom = 1.0 / 3.0;
|
||||
let color = if has_color {
|
||||
let c = self.a.color.unwrap_or(BLACK) * denom
|
||||
+ self.b.color.unwrap_or(BLACK) * denom
|
||||
+ self.c.color.unwrap_or(BLACK) * denom;
|
||||
Some(c)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
Point2i { x, y, color }
|
||||
pub fn render_lines(&self, color: TGAColor, fb: &mut TGAImage) {
|
||||
line(self.a, self.b, color, fb);
|
||||
line(self.b, self.c, color, fb);
|
||||
line(self.c, self.a, color, fb);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
57
src/point.rs
57
src/point.rs
|
@ -1,60 +1,33 @@
|
|||
use std::{
|
||||
ops::{Add, Deref, DerefMut, Div, Mul, Sub},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use std::ops::{Add, Deref, DerefMut, Div, Mul, Sub};
|
||||
|
||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||
|
||||
use crate::{
|
||||
YELLOW,
|
||||
tga::{TGAColor, TGAImage},
|
||||
};
|
||||
use crate::tga::TGAColor;
|
||||
|
||||
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Point2i {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub color: Option<TGAColor>,
|
||||
pub c: Option<TGAColor>,
|
||||
}
|
||||
|
||||
impl Point2i {
|
||||
pub fn new(x: i32, y: i32) -> Self {
|
||||
Self { x, y, color: None }
|
||||
Self { x, y, c: None }
|
||||
}
|
||||
|
||||
pub fn newu(x: u32, y: u32) -> Self {
|
||||
Self {
|
||||
x: x as i32,
|
||||
y: y as i32,
|
||||
color: None,
|
||||
c: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_color(self, color: TGAColor) -> Self {
|
||||
Self {
|
||||
color: Some(color),
|
||||
c: Some(color),
|
||||
..self
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&self, radius: i32, fb: &mut TGAImage) {
|
||||
let xmin = self.x - radius;
|
||||
let xmax = self.x + radius;
|
||||
let ymin = self.y - radius;
|
||||
let ymax = self.y + radius;
|
||||
let r2 = radius.pow(2);
|
||||
let fb = Arc::new(Mutex::new(fb));
|
||||
(ymin..=ymax).into_par_iter().for_each(|y| {
|
||||
(xmin..=xmax).into_par_iter().for_each(|x| {
|
||||
let d2 = (x - self.x).pow(2) + (y - self.y).pow(2);
|
||||
if d2 <= r2
|
||||
&& let Ok(mut fb) = fb.lock()
|
||||
{
|
||||
fb.set(x as u32, y as u32, self.color.unwrap_or(YELLOW));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Point2i {
|
||||
|
@ -64,12 +37,7 @@ impl Add for Point2i {
|
|||
Self {
|
||||
x: self.x + rhs.x,
|
||||
y: self.y + rhs.y,
|
||||
color: if self.color.is_none() {
|
||||
rhs.color
|
||||
} else {
|
||||
self.color
|
||||
.map(|c| if let Some(oc) = rhs.color { c + oc } else { c })
|
||||
},
|
||||
c: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -81,12 +49,7 @@ impl Sub for Point2i {
|
|||
Self {
|
||||
x: self.x - rhs.x,
|
||||
y: self.y - rhs.y,
|
||||
color: if self.color.is_none() {
|
||||
rhs.color
|
||||
} else {
|
||||
self.color
|
||||
.map(|c| if let Some(oc) = rhs.color { c - oc } else { c })
|
||||
},
|
||||
c: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -98,7 +61,7 @@ impl Mul for Point2i {
|
|||
Self {
|
||||
x: self.x * rhs.x,
|
||||
y: self.y * rhs.y,
|
||||
color: None,
|
||||
c: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -180,7 +143,7 @@ impl From<Point3f> for Point2i {
|
|||
Self {
|
||||
x: p.x().round_ties_even() as i32,
|
||||
y: p.y().round_ties_even() as i32,
|
||||
color: None,
|
||||
c: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
165
src/tga.rs
165
src/tga.rs
|
@ -1,7 +1,7 @@
|
|||
use std::{
|
||||
fs::File,
|
||||
io::{Read, Write},
|
||||
ops::{Add, Deref, DerefMut, Mul, Sub},
|
||||
ops::{Deref, DerefMut},
|
||||
};
|
||||
|
||||
const HEADER_SIZE: usize = 18;
|
||||
|
@ -73,49 +73,6 @@ impl From<ColorBuf> for TGAColor {
|
|||
}
|
||||
}
|
||||
|
||||
impl Add for TGAColor {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
bgra: [
|
||||
self[0].saturating_add(rhs[0]),
|
||||
self[1].saturating_add(rhs[1]),
|
||||
self[2].saturating_add(rhs[2]),
|
||||
self[3].saturating_add(rhs[3]),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub for TGAColor {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self::Output {
|
||||
Self {
|
||||
bgra: [
|
||||
self[0].saturating_sub(rhs[0]),
|
||||
self[1].saturating_sub(rhs[1]),
|
||||
self[2].saturating_sub(rhs[2]),
|
||||
self[3].saturating_sub(rhs[3]),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<f32> for TGAColor {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, rhs: f32) -> Self::Output {
|
||||
let b = (self.b() as f32 * rhs).round_ties_even() as u8;
|
||||
let g = (self.g() as f32 * rhs).round_ties_even() as u8;
|
||||
let r = (self.r() as f32 * rhs).round_ties_even() as u8;
|
||||
let a = (self.a() as f32 * rhs).round_ties_even() as u8;
|
||||
|
||||
Self { bgra: [b, g, r, a] }
|
||||
}
|
||||
}
|
||||
|
||||
impl TGAColor {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
|
@ -123,20 +80,20 @@ impl TGAColor {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn b(&self) -> u8 {
|
||||
self[0]
|
||||
pub fn b(&mut self) -> &mut u8 {
|
||||
&mut self[0]
|
||||
}
|
||||
|
||||
pub fn g(&self) -> u8 {
|
||||
self[1]
|
||||
pub fn g(&mut self) -> &mut u8 {
|
||||
&mut self[1]
|
||||
}
|
||||
|
||||
pub fn r(&self) -> u8 {
|
||||
self[2]
|
||||
pub fn r(&mut self) -> &mut u8 {
|
||||
&mut self[2]
|
||||
}
|
||||
|
||||
pub fn a(&self) -> u8 {
|
||||
self[3]
|
||||
pub fn a(&mut self) -> &mut u8 {
|
||||
&mut self[3]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -222,7 +179,7 @@ impl TGAImage {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn write_file(&mut self, file: &str, do_vflip: bool) -> IoResult {
|
||||
pub fn write_file(&mut self, file: &str, do_vflip: bool, do_rle: bool) -> IoResult {
|
||||
let mut file = std::fs::File::create(file)?;
|
||||
|
||||
let mut header = TGAHeader::default();
|
||||
|
@ -231,8 +188,10 @@ impl TGAImage {
|
|||
header.height = self.height as u16;
|
||||
|
||||
header.datatype_code = match self.format {
|
||||
TGAFormat::Grayscale => 11,
|
||||
_ => 10,
|
||||
TGAFormat::Grayscale if do_rle => 11,
|
||||
TGAFormat::Grayscale => 3,
|
||||
_ if do_rle => 10,
|
||||
_ => 2,
|
||||
};
|
||||
|
||||
header.image_descriptor = if do_vflip { 0x00 } else { 0x20 };
|
||||
|
@ -241,49 +200,10 @@ impl TGAImage {
|
|||
|
||||
file.write_all(&mut header)?;
|
||||
|
||||
let max_chunk_len = 128;
|
||||
let num_pixels = (self.width * self.height) as usize;
|
||||
let mut current_pixel = 0;
|
||||
|
||||
let bpp = self.bytes_per_pixel() as usize;
|
||||
|
||||
while current_pixel < num_pixels {
|
||||
let chunk_start = current_pixel * bpp;
|
||||
let mut current_byte = current_pixel * bpp;
|
||||
|
||||
let mut run_length = 1;
|
||||
let mut is_raw = true;
|
||||
|
||||
while (current_pixel + run_length) < num_pixels && run_length < max_chunk_len {
|
||||
let mut succ_eq = true;
|
||||
let mut t = 0;
|
||||
while succ_eq && t < bpp {
|
||||
succ_eq = self.data[current_byte + t] == self.data[current_byte + t + bpp];
|
||||
t += 1;
|
||||
}
|
||||
current_byte += bpp;
|
||||
if 1 == run_length {
|
||||
is_raw = !succ_eq;
|
||||
}
|
||||
if is_raw && succ_eq {
|
||||
run_length -= 1;
|
||||
break;
|
||||
}
|
||||
if !(is_raw || succ_eq) {
|
||||
break;
|
||||
}
|
||||
run_length += 1;
|
||||
}
|
||||
current_pixel += run_length;
|
||||
let mut out = if is_raw {
|
||||
[run_length as u8 - 1]
|
||||
if do_rle {
|
||||
self.write_rle_file(&mut file)?;
|
||||
} else {
|
||||
[run_length as u8 + 127]
|
||||
};
|
||||
|
||||
file.write_all(&mut out)?;
|
||||
let chunk_end = chunk_start + if is_raw { run_length * bpp } else { bpp };
|
||||
file.write_all(&mut self.data[chunk_start..chunk_end])?;
|
||||
file.write_all(&mut self.data)?;
|
||||
}
|
||||
|
||||
file.flush()?;
|
||||
|
@ -389,4 +309,55 @@ impl TGAImage {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_rle_file(&mut self, file: &mut File) -> IoResult {
|
||||
let max_chunk_len = 128;
|
||||
let num_pixels = (self.width * self.height) as usize;
|
||||
let mut current_pixel = 0;
|
||||
|
||||
let bpp = self.bytes_per_pixel() as usize;
|
||||
|
||||
while current_pixel < num_pixels {
|
||||
let chunk_start = current_pixel * bpp;
|
||||
let mut current_byte = current_pixel * bpp;
|
||||
|
||||
let mut run_length = 1;
|
||||
let mut is_raw = true;
|
||||
|
||||
while (current_pixel + run_length) < num_pixels && run_length < max_chunk_len {
|
||||
let mut succ_eq = true;
|
||||
let mut t = 0;
|
||||
while succ_eq && t < bpp {
|
||||
succ_eq = self.data[current_byte + t] == self.data[current_byte + t + bpp];
|
||||
t += 1;
|
||||
}
|
||||
current_byte += bpp;
|
||||
if 1 == run_length {
|
||||
is_raw = !succ_eq;
|
||||
}
|
||||
if is_raw && succ_eq {
|
||||
run_length -= 1;
|
||||
break;
|
||||
}
|
||||
if !(is_raw || succ_eq) {
|
||||
break;
|
||||
}
|
||||
run_length += 1;
|
||||
}
|
||||
current_pixel += run_length;
|
||||
let mut out = if is_raw {
|
||||
[run_length as u8 - 1]
|
||||
} else {
|
||||
[run_length as u8 + 127]
|
||||
};
|
||||
|
||||
file.write_all(&mut out)?;
|
||||
let chunk_end = chunk_start + if is_raw { run_length * bpp } else { bpp };
|
||||
file.write_all(&mut self.data[chunk_start..chunk_end])?;
|
||||
}
|
||||
|
||||
file.flush()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue