Refraction

This commit is contained in:
Armin Friedl 2022-07-30 18:41:07 +02:00
parent 7c2cead577
commit 8f7a44935a
3 changed files with 97 additions and 73 deletions

View file

@ -46,11 +46,12 @@ int main() {
hittable_list world;
auto material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0));
auto material_center = make_shared<lambertian>(color(0.7, 0.3, 0.3));
auto material_left = make_shared<metal>(color(0.8, 0.8, 0.8), 0.3);
auto material_center = make_shared<dielectric>(1.5);
auto material_left = make_shared<dielectric>(1.5);
auto material_right = make_shared<metal>(color(0.8, 0.6, 0.2), 1.0);
world.add(make_shared<sphere>(point3( 0.0, -100.5, -1.0), 100.0, material_ground));
world.add(
make_shared<sphere>(point3(0.0, -100.5, -1.0), 100.0, material_ground));
world.add(make_shared<sphere>(point3(0.0, 0.0, -1.0), 0.5, material_center));
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, material_left));
world.add(make_shared<sphere>(point3(1.0, 0.0, -1.0), 0.5, material_right));

View file

@ -13,15 +13,12 @@ public:
color &attenuation, ray &scattered) const = 0;
};
class lambertian : public material {
public:
lambertian(const color &a) : albedo(a) {}
virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
) const override {
virtual bool scatter(const ray &r_in, const hit_record &rec,
color &attenuation, ray &scattered) const override {
auto scatter_direction = rec.normal + random_in_unit_sphere();
// Catch degenerate scatter direction
@ -41,9 +38,8 @@ class metal : public material {
public:
metal(const color &a, double f) : albedo(a), fuzz(f < 1 ? f : 1) {}
virtual bool scatter(
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
) const override {
virtual bool scatter(const ray &r_in, const hit_record &rec,
color &attenuation, ray &scattered) const override {
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
scattered = ray(rec.p, reflected + fuzz * random_in_unit_sphere());
attenuation = albedo;
@ -55,4 +51,24 @@ public:
double fuzz;
};
class dielectric : public material {
public:
dielectric(double index_of_refraction) : ir(index_of_refraction) {}
virtual bool scatter(const ray &r_in, const hit_record &rec,
color &attenuation, ray &scattered) const override {
attenuation = color(1.0, 1.0, 1.0);
double refraction_ratio = rec.front_face ? (1.0 / ir) : ir;
vec3 unit_direction = unit_vector(r_in.direction());
vec3 refracted = refract(unit_direction, rec.normal, refraction_ratio);
scattered = ray(rec.p, refracted);
return true;
}
public:
double ir;
};
#endif // MATERIAL_H_

View file

@ -124,4 +124,11 @@ vec3 random_in_hemisphere(const vec3 &normal) {
vec3 reflect(const vec3 &v, const vec3 &n) { return v - 2 * dot(v, n) * n; }
vec3 refract(const vec3 &v, const vec3 &n, double etai_over_etat) {
auto cos_theta = fmin(dot(-v, n), 1.0);
vec3 r_out_perp = etai_over_etat * (v + cos_theta * n);
vec3 r_out_parallel = -sqrt(fabs(1.0 - r_out_perp.length_squared())) * n;
return r_out_perp + r_out_parallel;
}
#endif // RTIWW_VEC3_H