Add fuzziness to metal materials.

This commit is contained in:
Faerbit 2020-06-06 23:28:33 +02:00
parent bdb68df296
commit 129854222d
3 changed files with 6 additions and 5 deletions

View File

@ -11,7 +11,7 @@ bool Lambertian::scatter(const Ray& r_in, const hit_record& rec, Color& attenuat
bool Metal::scatter(const Ray& r_in, const hit_record& rec, Color& attenuation, Ray& scattered) const {
Vec3 reflected = reflect(unit_vector(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);
}

View File

@ -23,11 +23,12 @@ private:
class Metal : public Material {
public:
Metal(const Color& a) : albedo(a) {}
Metal(const Color& a, double f) : albedo(a), fuzz(f < 1 ? f : 1) {}
virtual bool scatter(const Ray& r_in, const hit_record& rec, Color& attenuation, Ray& scattered) const;
private:
Color albedo;
double fuzz;
};
#endif // MATERIAL_H

View File

@ -45,10 +45,10 @@ int main() {
world.add(std::make_shared<Sphere>(Point3(0, -100.5, -1), 100));*/
world.add(std::make_shared<Sphere>(Point3(0, 0, -1), 0.5, std::make_shared<Lambertian>(Color(0.7, 0.3, 0.3))));
world.add(std::make_shared<Sphere>(Point3(0, -100.5, -1), 100, std::make_shared<Lambertian>(Color(0.8, 0.8, 0.8))));
world.add(std::make_shared<Sphere>(Point3(0, -100.5, -1), 100, std::make_shared<Lambertian>(Color(0.8, 0.8, 0.0))));
world.add(std::make_shared<Sphere>(Point3(1, 0, -1), 0.5, std::make_shared<Metal>(Color(0.8, 0.6, 0.2))));
world.add(std::make_shared<Sphere>(Point3(-1, 0, -1), 0.5, std::make_shared<Metal>(Color(0.8, 0.8, 0.8))));
world.add(std::make_shared<Sphere>(Point3(1, 0, -1), 0.5, std::make_shared<Metal>(Color(0.8, 0.6, 0.2), 0.3)));
world.add(std::make_shared<Sphere>(Point3(-1, 0, -1), 0.5, std::make_shared<Metal>(Color(0.8, 0.8, 0.8), 1.0)));
Camera cam;