Compare commits

...

4 Commits

Author SHA1 Message Date
Faerbit
af86494d89 Add cornell box; move camera creation to scene setup. 2020-06-13 22:02:57 +02:00
Faerbit
d401600f1c Add Flip_face. 2020-06-13 22:02:26 +02:00
Faerbit
7699f26cf2 Add diffuse lights. 2020-06-13 22:01:52 +02:00
Faerbit
f9605a7da2 Add axis aligned rectangles. 2020-06-13 22:00:05 +02:00
6 changed files with 262 additions and 30 deletions

View File

@ -2,8 +2,8 @@ CXX = g++
CXXFLAGS = -std=c++20 -Wall -Wextra -Wno-unused-parameter -march=native -O3 -flto -fopenmp
DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h material.h lodepng.h moving_sphere.h bvh.h aabb.h texture.h
OBJ = main.o material.o vec3.o lodepng.o sphere.o moving_sphere.o bvh.o
DEPS = util.h vec3.h color.h ray.h camera.h hittable.h hittable_list.h sphere.h material.h lodepng.h moving_sphere.h bvh.h aabb.h texture.h aarect.h
OBJ = main.o material.o vec3.o lodepng.o sphere.o moving_sphere.o bvh.o aarect.o
TARGET = toytracer

79
aarect.cpp Normal file
View File

@ -0,0 +1,79 @@
#include "aarect.h"
bool Xy_rect::bounding_box(double t0, double t1, Aabb& output_box) const {
output_box = Aabb(Point3(x0, y0, k - 0.0001), Point3(x1, y1, k + 0.0001));
return true;
}
bool Xz_rect::bounding_box(double t0, double t1, Aabb& output_box) const {
output_box = Aabb(Point3(x0, k - 0.0001, z0), Point3(x1, k + 0.0001, z1));
return true;
}
bool Yz_rect::bounding_box(double t0, double t1, Aabb& output_box) const {
output_box = Aabb(Point3(k - 0.0001, y0, z0), Point3(k + 0.0001, y1, z1));
return true;
}
bool Xy_rect::hit(const Ray& r, double t0, double t1, hit_record& rec) const {
auto t = (k - r.origin().z()) / r.direction().z();
if (t < t0 || t > t1)
return false;
auto x = r.origin().x() + t * r.direction().x();
auto y = r.origin().y() + t * r.direction().y();
if (x < x0 || x > x1 || y < y0 || y > y1)
return false;
rec.u = (x - x0) / (x1 - x0);
rec.v = (y - y0) / (y1 - y0);
rec.t = t;
auto outward_normal = Vec3(0, 0, 1);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mat_ptr;
rec.p = r.at(t);
return true;
}
bool Xz_rect::hit(const Ray& r, double t0, double t1, hit_record& rec) const {
auto t = (k - r.origin().y()) / r.direction().y();
if (t < t0 || t > t1)
return false;
auto x = r.origin().x() + t * r.direction().x();
auto z = r.origin().z() + t * r.direction().z();
if (x < x0 || x > x1 || z < z0 || z > z1)
return false;
rec.u = (x - x0) / (x1 - x0);
rec.v = (z - z0) / (z1 - z0);
rec.t = t;
auto outward_normal = Vec3(0, 1, 0);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mat_ptr;
rec.p = r.at(t);
return true;
}
bool Yz_rect::hit(const Ray& r, double t0, double t1, hit_record& rec) const {
auto t = (k - r.origin().x()) / r.direction().x();
if (t < t0 || t > t1)
return false;
auto y = r.origin().y() + t * r.direction().y();
auto z = r.origin().z() + t * r.direction().z();
if (y < y0 || y > y1 || z < z0 || z > z1)
return false;
rec.u = (y - y0) / (y1 - y0);
rec.v = (z - z0) / (z1 - z0);
rec.t = t;
auto outward_normal = Vec3(1, 0, 0);
rec.set_face_normal(r, outward_normal);
rec.mat_ptr = mat_ptr;
rec.p = r.at(t);
return true;
}

