#include #include #include #include #include #define LODEPNG_NO_COMPILE_DECODER #include "lodepng.h" #include #ifdef _OPENMP #include #endif #include "color.h" #include "vec3.h" #include "ray.h" #include "util.h" #include "hittable_list.h" #include "sphere.h" #include "camera.h" Color ray_color(const Ray& r, 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()); 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); } Hittable_list setup_random_scene(const int sph_i) { Hittable_list world; auto ground_material = std::make_shared(Color(0.5, 0.5, 0.5)); world.add(std::make_shared(Point3(0, -1000, 0), 1000, ground_material)); for (int a = -sph_i; a 0.9) { std::shared_ptr sphere_material; if (choose_mat < 0.7) { // diffuse auto albedo = Color::random() * Color::random(); sphere_material = std::make_shared(albedo); world.add(std::make_shared(center, 0.2, sphere_material)); } else if (choose_mat < 0.95) { // metal auto albedo = Color::random(0.5, 1); auto fuzz = random_double(0, 0.5); sphere_material = std::make_shared(albedo, fuzz); world.add(std::make_shared(center, 0.2, sphere_material)); } else { // glass sphere_material = std::make_shared(1.45); world.add(std::make_shared(center, 0.2, sphere_material)); } } } } auto material1 = std::make_shared(1.45); world.add(std::make_shared(Point3(0, 1, 0), 1.0, material1)); auto material2 = std::make_shared(Color(0.4, 0.2, 0.1)); world.add(std::make_shared(Point3(-4, 1, 0), 1.0, material2)); auto material3 = std::make_shared(Color(0.7, 0.6, 0.5), 0.0); world.add(std::make_shared(Point3(4, 1, 0), 1.0, material3)); return world; } struct render_params { int image_width; int image_height; int samples_per_pixel; int max_depth; Hittable_list& world; Camera& cam; }; struct render_tile { int start_x; int start_y; int end_x; int end_y; }; void split_line(const int& line_length, int& start, int& end) { #ifdef _OPENMP const int line_length_thread = static_cast(line_length/omp_get_num_threads()); start = omp_get_thread_num() * line_length_thread; end = std::min((omp_get_thread_num()+1) * line_length_thread, line_length); #else start = 0; end = line_length; #endif } void render(boost::lockfree::queue& queue, std::atomic& queue_counter, const render_params& params, std::vector& image) { render_tile rt; while (queue.pop(rt)) { --queue_counter; int qc = queue_counter; printf("\rTiles remaining: %4d", qc); std::cout << std::flush; for (int i = rt.start_x; i(image_width / aspect_ratio); //const int samples_per_pixel = 2000; //const int samples_per_pixel = 1000; const int samples_per_pixel = 400; const int max_depth = 50; //const int tile_size = 128; const int tile_size = 64; const int sph_i = 3; //const int sph_i = 5; //const int sph_i = 7; //const int sph_i = 11; auto world = setup_random_scene(sph_i); Point3 lookfrom(13, 2, 3); Point3 lookat(0, 0, 0); Vec3 vup(0, 1, 0); auto dist_to_focus = (lookfrom - lookat).length(); auto aperture = 0.1; Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus); render_params params = { image_width, image_height, samples_per_pixel, max_depth, world, cam, }; std::atomic queue_counter; boost::lockfree::queue queue(0); for (int i = 0; i image(image_width * image_height); #pragma omp parallel { render(queue, queue_counter, params, image); } std::cerr << "\nAssembling image.\n"; std::vector img_lode(image_width * image_height * 3); for (int i = 0; i