Skip to content

Commit 19260f9

Browse files
committed
fix it with enums (incredibly unreadable - im sorry me)
1 parent d246af4 commit 19260f9

File tree

6 files changed

+48
-18
lines changed

6 files changed

+48
-18
lines changed

src/camera.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use std::{f64::INFINITY, io::stdout};
22

33
use rand::{thread_rng, Rng};
44

5-
use crate::{color::{write_color, Color}, hittable::{HitRecord, Hittable}, interval::Interval, ray::Ray, vec3::{random_unit_vector, unit_vector, Point3, Vec3}};
6-
5+
use crate::{color::{write_color, Color}, hittable::{HitRecord, Hittable}, interval::Interval, ray::Ray, vec3::{unit_vector, Point3, Vec3}};
6+
use crate::material::Material;
77
#[derive(Default)]
88
pub struct Camera {
99
pub aspect_ratio: f64,

src/hittable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use crate::{interval::Interval, material::Material, ray::Ray, vec3::{dot, Point3, Vec3}};
1+
use crate::{interval::Interval, material::EMaterial, ray::Ray, vec3::{dot, Point3, Vec3}};
22

33
#[derive(Clone, Default)]
44
pub struct HitRecord {
55
pub p: Point3,
66
pub normal: Vec3,
7-
pub mat: Box<dyn Material>,
7+
pub mat: Box<EMaterial>,
88
pub t: f64,
99
pub front_face: bool,
1010
}

src/hittable_list.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ impl Hittable for HittableList {
2020
for object in &self.objects {
2121
if object.hit(r, Interval::from(ray_t.min, closest_so_far), &mut temp_rec) {
2222
hit_anything = true;
23-
closest_so_far = temp_rec.t;
24-
*rec = temp_rec;
23+
closest_so_far = temp_rec.clone().t;
24+
*rec = temp_rec.clone();
2525
}
2626
}
2727
hit_anything

src/main.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use camera::Camera;
2-
use material::Lambertian;
2+
use color::Color;
3+
use material::{Lambertian, Metal};
34

45
use crate::{hittable_list::HittableList, sphere::Sphere, vec3::Point3};
56

@@ -14,14 +15,22 @@ mod camera;
1415
mod material;
1516

1617
fn main() {
17-
let mat1 = Lambertian::default();
18+
let ground = material::EMaterial::Lambertian(Lambertian {albedo: Color::new(0.8,0.8,0.0)});
19+
let center = material::EMaterial::Lambertian(Lambertian {albedo: Color::new(0.1, 0.2, 0.5)});
20+
let left = material::EMaterial::Metal(Metal {albedo: Color::new(0.8, 0.8, 0.8)});
21+
let right = material::EMaterial::Metal(Metal {albedo: Color::new(0.8, 0.6, 0.2)});
22+
1823
let mut world = HittableList::new();
1924

20-
let sphere1 = Sphere::new(Point3::new(0.0, 0.0, -1.0), 0.5, Box::new(mat1));
21-
let sphere2 = Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0, Box::new(mat1));
25+
let ground = Sphere::new(Point3::new(0.0, -100.5, -1.0), 100.0, Box::new(ground));
26+
let center = Sphere::new(Point3::new(0.0, 0.0, -1.2), 0.5, Box::new(center));
27+
let left = Sphere::new(Point3::new(-1.0, 0.0, -1.0), 0.5, Box::new(left));
28+
let right = Sphere::new(Point3::new(1.0, 0.0, -1.0), 0.5, Box::new(right));
2229

23-
world.add(Box::new(sphere1));
24-
world.add(Box::new(sphere2));
30+
world.add(Box::new(ground));
31+
world.add(Box::new(center));
32+
world.add(Box::new(left));
33+
world.add(Box::new(right));
2534

2635
let mut cam = Camera::new(16.0/9.0, 400);
2736
cam.samples_per_pixel = 100;

src/material.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
use std::default;
2+
13
use crate::{color::Color, hittable::HitRecord, ray::Ray, vec3::{random_unit_vector, reflect, Vec3}};
24

3-
pub trait Material: Clone + Default {
5+
#[derive(Clone, Copy, Default)]
6+
pub enum EMaterial {
7+
Lambertian(Lambertian),
8+
Metal(Metal),
9+
#[default]
10+
DefaultMaterial
11+
}
12+
13+
impl Material for EMaterial {
14+
fn scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
15+
match self {
16+
EMaterial::Lambertian(mat) => mat.scatter(r_in, rec, attenuation, scattered),
17+
EMaterial::Metal(mat) => mat.scatter(r_in, rec, attenuation, scattered),
18+
EMaterial::DefaultMaterial => false,
19+
// Implement scatter method for other variants
20+
}
21+
}
22+
}
23+
pub trait Material {
424
fn scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool;
525
}
626
#[derive(Default, Clone, Copy)]
7-
pub struct Lambertian { albedo: Color }
27+
pub struct Lambertian { pub albedo: Color }
828
impl Material for Lambertian {
929
fn scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
1030
let mut scatter_dir = rec.normal + random_unit_vector();
@@ -21,7 +41,7 @@ impl Material for Lambertian {
2141
}
2242

2343
#[derive(Default, Clone, Copy)]
24-
pub struct Metal { albedo: Color }
44+
pub struct Metal { pub albedo: Color }
2545
impl Material for Metal {
2646
fn scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
2747
let reflected = reflect(r_in.dir(), &rec.normal);

src/sphere.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use crate::{hittable::{HitRecord, Hittable}, interval::Interval, material::Material, vec3::{dot, Point3}};
1+
use crate::{hittable::{HitRecord, Hittable}, interval::Interval, material::EMaterial, vec3::{dot, Point3}};
22

33
pub struct Sphere {
44
center: Point3,
55
radius: f64,
6-
mat: Box<dyn Material>
6+
mat: Box<EMaterial>
77
}
88
impl Sphere {
9-
pub fn new(center: Point3, radius: f64, mat: Box<dyn Material>) -> Self {
9+
pub fn new(center: Point3, radius: f64, mat: Box<EMaterial>) -> Self {
1010
Sphere {center, radius, mat}
1111
}
1212
}
@@ -36,6 +36,7 @@ impl Hittable for Sphere {
3636
rec.p = r.at(rec.t);
3737
let outward_normal = (rec.p - self.center) / self.radius;
3838
rec.set_face_normal(r, &outward_normal);
39+
rec.mat = self.mat.clone();
3940

4041
true
4142
}

0 commit comments

Comments
 (0)