Total internal reflection
This commit is contained in:
parent
8f7a44935a
commit
7ca342d5a4
2 changed files with 17 additions and 8 deletions
|
@ -46,9 +46,9 @@ int main() {
|
||||||
hittable_list world;
|
hittable_list world;
|
||||||
|
|
||||||
auto material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0));
|
auto material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0));
|
||||||
auto material_center = make_shared<dielectric>(1.5);
|
auto material_center = make_shared<lambertian>(color(0.1, 0.2, 0.5));
|
||||||
auto material_left = 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);
|
auto material_right = make_shared<metal>(color(0.8, 0.6, 0.2), 0.0);
|
||||||
|
|
||||||
world.add(
|
world.add(
|
||||||
make_shared<sphere>(point3(0.0, -100.5, -1.0), 100.0, material_ground));
|
make_shared<sphere>(point3(0.0, -100.5, -1.0), 100.0, material_ground));
|
||||||
|
|
|
@ -58,12 +58,21 @@ public:
|
||||||
virtual bool scatter(const ray &r_in, const hit_record &rec,
|
virtual bool scatter(const ray &r_in, const hit_record &rec,
|
||||||
color &attenuation, ray &scattered) const override {
|
color &attenuation, ray &scattered) const override {
|
||||||
attenuation = color(1.0, 1.0, 1.0);
|
attenuation = color(1.0, 1.0, 1.0);
|
||||||
double refraction_ratio = rec.front_face ? (1.0 / ir) : ir;
|
double refraction_ratio = rec.front_face ? (1.0/ir) : ir;
|
||||||
|
|
||||||
vec3 unit_direction = unit_vector(r_in.direction());
|
vec3 unit_direction = unit_vector(r_in.direction());
|
||||||
vec3 refracted = refract(unit_direction, rec.normal, refraction_ratio);
|
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
|
||||||
|
double sin_theta = sqrt(1.0 - cos_theta*cos_theta);
|
||||||
|
|
||||||
scattered = ray(rec.p, refracted);
|
bool cannot_refract = refraction_ratio * sin_theta > 1.0;
|
||||||
|
vec3 direction;
|
||||||
|
|
||||||
|
if (cannot_refract)
|
||||||
|
direction = reflect(unit_direction, rec.normal);
|
||||||
|
else
|
||||||
|
direction = refract(unit_direction, rec.normal, refraction_ratio);
|
||||||
|
|
||||||
|
scattered = ray(rec.p, direction);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue