This commit is contained in:
2023-02-01 22:30:20 +01:00
parent 0a70a1e1de
commit 560b2b1196
5 changed files with 21 additions and 27 deletions

View File

@@ -1,21 +1,19 @@
use std::rc::Rc;
use crate::material::Material; use crate::material::Material;
use crate::ray::*; use crate::ray::*;
use crate::vec3::*; use crate::vec3::*;
#[derive(Clone)] #[derive(Clone)]
pub struct HitRecord { pub struct HitRecord<'a> {
pub p: Point3, pub p: Point3,
pub normal: Vec3, pub normal: Vec3,
pub mat_ptr: Rc<Material>, pub mat_ptr: &'a dyn Material,
pub t: f64, pub t: f64,
pub front_face: bool, pub front_face: bool,
} }
impl HitRecord { impl HitRecord<'_> {
pub fn new_empty() -> Self { pub fn new_empty<T: Material>(material: &T) -> Self {
HitRecord { p: Point3::new(0.0, 0.0, 0.0), normal: Vec3::new(0.0, 0.0, 0.0), t: 0.0, front_face: false, mat_ptr: Rc::new(Material { }) } HitRecord { p: Point3::new(0.0, 0.0, 0.0), normal: Vec3::new(0.0, 0.0, 0.0), t: 0.0, front_face: false, mat_ptr: material }
} }
pub fn set_face_normal(&mut self, r: &Ray, outward_normal: &Vec3) { pub fn set_face_normal(&mut self, r: &Ray, outward_normal: &Vec3) {

View File

@@ -20,7 +20,7 @@ impl HittableList {
impl Hittable for HittableList { impl Hittable for HittableList {
fn hit(&mut self, r: &crate::ray::Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool { fn hit(&mut self, r: &crate::ray::Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool {
let mut temp_rec = HitRecord::new_empty(); let mut temp_rec = HitRecord::new_empty(rec.mat_ptr.clone());
let mut hit_anything = false; let mut hit_anything = false;
let mut closest_so_far = t_max; let mut closest_so_far = t_max;

View File

@@ -10,6 +10,8 @@ pub mod material;
use std::{io::{BufWriter, Write, stdout}, fs::File, time::Instant}; use std::{io::{BufWriter, Write, stdout}, fs::File, time::Instant};
use material::Metal;
use crate::{ use crate::{
color::*, color::*,
sphere::*, sphere::*,
@@ -22,7 +24,7 @@ use crate::{
}; };
fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color { fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
let mut rec = HitRecord::new_empty(); let mut rec = HitRecord::new_empty(&Metal{ albedo: Color::new_empty() });
// Limit the bounces // Limit the bounces
if depth <= 0 { if depth <= 0 {
@@ -33,7 +35,7 @@ fn ray_color(r: &Ray, world: &mut dyn Hittable, depth: i32) -> Color {
let mut scattered = Ray::new_empty(); let mut scattered = Ray::new_empty();
let mut attenuation = Color::new_empty(); let mut attenuation = Color::new_empty();
if rec.mat_ptr //if rec.mat_ptr
} }
let unit_direction: Vec3 = r.direction().unit_vector(); let unit_direction: Vec3 = r.direction().unit_vector();
@@ -52,8 +54,8 @@ fn main() {
// World // World
let mut world = HittableList::new_empty(); 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, 0.0, -1.0), 0.5)));
world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0))); //world.add(Box::new(Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0)));
// Camera // Camera
let cam = Camera::new(); let cam = Camera::new();

View File

@@ -2,11 +2,7 @@ use crate::hittable::HitRecord;
use crate::ray::Ray; use crate::ray::Ray;
use crate::vec3::{Color, random_unit_vector, reflect}; use crate::vec3::{Color, random_unit_vector, reflect};
pub struct Material { pub trait Material: {
}
pub trait Scatter: {
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool; fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool;
} }
@@ -20,7 +16,7 @@ impl Lambertian {
} }
} }
impl Scatter for Lambertian { impl Material for Lambertian {
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool { fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
let scatter_direction = rec.normal + random_unit_vector(); let scatter_direction = rec.normal + random_unit_vector();
@@ -44,7 +40,7 @@ impl Metal {
} }
} }
impl Scatter for Metal { impl Material for Metal {
fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool { fn scatter(&self, r_in: &Ray, rec: HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
let reflected = reflect(&r_in.direction(), &rec.normal); let reflected = reflect(&r_in.direction(), &rec.normal);
scattered = &mut Ray::new(rec.p, rec.normal); scattered = &mut Ray::new(rec.p, rec.normal);

View File

@@ -1,22 +1,20 @@
use std::rc::Rc; use crate::hittable::*;
use crate::{hittable::*, material};
use crate::material::Material; use crate::material::Material;
use crate::vec3::*; use crate::vec3::*;
pub struct Sphere { pub struct Sphere<'a> {
center: Point3, center: Point3,
radius: f64, radius: f64,
mat_ptr: Rc<Material>, mat_ptr: &'a dyn Material,
} }
impl Sphere { impl Sphere<'_> {
pub fn new(center: Point3, radius: f64, mat_ptr: Rc<Material>) -> Self { pub fn new<T: Material>(center: Point3, radius: f64, mat_ptr: &T) -> Self {
Sphere {center, radius, mat_ptr} Sphere {center, radius, mat_ptr}
} }
} }
impl Hittable for Sphere { impl Hittable for Sphere<'_> {
fn hit(&mut self, r: &crate::ray::Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool { fn hit(&mut self, r: &crate::ray::Ray, t_min: f64, t_max: f64, rec: &mut HitRecord) -> bool {
let oc = r.origin() - self.center; let oc = r.origin() - self.center;
let a = r.direction().length_squared(); let a = r.direction().length_squared();