added antialiasing

This commit is contained in:
2023-01-29 20:06:49 +01:00
parent 7a4dd5a1b9
commit 95f5132d1d
9 changed files with 637 additions and 566 deletions

View File

@@ -4,34 +4,26 @@ pub mod ray;
pub mod sphere;
pub mod hittable;
pub mod hittable_list;
pub mod rtweekend;
pub mod camera;
use std::{io::{BufWriter, Write}, fs::File};
use camera::Camera;
use crate::{
vec3::*,
color::*,
sphere::*,
rtweekend::*,
hittable_list::*,
vec3::*,
ray::*,
hittable::*,
};
fn hit_sphere(center: &Point3, radius: f64, r: &Ray) -> f64 {
let oc = r.origin() - *center;
let a = r.direction().length_squared();
let half_b = oc.dot(&r.direction());
let c = oc.length_squared() - radius*radius;
let discriminant = half_b*half_b - a*c;
if discriminant < 0.0 {
-1.0
} else {
(-half_b - discriminant.sqrt()) / a
}
}
fn ray_color(r: &Ray) -> Color {
let t = hit_sphere(&Point3::new(0.0, 0.0, -1.0), 0.5, r);
if t > 0.0 {
let n = (r.at(t) - Vec3::new(0.0, 0.0, -1.0)).unit_vector();
return 0.5 * Color::new(n.x() + 1.0, n.y() + 1.0, n.z() + 1.0);
fn ray_color(r: &Ray, world: &mut dyn Hittable) -> Color {
let mut rec = HitRecord::new_empty();
if world.hit(r, 0.0, INFINITY, &mut rec) {
return 0.5 * (rec.normal + Color::new(1.0, 1.0, 1.0))
}
let unit_direction: Vec3 = r.direction().unit_vector();
@@ -43,37 +35,39 @@ fn ray_color(r: &Ray) -> Color {
fn main() {
// Image
let aspect_ratio: f64 = 1.0 / 1.0;
let image_width: i32 = 400;
let image_width: i32 = 512;
let image_height: i32 = (image_width as f64 / aspect_ratio as f64) as i32;
let samples_per_pixel = 100;
// World
let mut world = HittableList::new_empty();
world.add(Box::new(Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5)));
world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)));
// Camera
let viewport_height: f64 = 2.0;
let viewport_width: f64 = aspect_ratio * viewport_height;
let focal_length: f64 = 1.0;
let origin = Point3::new(0.0,0.0,0.0);
let horizontal = Vec3::new(viewport_width, 0.0, 0.0);
let vertical = Vec3::new(0.0, viewport_height, 0.0);
let lower_left_corner = origin - horizontal/2.0 - vertical/2.0 - Vec3::new(0.0,0.0, focal_length);
let mut cam = Camera::new();
// Render
let mut file = BufWriter::new(File::create("img.ppm").expect("File creation failed"));
writeln!(file, "P3\n{image_width} {image_height}\n255").expect("Error while writing Magic Byte");
write!(file, "P3\n{image_width} {image_height}\n255").expect("Error while writing Magic Byte");
for j in 0..image_width {
println!("Rows remaining: {}", image_width - j);
for j in (0..image_width).rev() {
write!(file, "\n").expect("grr");
for i in 0..image_height {
let u: f64 = i as f64 / image_width as f64;
let v: f64 = j as f64 / image_height as f64;
let r = Ray::new(origin, lower_left_corner + u * horizontal + v * vertical - origin);
let pixel_color = ray_color(&r);
write_color(&mut file, pixel_color);
let mut pixel_color = Color::new(0.0, 0.0, 0.0);
for s in 0..samples_per_pixel {
let u = (i as f64 + random_f64()) / (image_width - 1) as f64;
let v = (j as f64 + random_f64()) / (image_height - 1) as f64;
let r: Ray = cam.get_ray(u, v);
pixel_color += ray_color(&r, &mut world)
}
write_color(&mut file, pixel_color, samples_per_pixel);
}
println!("[ {:.2} % ]", (1.0 - ((j + 1) as f64 / image_width as f64)) * 100.0);
}
}