Refraction
This commit is contained in:
parent
7c2cead577
commit
8f7a44935a
3 changed files with 97 additions and 73 deletions
|
@ -16,19 +16,19 @@ color ray_color(const ray &r, const hittable &world, int depth) {
|
||||||
|
|
||||||
// If we've exceeded the ray bounce limit, no more light is gathered.
|
// If we've exceeded the ray bounce limit, no more light is gathered.
|
||||||
if (depth <= 0)
|
if (depth <= 0)
|
||||||
return color(0,0,0);
|
return color(0, 0, 0);
|
||||||
|
|
||||||
if (world.hit(r, 0.000001, infinity, rec)) {
|
if (world.hit(r, 0.000001, infinity, rec)) {
|
||||||
ray scattered;
|
ray scattered;
|
||||||
color attenuation;
|
color attenuation;
|
||||||
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered))
|
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered))
|
||||||
return attenuation * ray_color(scattered, world, depth-1);
|
return attenuation * ray_color(scattered, world, depth - 1);
|
||||||
return color(0,0,0);
|
return color(0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 unit_direction = unit_vector(r.direction());
|
vec3 unit_direction = unit_vector(r.direction());
|
||||||
auto t = 0.5*(unit_direction.y() + 1.0);
|
auto t = 0.5 * (unit_direction.y() + 1.0);
|
||||||
return (1.0-t)*color(1.0, 1.0, 1.0) + t*color(0.5, 0.7, 1.0);
|
return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
@ -46,14 +46,15 @@ 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<lambertian>(color(0.7, 0.3, 0.3));
|
auto material_center = make_shared<dielectric>(1.5);
|
||||||
auto material_left = make_shared<metal>(color(0.8, 0.8, 0.8), 0.3);
|
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), 1.0);
|
||||||
|
|
||||||
world.add(make_shared<sphere>(point3( 0.0, -100.5, -1.0), 100.0, material_ground));
|
world.add(
|
||||||
world.add(make_shared<sphere>(point3( 0.0, 0.0, -1.0), 0.5, material_center));
|
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_left));
|
||||||
world.add(make_shared<sphere>(point3( 1.0, 0.0, -1.0), 0.5, material_right));
|
world.add(make_shared<sphere>(point3(1.0, 0.0, -1.0), 0.5, material_right));
|
||||||
|
|
||||||
// Camera
|
// Camera
|
||||||
|
|
||||||
|
@ -63,13 +64,13 @@ int main() {
|
||||||
|
|
||||||
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
|
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
|
||||||
|
|
||||||
for (int j = image_height-1; j >= 0; --j) {
|
for (int j = image_height - 1; j >= 0; --j) {
|
||||||
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
||||||
for (int i = 0; i < image_width; ++i) {
|
for (int i = 0; i < image_width; ++i) {
|
||||||
color pixel_color(0, 0, 0);
|
color pixel_color(0, 0, 0);
|
||||||
for (int s = 0; s < samples_per_pixel; ++s) {
|
for (int s = 0; s < samples_per_pixel; ++s) {
|
||||||
auto u = (i + random_double()) / (image_width-1);
|
auto u = (i + random_double()) / (image_width - 1);
|
||||||
auto v = (j + random_double()) / (image_height-1);
|
auto v = (j + random_double()) / (image_height - 1);
|
||||||
ray r = cam.get_ray(u, v);
|
ray r = cam.get_ray(u, v);
|
||||||
pixel_color += ray_color(r, world, max_depth);
|
pixel_color += ray_color(r, world, max_depth);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,16 +13,13 @@ public:
|
||||||
color &attenuation, ray &scattered) const = 0;
|
color &attenuation, ray &scattered) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class lambertian : public material {
|
class lambertian : public material {
|
||||||
public:
|
public:
|
||||||
lambertian(const color& a) : albedo(a) {}
|
lambertian(const color &a) : albedo(a) {}
|
||||||
|
|
||||||
virtual bool scatter(
|
virtual bool scatter(const ray &r_in, const hit_record &rec,
|
||||||
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
|
color &attenuation, ray &scattered) const override {
|
||||||
) const override {
|
auto scatter_direction = rec.normal + random_in_unit_sphere();
|
||||||
auto scatter_direction = rec.normal+random_in_unit_sphere();
|
|
||||||
|
|
||||||
// Catch degenerate scatter direction
|
// Catch degenerate scatter direction
|
||||||
if (scatter_direction.near_zero())
|
if (scatter_direction.near_zero())
|
||||||
|
@ -39,13 +36,12 @@ public:
|
||||||
|
|
||||||
class metal : public material {
|
class metal : public material {
|
||||||
public:
|
public:
|
||||||
metal(const color& a, double f) : albedo(a), fuzz(f<1 ? f: 1) {}
|
metal(const color &a, double f) : albedo(a), fuzz(f < 1 ? f : 1) {}
|
||||||
|
|
||||||
virtual bool scatter(
|
virtual bool scatter(const ray &r_in, const hit_record &rec,
|
||||||
const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered
|
color &attenuation, ray &scattered) const override {
|
||||||
) const override {
|
|
||||||
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
|
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
|
||||||
scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere());
|
scattered = ray(rec.p, reflected + fuzz * random_in_unit_sphere());
|
||||||
attenuation = albedo;
|
attenuation = albedo;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -55,4 +51,24 @@ public:
|
||||||
double fuzz;
|
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_
|
#endif // MATERIAL_H_
|
||||||
|
|
|
@ -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 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
|
#endif // RTIWW_VEC3_H
|
||||||
|
|
Loading…
Reference in a new issue