diff --git a/rtiww/CMakeLists.txt b/rtiww/CMakeLists.txt index 6da8934..c1c9b96 100644 --- a/rtiww/CMakeLists.txt +++ b/rtiww/CMakeLists.txt @@ -10,3 +10,8 @@ add_executable(rtiww src/main.cpp src/sphere.h src/camera.h src/rtweekend.h) + +find_package(OpenMP) +if(OpenMP_CXX_FOUND) + target_link_libraries(rtiww PUBLIC OpenMP::OpenMP_CXX) +endif() diff --git a/rtiww/src/color.h b/rtiww/src/color.h index 9e9622a..7b2038d 100644 --- a/rtiww/src/color.h +++ b/rtiww/src/color.h @@ -5,20 +5,51 @@ #include "vec3.h" #include +#include -void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) { - auto r = pixel_color.x(); - auto g = pixel_color.y(); - auto b = pixel_color.z(); +class picture { +public: + picture() : buffer{} {} - auto scale = 1.0 / samples_per_pixel; - r = sqrt(scale * r); - g = sqrt(scale * g); - b = sqrt(scale * b); + void set_pixel(int line, int offset, color pixel_color) { + int hash = pairing(line, offset); + this->buffer[hash] = pixel_color; + } - out << static_cast(255.999 * clamp(r, 0.0, 0.999)) << ' ' - << static_cast(255.999 * clamp(g, 0.0, 0.999)) << ' ' - << static_cast(255.999 * clamp(b, 0.0, 0.999)) << '\n'; -} + void write(std::ostream &out, int image_height, int image_width, + int samples_per_pixel) { + for (int j = image_height - 1; j >= 0; --j) { + for (int i = 0; i < image_width; ++i) { + int hash = pairing(j, i); + color c = this->buffer[hash]; + write_color(out, c, samples_per_pixel); + } + } + } + +public: + std::unordered_map buffer; + +private: + int pairing(int a, int b) { + return 0.5 * (a + b) * (a + b + 1) + b; + } + + void write_color(std::ostream &out, color pixel_color, + int samples_per_pixel) { + auto r = pixel_color.x(); + auto g = pixel_color.y(); + auto b = pixel_color.z(); + + auto scale = 1.0 / samples_per_pixel; + r = sqrt(scale * r); + g = sqrt(scale * g); + b = sqrt(scale * b); + + out << static_cast(255.999 * clamp(r, 0.0, 0.999)) << ' ' + << static_cast(255.999 * clamp(g, 0.0, 0.999)) << ' ' + << static_cast(255.999 * clamp(b, 0.0, 0.999)) << '\n'; + } +}; #endif // RTIWW_COLOR_H diff --git a/rtiww/src/main.cpp b/rtiww/src/main.cpp index ea5edce..8d92978 100644 --- a/rtiww/src/main.cpp +++ b/rtiww/src/main.cpp @@ -44,10 +44,12 @@ int main() { // Render std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n"; + picture p; + for (int j = image_height - 1; j >= 0; --j) { - std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush; for (int i = 0; i < image_width; ++i) { color pixel_color(0, 0, 0); + for (int s = 0; s < samples_per_pixel; ++s) { auto u = (i + random_double()) / (image_width - 1); auto v = (j + random_double()) / (image_height - 1); @@ -55,10 +57,12 @@ int main() { ray r = cam.get_ray(u, v); pixel_color += ray_color(r, world, max_depth); } - write_color(std::cout, pixel_color, samples_per_pixel); + + p.set_pixel(j,i,pixel_color); } } + p.write(std::cout, image_height, image_width, samples_per_pixel); std::cerr << "\nDone.\n"; return 0;