2022-05-05 13:19:56 +02:00
|
|
|
extern crate image;
|
|
|
|
|
2022-05-11 18:38:30 +02:00
|
|
|
mod camera;
|
2022-04-20 18:58:58 +02:00
|
|
|
mod color;
|
2022-04-27 18:27:07 +02:00
|
|
|
mod hittable;
|
2022-05-04 18:21:48 +02:00
|
|
|
mod hittable_list;
|
2022-04-20 18:58:58 +02:00
|
|
|
mod ray;
|
2022-05-04 18:21:48 +02:00
|
|
|
mod utility;
|
2022-05-11 18:38:30 +02:00
|
|
|
mod vec3;
|
|
|
|
|
2022-04-20 18:58:58 +02:00
|
|
|
use std::env;
|
2022-05-11 18:38:30 +02:00
|
|
|
//use std::fs::File;
|
|
|
|
//use std::io::BufWriter;
|
|
|
|
//use std::io::Write;
|
|
|
|
use camera::Camera;
|
|
|
|
use hittable::{HitRecord, Hittable, Sphere};
|
2022-05-04 18:21:48 +02:00
|
|
|
use hittable_list::HittableList;
|
2022-05-11 18:38:30 +02:00
|
|
|
use image::{Rgb, RgbImage};
|
2022-05-11 21:41:41 +02:00
|
|
|
use rayon::prelude::*;
|
2022-05-05 13:19:56 +02:00
|
|
|
use ray::Ray;
|
2022-05-11 18:38:30 +02:00
|
|
|
use vec3::{Color, Point3, Vec3};
|
2022-04-20 18:58:58 +02:00
|
|
|
|
2022-05-04 18:21:48 +02:00
|
|
|
fn ray_color(r: &Ray, world: &HittableList) -> Color {
|
|
|
|
let mut rec = HitRecord::empty();
|
2022-04-20 18:59:29 +02:00
|
|
|
|
2022-05-04 18:21:48 +02:00
|
|
|
if world.hit(r, 0.0, f64::INFINITY, &mut rec) {
|
|
|
|
return 0.5 * (rec.normal + Color::new(1.0, 1.0, 1.0));
|
|
|
|
}
|
2022-04-20 18:58:58 +02:00
|
|
|
let unit_direction = r.direction();
|
2022-04-20 18:59:29 +02:00
|
|
|
let t = 0.5 * (unit_direction.y() + 1.0);
|
|
|
|
return (1.0 - t) * Color::new(1.0, 1.0, 1.0) + t * Color::new(0.5, 0.7, 1.0);
|
2022-04-20 18:58:58 +02:00
|
|
|
}
|
2022-04-12 19:14:11 +02:00
|
|
|
|
2022-04-07 23:04:50 +02:00
|
|
|
fn main() {
|
2022-04-20 18:58:58 +02:00
|
|
|
// File
|
|
|
|
let mut default_file = "image.ppm";
|
|
|
|
|
|
|
|
// Image
|
|
|
|
let aspect_ratio = 16.0 / 9.0;
|
2022-05-04 18:21:48 +02:00
|
|
|
let image_width = 1000;
|
2022-05-05 13:19:56 +02:00
|
|
|
let image_height = (image_width as f64 / aspect_ratio) as u32;
|
2022-05-11 18:38:30 +02:00
|
|
|
let samples_per_pixel = 50_u32;
|
2022-04-20 18:58:58 +02:00
|
|
|
|
2022-05-04 18:21:48 +02:00
|
|
|
// World
|
|
|
|
let mut world = HittableList::new();
|
2022-05-18 16:58:02 +02:00
|
|
|
world.add(Box::<Sphere>::new(Sphere::new(
|
2022-05-11 18:38:30 +02:00
|
|
|
Point3::new(0.0, 0.0, -1.0),
|
|
|
|
0.5,
|
|
|
|
)));
|
2022-05-18 16:58:02 +02:00
|
|
|
world.add(Box::<Sphere>::new(Sphere::new(
|
2022-05-11 18:38:30 +02:00
|
|
|
Point3::new(0.0, -100.5, -150.0),
|
|
|
|
100.0,
|
|
|
|
)));
|
2022-05-18 16:58:02 +02:00
|
|
|
world.add(Box::<Sphere>::new(Sphere::new(
|
2022-05-11 18:38:30 +02:00
|
|
|
Point3::new(1.0, 0.0, -1.5),
|
|
|
|
0.3,
|
|
|
|
)));
|
2022-05-18 16:58:02 +02:00
|
|
|
world.add(Box::<Sphere>::new(Sphere::new(
|
2022-05-11 18:38:30 +02:00
|
|
|
Point3::new(1.0, 1.1, -1.5),
|
|
|
|
0.3,
|
|
|
|
)));
|
2022-05-18 16:58:02 +02:00
|
|
|
world.add(Box::<Sphere>::new(Sphere::new(
|
2022-05-11 18:38:30 +02:00
|
|
|
Point3::new(-1.0, 1.1, -1.5),
|
|
|
|
0.3,
|
|
|
|
)));
|
|
|
|
|
|
|
|
/*
|
|
|
|
for i in -15..15 {
|
|
|
|
for j in -15..15 {
|
2022-05-18 16:58:02 +02:00
|
|
|
world.add(Box::<Sphere>::new(Sphere::new(Point3::new(j as f64/6.0 as f64, i as f64/6.0 as f64, -1.5), 0.05)));
|
2022-05-11 18:38:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2022-05-04 18:21:48 +02:00
|
|
|
|
2022-04-20 18:58:58 +02:00
|
|
|
// Camera
|
2022-05-11 18:38:30 +02:00
|
|
|
let cam = Camera::new();
|
2022-04-20 18:59:29 +02:00
|
|
|
|
2022-04-20 18:58:58 +02:00
|
|
|
// Render
|
|
|
|
let args: Vec<String> = env::args().collect();
|
|
|
|
if args.len() > 1 && args[1] != "" {
|
|
|
|
default_file = &args[1];
|
|
|
|
}
|
2022-05-05 13:19:56 +02:00
|
|
|
|
2022-05-05 13:33:06 +02:00
|
|
|
let mut image = RgbImage::new(image_width, image_height);
|
2022-05-05 13:19:56 +02:00
|
|
|
|
2022-05-05 13:33:06 +02:00
|
|
|
//let mut file = BufWriter::new(File::create(default_file).unwrap());
|
|
|
|
//writeln!(&mut file, "P3\n{image_width} {image_height}\n255\n").unwrap();
|
2022-04-07 23:04:50 +02:00
|
|
|
|
2022-05-18 16:58:02 +02:00
|
|
|
TODO: // world in vec umwandeln
|
2022-05-11 21:41:41 +02:00
|
|
|
|
2022-04-20 18:58:58 +02:00
|
|
|
for j in (0..image_height).rev() {
|
|
|
|
eprint!("\rScanlines remaining: {j:5}");
|
2022-05-18 16:58:02 +02:00
|
|
|
(0..image_width).into_par_iter().for_each(|i| pixel_color_calc(&mut image, &cam, &world, j, i, image_width, image_height, samples_per_pixel));
|
2022-05-11 21:41:41 +02:00
|
|
|
}
|
|
|
|
image.save(default_file).unwrap();
|
|
|
|
eprintln!("\nDone!");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pixel_color_calc(image: &mut RgbImage, cam: &Camera, world: &HittableList, j: u32, i: u32, image_width: u32, image_height: u32, samples_per_pixel: u32) {
|
|
|
|
let mut pixel_color = Color::new(0.0, 0.0, 0.0);
|
2022-05-11 18:38:30 +02:00
|
|
|
for _ in 0..samples_per_pixel {
|
|
|
|
let u = (i as f64 + utility::random_f64()) / (image_width - 1) as f64;
|
|
|
|
let v = (j as f64 + utility::random_f64()) / (image_height - 1) as f64;
|
|
|
|
let r = cam.get_ray(u, v);
|
2022-05-11 21:41:41 +02:00
|
|
|
pixel_color += ray_color(&r, &world);
|
2022-05-11 18:38:30 +02:00
|
|
|
}
|
2022-04-07 23:04:50 +02:00
|
|
|
|
2022-05-05 13:33:06 +02:00
|
|
|
//color::write_color(&mut file, &pixel_color);
|
2022-05-11 18:38:30 +02:00
|
|
|
color::put_color(
|
|
|
|
&mut image,
|
|
|
|
&pixel_color,
|
|
|
|
i,
|
|
|
|
image_height - j - 1,
|
|
|
|
samples_per_pixel,
|
|
|
|
);
|
2022-05-11 21:41:41 +02:00
|
|
|
}
|