-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
135 lines (119 loc) · 5.12 KB
/
main.cpp
File metadata and controls
135 lines (119 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// Created by MattFor on 13.04.2024.
//
#include "include/RayTracer.h"
#include "include/Camera/Camera.h"
#include "include/Scene/Intersectable.h"
#include "include/Scene/IntersectableList.h"
#include "include/Rendering/Material.h"
#include "include/Scene/Sphere.h"
// ! Now unused. Moved to its own folder with Intersectable related code
//double sphere_intersection (const Vec3& center, const double radius, const Ray& r)
//{
// // A, B, C as in y = Ax^2 + Bx + C
//
// Vec3 origin_to_center = center - r.origin();
//
// double A = r.direction().len_squared();
// // OLD: double B = dot_prod(r.direction(), origin_to_center) * -2;
// double H = dot_prod(r.direction(), origin_to_center);
// double C = origin_to_center.len_squared() - radius*radius;
//
// // Δ > 0 - 2 intersection points
// // Δ == 0 - 1 intersection point
// // Δ < 0 - 0 intersection points
// // OLD: double dlt = B*B - 4*A*C;
// // Changed because:
// // - vector dotted with itself is just the len of itself squared
// // - the equation simplifies if "-2H" is substituted for "B"
// double dlt = H*H - A*C;
//
// if (dlt < 0)
// {
// return -1;
// }
// else
// {
// // OLD: return (-B - sqrt(dlt)) / (2 * A);
// return (H - sqrt(dlt)) / A;
// }
//}
int main()
{
// OLD OLD OLD:
// IntersectableList scene;
// //
// // // OLD OLD:
// // //
// // // // OLD setup of spheres
// // // // scene.add (std::make_shared <Sphere> (Vec3(0, 0, -1), 0.5));
// // // // scene.add (std::make_shared <Sphere> (Vec3(0, -100.5, -1), 100));
// // //
// // // std::shared_ptr <Lambertian> mat_ground = std::make_shared <Lambertian> (Color(0.8, 0.8, 0));
// // // std::shared_ptr <Lambertian> mat_center = std::make_shared <Lambertian> (Color(0.1, 0.2, 0.5));
// // // std::shared_ptr <Metal> mat_left = std::make_shared <Metal> (Color(0.8, 0.8, 0.8), 0.3);
// // // // std::shared_ptr <Dielectric> mat_left = std::make_shared <Dielectric> (1.5);
// // // std::shared_ptr <Dielectric> mat_bubble = std::make_shared <Dielectric> (1.0 / 1.5);
// // // std::shared_ptr <Metal> mat_right = std::make_shared <Metal> (Color(0.8, 0.6, 0.2), 1.0);
// // //
// // // scene.add(std::make_shared <Sphere> (Vec3( 0.0, -100.5, 1.0), 100.0, mat_ground));
// // // scene.add(std::make_shared <Sphere> (Vec3( 0.0, 0.0, 1.2), 0.5, mat_center));
// // // scene.add(std::make_shared <Sphere> (Vec3( -1.0, 0.0, 1.0), 0.5, mat_left));
// // // // scene.add(std::make_shared <Sphere> (Vec3( -1, 0, -1), 0.4, mat_bubble));
// // // scene.add(std::make_shared <Sphere> (Vec3( 1.0, 0.0, 1.0), 0.5, mat_right));
// // //
// // //
// // // const double R = std::cos(PI / 4.0);
// // // std::shared_ptr <Lambertian> mat_left = std::make_shared <Lambertian> (Color(0.0, 0.0, 1.0));
// // // std::shared_ptr <Lambertian> mat_right = std::make_shared <Lambertian> (Color(1.0, 0.0, 0.0));
// // //
// // // scene.add(std::make_shared <Sphere> (Vec3(-R, 0.0, 1.0), R, mat_left));
// // // scene.add(std::make_shared <Sphere> (Vec3( R, 0.0, 1.0), R, mat_right));
// // //
IntersectableList scene;
std::shared_ptr <Lambertian> ground_material = std::make_shared <Lambertian> (Color(0.5, 0.5, 0.5));
scene.add(std::make_shared <Sphere> (Vec3(0, -1000, 0), 1000, ground_material));
for (int a = -11; a < 11; a++) {
for (int b = -11; b < 11; b++) {
double choose_mat = rand_double();
Vec3 center(a + 0.9 * rand_double(), 0.2 + 0.2 * rand_double(), b + 0.9 * rand_double());
if ((center - Vec3(4, 0.2, 0)).len() > 0.9) {
std::shared_ptr <Material> sphere_material;
if (choose_mat < 0.8) {
// diffuse
Vec3 albedo = Color::rand() * Color::rand();
sphere_material = std::make_shared <Lambertian> (albedo);
scene.add(std::make_shared <Sphere> (center, 0.2, sphere_material));
} else if (choose_mat < 0.95) {
// Metal
Vec3 albedo = Color::rand(0.5, 1);
double fuzz = rand_double(0, 0.5);
sphere_material = std::make_shared <Metal> (albedo, fuzz);
scene.add(std::make_shared <Sphere> (center, 0.2, sphere_material));
} else {
// glass
sphere_material = std::make_shared <Dielectric> (1.5);
scene.add(std::make_shared <Sphere> (center, 0.2, sphere_material));
}
}
}
}
std::shared_ptr <Dielectric> mat1 = std::make_shared <Dielectric> (1.5);
std::shared_ptr <Lambertian> mat2 = std::make_shared <Lambertian> (Color(0.4, 0.2, 0.1));
std::shared_ptr <Metal> mat3 = std::make_shared <Metal> (Color(0.7, 0.6, 0.5), 0.0);
scene.add(std::make_shared <Sphere> (Vec3(2, 1, 0), 1.0, mat1));
scene.add(std::make_shared <Sphere> (Vec3(-4, 1, 2), 1.0, mat2));
scene.add(std::make_shared <Sphere> (Vec3(4, 1, -2), 1.0, mat3));
Camera cam;
cam.aspect_ratio = 16.0 / 9.0;
cam.img_width = 2560;
cam.samp_per_pix = 10;
cam.max_ray_depth = 20;
cam.vert_fov = 20;
cam.cam_origin = Vec3(13, 2, 3);
cam.cam_facing_point = Vec3(0, 0, 0);
cam.v_up = Vec3(0, 1, 0);
cam.defocus_ang = 0.3;
cam.focus_dist = 10.0;
cam.render(scene);
}