ft: ran linter
This commit is contained in:
37
scenes/highDef.json
Normal file
37
scenes/highDef.json
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
{
|
||||||
|
"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,
|
||||||
|
"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": "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}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -2,8 +2,8 @@
|
|||||||
"camera": {
|
"camera": {
|
||||||
"image_width": 1920,
|
"image_width": 1920,
|
||||||
"image_height": 1080,
|
"image_height": 1080,
|
||||||
"anti_alias_rate": 23,
|
"anti_alias_rate": 2,
|
||||||
"max_depth": 100,
|
"max_depth": 10,
|
||||||
"fov": 20.0,
|
"fov": 20.0,
|
||||||
"look_from": { "x": -10, "y": 5, "z": 10 },
|
"look_from": { "x": -10, "y": 5, "z": 10 },
|
||||||
"look_at": { "x": 0.0, "y": 0.0, "z": -1.0 },
|
"look_at": { "x": 0.0, "y": 0.0, "z": -1.0 },
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ impl Camera {
|
|||||||
|
|
||||||
fn defocus_disk_sample(&self) -> Vec3 {
|
fn defocus_disk_sample(&self) -> Vec3 {
|
||||||
let p = Vec3::random_in_unit_disk();
|
let p = Vec3::random_in_unit_disk();
|
||||||
return self.look_from + (p.x() * self.defocus_disk_u) + (p.y() * self.defocus_disk_v);
|
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>>) {
|
pub fn render(&mut self, hittables: &Vec<Arc<dyn Hittable>>) {
|
||||||
@@ -209,7 +209,7 @@ impl Camera {
|
|||||||
let ray_orig = if self.defocus_angle > 0. { self.defocus_disk_sample() } else {self.look_from};
|
let ray_orig = if self.defocus_angle > 0. { self.defocus_disk_sample() } else {self.look_from};
|
||||||
let ray_dir = pixel_loc - ray_orig;
|
let ray_dir = pixel_loc - ray_orig;
|
||||||
let r = Ray::new(ray_orig, ray_dir);
|
let r = Ray::new(ray_orig, ray_dir);
|
||||||
pixel_colour += self.ray_colour(&hittables, &r, self.max_depth);
|
pixel_colour += self.ray_colour(hittables, &r, self.max_depth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,7 +220,7 @@ impl Camera {
|
|||||||
}
|
}
|
||||||
|
|
||||||
info!("Writing image file...");
|
info!("Writing image file...");
|
||||||
imgbuf.save("output.png").unwrap();
|
imgbuf.save("output2.png").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ray_colour(&self, hittables: &Vec<Arc<dyn Hittable>>, r: &Ray, depth: u32) -> Colour {
|
fn ray_colour(&self, hittables: &Vec<Arc<dyn Hittable>>, r: &Ray, depth: u32) -> Colour {
|
||||||
|
|||||||
12
src/main.rs
12
src/main.rs
@@ -6,6 +6,7 @@ mod ray;
|
|||||||
mod scenes;
|
mod scenes;
|
||||||
mod vec3;
|
mod vec3;
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::camera::Camera;
|
use crate::camera::Camera;
|
||||||
@@ -14,9 +15,9 @@ use crate::objects::materials::lambertian::{Lambertian, Metal};
|
|||||||
use crate::objects::sphere::Sphere;
|
use crate::objects::sphere::Sphere;
|
||||||
use crate::objects::traits::Hittable;
|
use crate::objects::traits::Hittable;
|
||||||
use crate::ray::Ray;
|
use crate::ray::Ray;
|
||||||
|
use crate::scenes::scene::Scene;
|
||||||
use crate::vec3::Vec3;
|
use crate::vec3::Vec3;
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use pretty_env_logger;
|
|
||||||
use rand::RngExt;
|
use rand::RngExt;
|
||||||
|
|
||||||
// TODO: implement scene serialization
|
// TODO: implement scene serialization
|
||||||
@@ -25,10 +26,11 @@ fn main() {
|
|||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
// TODO: use cli arg for scenefile
|
// TODO: use cli arg for scenefile
|
||||||
// let json_file = "./scenes/scene.json";
|
let json_file = "./scenes/scene.json";
|
||||||
// let json_str = fs::read_to_string(json_file).expect("Reading specified scene file failed!");
|
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();
|
let mut scene: Scene = serde_json::from_str(&json_str).unwrap();
|
||||||
// scene.render();
|
scene.render();
|
||||||
|
return;
|
||||||
|
|
||||||
// random spheres code; thought: make this available as cli flag?
|
// 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(0.5, 0.5, 0.5, 1.);
|
||||||
|
|||||||
@@ -17,11 +17,11 @@ pub struct Hit {
|
|||||||
impl Hit {
|
impl Hit {
|
||||||
pub fn new(t: f32, p: Vec3, n: Vec3, mat: Arc<dyn Material>, front_face: bool) -> Self {
|
pub fn new(t: f32, p: Vec3, n: Vec3, mat: Arc<dyn Material>, front_face: bool) -> Self {
|
||||||
Self {
|
Self {
|
||||||
t: t,
|
t,
|
||||||
p: p,
|
p,
|
||||||
n: if front_face { n } else { -n },
|
n: if front_face { n } else { -n },
|
||||||
mat: mat,
|
mat,
|
||||||
front_face: front_face,
|
front_face,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,6 +57,6 @@ impl Hit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return closest;
|
closest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,15 +17,15 @@ pub struct Dielectric {
|
|||||||
impl Dielectric {
|
impl Dielectric {
|
||||||
pub fn new(refraction_index: f32) -> Self {
|
pub fn new(refraction_index: f32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
refraction_index: refraction_index,
|
refraction_index,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _reflectance(cos: f32, refraction_index: f32) -> f32 {
|
fn reflectance(cos: f32, refraction_index: f32) -> f32 {
|
||||||
// Shlick's approximation
|
// Shlick's approximation
|
||||||
let mut r0 = (1. - refraction_index) / (1. + refraction_index);
|
let mut r0 = (1. - refraction_index) / (1. + refraction_index);
|
||||||
r0 *= r0;
|
r0 *= r0;
|
||||||
return r0 + (1. - r0) * (1. - cos).powf(5.);
|
r0 + (1. - r0) * (1. - cos).powf(5.)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,13 +45,12 @@ impl Material for Dielectric {
|
|||||||
let sin_theta = sqrt(1. - cos_theta * cos_theta);
|
let sin_theta = sqrt(1. - cos_theta * cos_theta);
|
||||||
let cannot_refract = ri * sin_theta > 1.;
|
let cannot_refract = ri * sin_theta > 1.;
|
||||||
|
|
||||||
let dir: Vec3;
|
|
||||||
let mut rng = rand::rng();
|
let mut rng = rand::rng();
|
||||||
if cannot_refract || Dielectric::_reflectance(cos_theta, ri) > rng.random::<f32>() {
|
let dir = if cannot_refract || Dielectric::reflectance(cos_theta, ri) > rng.random::<f32>() {
|
||||||
dir = unit.reflect(hit.n());
|
unit.reflect(hit.n())
|
||||||
} else {
|
} else {
|
||||||
dir = unit.refract(hit.n(), ri);
|
unit.refract(hit.n(), ri)
|
||||||
}
|
};
|
||||||
Some((Ray::new(*hit.p(), dir), Colour::new(1., 1., 1.)))
|
Some((Ray::new(*hit.p(), dir), Colour::new(1., 1., 1.)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,21 +16,21 @@ pub struct Lambertian {
|
|||||||
impl Lambertian {
|
impl Lambertian {
|
||||||
pub fn new(albedo: Colour, prob: f32) -> Self {
|
pub fn new(albedo: Colour, prob: f32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
albedo: albedo,
|
albedo,
|
||||||
prob: prob,
|
prob,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rgb(r: f32, g: f32, b: f32, prob: f32) -> Self {
|
pub fn rgb(r: f32, g: f32, b: f32, prob: f32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
albedo: Colour::new(r, g, b),
|
albedo: Colour::new(r, g, b),
|
||||||
prob: prob,
|
prob,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Material for Lambertian {
|
impl Material for Lambertian {
|
||||||
fn scatter(&self, hit: &Hit, ray: &Ray) -> Option<(Ray, Colour)> {
|
fn scatter(&self, hit: &Hit, _ray: &Ray) -> Option<(Ray, Colour)> {
|
||||||
let mut rng = rand::rng();
|
let mut rng = rand::rng();
|
||||||
if self.prob >= rng.random::<f32>() {
|
if self.prob >= rng.random::<f32>() {
|
||||||
let mut dir = *hit.n() + Vec3::random_unit();
|
let mut dir = *hit.n() + Vec3::random_unit();
|
||||||
@@ -40,7 +40,7 @@ impl Material for Lambertian {
|
|||||||
let scattered = Ray::new(*hit.p(), dir);
|
let scattered = Ray::new(*hit.p(), dir);
|
||||||
return Some((scattered, self.albedo));
|
return Some((scattered, self.albedo));
|
||||||
}
|
}
|
||||||
return None;
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,17 +54,17 @@ pub struct Metal {
|
|||||||
impl Metal {
|
impl Metal {
|
||||||
pub fn new(albedo: Colour, prob: f32, fuzz: f32) -> Self {
|
pub fn new(albedo: Colour, prob: f32, fuzz: f32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
albedo: albedo,
|
albedo,
|
||||||
prob: prob,
|
prob,
|
||||||
fuzz: fuzz,
|
fuzz,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rgb(r: f32, g: f32, b: f32, prob: f32, fuzz: f32) -> Self {
|
pub fn rgb(r: f32, g: f32, b: f32, prob: f32, fuzz: f32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
albedo: Colour::new(r, g, b),
|
albedo: Colour::new(r, g, b),
|
||||||
prob: prob,
|
prob,
|
||||||
fuzz: fuzz,
|
fuzz,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,6 +77,6 @@ impl Material for Metal {
|
|||||||
refl = refl.get_unit() + self.fuzz * Vec3::random_unit();
|
refl = refl.get_unit() + self.fuzz * Vec3::random_unit();
|
||||||
return Some((Ray::new(*hit.p(), refl), self.albedo));
|
return Some((Ray::new(*hit.p(), refl), self.albedo));
|
||||||
}
|
}
|
||||||
return None;
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{objects::hit::Hit, ray::Ray, vec3::Colour};
|
use crate::{objects::hit::Hit, ray::Ray, vec3::Colour};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
pub trait Material: Debug {
|
pub trait Material: Debug + Send + Sync{
|
||||||
fn scatter(&self, hit: &Hit, ray: &Ray) -> Option<(Ray, Colour)>;
|
fn scatter(&self, hit: &Hit, ray: &Ray) -> Option<(Ray, Colour)>;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ use core::f32::math::sqrt;
|
|||||||
use std::fmt::{self, Debug};
|
use std::fmt::{self, Debug};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
use crate::objects::hit::Hit;
|
use crate::objects::hit::Hit;
|
||||||
use crate::objects::materials::traits::Material;
|
use crate::objects::materials::traits::Material;
|
||||||
@@ -16,6 +15,7 @@ pub struct Sphere {
|
|||||||
material: Arc<dyn Material>,
|
material: Arc<dyn Material>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Debug for Sphere {
|
impl Debug for Sphere {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.debug_struct("Sphere")
|
f.debug_struct("Sphere")
|
||||||
@@ -29,7 +29,7 @@ impl Debug for Sphere {
|
|||||||
impl Sphere {
|
impl Sphere {
|
||||||
pub fn new(center: Vec3, r: f32, mat: Arc<dyn Material>) -> Self {
|
pub fn new(center: Vec3, r: f32, mat: Arc<dyn Material>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
center: center,
|
center,
|
||||||
radius: r,
|
radius: r,
|
||||||
material: mat,
|
material: mat,
|
||||||
}
|
}
|
||||||
@@ -42,16 +42,6 @@ impl Sphere {
|
|||||||
material: mat,
|
material: mat,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn center(&self) -> &Vec3 {
|
|
||||||
&self.center
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_center(&mut self, center: Vec3) {
|
|
||||||
if self.center != center {
|
|
||||||
self.center = center;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hittable for Sphere {
|
impl Hittable for Sphere {
|
||||||
@@ -83,12 +73,4 @@ impl Hittable for Sphere {
|
|||||||
fn normal_at(&self, p: &Vec3) -> Vec3 {
|
fn normal_at(&self, p: &Vec3) -> Vec3 {
|
||||||
(*p - self.center).get_unit()
|
(*p - self.center).get_unit()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inside(&self, p: &Vec3) -> bool {
|
|
||||||
(*p - self.center).length() < self.radius
|
|
||||||
}
|
|
||||||
|
|
||||||
fn closest_on_surface(&self, p: &Vec3) -> Vec3 {
|
|
||||||
self.normal_at(p) * self.radius
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,7 @@ use crate::objects::hit::Hit;
|
|||||||
use crate::Ray;
|
use crate::Ray;
|
||||||
use crate::Vec3;
|
use crate::Vec3;
|
||||||
|
|
||||||
pub trait Hittable: Debug {
|
pub trait Hittable: Debug + Send + Sync {
|
||||||
fn hit(&self, r: &Ray) -> Option<Hit>;
|
fn hit(&self, r: &Ray) -> Option<Hit>;
|
||||||
fn normal_at(&self, p: &Vec3) -> Vec3;
|
fn normal_at(&self, p: &Vec3) -> Vec3;
|
||||||
// fn intersect<H: Hittable>(&self, o: &H) -> bool;
|
|
||||||
fn inside(&self, p: &Vec3) -> bool;
|
|
||||||
fn closest_on_surface(&self, p: &Vec3) -> Vec3;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ pub struct Ray {
|
|||||||
impl Ray {
|
impl Ray {
|
||||||
pub fn new(origin: Vec3, dir: Vec3) -> Self {
|
pub fn new(origin: Vec3, dir: Vec3) -> Self {
|
||||||
Self {
|
Self {
|
||||||
origin: origin,
|
origin,
|
||||||
dir: dir,
|
dir,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ impl Vec3 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn length_squared(&self) -> f32 {
|
pub fn length_squared(&self) -> f32 {
|
||||||
(self.x * self.x + self.y * self.y + self.z * self.z) as f32
|
self.x * self.x + self.y * self.y + self.z * self.z
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dot(&self, other: &Self) -> f32 {
|
pub fn dot(&self, other: &Self) -> f32 {
|
||||||
@@ -123,7 +123,7 @@ impl Vec3 {
|
|||||||
let r_out_perp = etai_over_etat * (*self + cos_theta * n);
|
let r_out_perp = etai_over_etat * (*self + cos_theta * n);
|
||||||
let r_out_parr = -sqrt((1. - r_out_perp.length_squared()).abs()) * n;
|
let r_out_parr = -sqrt((1. - r_out_perp.length_squared()).abs()) * n;
|
||||||
|
|
||||||
return r_out_perp + r_out_parr;
|
r_out_perp + r_out_parr
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn output(self) -> image::Rgb<u8> {
|
pub fn output(self) -> image::Rgb<u8> {
|
||||||
|
|||||||
Reference in New Issue
Block a user