feat: add dialectric material
This commit is contained in:
parent
14ac9f2ec0
commit
4d2b7757d4
19
main.cc
19
main.cc
@ -33,21 +33,22 @@ int main() {
|
|||||||
const auto aspect_ratio = 16.0 / 9.0;
|
const auto aspect_ratio = 16.0 / 9.0;
|
||||||
const int image_width = 400;
|
const int image_width = 400;
|
||||||
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
||||||
const int samples_per_pixel = 16;
|
const int samples_per_pixel = 32;
|
||||||
const int max_depth = 25;
|
const int max_depth = 32;
|
||||||
|
|
||||||
// World
|
// World
|
||||||
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<lambertian>(color(0.7, 0.3, 0.3));
|
auto material_center = make_shared<lambertian>(color(0.1, 0.2, 0.5));
|
||||||
auto material_left = make_shared<metal>(color(0.8, 0.8, 0.8), 0.3);
|
auto material_left = make_shared<dialectric>(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.1);
|
||||||
|
|
||||||
world.add(make_shared<sphere>(point3(0, -100.5, -1), 100, material_ground));
|
world.add(make_shared<sphere>(point3( 0, -100.5, -1), 100, material_ground));
|
||||||
world.add(make_shared<sphere>(point3(0, 0, -1), 0.5, material_center));
|
world.add(make_shared<sphere>(point3( 0, 0, -1), 0.5, material_center));
|
||||||
world.add(make_shared<sphere>(point3(-1, 0, -1), 0.5, material_left));
|
world.add(make_shared<sphere>(point3(-1, 0, -1), 0.5, material_left));
|
||||||
world.add(make_shared<sphere>(point3(1, 0, -1), 0.5, material_right));
|
world.add(make_shared<sphere>(point3(-1, 0, -1), -0.4, material_left));
|
||||||
|
world.add(make_shared<sphere>(point3( 1, 0, -1), 0.5, material_right));
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
camera cam;
|
camera cam;
|
||||||
|
38
material.h
38
material.h
@ -53,4 +53,42 @@ class metal : public material {
|
|||||||
double fuzz;
|
double fuzz;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class dialectric : public material {
|
||||||
|
public:
|
||||||
|
dialectric(double index_of_refraction): ir(index_of_refraction) {}
|
||||||
|
|
||||||
|
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());
|
||||||
|
double cos_theta = fmin(dot(-unit_direction, rec.normal), 1.0);
|
||||||
|
double sin_theta = sqrt(1 - cos_theta*cos_theta);
|
||||||
|
|
||||||
|
bool cannot_refract = refraction_ratio * sin_theta > 1.0;
|
||||||
|
vec3 direction;
|
||||||
|
if (cannot_refract || reflectance(cos_theta, refraction_ratio) > random_double())
|
||||||
|
direction = reflect(unit_direction, rec.normal);
|
||||||
|
else
|
||||||
|
direction = refract(unit_direction, rec.normal, refraction_ratio);
|
||||||
|
|
||||||
|
scattered = ray(rec.p, direction);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public:
|
||||||
|
double ir; // Index of Refraction
|
||||||
|
|
||||||
|
private:
|
||||||
|
static double reflectance(double cosine, double ref_idx) {
|
||||||
|
// Use Schlick's approximation for reflectance
|
||||||
|
auto r0 = (1-ref_idx) /(1+ref_idx);
|
||||||
|
r0 = r0*r0;
|
||||||
|
return r0 + (1-r0)*pow((1 - cosine), 5);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
7
vec3.h
7
vec3.h
@ -138,4 +138,11 @@ vec3 reflect(const vec3& v, const vec3& n) {
|
|||||||
return v - 2*dot(v,n)*n;
|
return v - 2*dot(v,n)*n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vec3 refract(const vec3& uv, const vec3& n, double etai_over_etat) {
|
||||||
|
auto cos_theta = fmin(dot(-uv, n), 1.0);
|
||||||
|
vec3 r_out_perp = etai_over_etat * (uv + cos_theta*n);
|
||||||
|
vec3 r_out_parrallel = -sqrt(fabs(1.0 - r_out_perp.length_squared())) * n;
|
||||||
|
return r_out_perp + r_out_parrallel;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user