Skip to content

Commit

Permalink
Preview picture in Ascii art
Browse files Browse the repository at this point in the history
  • Loading branch information
fremag committed Feb 2, 2024
1 parent e8343eb commit c4fb6a0
Show file tree
Hide file tree
Showing 12 changed files with 1,145 additions and 80 deletions.
1,160 changes: 1,108 additions & 52 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions ray-tracer-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ edition = "2021"
[dependencies]
rand = { version = "0.8.5", features = [] }
ray-tracer-lib = { version = "0.1.0", path = "../ray-tracer-lib" }
artem = "2.0.2"
image = { version = "0.24.8", features = [] }
27 changes: 17 additions & 10 deletions ray-tracer-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ fn main() {
}

fn render_all() {
CloverScene::new(0.0, 0.0, -9.0).render(800, 600, "e:\\tmp\\clover.png");
CloverTriangleScene::new(0.0, 0.0, -9.0).render(800, 600, "e:\\tmp\\clover_triangle.png");
CylinderScene{}.render(400, 400, "e:\\tmp\\cylinders_scene.png");
ConeScene{}.render(400, 400, "e:\\tmp\\cones_scene.png");
CubeScene{}.render(400, 400, "e:\\tmp\\cubes_scene.png");
GroupScene{}.render(400, 400, "e:\\tmp\\group_scene.png");
PatternsScene{}.render(400, 400, "e:\\tmp\\all_patterns_scene.png");
StripePatternScene{}.render(400, 400, "e:\\tmp\\pattern_stripe_scene.png");
BasicRefractionScene{}.render(640, 400, "e:\\tmp\\basic_refraction_sphere_scene.png");
RefractionSphereScene{}.render(400, 400, "e:\\tmp\\refraction_sphere_scene.png");
render(&CloverScene::new(0.0, 0.0, -9.0), 800, 600, "e:\\tmp\\clover.png");
render(&CloverTriangleScene::new(0.0, 0.0, -9.0),800, 600, "e:\\tmp\\clover_triangle.png");
render(&CylinderScene{},400, 400, "e:\\tmp\\cylinders_scene.png");
render(&ConeScene{},400, 400, "e:\\tmp\\cones_scene.png");
render(&CubeScene{},400, 400, "e:\\tmp\\cubes_scene.png");
render(&GroupScene{},400, 400, "e:\\tmp\\group_scene.png");
render(&PatternsScene{},400, 400, "e:\\tmp\\all_patterns_scene.png");
render(&StripePatternScene{},400, 400, "e:\\tmp\\pattern_stripe_scene.png");
render(&BasicRefractionScene{},640, 400, "e:\\tmp\\basic_refraction_sphere_scene.png");
render(&RefractionSphereScene{},400, 400, "e:\\tmp\\refraction_sphere_scene.png");
}