55
aarect.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef AARECT_H
#define AARECT_H
#include "hittable.h"
#include "material.h"
class Xy_rect : public Hittable {
public:
Xy_rect() {}
Xy_rect(double x0_, double x1_, double y0_, double y1_, double k_, std::shared_ptr<Material> mat)
: x0(x0_), x1(x1_), y0(y0_), y1(y1_), k(k_), mat_ptr(mat) {}
virtual bool hit(const Ray& r, double t0, double t1, hit_record& rec) const;
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const;
private:
double x0, x1, y0, y1, k;
std::shared_ptr<Material> mat_ptr;
};
class Xz_rect : public Hittable {
public:
Xz_rect() {}
Xz_rect(double x0_, double x1_, double z0_, double z1_, double k_, std::shared_ptr<Material> mat)
: x0(x0_), x1(x1_), z0(z0_), z1(z1_), k(k_), mat_ptr(mat) {}
virtual bool hit(const Ray& r, double t0, double t1, hit_record& rec) const;
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const;
private:
double x0, x1, z0, z1, k;
std::shared_ptr<Material> mat_ptr;
};
class Yz_rect : public Hittable {
public:
Yz_rect() {}
Yz_rect(double y0_, double y1_, double z0_, double z1_, double k_, std::shared_ptr<Material> mat)
: y0(y0_), y1(y1_), z0(z0_), z1(z1_), k(k_), mat_ptr(mat) {}
virtual bool hit(const Ray& r, double t0, double t1, hit_record& rec) const;
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const;
private:
double y0, y1, z0, z1, k;
std::shared_ptr<Material> mat_ptr;
};
#endif // AARECT_H

View File

@ -30,4 +30,24 @@ public:
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const = 0;
};
class Flip_face : public Hittable {
public:
Flip_face(std::shared_ptr<Hittable> p) : ptr(p) {}
virtual bool hit(const Ray &r, double tmin, double tmax, hit_record &rec) const {
if (!ptr->hit(r, tmin, tmax, rec))
return false;
rec.front_face = !rec.front_face;
return true;
}
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const {
return ptr->bounding_box(t0, t1, output_box);
}
private:
std::shared_ptr<Hittable> ptr;
};
#endif // HITTABLE_H

119
main.cpp
View File

