Skip to content

Commit

Permalink
glass
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherBiscardi committed Aug 28, 2023
1 parent 40e2313 commit 63fa363
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/main.rs
Expand Up @@ -10,12 +10,11 @@ fn main() -> io::Result<()> {
let material_ground = Material::Lambertian {
albedo: DVec3::new(0.8, 0.8, 0.0),
};
let material_center = Material::Lambertian {
albedo: DVec3::new(0.7, 0.3, 0.3),
let material_center = Material::Dielectric {
index_of_refraction: 1.5,
};
let material_left = Material::Metal {
albedo: DVec3::new(0.8, 0.8, 0.8),
fuzz: 0.3,
let material_left = Material::Dielectric {
index_of_refraction: 1.5,
};
let material_right = Material::Metal {
albedo: DVec3::new(0.8, 0.6, 0.2),
Expand Down Expand Up @@ -253,6 +252,7 @@ trait Hittable {
enum Material {
Lambertian { albedo: DVec3 },
Metal { albedo: DVec3, fuzz: f64 },
Dielectric { index_of_refraction: f64 },
}
struct Scattered {
attenuation: DVec3,
Expand Down Expand Up @@ -312,6 +312,33 @@ impl Material {
None
}
}
Material::Dielectric {
index_of_refraction,
} => {
let attenuation = DVec3::splat(1.0);
let refraction_ratio: f64 =
if hit_record.front_face {
index_of_refraction.recip()
} else {
*index_of_refraction
};

let unit_direction =
r_in.direction.normalize();
let refracted = refract(
unit_direction,
hit_record.normal,
refraction_ratio,
);

Some(Scattered {
attenuation,
scattered: Ray {
origin: hit_record.point,
direction: refracted,
},
})
}
_ => None,
}
}
Expand Down Expand Up @@ -512,3 +539,16 @@ fn random_on_hemisphere(normal: &DVec3) -> DVec3 {
fn reflect(v: DVec3, n: DVec3) -> DVec3 {
return v - 2. * v.dot(n) * n;
}

fn refract(
uv: DVec3,
n: DVec3,
etai_over_etat: f64,
) -> DVec3 {
let cos_theta = (-uv).dot(n).min(1.0);
let r_out_perp = etai_over_etat * (uv + cos_theta * n);
let r_out_parallel: DVec3 =
-((1.0 - r_out_perp.length_squared()).abs()).sqrt()
* n;
return r_out_perp + r_out_parallel;
}

0 comments on commit 63fa363

Please sign in to comment.