diff --git a/main.cpp b/main.cpp index 115b234..e2cda12 100644 --- a/main.cpp +++ b/main.cpp @@ -18,22 +18,28 @@ #include "moving_sphere.h" #include "camera.h" -Color ray_color(const Ray& r, const Hittable& world, int depth) { +Color ray_color(const Ray& r, const Color& background, const Hittable& world, int depth) { hit_record rec; if (depth <= 0) return Color(0, 0, 0); - if (world.hit(r, 0.001, infinity, rec)) { - Ray scattered; - Color attenuation; - if (rec.mat_ptr->scatter(r, rec, attenuation, scattered)) - return attenuation * ray_color(scattered, world, depth - 1); - return Color(0, 0, 0); - } - Vec3 unit_direction = unit_vector(r.direction()); + if (!world.hit(r, 0.001, infinity, rec)) + return background; + + Ray scattered; + Color attenuation; + Color emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p); + + if (!rec.mat_ptr->scatter(r, rec, attenuation, scattered)) + return emitted; + + return emitted + attenuation * ray_color(scattered, background, world, depth - 1); + + // fading background + /*Vec3 unit_direction = unit_vector(r.direction()); auto t = 0.5 * (unit_direction.y() + 1.0); - return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0); + return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);*/ } Bvh_node setup_random_scene(const int sph_i) { @@ -123,9 +129,12 @@ 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 = 10000; + //const int samples_per_pixel = 5000; //const int samples_per_pixel = 2000; - //const int samples_per_pixel = 1000; - const int samples_per_pixel = 400; + const int samples_per_pixel = 1000; + //const int samples_per_pixel = 400; + //const int samples_per_pixel = 50; const int max_depth = 50; const int tile_size = 128; @@ -148,6 +157,7 @@ int main() { //auto aperture = 0.0; Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus, 0.0, 1.0); + const Color background(0, 0, 0); std::atomic queue_counter; boost::lockfree::queue queue(0); @@ -181,8 +191,8 @@ int main() { for (int s = 0; s a) : emit(a) {} + + virtual bool scatter(const Ray& r_in, const hit_record& rec, Color& attenuation, Ray& scattered) const { + return false; + } + + virtual Color emitted(double u, double v, const Point3& p) const { return emit->value(u, v, p); } + + private: + std::shared_ptr emit; +}; + #endif // MATERIAL_H