Output images as png.
This commit is contained in:
parent
f8a7934a03
commit
eaf035cdee
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
*.o
|
||||
wtracer
|
||||
*.ppm
|
||||
*.png
|
||||
|
8
Makefile
8
Makefile
@ -2,16 +2,16 @@ CXX = g++
|
||||
|
||||
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
|
||||
OBJ = wtracer.o material.o vec3.o
|
||||
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 lodepng.o
|
||||
|
||||
TARGET = wtracer
|
||||
|
||||
all: $(TARGET) run
|
||||
|
||||
run: $(TARGET)
|
||||
time -f '%E elapsed' ./wtracer > image.ppm
|
||||
eog image.ppm
|
||||
time -f '%E elapsed' ./wtracer
|
||||
eog image.png
|
||||
|
||||
%.o: %.cpp $(DEPS)
|
||||
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||
|
17
color.h
17
color.h
@ -1,8 +1,10 @@
|
||||
#ifndef COLOR_H
|
||||
#define COLOR_H
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
|
||||
#include "vec3.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";
|
||||
}
|
||||
|
||||
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
|
||||
|
6410
lodepng.cpp
Normal file
6410
lodepng.cpp
Normal file
File diff suppressed because it is too large
Load Diff
37
wtracer.cpp
37
wtracer.cpp
@ -1,11 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
|
||||
#define LODEPNG_NO_COMPILE_DECODER
|
||||
#include "lodepng.h"
|
||||
|
||||
#include "color.h"
|
||||
#include "vec3.h"
|
||||
#include "ray.h"
|
||||
#include "util.h"
|
||||
|
||||
#include "hittable_list.h"
|
||||
#include "sphere.h"
|
||||
#include "camera.h"
|
||||
@ -95,29 +98,37 @@ int main() {
|
||||
|
||||
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) {
|
||||
std::cerr << "\rScanlines remaining: " << j << " " << std::flush;
|
||||
for (int i = image_height-1; i>=0; --i) {
|
||||
std::cerr << "\rScanline remaining: " << std::setw(4) << i << std::flush;
|
||||
#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);
|
||||
for (int s = 0; s<samples_per_pixel; ++s) {
|
||||
auto u = double(i + random_double(-0.5, 0.5)) / (image_width - 1);
|
||||
auto v = double(j + random_double(-0.5, 0.5)) / (image_height - 1);
|
||||
auto u = double(j + random_double(-0.5, 0.5)) / (image_width - 1);
|
||||
auto v = double(i + random_double(-0.5, 0.5)) / (image_height - 1);
|
||||
Ray r = cam.get_ray(u, v);
|
||||
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";
|
||||
for (int j = image_height - 1; j >= 0; --j) {
|
||||
for (int i = 0; i < image_width; ++i) {
|
||||
write_color(std::cout, image[i*image_height+j], samples_per_pixel);
|
||||
std::cerr << "\nAssembling image.\n";
|
||||
|
||||
std::vector<unsigned char> img_lode(image_width * image_height * 3);
|
||||
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";
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user