Compare commits
No commits in common. "af86494d89e0fbfb06b78e378d6f413296f24ce1" and "5243882ed56b0b9cb75909ab65291f0ff72376f1" have entirely different histories.
af86494d89
...
5243882ed5
4
Makefile
4
Makefile
@ -2,8 +2,8 @@ CXX = g++
|
|||||||
|
|
||||||
CXXFLAGS = -std=c++20 -Wall -Wextra -Wno-unused-parameter -march=native -O3 -flto -fopenmp
|
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 aarect.h
|
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 aarect.o
|
OBJ = main.o material.o vec3.o lodepng.o sphere.o moving_sphere.o bvh.o
|
||||||
|
|
||||||
TARGET = toytracer
|
TARGET = toytracer
|
||||||
|
|
||||||
|
79
aarect.cpp
79
aarect.cpp
@ -1,79 +0,0 @@
|
|||||||
#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
55
aarect.h
@ -1,55 +0,0 @@
|
|||||||
#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
|
|
20
hittable.h
20
hittable.h
@ -30,24 +30,4 @@ public:
|
|||||||
virtual bool bounding_box(double t0, double t1, Aabb& output_box) const = 0;
|
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
|
#endif // HITTABLE_H
|
||||||
|
119
main.cpp
119
main.cpp
@ -3,7 +3,6 @@
|
|||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#define LODEPNG_NO_COMPILE_DECODER
|
#define LODEPNG_NO_COMPILE_DECODER
|
||||||
#include "lodepng.h"
|
#include "lodepng.h"
|
||||||
@ -18,33 +17,26 @@
|
|||||||
#include "sphere.h"
|
#include "sphere.h"
|
||||||
#include "moving_sphere.h"
|
#include "moving_sphere.h"
|
||||||
#include "camera.h"
|
#include "camera.h"
|
||||||
#include "aarect.h"
|
|
||||||
|
|
||||||
Color ray_color(const Ray& r, const Color& background, const Hittable& world, int depth) {
|
Color ray_color(const Ray& r, const Hittable& world, int depth) {
|
||||||
hit_record rec;
|
hit_record rec;
|
||||||
|
|
||||||
if (depth <= 0)
|
if (depth <= 0)
|
||||||
return Color(0, 0, 0);
|
return Color(0, 0, 0);
|
||||||
|
|
||||||
if (!world.hit(r, 0.001, infinity, rec))
|
if (world.hit(r, 0.001, infinity, rec)) {
|
||||||
return background;
|
Ray scattered;
|
||||||
|
Color attenuation;
|
||||||
Ray scattered;
|
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered))
|
||||||
Color attenuation;
|
return attenuation * ray_color(scattered, world, depth - 1);
|
||||||
Color emitted = rec.mat_ptr->emitted(rec.u, rec.v, rec.p);
|
return Color(0, 0, 0);
|
||||||
|
}
|
||||||
if (!rec.mat_ptr->scatter(r, rec, attenuation, scattered))
|
Vec3 unit_direction = unit_vector(r.direction());
|
||||||
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);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::pair<Bvh_node, Camera> setup_random_scene(const int sph_i, const double aspect_ratio) {
|
Bvh_node setup_random_scene(const int sph_i) {
|
||||||
Hittable_list world;
|
Hittable_list world;
|
||||||
|
|
||||||
//auto ground_material = std::make_shared<Lambertian>(std::make_shared<Solid_color>(Color(0.5, 0.5, 0.5)));
|
//auto ground_material = std::make_shared<Lambertian>(std::make_shared<Solid_color>(Color(0.5, 0.5, 0.5)));
|
||||||
@ -98,17 +90,7 @@ std::pair<Bvh_node, Camera> setup_random_scene(const int sph_i, const double asp
|
|||||||
auto material3 = std::make_shared<Metal>(Color(0.7, 0.6, 0.5), 0.0);
|
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));
|
world.add(std::make_shared<Sphere>(Point3(4, 1, 0), 1.0, material3));
|
||||||
|
|
||||||
Point3 lookfrom(13, 2, 3);
|
return world.generate_bvh(0, 1);
|
||||||
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() {
|
Hittable_list two_spheres() {
|
||||||
@ -127,54 +109,6 @@ Hittable_list two_spheres() {
|
|||||||
return objects;
|
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 {
|
struct render_tile {
|
||||||
int start_x;
|
int start_x;
|
||||||
int start_y;
|
int start_y;
|
||||||
@ -189,12 +123,9 @@ int main() {
|
|||||||
const int image_width = 768;
|
const int image_width = 768;
|
||||||
//const int image_width = 384;
|
//const int image_width = 384;
|
||||||
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
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 = 2000;
|
||||||
const int samples_per_pixel = 1000;
|
//const int samples_per_pixel = 1000;
|
||||||
//const int samples_per_pixel = 400;
|
const int samples_per_pixel = 400;
|
||||||
//const int samples_per_pixel = 50;
|
|
||||||
const int max_depth = 50;
|
const int max_depth = 50;
|
||||||
|
|
||||||
const int tile_size = 128;
|
const int tile_size = 128;
|
||||||
@ -202,15 +133,21 @@ int main() {
|
|||||||
|
|
||||||
//const int sph_i = 3;
|
//const int sph_i = 3;
|
||||||
//const int sph_i = 5;
|
//const int sph_i = 5;
|
||||||
//const int sph_i = 7;
|
const int sph_i = 7;
|
||||||
//const int sph_i = 11;
|
//const int sph_i = 11;
|
||||||
|
|
||||||
//auto [world, camera] = setup_random_scene(sph_i, aspect_ratio);
|
auto world = setup_random_scene(sph_i);
|
||||||
//auto [world, camera] = two_spheres(aspect_ratio);
|
//auto world = two_spheres();
|
||||||
//auto [world, camera] = simple_light(aspect_ratio);
|
|
||||||
auto [world, camera] = cornell_box(aspect_ratio);
|
|
||||||
|
|
||||||
const Color background(0, 0, 0);
|
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);
|
||||||
|
|
||||||
std::atomic<int> queue_counter;
|
std::atomic<int> queue_counter;
|
||||||
boost::lockfree::queue<render_tile> queue(0);
|
boost::lockfree::queue<render_tile> queue(0);
|
||||||
@ -244,8 +181,8 @@ int main() {
|
|||||||
for (int s = 0; s<samples_per_pixel; ++s) {
|
for (int s = 0; s<samples_per_pixel; ++s) {
|
||||||
auto u = double(j + 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(i + random_double(-0.5, 0.5)) / (image_height - 1);
|
auto v = double(i + random_double(-0.5, 0.5)) / (image_height - 1);
|
||||||
Ray r = camera.get_ray(u, v);
|
Ray r = cam.get_ray(u, v);
|
||||||
pixel_color += ray_color(r, background, world, max_depth);
|
pixel_color += ray_color(r, world, max_depth);
|
||||||
}
|
}
|
||||||
image[i*image_width+j] = pixel_color;
|
image[i*image_width+j] = pixel_color;
|
||||||
}
|
}
|
||||||
|
15
material.h
15
material.h
@ -10,7 +10,6 @@ struct hit_record;
|
|||||||
class Material {
|
class Material {
|
||||||
public:
|
public:
|
||||||
virtual bool scatter(const Ray& r_in, const hit_record& rec, Color& attenuation, Ray& scattered) const = 0;
|
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 {
|
class Lambertian : public Material {
|
||||||
@ -43,18 +42,4 @@ private:
|
|||||||
double ref_idx;
|
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
|
#endif // MATERIAL_H
|
||||||
|
Loading…
Reference in New Issue
Block a user