Compare commits

...

3 Commits

Author SHA1 Message Date
549707fbb3 fx: json files 2026-05-03 16:06:54 +02:00
10f9c0984d wip: parallelization 2026-05-03 14:58:56 +02:00
eb90c36ae8 refactor: scene deserialization organisation 2026-05-02 18:02:53 +02:00
27 changed files with 475 additions and 431 deletions

1
Cargo.lock generated
View File

@@ -1240,6 +1240,7 @@ dependencies = [
"ops",
"pretty_env_logger",
"rand 0.10.1",
"rayon",
"serde",
"serde_json",
]

View File

@@ -5,11 +5,12 @@ edition = "2024"
[dependencies]
dotenv = "0.15.0"
image = "0.25.10"
image = {version = "0.25.10", features = ["png"]}
is_close = "0.1.3"
log = "0.4.29"
ops = "0.6.0"
pretty_env_logger = "0.5.0"
rand = "0.10.1"
rayon = "1.12.0"
serde = {version = "1.0.228", features = ["derive"]}
serde_json = "1.0.149"

View File

@@ -1,22 +0,0 @@
{
"camera": {
"image_width": 1920,
"image_height": 1080,
"anti_alias_rate": 1,
"max_depth": 10,
"fov": 90.0,
"look_from": { "x": -10, "y": 5, "z": 10 },
"look_at": { "x": 0.0, "y": 0.0, "z": -1.0 },
"vup": { "x": 0.0, "y": 1.0, "z": 0.0 }
},
"materials": [
{ "type": "metal", "albedo": { "x": 0.2, "y": 0.4, "z": 0.8 }, "prob": 1.0, "fuzz": 0.1 }
],
"objects": [
{ "type": "sphere", "center": { "x": 0, "y": 0.7, "z": -0.4 }, "radius": 0.2, "material": 1}
]
}

View File

@@ -1,35 +1,35 @@
{
"filename": "highDef.png",
"image_width": 1920,
"image_height": 1080,
"max_depth": 50,
"camera": {
"image_width": 1920,
"image_height": 1080,
"anti_alias_rate": 23,
"max_depth": 50,
"fov": 20.0,
"look_from": { "x": -10, "y": 5, "z": 10 },
"look_at": { "x": 0.0, "y": 0.0, "z": -1.0 },
"vup": { "x": 0.0, "y": 1.0, "z": 0.0 },
"defocus_blur": false,
"defocus_angle": 2,
"look_from": [-10, 5, 10],
"look_at": [0.0, 0.0, -1.0],
"vup": [0.0, 1.0, 0.0],
"defocus_angle": 0,
"focus_dist": 15.68
},
"materials": [
{ "type": "metal", "albedo": { "x": 0.2, "y": 0.4, "z": 0.8 }, "prob": 1.0, "fuzz": 0.1 },
{ "type": "metal", "albedo": { "x": 0.7, "y": 0.4, "z": 0.2 }, "prob": 1.0, "fuzz": 0.1 },
{ "type": "lambertian", "albedo": { "x": 0.8, "y": 0.8, "z": 0.0 }, "prob": 1.0 },
{ "type": "lambertian", "albedo": { "x": 0.1, "y": 0.2, "z": 0.5 }, "prob": 1.0 },
{ "type": "metal", "albedo": [0.2, 0.4, 0.8 ], "prob": 1.0, "fuzz": 0.1},
{ "type": "metal", "albedo": [0.7, 0.4, 0.2 ], "prob": 1.0, "fuzz": 0.1},
{ "type": "lambertian", "albedo": [0.8, 0.8, 0.0 ], "prob": 1.0},
{ "type": "lambertian", "albedo": [0.1, 0.2, 0.5 ], "prob": 1.0},
{ "type": "dielectric", "refraction_index": 1.5},
{ "type": "dielectric", "refraction_index": 0.67},
{ "type": "metal", "albedo": { "x": 0.8, "y": 0.6, "z": 0.2 }, "prob": 1.0, "fuzz": 1.0 }
{ "type": "metal", "albedo": [0.8, 0.6, 0.2 ], "prob": 1.0, "fuzz": 1.0}
],
"objects": [
{ "type": "sphere", "center": { "x": 0, "y": 0.7, "z": -0.4 }, "radius": 0.2, "material": 0},
{ "type": "sphere", "center": { "x": 0.0, "y": 0.5, "z": -0.8 }, "radius": 0.1, "material": 1},
{ "type": "sphere", "center": { "x": 0.0, "y": -100.5, "z": -1.0 }, "radius": 100.0, "material": 2},
{ "type": "sphere", "center": { "x": 0.0, "y": 0.0, "z": -1.2 }, "radius": 0.5, "material": 3},
{ "type": "sphere", "center": { "x": -1, "y": 0, "z": -1 }, "radius": 0.4, "material": 5},
{ "type": "sphere", "center": { "x": 1, "y": 0, "z": -1 }, "radius": 0.5, "material": 6},
{ "type": "sphere", "center": { "x": 20, "y": 7, "z": -15 }, "radius": 10.5, "material": 0}
{ "type": "sphere", "center": [0, 0.7, -0.4], "radius": 0.2, "material": 0},
{ "type": "sphere", "center": [0.0, 0.5, -0.8], "radius": 0.1, "material": 1},
{ "type": "sphere", "center": [0.0, -100.5, -1.0], "radius": 100.0, "material": 2},
{ "type": "sphere", "center": [0.0, 0.0, -1.2], "radius": 0.5, "material": 3},
{ "type": "sphere", "center": [-1, 0, -1], "radius": 0.4, "material": 5},
{ "type": "sphere", "center": [1, 0, -1], "radius": 0.5, "material": 6},
{ "type": "sphere", "center": [20, 7, -15], "radius": 10.5, "material": 0}
]
}

