Refactor scatter method

This commit is contained in:
2026-02-17 02:13:20 +05:30
committed by GitHub
parent 31aebe5ade
commit 12c0a2e387
+3 -4
View File
@@ -23,7 +23,7 @@ class lambertian : public material {
bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered) bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered)
const override { const override {
auto scatter_direction = rec.normal + random_unit_vector(); auto scatter_direction = rec.normal + random_unit_vector();
// Catch degenerate scatter direction
if (scatter_direction.near_zero()) if (scatter_direction.near_zero())
scatter_direction = rec.normal; scatter_direction = rec.normal;
@@ -81,11 +81,10 @@ class dielectric : public material {
} }
private: 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; double refraction_index;
static double reflectance(double cosine, double refraction_index) { static double reflectance(double cosine, double refraction_index) {
// Use Schlick's approximation for reflectance.
auto r0 = (1 - refraction_index) / (1 + refraction_index); auto r0 = (1 - refraction_index) / (1 + refraction_index);
r0 = r0*r0; r0 = r0*r0;
return r0 + (1-r0)*std::pow((1 - cosine),5); return r0 + (1-r0)*std::pow((1 - cosine),5);