diff --git a/wtracer.cpp b/wtracer.cpp index ddfd00c..fa40e3b 100644 --- a/wtracer.cpp +++ b/wtracer.cpp @@ -1,11 +1,14 @@ #include #include +#include #include #include #define LODEPNG_NO_COMPILE_DECODER #include "lodepng.h" +#include + #ifdef _OPENMP #include #endif @@ -36,11 +39,9 @@ Color ray_color(const Ray& r, const Hittable& world, int depth) { return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0); } -Hittable_list setup_random_scene() { +Hittable_list setup_random_scene(const int sph_i) { Hittable_list world; - const int sph_i = 3; - 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.8) { + if (choose_mat < 0.7) { // diffuse auto albedo = Color::random() * Color::random(); sphere_material = std::make_shared(albedo); @@ -92,6 +93,13 @@ struct render_params { 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()); @@ -103,38 +111,51 @@ void split_line(const int& line_length, int& start, int& end) { #endif } -void render(const render_params& params, std::vector& image) { - int line_start, line_end; - split_line(params.image_width, line_start, line_end); - for (int i = params.image_height-1; i>=0; --i) { - #ifdef _OPENMP - if (omp_get_thread_num() == 0) - #endif - std::cerr << "\rScanline remaining: " << std::setw(4) << i << std::flush; - for (int j = line_start; j& 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; - auto world = setup_random_scene(); + //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); @@ -153,11 +174,27 @@ int main() { cam, }; + std::atomic queue_counter; + boost::lockfree::queue queue(0); + + for (int i = 0; i image(image_width * image_height); #pragma omp parallel { - render(params, image); + render(queue, queue_counter, params, image); } std::cerr << "\nAssembling image.\n";