View File

@@ -1,32 +0,0 @@
{
"camera": {
"anti_alias_rate": 23,
"max_depth": 100,
"fov": 40.0,
"look_from": { "x": -10, "y": 5, "z": 10 },
"look_at": { "x": 0.0, "y": 0.0, "z": -1.0 },
"vup": { "x": 0.0, "y": 1.0, "z": 0.0 }
},
"materials": [
{ "type": "metal", "albedo": { "x": 0.2, "y": 0.4, "z": 0.8 }, "prob": 1.0, "fuzz": 0.1 },
{ "type": "metal", "albedo": { "x": 0.7, "y": 0.4, "z": 0.2 }, "prob": 1.0, "fuzz": 0.1 },
{ "type": "lambertian", "albedo": { "x": 0.8, "y": 0.8, "z": 0.0 }, "prob": 1.0 },
{ "type": "lambertian", "albedo": { "x": 0.1, "y": 0.2, "z": 0.5 }, "prob": 1.0 },
{ "type": "dielectric", "refraction_index": 1.5},
{ "type": "dielectric", "refraction_index": 0.67},
{ "type": "metal", "albedo": { "x": 0.8, "y": 0.6, "z": 0.2 }, "prob": 1.0, "fuzz": 1.0 }
],
"objects": [
{ "type": "sphere", "center": { "x": 0, "y": 0.7, "z": -0.4 }, "radius": 0.2, "material": 0},
{ "type": "sphere", "center": { "x": 0.0, "y": 0.5, "z": -0.8 }, "radius": 0.1, "material": 1},
{ "type": "sphere", "center": { "x": 0.0, "y": -100.5, "z": -1.0 }, "radius": 100.0, "material": 2},
{ "type": "sphere", "center": { "x": 0.0, "y": 0.0, "z": -1.2 }, "radius": 0.5, "material": 3},
{ "type": "sphere", "center": { "x": -1, "y": 0, "z": -1 }, "radius": 0.4, "material": 5},
{ "type": "sphere", "center": { "x": 1, "y": 0, "z": -1 }, "radius": 0.5, "material": 6},
{ "type": "sphere", "center": { "x": 20, "y": 7, "z": -15 }, "radius": 10.5, "material": 0}
]
}

View File

@@ -1,12 +1,13 @@
{
"filename": "output.png",
"image_width": 1920,
"image_height": 1080,
"max_depth": 50,
"camera": {
"image_width": 1920,
"image_height": 1080,
"anti_alias_rate": 23,
"max_depth": 50,
"fov": 90.0,
"look_from": [15, 4, 15],
"look_at": [0.0, 0.0, 0.0],
"fov": 70.0,
"look_from": [-10, 4, 15],
"look_at": [-11.0, 0.0, 0.0],
"vup": [0.0, 1.0, 0.0],
"defocus_angle": 0,
"focus_dist": 15.68
@@ -28,7 +29,8 @@
{ "type": "quad", "p1": [-20, -1, 20], "p2": [-20, -1, -20], "p3": [-20, 20, -20], "p4": [-20, 20, 20], "material": 0},
{ "type": "quad", "p1": [-20, -1, 20], "p2": [20, -1, 20], "p3": [20, -1, -20], "p4": [-20, -1, -20], "material": 0},
{ "type": "quad", "p1": [20, -1, 20], "p2": [20, -1, -20], "p3": [20, 20, -20], "p4": [20, 20, 20], "material": 0},
{ "type": "cube", "p1": [8, 0, 2], "p2": [12, 0, 2], "p3": [12, 4, 2], "p4": [8, 4, 2], "p5": [8, 0, -2], "p6": [12, 0, -2], "p7": [12, 4, -2], "p8": [8, 4, -2], "material": 3}
{ "type": "cube", "p1": [8, 0, 2], "p2": [12, 0, 2], "p3": [12, 4, 2], "p4": [8, 4, 2], "p5": [8, 0, -2], "p6": [12, 0, -2], "p7": [12, 4, -2], "p8": [8, 4, -2], "material": 3},
{ "type": "circle", "center": [-9, 3, 0], "radius": 3, "normal": [0, 1, 0.5], "material": {"type": "metal", "albedo": [0.9, 0.9, 0.9], "prob": 1.0, "fuzz": 0.3}}
]
}

View File

