toytracer/wtracer.cpp

217 lines
6.8 KiB
C++
Raw Normal View History

2020-06-02 22:06:53 +00:00
#include <iostream>
2020-06-07 01:28:59 +00:00
#include <iomanip>
#include <atomic>
2020-06-06 14:31:51 +00:00
#include <memory>
2020-06-07 10:10:51 +00:00
#include <cstdio>
2020-06-02 22:06:53 +00:00
2020-06-07 01:28:59 +00:00
#define LODEPNG_NO_COMPILE_DECODER
#include "lodepng.h"
#include <boost/lockfree/queue.hpp>
2020-06-07 10:10:51 +00:00
#ifdef _OPENMP
#include <omp.h>
#endif
2020-06-02 22:06:53 +00:00
#include "color.h"
#include "vec3.h"
2020-06-03 20:39:40 +00:00
#include "ray.h"
2020-06-03 22:15:32 +00:00
#include "util.h"
#include "hittable_list.h"
#include "sphere.h"
2020-06-03 22:39:14 +00:00
#include "camera.h"
2020-06-03 22:15:32 +00:00
2020-06-06 12:47:39 +00:00
Color ray_color(const Ray& r, const Hittable& world, int depth) {
2020-06-03 22:15:32 +00:00
hit_record rec;
2020-06-06 12:47:39 +00:00
if (depth <= 0)
return Color(0, 0, 0);
2020-06-06 14:16:31 +00:00
if (world.hit(r, 0.001, infinity, rec)) {
Ray scattered;
Color attenuation;
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered))
return attenuation * ray_color(scattered, world, depth - 1);
return Color(0, 0, 0);
2020-06-03 21:25:47 +00:00
}
2020-06-03 20:39:40 +00:00
Vec3 unit_direction = unit_vector(r.direction());
2020-06-03 22:15:32 +00:00
auto t = 0.5 * (unit_direction.y() + 1.0);
2020-06-03 20:39:40 +00:00
return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);
}
2020-06-02 22:06:53 +00:00
Hittable_list setup_random_scene(const int sph_i) {
2020-06-06 23:02:11 +00:00
Hittable_list world;
auto ground_material = std::make_shared<Lambertian>(Color(0.5, 0.5, 0.5));
world.add(std::make_shared<Sphere>(Point3(0, -1000, 0), 1000, ground_material));
for (int a = -sph_i; a<sph_i; a++) {
for (int b = -sph_i; b<sph_i; b++) {
auto choose_mat = random_double();
Point3 center(a + 0.9 * (11/sph_i) * random_double(), 0.2, b + 0.9 * (11/sph_i) * random_double());
if ((center - Point3(4, 0.2, 0)).length() > 0.9) {
std::shared_ptr<Material> sphere_material;
if (choose_mat < 0.7) {
2020-06-06 23:02:11 +00:00
// diffuse
auto albedo = Color::random() * Color::random();
sphere_material = std::make_shared<Lambertian>(albedo);
world.add(std::make_shared<Sphere>(center, 0.2, sphere_material));
} else if (choose_mat < 0.95) {
// metal
auto albedo = Color::random(0.5, 1);
auto fuzz = random_double(0, 0.5);
sphere_material = std::make_shared<Metal>(albedo, fuzz);
world.add(std::make_shared<Sphere>(center, 0.2, sphere_material));
} else {
// glass
sphere_material = std::make_shared<Dielectric>(1.45);
world.add(std::make_shared<Sphere>(center, 0.2, sphere_material));
}
}
}
}
auto material1 = std::make_shared<Dielectric>(1.45);
world.add(std::make_shared<Sphere>(Point3(0, 1, 0), 1.0, material1));
auto material2 = std::make_shared<Lambertian>(Color(0.4, 0.2, 0.1));
world.add(std::make_shared<Sphere>(Point3(-4, 1, 0), 1.0, material2));
auto material3 = std::make_shared<Metal>(Color(0.7, 0.6, 0.5), 0.0);
world.add(std::make_shared<Sphere>(Point3(4, 1, 0), 1.0, material3));
return world;
}
2020-06-07 10:10:51 +00:00
struct render_params {
int image_width;
int image_height;
int samples_per_pixel;
int max_depth;
Hittable_list& world;
Camera& cam;
};
struct render_tile {
int start_x;
int start_y;
int end_x;
int end_y;
};
2020-06-07 10:10:51 +00:00
void split_line(const int& line_length, int& start, int& end) {
#ifdef _OPENMP
const int line_length_thread = static_cast<int>(line_length/omp_get_num_threads());
start = omp_get_thread_num() * line_length_thread;
end = std::min((omp_get_thread_num()+1) * line_length_thread, line_length);
#else
start = 0;
end = line_length;
#endif
}
void render(boost::lockfree::queue<render_tile>& queue,
std::atomic<int>& queue_counter,
const render_params& params, std::vector<Color>& image) {
render_tile rt;
while (queue.pop(rt)) {
--queue_counter;
int qc = queue_counter;
printf("\rTiles remaining: %4d", qc);
std::cout << std::flush;
for (int i = rt.start_x; i<rt.end_x; ++i) {
for (int j = rt.start_y; j<rt.end_y; ++j) {
Color pixel_color(0, 0, 0);
for (int s = 0; s<params.samples_per_pixel; ++s) {
auto u = double(j + random_double(-0.5, 0.5)) / (params.image_width - 1);
auto v = double(i + random_double(-0.5, 0.5)) / (params.image_height - 1);
Ray r = params.cam.get_ray(u, v);
pixel_color += ray_color(r, params.world, params.max_depth);
}
image[i*params.image_width+j] = pixel_color;
2020-06-07 10:10:51 +00:00
}
}
}
}
2020-06-02 22:06:53 +00:00
int main() {
2020-06-03 20:39:40 +00:00
const auto aspect_ratio = 16.0 / 9.0;
//const int image_width = 1920;
2020-06-06 12:47:39 +00:00
//const int image_width = 1280;
2020-06-07 10:10:51 +00:00
//const int image_width = 768;
const int image_width = 384;
2020-06-03 20:39:40 +00:00
const int image_height = static_cast<int>(image_width / aspect_ratio);
//const int samples_per_pixel = 2000;
//const int samples_per_pixel = 1000;
2020-06-06 14:16:31 +00:00
const int samples_per_pixel = 400;
2020-06-06 12:47:39 +00:00
const int max_depth = 50;
2020-06-02 22:06:53 +00:00
//const int tile_size = 128;
const int tile_size = 64;
const int sph_i = 3;
//const int sph_i = 5;
//const int sph_i = 7;
//const int sph_i = 11;
auto world = setup_random_scene(sph_i);
2020-06-06 23:02:11 +00:00
Point3 lookfrom(13, 2, 3);
Point3 lookat(0, 0, 0);
2020-06-06 22:32:31 +00:00
Vec3 vup(0, 1, 0);
auto dist_to_focus = (lookfrom - lookat).length();
2020-06-06 23:02:11 +00:00
auto aperture = 0.1;
2020-06-06 22:32:31 +00:00
Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
2020-06-03 22:39:14 +00:00
2020-06-07 10:10:51 +00:00
render_params params = {
image_width,
image_height,
samples_per_pixel,
max_depth,
world,
cam,
};
std::atomic<int> queue_counter;
boost::lockfree::queue<render_tile> queue(0);
for (int i = 0; i<std::ceil(double(image_height)/double(tile_size)); i++) {
for (int j = 0; j<std::ceil(double(image_width)/double(tile_size)); j++) {
render_tile rt = {
i*tile_size,
j*tile_size,
std::min((i+1)*tile_size, image_height),
std::min((j+1)*tile_size, image_width)
};
queue.push(rt);
++queue_counter;
}
}
2020-06-07 01:28:59 +00:00
std::vector<Color> image(image_width * image_height);
2020-06-07 10:10:51 +00:00
#pragma omp parallel
{
render(queue, queue_counter, params, image);
}
2020-06-06 23:02:11 +00:00
2020-06-07 01:28:59 +00:00
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) {
2020-06-07 10:10:51 +00:00
write_color_vec(img_lode, i*image_width*3+j*3, image[(image_height-1-i)*image_width+j], samples_per_pixel);
2020-06-02 22:06:53 +00:00
}
}
2020-06-07 01:28:59 +00:00
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";
2020-06-02 22:06:53 +00:00
}