ray-tracer/src/renderer.h

122 lines
3.5 KiB
C
Raw Normal View History

#pragma once
2023-12-30 12:37:27 +01:00
#include <array>
#include <cassert>
#include <iostream>
2023-12-30 12:37:27 +01:00
#include <thread>
#include "camera.h"
#include "colour.h"
#include "image.h"
#include "interval.h"
2023-12-30 12:37:27 +01:00
#include "material.h"
#include "rand.h"
#include "ray.h"
#include "raytracer.h"
#include "renderobject.h"
#include "vec3.h"
class Renderer {
public:
2023-12-30 12:37:27 +01:00
constexpr explicit Renderer(Camera camera, u32 samples_per_pixel = 100, u32 max_bounces = 50)
: camera_{camera}, samples_per_pixel_{samples_per_pixel}, max_bounces_{max_bounces} {}
2023-12-30 12:37:27 +01:00
constexpr u32& samples_per_pixel() { return samples_per_pixel_; }
constexpr u32 samples_per_pixel() const { return samples_per_pixel_; }
constexpr u32& max_bounces() { return max_bounces_; }
constexpr u32 max_bounces() const { return max_bounces_; }
Image Render(const RenderObject& world) {
Image img{camera_.image_width(), camera_.image_height()};
2023-12-30 12:37:27 +01:00
constexpr u32 kNumThreads = 4;
2023-12-30 12:37:27 +01:00
auto render_thread = [this, &world, &img](u32 thread) {
for (u32 j = thread; j < img.height(); j += kNumThreads) {
std::clog << "\rWriting line " << j << " of " << img.height() << std::flush;
2023-12-30 12:37:27 +01:00
for (u32 i = 0; i < img.width(); i++) {
Colour colour_sum{0.0, 0.0, 0.0};
2023-12-30 12:37:27 +01:00
for (u32 num_sample = 0; num_sample < samples_per_pixel_; num_sample++) {
auto ray = SampleRay(i, j);
colour_sum += Cast(ray, world, max_bounces_);
}
2023-12-30 12:37:27 +01:00
img[i, j] = colour_sum / samples_per_pixel_;
}
}
2023-12-30 12:37:27 +01:00
};
// render_thread(0);
std::array<std::thread, kNumThreads> render_threads;
for (u32 i = 0; i < kNumThreads; i++) {
render_threads[i] = std::thread{render_thread, i};
}
2023-12-30 12:37:27 +01:00
for (auto& t : render_threads) {
t.join();
}
std::clog << "\rDone " << newline;
return img;
}
private:
constexpr Ray SampleRay(u32 i, u32 j) {
auto pixel_centre = camera_.PixelToWorld(i, j);
2023-12-29 10:59:06 +01:00
Vec3 random_shift = rand_.GenUniform(-0.5, 0.5) * camera_.d_u_pixel() +
rand_.GenUniform(-0.5, 0.5) * camera_.d_v_pixel();
2023-12-27 20:18:52 +01:00
pixel_centre += random_shift;
auto ray_direction = pixel_centre - camera_.centre();
return Ray{camera_.centre(), ray_direction};
}
2023-12-30 12:37:27 +01:00
constexpr Colour Cast(const Ray& ray, const RenderObject& world, u32 bounces) {
auto hit_record = world.hit(ray, Interval{0.001, kInf});
if (hit_record.has_value()) {
2023-12-30 12:37:27 +01:00
if (!hit_record->front_face || bounces == 0) {
return Colour::kBlack;
}
2023-12-30 12:37:27 +01:00
/* Vec3 normal = hit_record->normal;
2023-12-29 10:59:06 +01:00
Vec3 new_origin = hit_record->p;
2023-12-30 12:37:27 +01:00
// Vec3 new_direction = rand_.GenOnHemisphere(normal);
Vec3 new_direction = normal + rand_.GenOnUnitSphere();
Ray new_ray{new_origin, new_direction}; */
auto res = hit_record->mat->Scatter(ray, hit_record.value());
auto [albedo, out_ray] = res.value();
2023-12-29 10:59:06 +01:00
2023-12-30 12:37:27 +01:00
assert(bounces > 0);
return 0.5 * Cast(out_ray, world, bounces - 1);
}
auto unit_dir = ray.direction().normed();
f64 a = 0.5 * (unit_dir.y() + 1.0);
return (1.0 - a) * Colour{1.0, 1.0, 1.0} + a * Colour{0.5, 0.7, 1.0};
}
Camera camera_;
2023-12-29 10:59:06 +01:00
RandomGen rand_{};
u32 samples_per_pixel_;
2023-12-30 12:37:27 +01:00
u32 max_bounces_;
};