In-memory picture
This commit is contained in:
parent
6f206bb898
commit
77e0bf6b4b
3 changed files with 54 additions and 14 deletions
|
@ -10,3 +10,8 @@ add_executable(rtiww src/main.cpp
|
||||||
src/sphere.h
|
src/sphere.h
|
||||||
src/camera.h
|
src/camera.h
|
||||||
src/rtweekend.h)
|
src/rtweekend.h)
|
||||||
|
|
||||||
|
find_package(OpenMP)
|
||||||
|
if(OpenMP_CXX_FOUND)
|
||||||
|
target_link_libraries(rtiww PUBLIC OpenMP::OpenMP_CXX)
|
||||||
|
endif()
|
||||||
|
|
|
@ -5,20 +5,51 @@
|
||||||
#include "vec3.h"
|
#include "vec3.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
|
class picture {
|
||||||
auto r = pixel_color.x();
|
public:
|
||||||
auto g = pixel_color.y();
|
picture() : buffer{} {}
|
||||||
auto b = pixel_color.z();
|
|
||||||
|
|
||||||
auto scale = 1.0 / samples_per_pixel;
|
void set_pixel(int line, int offset, color pixel_color) {
|
||||||
r = sqrt(scale * r);
|
int hash = pairing(line, offset);
|
||||||
g = sqrt(scale * g);
|
this->buffer[hash] = pixel_color;
|
||||||
b = sqrt(scale * b);
|
}
|
||||||
|
|
||||||
out << static_cast<int>(255.999 * clamp(r, 0.0, 0.999)) << ' '
|
void write(std::ostream &out, int image_height, int image_width,
|
||||||
<< static_cast<int>(255.999 * clamp(g, 0.0, 0.999)) << ' '
|
int samples_per_pixel) {
|
||||||
<< static_cast<int>(255.999 * clamp(b, 0.0, 0.999)) << '\n';
|
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
|
#endif // RTIWW_COLOR_H
|
||||||
|
|
|
@ -44,10 +44,12 @@ int main() {
|
||||||
// Render
|
// Render
|
||||||
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
|
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
|
||||||
|
|
||||||
|
picture p;
|
||||||
|
|
||||||
for (int j = image_height - 1; j >= 0; --j) {
|
for (int j = image_height - 1; j >= 0; --j) {
|
||||||
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
|
|
||||||
for (int i = 0; i < image_width; ++i) {
|
for (int i = 0; i < image_width; ++i) {
|
||||||
color pixel_color(0, 0, 0);
|
color pixel_color(0, 0, 0);
|
||||||
|
|
||||||
for (int s = 0; s < samples_per_pixel; ++s) {
|
for (int s = 0; s < samples_per_pixel; ++s) {
|
||||||
auto u = (i + random_double()) / (image_width - 1);
|
auto u = (i + random_double()) / (image_width - 1);
|
||||||
auto v = (j + random_double()) / (image_height - 1);
|
auto v = (j + random_double()) / (image_height - 1);
|
||||||
|
@ -55,10 +57,12 @@ int main() {
|
||||||
ray r = cam.get_ray(u, v);
|
ray r = cam.get_ray(u, v);
|
||||||
pixel_color += ray_color(r, world, max_depth);
|
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";
|
std::cerr << "\nDone.\n";
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Reference in a new issue