fn render(scene : &dyn Scene, h_size : usize, v_size : usize, file_path: &str) {
scene.render(h_size, v_size, file_path);
let image = image::open(file_path).expect("Failed to open image");
let ascii_art = artem::convert(image, &artem::config::ConfigBuilder::new().build());
println!("{}", ascii_art);
}
8 changes: 4 additions & 4 deletions ray-tracer-cli/src/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub trait Scene {
fn get_world(&self) -> World;
fn get_camera(&self, h_size: usize, v_size: usize) -> Camera;

fn init_camera(h_size: usize, v_size: usize
fn init_camera(&self, h_size: usize, v_size: usize
, from_x: Float, from_y: Float, from_z: Float
, look_x: Float, look_y: Float, look_z: Float) -> Camera {
let mut camera = Camera::new(h_size, v_size, PI / 3.0);
Expand All @@ -29,12 +29,12 @@ pub trait Scene {
let canvas = camera.render(&world, file_path);
let result = canvas.save_png(file_path);
match result {
Ok(_) => { print!("Ok") }
Err(error) => { print!("Error: {}", error) }
Ok(_) => { println!("Ok") }
Err(error) => { println!("Error: {}", error) }
}
}

fn init_world(add_floor : bool) -> World {
fn init_world(&self, add_floor : bool) -> World {
let mut world = World::new();
let lights = vec!(
PointLight::new(point(0.0, 10.5, -10.0), Color::white()),
Expand Down
2 changes: 1 addition & 1 deletion ray-tracer-cli/src/scenes/clover_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Scene for CloverScene {
fn get_world(&self) -> World {
const R1: Float = 1.0;
const R2: Float = 0.25;
let mut world = Self::init_world(false);
let mut world = self.init_world(false);
fn path_clover(t: Float) -> Tuple {
let x = R1 * ((2.0 * PI * t).cos() + 2.0 * (2.0 * PI * 2.0 * t).cos());
let y = R1 * ((2.0 * PI * t).sin() - 2.0 * (2.0 * PI * 2.0 * t).sin());
Expand Down
2 changes: 1 addition & 1 deletion ray-tracer-cli/src/scenes/clover_triangles_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Scene for CloverTriangleScene {
fn get_world(&self) -> World {
const R1: Float = 1.0;
const R2: Float = 0.25;
let mut world = Self::init_world(false);
let mut world = self.init_world(false);
fn path_clover(t: Float) -> Tuple {
let x = R1 * ((2.0 * PI * t).cos() + 2.0 * (2.0 * PI * 2.0 * t).cos());
let y = R1 * ((2.0 * PI * t).sin() - 2.0 * (2.0 * PI * 2.0 * t).sin());
Expand Down
4 changes: 2 additions & 2 deletions ray-tracer-cli/src/tests/projectile_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ pub mod tests {

let result = canvas.save("e:\\tmp\\projectile.ppm");
match result {
Ok(_) => { print!("Ok") }
Err(error) => { print!("Error: {}", error) }
Ok(_) => { println!("Ok") }
Err(error) => { println!("Error: {}", error) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions ray-tracer-lib/src/tests/camera_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ mod tests {
let canvas = camera.render(&world, file_path);
let result = canvas.save(file_path);
match result {
Ok(_) => { print!("Ok")}
Err(error) => { print!("Error: {}", error)}
Ok(_) => { println!("Ok")}
Err(error) => { println!("Error: {}", error)}
}
}
}
4 changes: 2 additions & 2 deletions ray-tracer-lib/src/tests/intersection_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ mod tests {

let result = canvas.save("e:\\tmp\\sphere_silhouette.ppm");
match result {
Ok(_) => { print!("Ok") }
Err(error) => { print!("Error: {}", error) }
Ok(_) => { println!("Ok") }
Err(error) => { println!("Error: {}", error) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions ray-tracer-lib/src/tests/material_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ mod tests {

let result = canvas.save("e:\\tmp\\phong.ppm");
match result {
Ok(_) => { print!("Ok") }
Err(error) => { print!("Error: {}", error) }
Ok(_) => { println!("Ok") }
Err(error) => { println!("Error: {}", error) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions ray-tracer-lib/src/tests/plane_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ mod tests {
let canvas = camera.render(&world, file_path);
let result = canvas.save(file_path);
match result {
Ok(_) => { print!("Ok") }
Err(error) => { print!("Error: {}", error) }
Ok(_) => { println!("Ok") }
Err(error) => { println!("Error: {}", error) }
}
}
}
4 changes: 2 additions & 2 deletions ray-tracer-lib/src/tests/transform_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ mod tests {

let result = canvas.save("e:\\tmp\\clock.ppm");
match result {
Ok(_) => { print!("Ok") }
Err(error) => { print!("Error: {}", error) }
Ok(_) => { println!("Ok") }
Err(error) => { println!("Error: {}", error) }
}
}

Expand Down

0 comments on commit c4fb6a0

Please sign in to comment.