Skip to content

Commit c12b9e7

Browse files
committed
clean
1 parent b85ac89 commit c12b9e7

File tree

2 files changed

+26
-58
lines changed

2 files changed

+26
-58
lines changed

src/base/context.rs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::base::SimpleMesh;
21
use nalgebra::Matrix4;
3-
use std::f32;
2+
use crate::base::SimpleMesh;
3+
use std::{f32};
44

55
pub struct Context {
66
pub utransform: Matrix4<f32>,
@@ -15,12 +15,15 @@ impl Context {
1515
//TODO: Make this a constant struct
1616
Context {
1717
utransform: Matrix4::new(
18-
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
18+
1.0, 0.0, 0.0, 0.0,
19+
0.0, 1.0, 0.0, 0.0,
20+
0.0, 0.0, 1.0, 0.0,
21+
0.0, 0.0, 0.0, 1.0,
1922
),
2023
width: 0,
2124
height: 0,
2225
frame_buffer: vec![],
23-
z_buffer: vec![],
26+
z_buffer: vec![]
2427
}
2528
}
2629
pub fn clear(&mut self) {
@@ -36,14 +39,11 @@ impl Context {
3639
for pixel in &self.frame_buffer {
3740
x.push_str(pixel);
3841
}
39-
println!(
40-
"{}{}{}",
41-
termion::clear::All,
42-
termion::cursor::Goto(1, 1),
43-
x
44-
);
42+
println!("{}{}{}", termion::clear::All, termion::cursor::Goto(1, 1), x);
4543
}
46-
pub fn update(&mut self, mut old_size: (u16, u16), meshes: &[SimpleMesh]) {
44+
pub fn update(&mut self, mut old_size: (u16, u16),
45+
meshes: &[SimpleMesh],
46+
) {
4747
let terminal_size = termion::terminal_size().unwrap(); // Temporary size
4848
if old_size != terminal_size {
4949
// Check if the size changed
@@ -57,23 +57,10 @@ impl Context {
5757
.max(mesh.bounding_box.max.z);
5858
}
5959
scale = f32::from(old_size.1).min(f32::from(old_size.0) / 2.0) / scale / 2.0; // Constrain to width and height, whichever is smaller
60-
let t = Matrix4::new(
61-
scale,
62-
0.0,
63-
0.0,
64-
f32::from(old_size.0) / 4.0, // X translation is divided by 4 because there's a 1 char space between charxels
65-
0.0,
66-
-scale,
67-
0.0,
68-
f32::from(old_size.1) / 2.0, // Y translation is divided by 2 to center
69-
0.0,
70-
0.0,
71-
scale,
72-
0.0,
73-
0.0,
74-
0.0,
75-
0.0,
76-
1.0,
60+
let t = Matrix4::new(scale, 0.0, 0.0, f32::from(old_size.0) / 4.0, // X translation is divided by 4 because there's a 1 char space between charxels
61+
0.0, -scale, 0.0, f32::from(old_size.1) / 2.0, // Y translation is divided by 2 to center
62+
0.0, 0.0, scale, 0.0,
63+
0.0, 0.0, 0.0, 1.0,
7764
);
7865
self.utransform = t;
7966
self.width = (old_size.0) as usize;

src/base/rasterizer.rs

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::base::{Context, SimpleMesh, Triangle};
2+
use termion::{color};
23
use nalgebra::{Matrix4, Vector4};
3-
use termion::color;
44

55
pub fn default_shader(shade: f32) -> String {
66
let string = if shade <= 0.20 {
@@ -37,44 +37,25 @@ fn orient_triangle(triangle: &Triangle) -> f32 {
3737
}
3838

3939
// Writes multiple meshes to context
40-
pub fn draw_mesh<F>(context: &mut Context, mesh: &SimpleMesh, transform: Matrix4<f32>, shader: F)
41-
where
42-
F: Fn(f32) -> String,
43-
{
40+
pub fn draw_mesh<F>(context: &mut Context, mesh: &SimpleMesh, transform: Matrix4<f32>, shader: F) where F: Fn(f32) -> String {
4441
for triangle in &mesh.triangles {
4542
draw_triangle(context, &triangle, transform, &shader);
4643
}
4744
}
4845

4946
fn bg(src: String, bg: (u8, u8, u8)) -> String {
50-
format!(
51-
"{}{}{}{}",
52-
color::Fg(color::Rgb(bg.0, bg.1, bg.2)),
53-
color::Bg(color::Rgb(25, 25, 25)),
54-
src,
55-
color::Fg(color::Reset)
56-
)
47+
format!("{}{}{}{}", color::Fg(color::Rgb(bg.0, bg.1, bg.2)),
48+
color::Bg(color::Rgb(25,25,25)),
49+
src,
50+
color::Fg(color::Reset))
5751
}
5852

59-
pub fn draw_triangle<F>(
60-
context: &mut Context,
61-
triangle: &Triangle,
62-
transform: Matrix4<f32>,
63-
shader: F,
64-
) where
65-
F: Fn(f32) -> String,
66-
{
53+
pub fn draw_triangle<F>(context: &mut Context, triangle: &Triangle, transform: Matrix4<f32>, shader: F) where F: Fn(f32) -> String {
6754
let mut dist_triangle = triangle.clone();
6855
dist_triangle.mul(context.utransform * transform);
6956
let aabb = dist_triangle.to_aabb(); // Calculate triangle bounds
70-
let mins: (usize, usize) = (
71-
aabb.min[0].max(1.0).ceil() as usize,
72-
aabb.min[1].max(1.0).ceil() as usize,
73-
);
74-
let maxs: (usize, usize) = (
75-
(aabb.max[0] * 2.0).min((context.width - 1) as f32).ceil() as usize,
76-
aabb.max[1].min((context.height - 1) as f32).ceil() as usize,
77-
);
57+
let mins: (usize, usize) = (aabb.min[0].max(1.0).ceil() as usize, aabb.min[1].max(1.0).ceil() as usize);
58+
let maxs: (usize, usize) = ((aabb.max[0] * 2.0).min((context.width - 1) as f32).ceil() as usize, aabb.max[1].min((context.height - 1) as f32).ceil() as usize);
7859
let a = 1.0 / orient_triangle(&dist_triangle);
7960

8061
for y in mins.1..maxs.1 {
@@ -87,13 +68,13 @@ pub fn draw_triangle<F>(
8768
let pixel_shade = dist_triangle.normal().z * a * (w0 + w1 + w2);
8869
let z = dist_triangle.v1[2]
8970
+ a * (w1 * (dist_triangle.v2[2] - dist_triangle.v1[2])
90-
+ w2 * (dist_triangle.v3[2] - dist_triangle.v1[2]));
71+
+ w2 * (dist_triangle.v3[2] - dist_triangle.v1[2]));
9172
let id = y * context.width + x * 2;
9273
if z < context.z_buffer[id] {
9374
context.z_buffer[id] = z;
9475
let pixel = bg(shader(pixel_shade), dist_triangle.color);
9576
context.frame_buffer[id] = (&pixel).to_string();
96-
context.frame_buffer[id + 1] = (&pixel).to_string();
77+
context.frame_buffer[id+1] = (&pixel).to_string();
9778
}
9879
}
9980
}

0 commit comments

Comments
 (0)