T4 Refraction (Milestone 2)
T4.1 Add refract() to calculate the refrac vector direction (vec3.h)
inline vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat) {
auto cos_theta = std::fmin(dot(-uv, n), 1.0);
vec3 r_out_perp = etai_over_etat * (uv + cos_theta*n);
vec3 r_out_parallel = -std::sqrt(std::fabs(1.0 - r_out_perp.length_squared())) * n;
return r_out_perp + r_out_parallel;
}T4.2 Add dielectric material (material.h)
class material {
//...
};
class lambertian : public material {
//...
};
class metal : public material {
//...
};
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
class dielectric : public material {
private:
// Refractive index in vacuum or air, or the ratio of the material's refractive index over
// the refractive index of the enclosing media
double refraction_index;
public:
dielectric(double refraction_index) : refraction_index(refraction_index) {}
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 ri = rec.front_face ? (1.0/refraction_index) : refraction_index;
vec3 unit_direction = unit_vector(r_in.direction());
// refract vector
vec3 refracted = refract(unit_direction, rec.normal, ri);
scattered = ray(rec.p, refracted);
return true;
}
};
//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<T4.3 Update the scene (main.cpp)
Build and Run Your Program

T4.4 Add total internal reflection and Schlick Approximation in
Add new material in main() and add a hollow glass sphere

Last updated