@ -3,6 +3,7 @@
#include <atomic>
#include <memory>
#include <cstdio>
#include <utility>
#define LODEPNG_NO_COMPILE_DECODER
#include "lodepng.h"
@ -17,26 +18,33 @@
#include "sphere.h"
#include "moving_sphere.h"
#include "camera.h"
#include "aarect.h"
Color ray_color(const Ray& r, const Hittable& world, int depth) {
Color ray_color(const Ray& r, const Color& background, const Hittable& world, int depth) {
hit_record rec;
if (depth <= 0)
return Color(0, 0, 0);
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);
}
Vec3 unit_direction = unit_vector(r.direction());
if (!world.hit(r, 0.001, infinity, rec))
return background;
Ray scattered;
Color attenuation;
Color emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p);
if (!rec.mat_ptr->scatter(r, rec, attenuation, scattered))
return emitted;
return emitted + attenuation * ray_color(scattered, background, world, depth - 1);
// fading background
/*Vec3 unit_direction = unit_vector(r.direction());
auto t = 0.5 * (unit_direction.y() + 1.0);
return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);
return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);*/
}
Bvh_node setup_random_scene(const int sph_i) {
std::pair<Bvh_node, Camera> setup_random_scene(const int sph_i, const double aspect_ratio) {
Hittable_list world;
//auto ground_material = std::make_shared<Lambertian>(std::make_shared<Solid_color>(Color(0.5, 0.5, 0.5)));
@ -90,7 +98,17 @@ Bvh_node setup_random_scene(const int sph_i) {
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.generate_bvh(0, 1);
Point3 lookfrom(13, 2, 3);
Point3 lookat(0, 0, 0);
Vec3 vup(0, 1, 0);
auto dist_to_focus = (lookfrom - lookat).length();
//auto dist_to_focus = 10;
auto aperture = 0.1;
auto vfov = 20;
Camera cam(lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus, 0.0, 1.0);
return std::make_pair(world.generate_bvh(0, 1), cam);
}
Hittable_list two_spheres() {
@ -109,6 +127,54 @@ Hittable_list two_spheres() {
return objects;
}
Hittable_list simple_light() {
Hittable_list objects;
auto checker = std::make_shared<Checker_texture>(
std::make_shared<Solid_color>(0.2, 0.3, 0.1),
std::make_shared<Solid_color>(0.9, 0.9, 0.9)
);
auto material = std::make_shared<Lambertian>(checker);
objects.add(std::make_shared<Sphere>(Point3(0, -1000, 0), 1000, material));
objects.add(std::make_shared<Sphere>(Point3(0, 2, 0), 2, material));
auto difflight = std::make_shared<Diffuse_light>(std::make_shared<Solid_color>(4, 4, 4));
objects.add(std::make_shared<Sphere>(Point3(0, 7, 0), 2, difflight));
objects.add(std::make_shared<Xy_rect>(3, 5, 1, 3, -2, difflight));
return objects;
}
std::pair<Hittable_list, Camera> cornell_box(const double aspect_ratio) {
Hittable_list objects;
auto red = std::make_shared<Lambertian>(std::make_shared<Solid_color>(0.65, 0.05, 0.05));
auto white = std::make_shared<Lambertian>(std::make_shared<Solid_color>(0.73, 0.73, 0.73));
auto green = std::make_shared<Lambertian>(std::make_shared<Solid_color>(0.12, 0.45, 0.15));
auto light = std::make_shared<Diffuse_light>(std::make_shared<Solid_color>(15, 15, 15));
objects.add(std::make_shared<Flip_face>(std::make_shared<Yz_rect>( 0, 555, 0, 555, 555, green)));
objects.add(std::make_shared<Yz_rect>( 0, 555, 0, 555, 0, red ));
objects.add(std::make_shared<Xz_rect>(213, 343, 227, 332, 554, light));
objects.add(std::make_shared<Flip_face>(std::make_shared<Xz_rect>( 0, 555, 0, 555, 0, white)));
objects.add(std::make_shared<Xz_rect>( 0, 555, 0, 555, 555, white));
objects.add(std::make_shared<Flip_face>(std::make_shared<Xy_rect>( 0, 555, 0, 555, 555, white)));
Point3 lookfrom(278, 278, -800);
Point3 lookat(278, 278, 0);
Vec3 vup(0, 1, 0);
//auto dist_to_focus = (lookfrom - lookat).length();
auto dist_to_focus = 10;
auto aperture = 0.0;
auto vfov = 40;
Camera cam(lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus, 0.0, 1.0);
return std::make_pair(objects, cam);
}
struct render_tile {
int start_x;
int start_y;
@ -123,9 +189,12 @@ int main() {
const int image_width = 768;
//const int image_width = 384;
const int image_height = static_cast<int>(image_width / aspect_ratio);
//const int samples_per_pixel = 10000;
//const int samples_per_pixel = 5000;
//const int samples_per_pixel = 2000;
//const int samples_per_pixel = 1000;
const int samples_per_pixel = 400;
const int samples_per_pixel = 1000;
//const int samples_per_pixel = 400;
//const int samples_per_pixel = 50;
const int max_depth = 50;
const int tile_size = 128;
@ -133,21 +202,15 @@ int main() {
//const int sph_i = 3;
//const int sph_i = 5;
const int sph_i = 7;
//const int sph_i = 7;
//const int sph_i = 11;
auto world = setup_random_scene(sph_i);
//auto world = two_spheres();
//auto [world, camera] = setup_random_scene(sph_i, aspect_ratio);
//auto [world, camera] = two_spheres(aspect_ratio);
//auto [world, camera] = simple_light(aspect_ratio);
auto [world, camera] = cornell_box(aspect_ratio);
Point3 lookfrom(13, 2, 3);
Point3 lookat(0, 0, 0);
Vec3 vup(0, 1, 0);
//auto dist_to_focus = (lookfrom - lookat).length();
auto dist_to_focus = 10;
auto aperture = 0.1;
//auto aperture = 0.0;
Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus, 0.0, 1.0);
const Color background(0, 0, 0);
std::atomic<int> queue_counter;
boost::lockfree::queue<render_tile> queue(0);
@ -181,8 +244,8 @@ int main() {
for (int s = 0; s<samples_per_pixel; ++s) {
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);
Ray r = camera.get_ray(u, v);
pixel_color += ray_color(r, background, world, max_depth);
}
image[i*image_width+j] = pixel_color;
}

View File

@ -10,6 +10,7 @@ struct hit_record;
class Material {
public:
virtual bool scatter(const Ray& r_in, const hit_record& rec, Color& attenuation, Ray& scattered) const = 0;
virtual Color emitted(double u, double v, const Point3& p) const { return Color(0, 0, 0); }
};
class Lambertian : public Material {
@ -42,4 +43,18 @@ private:
double ref_idx;
};
class Diffuse_light : public Material {
public:
Diffuse_light(std::shared_ptr<Texture> a) : emit(a) {}
virtual bool scatter(const Ray& r_in, const hit_record& rec, Color& attenuation, Ray& scattered) const {
return false;
}
virtual Color emitted(double u, double v, const Point3& p) const { return emit->value(u, v, p); }
private:
std::shared_ptr<Texture> emit;
};
#endif // MATERIAL_H