Skip to content

Commit 89b87c1

Browse files
committed
😲 somethings zjebane but I have no idea what...
1 parent fff5d55 commit 89b87c1

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

src/material.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@ impl Lambert {
4747
impl Scatter for Lambert {
4848
fn scatter(&self, _ray_in: &Ray, rec: &HitRecord, attenuation: &mut Vec3D, scattered: &mut Ray) -> bool {
4949
let target = rec.p + rec.normal + random_in_unit_sphere();
50-
let new_scattered = Ray::new(rec.p, target - rec.p);
51-
scattered.direction = new_scattered.direction;
52-
scattered.origin = new_scattered.origin;
53-
attenuation.x = self.albedo.x;
54-
attenuation.y = self.albedo.y;
55-
attenuation.z = self.albedo.z;
50+
*scattered = Ray::new(rec.p, target - rec.p);
51+
*attenuation = self.albedo;
5652
true
5753
}
5854
}
@@ -75,12 +71,8 @@ impl Metal {
7571
impl Scatter for Metal {
7672
fn scatter(&self, ray_in: &Ray, rec: &HitRecord, attenuation: &mut Vec3D, scattered: &mut Ray) -> bool {
7773
let reflected = reflect(ray_in.direction.unit(), rec.normal);
78-
let new_scattered = Ray::new(rec.p, reflected + random_in_unit_sphere() * self.fuzz);
79-
scattered.direction = new_scattered.direction;
80-
scattered.origin = new_scattered.origin;
81-
attenuation.x = self.albedo.x;
82-
attenuation.y = self.albedo.y;
83-
attenuation.z = self.albedo.z;
74+
*scattered = Ray::new(rec.p, reflected + random_in_unit_sphere() * self.fuzz);
75+
*attenuation = self.albedo;
8476
scattered.direction.dot(rec.normal) > 0.0
8577
}
8678
}
@@ -102,23 +94,21 @@ impl Scatter for Dielectric {
10294
fn scatter(&self, ray_in: &Ray, rec: &HitRecord, attenuation: &mut Vec3D, scattered: &mut Ray) -> bool {
10395
let outward_normal: Vec3D;
10496
let ni_over_nt: f64;
105-
let mut refracted = Vec3D::new(0.0, 0.0, 0.0);
97+
let mut refracted = Vec3D::new(1.0, 1.0, 1.0);
10698
let reflect_prob: f64;
10799
let cosine: f64;
108100

109-
let reflected = reflect(ray_in.direction.unit(), rec.normal);
110-
attenuation.x = 1.0;
111-
attenuation.y = 1.0;
112-
attenuation.z = 1.0;
101+
let reflected = reflect(ray_in.direction, rec.normal);
102+
*attenuation = Vec3D::new(1.0, 1.0, 1.0);
113103

114104
if ray_in.direction.dot(rec.normal) > 0.0 {
115105
outward_normal = rec.normal * -1.0;
116106
ni_over_nt = self.ref_idx;
117107
cosine = self.ref_idx * ray_in.direction.dot(rec.normal) / ray_in.direction.mag();
118108
} else {
119-
outward_normal = rec.normal;
109+
outward_normal = rec.normal * 1.0;
120110
ni_over_nt = 1.0 / self.ref_idx;
121-
cosine = ray_in.direction.dot(rec.normal) * -1.0 / ray_in.direction.mag();
111+
cosine = (ray_in.direction.dot(rec.normal) * -1.0) / ray_in.direction.mag();
122112
}
123113

124114
if refract(ray_in.direction, outward_normal, ni_over_nt, &mut refracted) {
@@ -128,13 +118,9 @@ impl Scatter for Dielectric {
128118
}
129119

130120
if rand::random::<f64>() < reflect_prob {
131-
// let new_scattered = Ray::new(rec.p, reflected);
132-
scattered.direction = reflected;
133-
scattered.origin = rec.p;
121+
*scattered = Ray::new(rec.p, reflected);
134122
} else {
135-
// let new_scattered = Ray::new(rec.p, refracted);
136-
scattered.direction = refracted;
137-
scattered.origin = rec.p;
123+
*scattered = Ray::new(rec.p, refracted);
138124
}
139125

140126
true

src/utils.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ pub fn refract(v: Vec3D, n: Vec3D, ni_over_nt: f64, refracted: &mut Vec3D) -> bo
5353
let discriminant = 1.0 - ni_over_nt * ni_over_nt * (1.0 - dt * dt);
5454

5555
if discriminant > 0.0 {
56-
let new_refracted = (uv - n * dt) * ni_over_nt - n * (discriminant.sqrt());
57-
refracted.x = new_refracted.x;
58-
refracted.y = new_refracted.y;
59-
refracted.z = new_refracted.z;
56+
*refracted = (uv - n * dt) * ni_over_nt - n * (discriminant.sqrt());
6057
return true;
6158
} else {
6259
return false;

0 commit comments

Comments
 (0)