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

@ -1,4 +1,4 @@
raytracer: main.cpp vec3.hpp color.hpp ray.hpp hittable.hpp sphere.hpp hittable_list.hpp rtweekend.hpp raytracer: camera.hpp color.hpp hittable.hpp hittable_list.hpp material.hpp ray.hpp rtweekend.hpp sphere.hpp vec3.hpp
@g++ -g -O2 -Wall -Wextra -Wpedantic main.cpp -o raytracer @g++ -g -O2 -Wall -Wextra -Wpedantic main.cpp -o raytracer
image: raytracer image: raytracer

View file

@ -75,8 +75,8 @@ int main()
std::shared_ptr<lambertian> material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0)); std::shared_ptr<lambertian> material_ground = make_shared<lambertian>(color(0.8, 0.8, 0.0));
std::shared_ptr<lambertian> material_center = make_shared<lambertian>(color(0.7, 0.3, 0.3)); std::shared_ptr<lambertian> material_center = make_shared<lambertian>(color(0.7, 0.3, 0.3));
std::shared_ptr<metal> material_left = make_shared<metal>(color(0.8, 0.8, 0.8)); std::shared_ptr<metal> material_left = make_shared<metal>(color(0.8, 0.8, 0.8), 0.3);
std::shared_ptr<metal> material_right = make_shared<metal>(color(0.8, 0.6, 0.2)); std::shared_ptr<metal> 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(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( 0.0, 0.0, -1.0), 0.5, material_center));

View file

@ -37,13 +37,18 @@ struct lambertian : material {
struct metal : material { struct metal : material {
/* Attributes */ /* Attributes */
color albedo; color albedo;
double fuzz;
// Constructor // 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 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); 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; attenuation = albedo;
return (dot(scattered.direction, rec.normal) > 0); return (dot(scattered.direction, rec.normal) > 0);
} }