Output images as png.

This commit is contained in:
Faerbit 2020-06-07 03:28:59 +02:00
parent f8a7934a03
commit eaf035cdee
6 changed files with 8401 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.o *.o
wtracer wtracer
*.ppm *.ppm
*.png

View File

@ -2,16 +2,16 @@ CXX = g++
CXXFLAGS = -std=c++14 -Wall -Wextra -Wno-unused-parameter -march=native -Ofast -fopenmp -flto CXXFLAGS = -std=c++14 -Wall -Wextra -Wno-unused-parameter -march=native -Ofast -fopenmp -flto
DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h material.h DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h material.h lodepng.h
OBJ = wtracer.o material.o vec3.o OBJ = wtracer.o material.o vec3.o lodepng.o
TARGET = wtracer TARGET = wtracer
all: $(TARGET) run all: $(TARGET) run
run: $(TARGET) run: $(TARGET)
time -f '%E elapsed' ./wtracer > image.ppm time -f '%E elapsed' ./wtracer
eog image.ppm eog image.png
%.o: %.cpp $(DEPS) %.o: %.cpp $(DEPS)
$(CXX) $(CXXFLAGS) -c -o $@ $< $(CXX) $(CXXFLAGS) -c -o $@ $<

17
color.h
View File

@ -1,8 +1,10 @@
#ifndef COLOR_H #ifndef COLOR_H
#define COLOR_H #define COLOR_H
#include <vector>
#include <iostream> #include <iostream>
#include <cmath> #include <cmath>
#include <cstddef>
#include "vec3.h" #include "vec3.h"
#include "util.h" #include "util.h"
@ -22,4 +24,19 @@ void write_color(std::ostream &out, Color pixel_color, int sample_per_pixel) {
<< static_cast<int>(256 * clamp(b, 0.0, 0.999)) << "\n"; << static_cast<int>(256 * clamp(b, 0.0, 0.999)) << "\n";
} }
void write_color_vec(std::vector<unsigned char>& vec, std::size_t i, Color pixel_color, int sample_per_pixel) {
auto r = pixel_color.x();
auto g = pixel_color.y();
auto b = pixel_color.z();
auto scale = 1.0 / sample_per_pixel;
r = std::sqrt(scale * r);
g = std::sqrt(scale * g);
b = std::sqrt(scale * b);
vec[i+0] = static_cast<unsigned char>(256 * clamp(r, 0.0, 0.999));
vec[i+1] = static_cast<unsigned char>(256 * clamp(g, 0.0, 0.999));
vec[i+2] = static_cast<unsigned char>(256 * clamp(b, 0.0, 0.999));
}
#endif // COLOR_H #endif // COLOR_H

6410
lodepng.cpp Normal file

File diff suppressed because it is too large Load Diff

1945
lodepng.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,14 @@
#include <iostream> #include <iostream>
#include <iomanip>
#include <memory> #include <memory>
#define LODEPNG_NO_COMPILE_DECODER
#include "lodepng.h"
#include "color.h" #include "color.h"
#include "vec3.h" #include "vec3.h"
#include "ray.h" #include "ray.h"
#include "util.h" #include "util.h"
#include "hittable_list.h" #include "hittable_list.h"
#include "sphere.h" #include "sphere.h"
#include "camera.h" #include "camera.h"
@ -95,29 +98,37 @@ int main() {
Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus); Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
auto image = std::make_unique<Color[]>(image_height*image_width); std::vector<Color> image(image_width * image_height);
for (int j = image_height - 1; j >= 0; --j) { for (int i = image_height-1; i>=0; --i) {
std::cerr << "\rScanlines remaining: " << j << " " << std::flush; std::cerr << "\rScanline remaining: " << std::setw(4) << i << std::flush;
#pragma omp parallel for #pragma omp parallel for
for (int i = 0; i < image_width; ++i) { for (int j = 0; j<image_width; ++j) {
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 = double(i + random_double(-0.5, 0.5)) / (image_width - 1); auto u = double(j + random_double(-0.5, 0.5)) / (image_width - 1);
auto v = double(j + random_double(-0.5, 0.5)) / (image_height - 1); auto v = double(i + random_double(-0.5, 0.5)) / (image_height - 1);
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);
} }
image[i*image_height+j] = pixel_color; image.at(i*image_width+j) = pixel_color;
} }
} }
std::cerr << "\nWriting file.\n";
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; std::cerr << "\nAssembling image.\n";
for (int j = image_height - 1; j >= 0; --j) {
for (int i = 0; i < image_width; ++i) { std::vector<unsigned char> img_lode(image_width * image_height * 3);
write_color(std::cout, image[i*image_height+j], samples_per_pixel); for (int i = 0; i<image_height; ++i) {
for (int j = 0; j<image_width; ++j) {
write_color_vec(img_lode, i*image_width*3+j*3, image.at((image_height-1-i)*image_width+j), samples_per_pixel);
} }
} }
std::cerr << "Writing file \"image.png\".\n";
unsigned error = lodepng::encode("image.png", img_lode, image_width, image_height, LCT_RGB);
if (error)
std::cerr << "lodepng encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
std::cerr << "Done.\n"; std::cerr << "Done.\n";
} }