Skip to content

Commit bbf3952

Browse files
committed
raytracing in one weekend: final
1 parent f43e495 commit bbf3952

File tree

18 files changed

+95
-59
lines changed

18 files changed

+95
-59
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
.DS_Store
2-
/**/makefile
32
/.vscode

src/config.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ enum class diffuse_render_method: bool {
197197
namespace constants {
198198

199199
// Image
200-
constexpr f64 aspect_ratio = 16.0 / 9.0;
201-
constexpr i32 image_width = 400;
200+
constexpr f64 aspect_ratio = 3.0 / 2.0; // 16.0 / 9.0;
201+
constexpr i32 image_width = 1200; // 400;
202202
constexpr i32 image_height = static_cast<i32>(image_width / aspect_ratio);
203-
constexpr i32 samples_per_pixel = 100;
203+
constexpr i32 samples_per_pixel = 500; // 100;
204204
constexpr i32 max_depth = 50;
205205
constexpr f64 GAMMA = 2;
206206

src/hittable/hittablelist.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include "config.hpp"
4+
#include "ray.hpp"
5+
#include "hittable.hpp"
6+
7+
class HittableList: public Hittable {
8+
Vec<ptr<Hittable>> objects;
9+
10+
public:
11+
HittableList() = default;
12+
~HittableList() = default;
13+
14+
auto clear() -> void { objects.clear(); }
15+
template <typename T>
16+
auto push(const T &obj) -> void {
17+
if constexpr (std::is_convertible_v<T, ptr<Hittable>>)
18+
objects.push_back(obj);
19+
else {
20+
static_assert(std::is_base_of_v<Hittable, T>, "try to add an object which is not hittable into a hittable list");
21+
objects.emplace_back(std::make_shared<T>(obj));
22+
}
23+
}
24+
25+
auto hit(const Ray &ray, const f64 t_min, const f64 t_max) const -> std::optional<HitRecord> override;
26+
};
File renamed without changes.
File renamed without changes.

src/hittablelist.hpp

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/main.cpp

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,81 @@
1111
#include "metal.hpp"
1212
#include "dielectric.hpp"
1313

14+
auto random_scene() -> HittableList {
15+
HittableList scene;
16+
17+
const auto ground_material = std::make_shared<Lambertian>(RGB(0.5, 0.5, 0.5));
18+
scene.push(Sphere(p3d(0.0, -1000.0, 0.0), 1000.0, ground_material));
19+
20+
for (i32 a = -11; a < 11; ++a)
21+
for (i32 b = -11; b < 11; ++b) {
22+
const p3d center(a + 0.9 * random_f64(), 0.2, b + 0.9 * random_f64());
23+
24+
if ((center - p3d(4.0, 0.2, 0.0)).length() > 0.9) {
25+
const f64 which_material = random_f64();
26+
27+
if (which_material < 0.8) {
28+
// diffuse
29+
const auto albedo = RGB::random() * RGB::random();
30+
const auto sphere_material = std::make_shared<Lambertian>(albedo);
31+
scene.push(Sphere(center, 0.2, sphere_material));
32+
} else if (which_material < 0.95) {
33+
// metal
34+
const auto albedo = RGB::random(0.5, 1.0);
35+
const auto fuzz = random_f64(0.0, 0.5);
36+
const auto sphere_material = std::make_shared<Metal>(albedo, fuzz);
37+
scene.push(Sphere(center, 0.2, sphere_material));
38+
} else {
39+
// glass
40+
const auto sphere_material = std::make_shared<Dielectric>(1.5);
41+
scene.push(Sphere(center, 0.2, sphere_material));
42+
}
43+
}
44+
}
45+
46+
const auto material1 = std::make_shared<Dielectric>(1.5);
47+
scene.push(Sphere(p3d(0.0, 1.0, 0.0), 1.0, material1));
48+
49+
const auto material2 = std::make_shared<Lambertian>(RGB(0.4, 0.2, 0.1));
50+
scene.push(Sphere(p3d(-4.0, 1.0, 0.0), 1.0, material2));
51+
52+
const auto material3 = std::make_shared<Metal>(RGB(0.7, 0.6, 0.5), 0.0);
53+
scene.push(Sphere(p3d(4.0, 1.0, 0.0), 1.0, material3));
54+
55+
return scene;
56+
}
57+
1458
auto main() -> i32 {
15-
// World
16-
const auto material_ground = std::make_shared<Lambertian>(RGB(0.8, 0.8, 0.0));
17-
const auto material_center = std::make_shared<Lambertian>(RGB(0.1, 0.2, 0.5));
18-
const auto material_left = std::make_shared<Dielectric>(1.5);
19-
const auto material_right = std::make_shared<Metal>(RGB(0.8, 0.6, 0.2), 0.0);
20-
21-
HittableList world;
22-
world.push(std::make_shared<Sphere>(p3d( 0.0, -100.5, -1.0), 100.0, material_ground));
23-
world.push(std::make_shared<Sphere>(p3d( 0.0, 0.0, -1.0), 0.5, material_center));
24-
world.push(std::make_shared<Sphere>(p3d(-1.0, 0.0, -1.0), 0.5, material_left));
25-
world.push(std::make_shared<Sphere>(p3d(-1.0, 0.0, -1.0), -0.45, material_left));
26-
world.push(std::make_shared<Sphere>(p3d( 1.0, 0.0, -1.0), 0.5, material_right));
59+
// Scene
60+
const auto scene = random_scene();
2761

2862
// Camera
29-
const p3d lookfrom(3, 3, 2);
30-
const p3d lookat(0, 0, -1);
31-
const Vec3 viewup(0, 1, 0);
32-
const f64 focus_distance = (lookfrom - lookat).length();
33-
const f64 aperture = 2.0;
63+
const p3d lookfrom(13.0, 2.0, 3.0);
64+
const p3d lookat(0.0, 0.0, 0.0);
65+
const Vec3 viewup(0.0, 1.0, 0.0);
66+
const f64 focus_distance = 10.0;
67+
const f64 aperture = 0.1;
3468

35-
const Camera camera(lookfrom, lookat, viewup, 20, constants::aspect_ratio, aperture, focus_distance);
36-
// const Camera camera(p3d(-2, 2, 1), p3d(0, 0, -1), Vec3(0, 1, 0), 90, constants::aspect_ratio);
69+
const Camera camera(lookfrom, lookat, viewup, 20.0, constants::aspect_ratio, aperture, focus_distance);
3770

3871
// Render
3972
std::cout.tie(0);
4073
std::cout << "P3\n";
4174
std::cout << constants::image_width << ' ' << constants::image_height << '\n';
4275
std::cout << "255\n";
4376

44-
4577
for (i32 j = constants::image_height - 1; j >= 0; --j) {
46-
std::cerr << "Scanlines remaining: " << j << '\n' << std::flush;
78+
fprintf(stderr, "Rendering: %d lines remaining\n", j);
4779
for (i32 i = 0; i < constants::image_width; ++i) {
48-
RGB pixel_color(0, 0, 0);
80+
RGB pixel_color(0.0, 0.0, 0.0);
4981
for (i32 s = 0; s < constants::samples_per_pixel; ++s) {
5082
const f64 u = (i + random_f64()) / (constants::image_width - 1);
5183
const f64 v = (j + random_f64()) / (constants::image_height - 1);
52-
pixel_color += camera.get_ray(u, v).color(world, constants::max_depth);
84+
pixel_color += camera.get_ray(u, v).color(scene, constants::max_depth);
5385
}
5486
std::cout << pixel_color << '\n';
5587
}
5688
}
57-
std::cerr << "\nDone.\n";
89+
fprintf(stderr, "\nDone.\n");
5890
return 0;
5991
}

src/makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all: *.hpp *.cpp
2+
clang++ -Wall -Wextra -std=c++2a -I. -Iutilities -Ihittable -Imaterial -O2 *.cpp hittable/*.cpp -o main

0 commit comments

Comments
 (0)