@@ -1,34 +1,23 @@
use std::{f32::consts::PI, sync::Arc};
use std::f32::consts::PI;
use log::info;
use crate::{
objects::{hit::Hit, traits::Hittable},
ray::Ray,
vec3::{Colour, Vec3},
};
use crate::{ray::Ray, vec3::Vec3};
#[derive(Debug)]
pub struct Camera {
// output
image_width: u32,
image_height: u32,
// raytracing
anti_alias_rate: u32,
max_depth: u32,
pixel00_loc: Vec3,
pixel_delta_u: Vec3,
pixel_delta_v: Vec3,
pub anti_alias_rate: u32,
pub pixel00_loc: Vec3,
pub pixel_delta_u: Vec3,
pub pixel_delta_v: Vec3,
// camera
dirty: bool,
fov: f32,
look_from: Vec3,
look_at: Vec3,
pub fov: f32,
pub look_from: Vec3,
pub look_at: Vec3,
vup: Vec3,
defocus_angle: f32,
focus_dist: f32,
pub defocus_angle: f32,
pub focus_dist: f32,
// camera helpers
u: Vec3,
@@ -44,12 +33,9 @@ fn deg_to_rad(deg: f32) -> f32 {
}
impl Camera {
pub fn new(image_width: u32, image_height: u32) -> Self {
pub fn new() -> Self {
Self {
image_width,
image_height,
anti_alias_rate: 1,
max_depth: 10,
dirty: true,
fov: 60.,
pixel00_loc: Vec3::default(),
@@ -69,10 +55,7 @@ impl Camera {
}
pub fn new_full(
image_width: u32,
image_height: u32,
anti_alias_rate: u32,
max_depth: u32,
fov: f32,
look_from: Vec3,
look_at: Vec3,
@@ -81,10 +64,7 @@ impl Camera {
focus_dist: f32,
) -> Self {
Self {
image_width,
image_height,
anti_alias_rate,
max_depth,
pixel00_loc: Vec3::default(),
pixel_delta_u: Vec3::default(),
pixel_delta_v: Vec3::default(),
@@ -103,7 +83,11 @@ impl Camera {
}
}
fn init(&mut self) {
pub fn init(&mut self, w: f32, hi: f32) {
if !self.dirty {
return;
}
// camera
let theta = deg_to_rad(self.fov);
let h = (theta / 2.).tan();
@@ -113,13 +97,13 @@ impl Camera {
// viewport
let viewport_height = 2. * h * self.focus_dist;
let viewport_width = viewport_height * (self.image_width as f32 / self.image_height as f32);
let viewport_width = viewport_height * (w / hi);
let viewport_u = viewport_width * self.u;
let viewport_v = viewport_height * -self.v;
// variables
self.pixel_delta_u = viewport_u / self.image_width as f32;
self.pixel_delta_v = viewport_v / self.image_height as f32;
self.pixel_delta_u = viewport_u / w;
self.pixel_delta_v = viewport_v / hi;
self.pixel00_loc =
self.look_from - (self.focus_dist * self.w) - viewport_u / 2. - viewport_v / 2.;
self.dirty = false;
@@ -131,6 +115,19 @@ impl Camera {
}
}
pub fn get_ray(&self, pixel_tl: Vec3, x: u32, y: u32) -> Ray {
let pixel_loc = pixel_tl
+ (x * self.pixel_delta_u / (self.anti_alias_rate + 1) as f32)
+ (y * self.pixel_delta_v / (self.anti_alias_rate + 1) as f32);
let ray_orig = if self.defocus_angle > 0. {
self.defocus_disk_sample()
} else {
self.look_from
};
let ray_dir = pixel_loc - ray_orig;
Ray::new(ray_orig, ray_dir)
}
pub fn set_fov(&mut self, fov: f32) {
if self.fov != fov {
self.fov = fov;
@@ -138,13 +135,6 @@ impl Camera {
}
}
pub fn set_max_depth(&mut self, depth: u32) {
if self.max_depth != depth {
self.max_depth = depth;
self.dirty = true;
}
}
pub fn set_anti_alias_rate(&mut self, rate: u32) {
if self.anti_alias_rate != rate {
self.anti_alias_rate = rate;
@@ -181,72 +171,8 @@ impl Camera {
}
}
fn defocus_disk_sample(&self) -> Vec3 {
pub fn defocus_disk_sample(&self) -> Vec3 {
let p = Vec3::random_in_unit_disk();
self.look_from + (p.x() * self.defocus_disk_u) + (p.y() * self.defocus_disk_v)
}
pub fn render(&mut self, hittables: &Vec<Arc<dyn Hittable>>) {
if self.dirty {
self.init()
}
let mut imgbuf = image::ImageBuffer::new(self.image_width, self.image_height);
// render
for j in 0..self.image_height {
info!("{}\tScanlines remaining.", (self.image_height - j));
for i in 0..self.image_width {
let pixel_tl =
self.pixel00_loc + (i * self.pixel_delta_u) + (j * self.pixel_delta_v);
let mut pixel_colour = Colour::default();
for y in 1..(self.anti_alias_rate + 1) {
for x in 1..(self.anti_alias_rate + 1) {
let pixel_loc = pixel_tl
+ (x * self.pixel_delta_u / (self.anti_alias_rate + 1) as f32)
+ (y * self.pixel_delta_v / (self.anti_alias_rate + 1) as f32);
let ray_orig = if self.defocus_angle > 0. {
self.defocus_disk_sample()
} else {
self.look_from
};
let ray_dir = pixel_loc - ray_orig;
let r = Ray::new(ray_orig, ray_dir);
pixel_colour += self.ray_colour(hittables, &r, self.max_depth);
}
}
let pixel = imgbuf.get_pixel_mut(i, j);
*pixel =
(pixel_colour / (self.anti_alias_rate * self.anti_alias_rate) as f32).output();
}
}
info!("Writing image file...");
imgbuf.save("output.png").unwrap();
}
fn ray_colour(&self, hittables: &Vec<Arc<dyn Hittable>>, r: &Ray, depth: u32) -> Colour {
if depth == 0 {
return Colour::default();
}
let closest = Hit::hit_list(hittables, r);
if let Some(hit) = closest {
if let Some((scattered, att)) = hit.mat().scatter(&hit, r) {
if let Some(ray) = scattered {
return att * self.ray_colour(hittables, &ray, depth - 1);
}
return att;
}
return Colour::default();
}
// background
let unit_dir = r.dir().get_unit();
let a = 0.5 * (unit_dir.y() + 1.);
(1.0 - a) * Colour::new(1., 1., 1.) + a * Colour::new(0.5, 0.7, 1.0)
}
}

View File

@@ -3,10 +3,11 @@
mod camera;
mod objects;
mod ray;
mod raytracer;
mod scenes;
mod vec3;
use std::fs;
use std::{env, fs};
use std::sync::Arc;
use crate::camera::Camera;
@@ -15,6 +16,7 @@ use crate::objects::materials::lambertian::{Lambertian, Metal};
use crate::objects::sphere::Sphere;
use crate::objects::traits::Hittable;
use crate::ray::Ray;
use crate::raytracer::render;
use crate::scenes::scene::Scene;
use crate::vec3::Vec3;
use dotenv::dotenv;
@@ -25,15 +27,20 @@ fn main() {
dotenv().ok();
pretty_env_logger::init();
// TODO: use cli arg for scenefile
let json_file = "./scenes/scene.json";
// TODO: better cli parsing
let mut json_file = "./scenes/scene.json";
let args: Vec<String> = env::args().collect();
if args.len() > 1 {
json_file = &args[1];
}
let json_str = fs::read_to_string(json_file).expect("Reading specified scene file failed!");
let mut scene: Scene = serde_json::from_str(&json_str).unwrap();
scene.render();
render(&mut scene);
return;
// random spheres code; thought: make this available as cli flag?
let ground = Lambertian::rgb(0.5, 0.5, 0.5, 1.);
let ground = Lambertian::rgb(-2.5, 0.5, 0.5, 1.);
let mut world: Vec<Arc<dyn Hittable>> = vec![Arc::new(Sphere::xyz(
0.,
-1000.,
@@ -108,13 +115,14 @@ fn main() {
Arc::new(Metal::rgb(0.7, 0.6, 0.5, 1., 0.)),
)));
let mut c = Camera::new(1920, 1080);
let mut c = Camera::new();
c.set_fov(20.);
c.set_anti_alias_rate(23);
c.set_max_depth(50);
c.set_vup(Vec3::new(0., 1., 0.));
c.set_look_from(Vec3::new(13., 2., 3.));
c.set_look_at(Vec3::new(0., 0., 0.));
c.add_defocus_blur(0.6, 10.);
c.render(&world);
let mut s = Scene::new(c, world, "output.png".to_string(), 1920, 1080, 50);
render(&mut s);
}

View File

@@ -1,7 +1,10 @@
pub mod circle;
pub mod cube;
pub mod cylinder;
pub mod hit;
pub mod materials;
pub mod sphere;
pub mod triangle;
pub mod mesh;
pub mod quad;
pub mod cube;
pub mod sphere;
pub mod traits;
pub mod triangle;

52
src/objects/circle.rs Normal file
View File

@@ -0,0 +1,52 @@
use std::sync::Arc;
use crate::{
objects::{materials::traits::Material, traits::Hittable},
ray::Ray,
vec3::Vec3,
};
use super::hit::Hit;
#[derive(Debug)]
pub struct Circle {
radius: f32,
center: Vec3,
normal: Vec3,
material: Arc<dyn Material>,
}
impl Circle {
pub fn new(radius: f32, center: Vec3, normal: Vec3, material: Arc<dyn Material>) -> Self {
Self {
radius,
center,
normal: normal.get_unit(),
material,
}
}
}
impl Hittable for Circle {
fn hit(&self, r: &Ray) -> Option<Hit> {
// check if ray parallel to plane
let dot = self.normal.dot(r.dir());
if dot == 0.0 {
return None;
}
// hitpoint on plane
let t = self.normal.dot(&(self.center - r.origin())) / dot;
// hits behind camera
if t < 0. {
return None;
};
let p = r.at(t);
if (p - self.center).length() < self.radius {
return Some(Hit::new(t, p, self.normal, self.material.clone(), self.normal.dot(&r.dir()) < 0.));
}
None
}
}

View File

@@ -11,7 +11,6 @@ use super::hit::Hit;
#[derive(Debug)]
pub struct Cube {
faces: Vec<Arc<dyn Hittable>>,
material: Arc<dyn Material>,
}
impl Cube {
@@ -34,7 +33,7 @@ impl Cube {
Arc::new(Quad::new(p5, p6, p7, p8, material.clone())),
Arc::new(Quad::new(p4, p3, p7, p8, material.clone())),
];
Self { faces, material }
Self { faces }
}
}
@@ -42,9 +41,4 @@ impl Hittable for Cube {
fn hit(&self, r: &Ray) -> Option<Hit> {
Hit::hit_list(&self.faces, r)
}
fn normal_at(&self, _p: &Vec3) -> Vec3 {
// TODO: normal calc for cube
todo!()
}
}

26
src/objects/cylinder.rs Normal file
View File

@@ -0,0 +1,26 @@
use crate::{objects::traits::Hittable, vec3::Vec3};
#[derive(Debug)]
pub struct Cylinder {
radius: f32,
length: f32,
up: Vec3,
bottom_center: Vec3,
}
impl Cylinder {
pub fn new(radius: f32, length: f32, up: Vec3, bottom_center: Vec3) -> Self {
Self {
radius,
length,
up,
bottom_center,
}
}
}
impl Hittable for Cylinder {
fn hit(&self, r: &crate::ray::Ray) -> Option<super::hit::Hit> {
todo!()
}
}

View File

@@ -16,9 +16,7 @@ pub struct Dielectric {
impl Dielectric {
pub fn new(refraction_index: f32) -> Self {
Self {
refraction_index,
}
Self { refraction_index }
}
fn reflectance(cos: f32, refraction_index: f32) -> f32 {
@@ -46,7 +44,8 @@ impl Material for Dielectric {
let cannot_refract = ri * sin_theta > 1.;
let mut rng = rand::rng();
let dir = if cannot_refract || Dielectric::reflectance(cos_theta, ri) > rng.random::<f32>() {
let dir = if cannot_refract || Dielectric::reflectance(cos_theta, ri) > rng.random::<f32>()
{
unit.reflect(hit.n())
} else {
unit.refract(hit.n(), ri)

View File

@@ -15,10 +15,7 @@ pub struct Lambertian {
impl Lambertian {
pub fn new(albedo: Colour, prob: f32) -> Self {
Self {
albedo,
prob,
}
Self { albedo, prob }
}
pub fn rgb(r: f32, g: f32, b: f32, prob: f32) -> Self {
@@ -53,11 +50,7 @@ pub struct Metal {
impl Metal {
pub fn new(albedo: Colour, prob: f32, fuzz: f32) -> Self {
Self {
albedo,
prob,
fuzz,
}
Self { albedo, prob, fuzz }
}
pub fn rgb(r: f32, g: f32, b: f32, prob: f32, fuzz: f32) -> Self {

0
src/objects/mesh.rs Normal file
View File

View File

@@ -1,5 +1,4 @@
use crate::objects::hit::Hit;
use crate::objects::sphere::Sphere;
use crate::objects::traits::Hittable;
use crate::objects::triangle::Triangle;
use crate::ray::Ray;
@@ -27,14 +26,6 @@ impl Quad {
normal: (p2 - p1).cross(&(p4 - p1)).get_unit(),
}
}
pub fn corner_spheres(&self) -> Vec<Sphere> {
vec![
Sphere::new(self.p1, 1., self.material.clone()),
Sphere::new(self.p2, 1., self.material.clone()),
Sphere::new(self.p3, 1., self.material.clone()),
Sphere::new(self.p4, 1., self.material.clone()),
]
}
pub fn hit(
p1: Vec3,
@@ -78,9 +69,4 @@ impl Hittable for Quad {
r,
)
}
fn normal_at(&self, _p: &Vec3) -> Vec3 {
// FIXME: might cause ownership issues
self.normal
}
}

View File

@@ -2,12 +2,11 @@ use core::f32::math::sqrt;
use std::fmt::{self, Debug};
use std::sync::Arc;
use crate::Vec3;
use crate::objects::hit::Hit;
use crate::objects::materials::traits::Material;
use crate::objects::traits::Hittable;
use crate::ray::Ray;
use crate::Vec3;
pub struct Sphere {
center: Vec3,
@@ -63,14 +62,10 @@ impl Hittable for Sphere {
Some(Hit::new(
t,
p,
self.normal_at(&p),
(p - self.center).get_unit(),
self.material.clone(),
out_n.dot(r.dir()) < 0.,
))
}
}
fn normal_at(&self, p: &Vec3) -> Vec3 {
(*p - self.center).get_unit()
}
}

View File

@@ -1,10 +1,8 @@
use std::fmt::Debug;
use crate::objects::hit::Hit;
use crate::Ray;
use crate::Vec3;
use crate::objects::hit::Hit;
pub trait Hittable: Debug + Send + Sync {
fn hit(&self, r: &Ray) -> Option<Hit>;
fn normal_at(&self, p: &Vec3) -> Vec3;
}

View File

@@ -54,7 +54,7 @@ impl Triangle {
let diff = (a4 - a1 - a2 - a3).abs();
if diff < 0.001 {
Some(Hit::new(t, p, normal, material, normal.dot(&-r.dir()) > 0.))
Some(Hit::new(t, p, normal, material, normal.dot(&r.dir()) < 0.))
} else {
None
}
@@ -83,9 +83,4 @@ impl Hittable for Triangle {
r,
)
}
fn normal_at(&self, _p: &Vec3) -> Vec3 {
// FIXME: might cause ownership issues
self.normal
}
}

View File

@@ -7,10 +7,7 @@ pub struct Ray {
impl Ray {
pub fn new(origin: Vec3, dir: Vec3) -> Self {
Self {
origin,
dir,
}
Self { origin, dir }
}
pub fn at(&self, t: f32) -> Vec3 {

96
src/raytracer.rs Normal file
View File

@@ -0,0 +1,96 @@
use image::{ExtendedColorType, ImageEncoder, codecs::png::PngEncoder};
use log::{error, info};
use rayon::iter::IntoParallelIterator;
use rayon::prelude::*;
use std::{fs::File, sync::Arc};
use crate::{
objects::{hit::Hit, traits::Hittable},
ray::Ray,
scenes::scene::Scene,
vec3::Colour,
};
pub fn write_image(filename: &str, pixels: &[u8], width: u32, height: u32) {
match File::create(filename) {
Ok(out) => {
let enc = PngEncoder::new(out);
if let Err(e) = enc.write_image(pixels, width, height, ExtendedColorType::Rgb8) {
error!("Failed writing image: {}", e);
}
}
Err(e) => error!("Failed to create image file: {} ", e),
}
}
pub fn render(scene: &mut Scene) {
let camera = &mut scene.camera;
camera.init(scene.image_width as f32, scene.image_height as f32);
// TODO: currently splits per vertical line, but could be more granular (per chunk)
let mut pixels = vec![0 as u8; (scene.image_width * scene.image_height * 3) as usize];
let scanlines: Vec<(usize, &mut [u8])> = pixels
.chunks_mut(scene.image_width as usize * 3)
.enumerate()
.collect();
scanlines.into_par_iter().for_each(|(i, chunk)| {
render_chunk(chunk, scene, i as u32);
});
info!("Writing image file...");
write_image(
&scene.filename,
&pixels,
scene.image_width,
scene.image_height,
);
}
pub fn render_chunk(chunk: &mut [u8], scene: &Scene, y: u32) {
let camera = &scene.camera;
for i in 0..scene.image_width {
let pixel_tl = camera.pixel00_loc + (i * camera.pixel_delta_u) + (y * camera.pixel_delta_v);
// THOUGHT: the above seems more efficient (esp for larger aa rates) but can test for a
// get_ray_no_tl() function and compare performance.
let mut pixel_colour = Colour::default();
for y in 1..(camera.anti_alias_rate + 1) {
for x in 1..(camera.anti_alias_rate + 1) {
let r = camera.get_ray(pixel_tl, x, y);
pixel_colour += ray_colour(&scene.objects, &r, scene.max_depth);
}
}
let (r, g, b) =
(pixel_colour / (camera.anti_alias_rate * camera.anti_alias_rate) as f32).output();
chunk[(i * 3) as usize] = r;
chunk[(i * 3) as usize + 1] = g;
chunk[(i * 3) as usize + 2] = b;
}
info!("Line {}\tfinished.", y);
}
pub fn ray_colour(hittables: &Vec<Arc<dyn Hittable>>, r: &Ray, depth: u32) -> Colour {
if depth == 0 {
return Colour::default();
}
let closest = Hit::hit_list(hittables, r);
if let Some(hit) = closest {
if let Some((scattered, att)) = hit.mat().scatter(&hit, r) {
if let Some(ray) = scattered {
return att * ray_colour(hittables, &ray, depth - 1);
}
return att;
}
return Colour::default();
}
// background
let unit_dir = r.dir().get_unit();
let a = 0.5 * (unit_dir.y() + 1.);
(1.0 - a) * Colour::new(1., 1., 1.) + a * Colour::new(0.5, 0.7, 1.0)
}

View File

@@ -1,2 +1,4 @@
pub mod raw_camera;
pub mod material_def;
pub mod hittable_def;
pub mod scene;

137
src/scenes/hittable_def.rs Normal file
View File

@@ -0,0 +1,137 @@
use std::sync::Arc;
use log::warn;
use serde::Deserialize;
use crate::{
objects::{
circle::Circle, cube::Cube, materials::traits::Material, quad::Quad, sphere::Sphere,
traits::Hittable, triangle::Triangle,
},
scenes::material_def::MaterialDef,
vec3::Vec3,
};
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub(crate) enum HittableDef {
Sphere(RawSphere),
Triangle(RawTriangle),
Quad(RawQuad),
Cube(RawCube),
Circle(RawCircle),
}
impl HittableDef {
pub(crate) fn into_arc(self, materials: &Vec<Arc<dyn Material>>) -> Option<Arc<dyn Hittable>> {
match self {
HittableDef::Sphere(s) => {
let material = s.material.into_arc(materials);
match material {
Some(m) => Some(Arc::new(Sphere::new(s.center, s.radius, m))),
None => None,
}
}
HittableDef::Triangle(t) => {
let material = t.material.into_arc(materials);
match material {
Some(m) => Some(Arc::new(Triangle::new(t.p1, t.p2, t.p3, m))),
None => None,
}
}
HittableDef::Quad(q) => {
let material = q.material.into_arc(materials);
match material {
Some(m) => Some(Arc::new(Quad::new(q.p1, q.p2, q.p3, q.p4, m))),
None => None,
}
}
HittableDef::Cube(c) => {
let material = c.material.into_arc(materials);
match material {
Some(m) => Some(Arc::new(Cube::new(
c.p1, c.p2, c.p3, c.p4, c.p5, c.p6, c.p7, c.p8, m,
))),
None => None,
}
}
HittableDef::Circle(c) => {
let material = c.material.into_arc(materials);
match material {
Some(m) => Some(Arc::new(Circle::new(c.radius, c.center, c.normal, m))),
None => None,
}
}
}
}
}
#[derive(Deserialize)]
pub(crate) struct RawSphere {
pub center: Vec3,
pub radius: f32,
pub material: RawMaterial,
}
#[derive(Deserialize)]
pub(crate) struct RawTriangle {
pub p1: Vec3,
pub p2: Vec3,
pub p3: Vec3,
pub material: RawMaterial,
}
#[derive(Deserialize)]
pub(crate) struct RawQuad {
pub p1: Vec3,
pub p2: Vec3,
pub p3: Vec3,
pub p4: Vec3,
pub material: RawMaterial,
}
#[derive(Deserialize)]
pub(crate) struct RawCube {
pub p1: Vec3,
pub p2: Vec3,
pub p3: Vec3,
pub p4: Vec3,
pub p5: Vec3,
pub p6: Vec3,
pub p7: Vec3,
pub p8: Vec3,
pub material: RawMaterial,
}
#[derive(Deserialize)]
pub(crate) struct RawCircle {
radius: f32,
center: Vec3,
normal: Vec3,
material: RawMaterial,
}
#[derive(Deserialize)]
#[serde(untagged)]
pub(crate) enum RawMaterial {
Ref(u32),
Direct(MaterialDef),
}
impl RawMaterial {
pub fn into_arc(self, materials: &Vec<Arc<dyn Material>>) -> Option<Arc<dyn Material>> {
match self {
RawMaterial::Ref(r) => {
if r as usize >= materials.len() {
warn!(
"Hittable specified nonexistent material index {}; skipping...",
r
);
return None;
}
Some(materials.get(r as usize).unwrap().clone())
}
RawMaterial::Direct(m) => Some(m.into_arc()),
}
}
}

View File

@@ -0,0 +1,27 @@
use std::sync::Arc;
use serde::Deserialize;
use crate::objects::materials::{dielectric::Dielectric, lambertian::{Lambertian, Metal}, normal::Normal, traits::Material};
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub(crate) enum MaterialDef {
Lambertian(Lambertian),
Metal(Metal),
Dielectric(Dielectric),
Normal(Normal),
}
impl MaterialDef {
pub(crate) fn into_arc(self) -> Arc<dyn Material> {
match self {
MaterialDef::Lambertian(l) => Arc::new(l),
MaterialDef::Metal(m) => Arc::new(m),
MaterialDef::Dielectric(d) => Arc::new(d),
MaterialDef::Normal(n) => Arc::new(n),
}
}
}

View File

@@ -4,15 +4,9 @@ use crate::{camera::Camera, vec3::Vec3};
#[derive(Deserialize)]
pub struct RawCamera {
// output
image_width: u32,
image_height: u32,
// raytracing
#[serde(default)]
anti_alias_rate: u32,
#[serde(default)]
max_depth: u32,
// camera
#[serde(default)]
@@ -32,10 +26,7 @@ pub struct RawCamera {
impl Default for RawCamera {
fn default() -> Self {
Self {
image_width: 400,
image_height: 300,
anti_alias_rate: 1,
max_depth: 10,
fov: 70.,
look_from: Vec3::new(0., 0., 0.),
look_at: Vec3::new(0., 0., -1.),
@@ -49,10 +40,7 @@ impl Default for RawCamera {
impl From<RawCamera> for Camera {
fn from(raw: RawCamera) -> Self {
Camera::new_full(
raw.image_width,
raw.image_height,
raw.anti_alias_rate,
raw.max_depth,
raw.fov,
raw.look_from,
raw.look_at,

View File

@@ -1,47 +1,42 @@
use std::fmt::Debug;
use std::sync::Arc;
use log::warn;
use serde::Deserialize;
use crate::{
camera::Camera,
objects::{
cube::Cube,
materials::{
dielectric::Dielectric,
lambertian::{Lambertian, Metal},
normal::Normal,
traits::Material,
},
quad::Quad,
sphere::Sphere,
traits::Hittable,
triangle::Triangle,
},
scenes::raw_camera::RawCamera,
vec3::Vec3,
objects::{materials::traits::Material, traits::Hittable},
scenes::{hittable_def::HittableDef, material_def::MaterialDef, raw_camera::RawCamera},
};
#[derive(Debug)]
pub struct Scene {
pub camera: Camera,
pub materials: Vec<Arc<dyn Material>>,
pub camera: Camera, // FIXME: should not be public
pub objects: Vec<Arc<dyn Hittable>>,
}
impl Debug for Scene {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Scene")
.field("camera", &self.camera)
.field("materials", &self.materials)
.field("objects", &self.objects)
.finish()
}
// image
pub filename: String,
pub image_width: u32,
pub image_height: u32,
// raytracing // TODO: think about organisation of these vars, also in Camera
pub max_depth: u32,
}
impl Scene {
pub fn render(&mut self) {
self.camera.render(&self.objects);
pub fn new(
camera: Camera,
objects: Vec<Arc<dyn Hittable>>,
filename: String,
image_width: u32,
image_height: u32,
max_depth: u32,
) -> Self {
Self {
camera,
objects,
filename,
image_width,
image_height,
max_depth,
}
}
}
@@ -50,6 +45,10 @@ struct SceneDef {
pub camera: RawCamera,
pub materials: Vec<MaterialDef>,
pub objects: Vec<HittableDef>,
pub filename: String,
pub image_width: u32,
pub image_height: u32,
pub max_depth: u32,
}
impl<'de> Deserialize<'de> for Scene {
@@ -70,138 +69,11 @@ impl<'de> Deserialize<'de> for Scene {
.collect();
Ok(Self {
camera: Camera::from(conc.camera),
materials: mats,
objects: objs,
filename: conc.filename,
image_width: conc.image_width,
image_height: conc.image_height,
max_depth: conc.max_depth,
})
}
}
#[derive(Deserialize)]
#[serde(untagged)]
enum RawMaterial {
Ref(u32),
Direct(MaterialDef),
}
impl RawMaterial {
pub fn into_arc(self, materials: &Vec<Arc<dyn Material>>) -> Option<Arc<dyn Material>> {
match self {
RawMaterial::Ref(r) => {
if r as usize >= materials.len() {
warn!("Sphere specified nonexistent material {}; skipping...", r);
return None;
}
Some(materials.get(r as usize).unwrap().clone())
}
RawMaterial::Direct(m) => Some(m.into_arc()),
}
}
}
#[derive(Deserialize)]
struct RawSphere {
pub center: Vec3,
pub radius: f32,
pub material: RawMaterial,
}
#[derive(Deserialize)]
struct RawTriangle {
pub p1: Vec3,
pub p2: Vec3,
pub p3: Vec3,
pub material: RawMaterial,
}
#[derive(Deserialize)]
struct RawQuad {
pub p1: Vec3,
pub p2: Vec3,
pub p3: Vec3,
pub p4: Vec3,
pub material: RawMaterial,
}
#[derive(Deserialize)]
struct RawCube {
pub p1: Vec3,
pub p2: Vec3,
pub p3: Vec3,
pub p4: Vec3,
pub p5: Vec3,
pub p6: Vec3,
pub p7: Vec3,
pub p8: Vec3,
pub material: RawMaterial,
}
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
enum HittableDef {
Sphere(RawSphere),
Triangle(RawTriangle),
Quad(RawQuad),
Cube(RawCube),
}
impl HittableDef {
fn into_arc(self, materials: &Vec<Arc<dyn Material>>) -> Option<Arc<dyn Hittable>> {
// THOUGHT: i think this can be done better; in the map/filter call up there?
match self {
HittableDef::Sphere(s) => {
let material = s.material.into_arc(materials);
if let Some(m) = material {
Some(Arc::new(Sphere::new(s.center, s.radius, m)))
} else {
None
}
}
HittableDef::Triangle(t) => {
let material = t.material.into_arc(materials);
if let Some(m) = material {
Some(Arc::new(Triangle::new(t.p1, t.p2, t.p3, m)))
} else {
None
}
}
HittableDef::Quad(q) => {
let material = q.material.into_arc(materials);
if let Some(m) = material {
Some(Arc::new(Quad::new(q.p1, q.p2, q.p3, q.p4, m)))
} else {
None
}
}
HittableDef::Cube(c) => {
let material = c.material.into_arc(materials);
if let Some(m) = material {
Some(Arc::new(Cube::new(
c.p1, c.p2, c.p3, c.p4, c.p5, c.p6, c.p7, c.p8, m,
)))
} else {
None
}
}
}
}
}
#[derive(Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
enum MaterialDef {
Lambertian(Lambertian),
Metal(Metal),
Dielectric(Dielectric),
Normal(Normal),
}
impl MaterialDef {
fn into_arc(self) -> Arc<dyn Material> {
match self {
MaterialDef::Lambertian(l) => Arc::new(l),
MaterialDef::Metal(m) => Arc::new(m),
MaterialDef::Dielectric(d) => Arc::new(d),
MaterialDef::Normal(n) => Arc::new(n),
}
}
}

View File

@@ -122,7 +122,7 @@ impl Vec3 {
r_out_perp + r_out_parr
}
pub fn output(self) -> image::Rgb<u8> {
pub fn output(self) -> (u8, u8, u8) {
// gamma correction
let r = if self.x > 0. {
sqrt(self.x).clamp(0., 1.)
@@ -144,7 +144,7 @@ impl Vec3 {
let ig = (255.599 * g) as u8;
let ib = (255.599 * b) as u8;
image::Rgb([ir, ig, ib])
(ir, ig, ib)
}
pub fn clone(&self) -> Self {