From 267df23c87d02da419951a04021b44570e803e3b Mon Sep 17 00:00:00 2001 From: Faerbit Date: Sat, 6 Jun 2020 16:16:31 +0200 Subject: [PATCH] Add additional scattering methods. --- Makefile | 2 +- vec3.h | 15 +++++++++++++++ wtracer.cpp | 8 +++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index cc4f0df..283401a 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ TARGET = wtracer all: $(TARGET) run run: - ./wtracer > image.ppm + time -f '%E elapsed' ./wtracer > image.ppm eog image.ppm %.o: %.cpp $(DEPS) diff --git a/vec3.h b/vec3.h index df7f98b..92f42ea 100644 --- a/vec3.h +++ b/vec3.h @@ -120,4 +120,19 @@ Vec3 random_in_unit_sphere() { } } +Vec3 random_unit_vector() { + auto a = random_double(0, 2*pi); + auto z = random_double(-1, 1); + auto r = std::sqrt(1 - z*z); + return Vec3(r*std::cos(a), r*std::sin(a), z); +} + +Vec3 random_in_hemisphere(const Vec3& normal) { + Vec3 in_unit_sphere = random_in_unit_sphere(); + if (dot(in_unit_sphere, normal) > 0.0) + return in_unit_sphere; + else + return -in_unit_sphere; +} + #endif // VEC3_H diff --git a/wtracer.cpp b/wtracer.cpp index f21d0ad..d197041 100644 --- a/wtracer.cpp +++ b/wtracer.cpp @@ -15,8 +15,10 @@ Color ray_color(const Ray& r, const Hittable& world, int depth) { if (depth <= 0) return Color(0, 0, 0); - if (world.hit(r, 0, infinity, rec)) { - Point3 target = rec.p + rec.normal + random_in_unit_sphere(); + if (world.hit(r, 0.001, infinity, rec)) { + //Point3 target = rec.p + rec.normal + random_in_unit_sphere(); + //Point3 target = rec.p + rec.normal + random_unit_vector(); + Point3 target = rec.p + random_in_hemisphere(rec.normal); return 0.5 * ray_color(Ray(rec.p, target - rec.p), world, depth-1); } Vec3 unit_direction = unit_vector(r.direction()); @@ -30,7 +32,7 @@ int main() { const int image_width = 768; //const int image_width = 384; const int image_height = static_cast(image_width / aspect_ratio); - const int samples_per_pixel = 250; + const int samples_per_pixel = 400; const int max_depth = 50; std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";