Add fuzziness to metal materials

This commit is contained in:
David 2021-08-22 22:45:10 +02:00
commit 9870dc128e
3 changed files with 10 additions and 5 deletions

View file

@ -37,13 +37,18 @@ struct lambertian : material {
struct metal : material {
/* Attributes */
color albedo;
double fuzz;
// Constructor
metal(const color& c) { albedo = c; };
metal(const color& c, double f)
{
albedo = c;
fuzz = f;
};
virtual bool scatter(const ray& r_in, const hit_record& rec, color& attenuation, ray& scattered) const override
{
vec3 reflected = reflect(normalize(r_in.direction), rec.normal);
scattered = ray(rec.p, reflected);
scattered = ray(rec.p, reflected + fuzz*random_in_unit_sphere());
attenuation = albedo;
return (dot(scattered.direction, rec.normal) > 0);
}