In-memory picture

This commit is contained in:
Armin Friedl 2022-07-30 16:20:36 +02:00
parent 6f206bb898
commit 77e0bf6b4b
3 changed files with 54 additions and 14 deletions

View file

@ -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()

View file

@ -5,20 +5,51 @@
#include "vec3.h"
#include <iostream>
#include <unordered_map>
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<int>(255.999 * clamp(r, 0.0, 0.999)) << ' '
<< static_cast<int>(255.999 * clamp(g, 0.0, 0.999)) << ' '
<< static_cast<int>(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<int, color> 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<int>(255.999 * clamp(r, 0.0, 0.999)) << ' '
<< static_cast<int>(255.999 * clamp(g, 0.0, 0.999)) << ' '
<< static_cast<int>(255.999 * clamp(b, 0.0, 0.999)) << '\n';
}
};
#endif // RTIWW_COLOR_H

View file

@ -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;