57 lines
1.3 KiB
C++
57 lines
1.3 KiB
C++
#ifndef MATERIAL_H
|
|
#define MATERIAL_H
|
|
|
|
#include "hittable.h"
|
|
#include "rtweekend.h"
|
|
#include "vec3.h"
|
|
|
|
struct hit_record;
|
|
|
|
class material {
|
|
public:
|
|
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered) const = 0;
|
|
};
|
|
|
|
|
|
class lambertian : public material {
|
|
public:
|
|
lambertian(const color& a) : albedo(a) {}
|
|
|
|
bool scatter(
|
|
const ray &r_in, const hit_record &rec, color &attenuation, ray &scattered
|
|
) const override {
|
|
auto scatter_direction = rec.normal + random_in_hemisphere(rec.normal);
|
|
|
|
// Catch degenerate scatter direction
|
|
if (scatter_direction.near_zero())
|
|
scatter_direction = rec.normal;
|
|
|
|
scattered = ray(rec.p, scatter_direction);
|
|
attenuation = albedo;
|
|
return true;
|
|
}
|
|
|
|
public:
|
|
color albedo;
|
|
};
|
|
|
|
class metal : public material {
|
|
public:
|
|
metal(const color& a, double f) : albedo(a), fuzz(f < 1 ? f : 1) {}
|
|
|
|
bool scatter(
|
|
const ray &r_in, const hit_record &rec, color &attenuation, ray &scattered
|
|
) const override {
|
|
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
|
|
scattered = ray(rec.p, reflected + fuzz*random_in_hemisphere(rec.normal));
|
|
attenuation = albedo;
|
|
return (dot(scattered.direction(), rec.normal) > 0);
|
|
}
|
|
|
|
public:
|
|
color albedo;
|
|
double fuzz;
|
|
};
|
|
|
|
#endif
|