Something went wrong. Try again.
My attempt at doing Pathtracer in a weekend during work time
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071use std::path::Path;
use maths::{ camera::Camera, ray::Ray, vec3::{Colour, Point3, Vec3}, viewport::Viewport,};use ppm::PPM;use tracing::{info, Level};
fn main() { tracing::subscriber::set_global_default( tracing_subscriber::FmtSubscriber::builder() .with_max_level(Level::INFO) .finish(), ) .unwrap();
let viewport = Viewport::new(800); let camera = Camera::new(1.0, Point3::new(0., 0., 0.));
let (v_width, v_height) = viewport.calculate_viewport();
let origin = Point3::new(0., 0., 0.); let horizontal = Vec3::new(v_width, 0.0, 0.0); let vertical = Vec3::new(0.0, v_height, 0.0); let lower_left_corner = origin - horizontal / 2.0 - vertical / 2.0 - Vec3::new(0.0, 0.0, camera.focal_length);
let mut output = PPM::new(viewport.image_width, viewport.image_height).unwrap();
for height in (0..viewport.image_height).rev() { for width in (0..viewport.image_width).rev() { let u = (width as f64) / ((viewport.image_width - 1) as f64); let v = (height as f64) / ((viewport.image_height - 1) as f64);
let ray = Ray::new( origin, lower_left_corner + u * horizontal + v * vertical - origin, );
let pixel_color = ray_colour(&ray);
output.buffer.push(pixel_color); } }
output.write_to_file(Path::new("/tmp/hi.ppm")).unwrap();}
fn ray_colour(ray: &Ray) -> Colour { if hit_sphere(Point3::new(0., 0., -1.), 0.5, ray) { return Colour::new(1., 0., 0.); }
let unit_direction = ray.direction.unit_vector(); let y_blend = 0.5 * (unit_direction.y + 1.0); (1.0 - y_blend) * Colour::new(1.0, 1.0, 1.0) + y_blend * Colour::new(0.5, 0.7, 1.0)}
fn hit_sphere(center: Point3, radius: f64, ray: &Ray) -> bool { let oc = ray.origin - center; let a = ray.direction.dot(&ray.direction); let b = 2.0 * oc.dot(&ray.direction); let c = oc.dot(&oc) - radius * radius; let discriminant = b * b - 4. * a * c;
discriminant >= 0.}