-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp2
More file actions
443 lines (355 loc) · 15.8 KB
/
main.cpp2
File metadata and controls
443 lines (355 loc) · 15.8 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
rt: namespace = {
// Constants
infinity: double = std::numeric_limits<double>::infinity();
pi: double = std::numbers::pi;
samples_per_pixel : int = 5;
// Utility Functions
degrees_to_radians: (degrees: double) -> double = degrees * pi / 180.0;
random_double: () -> double = std::rand() / (RAND_MAX + 1.0);
random_double: (min: double, max: double) -> double = min + (max - min) * random_double();
///////////////////////////////////////// vec3 /////////////////////////////////////
vec3: @value type = {
public e: std::array<double, 3> = (0.0, 0.0, 0.0);
operator=: (out this, x_: double, y_: double, z_: double) = {
e = (x_, y_, z_);
}
x: (this) -> double = e[0];
y: (this) -> double = e[1];
z: (this) -> double = e[2];
operator-: (this) -> vec3 = vec3(-e[0], -e[1], -e[2]);
operator+=: (inout this, v: vec3) = {
e[0] += v.x();
e[1] += v.y();
e[2] += v.z();
}
operator*=: (inout this, t: double) = {
e[0] *= t;
e[1] *= t;
e[2] *= t;
}
operator/=: (inout this, t: double) = this *= 1 / t;
length: (this) -> double = std::sqrt(length_squared());
length_squared: (this) -> double = e[0] * e[0] + e[1] * e[1] + e[2] * e[2];
random: () -> vec3 = vec3(random_double(), random_double(), random_double());
random: (min: double, max: double) -> vec3 = vec3(
random_double(min, max),
random_double(min, max),
random_double(min, max));
near_zero: (this) -> bool = {
// Return true if the vector is close to zero in all dimensions.
s := 1e-8;
return (std::fabs(e[0]) < s) && (std::fabs(e[1]) < s) && (std::fabs(e[2]) < s);
}
print: (this, inout os: std::ostream, v: vec3) = {
os << v.e[0] << ' ' << v.e[1] << ' ' << v.e[2];
}
}
point3: type == vec3;
color: type == vec3;
operator+: (u: vec3, v: vec3) -> vec3 = vec3(u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2]);
operator-: (u: vec3, v: vec3) -> vec3 = vec3(u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] - v.e[2]);
operator*: (u: vec3, v: vec3) -> vec3 = vec3(u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2]);
operator*: (t: double, v: vec3) -> vec3 = vec3(t * v.e[0], t * v.e[1], t * v.e[2]);
operator*: (v: vec3, t: double) -> vec3 = t * v;
operator/: (v: vec3, t: double) -> vec3 = (1.0 / t) * v;
dot: (u: vec3, v: vec3) -> double = u.e[0] * v.e[0] + u.e[1] * v.e[1] + u.e[2] * v.e[2];
cross: (u: vec3, v: vec3) -> vec3 =
vec3(u.e[1] * v.e[2] - u.e[2] * v.e[1],
u.e[2] * v.e[0] - u.e[0] * v.e[2],
u.e[0] * v.e[1] - u.e[1] * v.e[0]);
reflect: (v: vec3, n: vec3) -> vec3 = v - 2.0 * dot(v, n) * n;
refract: (uv: vec3, n: vec3, etai_over_etat: double) -> vec3 = {
cos_theta := std::fmin(dot(-uv, n), 1.0);
r_out_perp := etai_over_etat * (uv + cos_theta * n);
r_out_parallel := -std::sqrt(std::fabs(1.0 - r_out_perp.length_squared())) * n;
return r_out_perp + r_out_parallel;
}
unit_vector: (v: vec3) -> vec3 = v / v.length();
random_in_unit_sphere: () -> vec3 = {
while true {
p := vec3::random(-1.0, 1.0);
if p.length_squared() >= 1.0 { continue; }
return p;
}
}
random_in_unit_disk: () -> vec3 = {
while (true) {
p := vec3(random_double(-1.0, 1.0), random_double(-1.0, 1.0), 0.0);
if p.length_squared() >= 1.0 { continue; }
return p;
}
}
random_unit_vector: () -> vec3 = unit_vector(random_in_unit_sphere());
write_color: (inout os: std::ostream, pixel_color: color) = {
r := pixel_color.x();
g := pixel_color.y();
b := pixel_color.z();
// Divide the color by the number of samples.
scale := 1.0 / samples_per_pixel;
r = std::sqrt(scale * r);
g = std::sqrt(scale * g);
b = std::sqrt(scale * b);
// Write the translated [0,255] value of each color component.
os << cpp2::unsafe_narrow<int>(256 * std::clamp(r, 0.0, 0.999)) << ' '
<< cpp2::unsafe_narrow<int>(256 * std::clamp(g, 0.0, 0.999)) << ' '
<< cpp2::unsafe_narrow<int>(256 * std::clamp(b, 0.0, 0.999)) << '\n';
}
///////////////////////////////////////// ray /////////////////////////////////////
ray: @value type = {
orig: point3 = (0, 0, 0);
dir: vec3 = (0, 0, 0);
operator=:(out this, the_origin: point3, the_direction: vec3) = {
orig = the_origin;
dir = the_direction;
}
origin: (this) -> point3 = orig;
direction: (this) -> vec3 = dir;
at: (this, t: double) -> point3 = orig + t * dir;
}
/////////////////////////////////////// camera ///////////////////////////////////////
camera: type = {
origin: point3 = (0, 0, 0);
horizontal: vec3 = (0, 0, 0);
vertical: vec3 = (0, 0, 0);
lower_left_corner: point3 = (0, 0, 0);
w: vec3 = (0, 0, 0);
u: vec3 = (0, 0, 0);
v: vec3 = (0, 0, 0);
lens_radius: double = 0.0;
operator=: (out this, lookfrom: point3, lookat: point3, vup: vec3, vfov: double,
aspect_ratio: double, aperture: double, focus_dist: double) = {
viewport_height := 2.0 * tan(degrees_to_radians(vfov) / 2.0);
viewport_width := aspect_ratio * viewport_height;
focal_length := 1.0;
w = unit_vector(lookfrom - lookat);
u = unit_vector(cross(vup, w));
v = cross(w, u);
origin = lookfrom;
horizontal = focus_dist * viewport_width * u;
vertical = focus_dist * viewport_height * v;
lower_left_corner = origin - horizontal / 2.0 - vertical / 2.0 - focus_dist * w;
lens_radius = aperture / 2.0;
}
get_ray: (this, s: double, t: double) -> ray = {
rd := lens_radius * random_in_unit_disk();
offset := u * rd.x() + v * rd.y();
return ray(origin + offset, lower_left_corner + s * horizontal + t * vertical - origin - offset);
}
}
///////////////////////////////////// material ///////////////////////////////////////
material: @interface type = {
scatter: (this, r_in: ray, rec: hit_record, inout attenuation: color, inout scattered: ray) -> bool;
}
lambertian: type = {
this: material = ();
albedo: color = (0, 0, 0);
operator=: (out this, a: color) = { albedo = a; }
scatter: (override this, r_in: ray, rec: hit_record, inout attenuation: color, inout scattered: ray) -> bool = {
scatter_direction := rec.normal + random_unit_vector();
// Catch degenerate scatter direction
if scatter_direction.near_zero() { scatter_direction = rec.normal; }
attenuation = albedo;
scattered = ray(rec.p, scatter_direction);
return true;
}
}
metal: type = {
this: material = ();
albedo: color = (0, 0, 0);
fuzz: double = 0.0;
operator=: (out this, a: color, f: double) = {
albedo = a;
fuzz = std::min(f, 1.0);
}
scatter: (override this, r_in: ray, rec: hit_record, inout attenuation: color, inout scattered: ray) -> bool = {
reflected := reflect(unit_vector(r_in.direction()), rec.normal);
scattered = ray(rec.p, reflected + fuzz * random_in_unit_sphere());
attenuation = albedo;
return (dot(scattered.direction(), rec.normal) > 0);
}
}
dielectric: type = {
this: material = ();
ir: double = 0.0; // Index of Refraction
operator=: (out this, index_of_refraction: double) = { ir = index_of_refraction; }
scatter: (override this, r_in: ray, rec: hit_record, inout attenuation: color, inout scattered: ray) -> bool = {
attenuation = color(1.0, 1.0, 1.0);
refraction_ratio := ir;
if rec.front_face { refraction_ratio = 1.0 / refraction_ratio; }
unit_direction := unit_vector(r_in.direction());
cos_theta := std::fmin(dot(-unit_direction, rec.normal), 1.0);
sin_theta := std::sqrt(1.0 - cos_theta * cos_theta);
cannot_refract := (refraction_ratio * sin_theta) > 1.0;
direction := vec3();
if cannot_refract || (reflectance(cos_theta, refraction_ratio) > random_double()) {
direction = reflect(unit_direction, rec.normal);
}
else {
direction = refract(unit_direction, rec.normal, refraction_ratio);
}
scattered = ray(rec.p, direction);
return true;
}
reflectance: (cosine: double, ref_idx: double) -> double = {
// Use Schlick's approximation for reflectance.
r0 := (1.0 - ref_idx) / (1.0 + ref_idx);
r0 = r0 * r0;
return r0 + (1.0 - r0) * std::pow(1.0 - cosine, 5.0);
}
}
///////////////////////////////////// hittable ///////////////////////////////////////
hit_record: @struct type = {
p: point3 = (0, 0, 0);
normal: vec3 = (0, 0, 0);
mat: std::shared_ptr<material> = ();
t: double = 0.0;
front_face: bool = false;
set_face_normal: (inout this, r: ray, outward_normal: vec3) = {
front_face = dot(r.direction(), outward_normal) < 0;
if front_face { normal = outward_normal; } else { normal = -outward_normal; }
}
}
hittable: @interface type = {
hit: (this, r: ray, t_min: double, t_max: double, inout rec: hit_record) -> bool;
}
sphere: type = {
this: hittable = ();
center: point3;
radius: double;
mat: std::shared_ptr<material>;
operator=:(out this, p: point3, r: double, m : std::shared_ptr<material>) = {
center = p;
radius = r;
mat = m;
}
hit: (override this, r: ray, t_min: double, t_max: double, inout rec: hit_record) -> bool = {
oc := r.origin() - center;
a := r.direction().length_squared();
half_b := dot(oc, r.direction());
c := oc.length_squared() - radius * radius;
discriminant := half_b * half_b - a * c;
if discriminant < 0 { return false; }
sqrtd := sqrt(discriminant);
// Find the nearest root that lies in the acceptable range.
root := (-half_b - sqrtd) / a;
if (root < t_min) || (t_max < root) {
root = (-half_b + sqrtd) / a;
if (root < t_min) || (t_max < root) { return false; }
}
rec.t = root;
rec.p = r.at(rec.t);
outward_normal := (rec.p - center) / radius;
rec.set_face_normal(r, outward_normal);
rec.mat = mat;
return true;
}
}
hittable_list: type = {
this: hittable = ();
objects: std::vector<std::shared_ptr<hittable>> = ();
operator=: (out this) = {}
add: (inout this, obj: std::shared_ptr<hittable>) = {
objects.push_back(obj);
}
hit: (override this, r: ray, t_min: double, t_max: double, inout rec: hit_record) -> bool = {
temp_rec := hit_record();
hit_anything := false;
closest_so_far := t_max;
for objects do (object) {
if object*.hit(r, t_min, closest_so_far, temp_rec) {
hit_anything = true;
closest_so_far = temp_rec.t;
rec = temp_rec;
}
}
return hit_anything;
}
}
/////////////////////////////////// ray tracing //////////////////////////////////////
ray_color: (r: ray, world: hittable_list, depth: int) -> color = {
if depth <= 0 { return color(0, 0, 0); }
rec := hit_record();
if world.hit(r, 0.001, infinity, rec) {
scattered := ray();
attenuation := color();
if rec.mat*.scatter(r, rec, attenuation, scattered) {
return attenuation * ray_color(scattered, world, depth-1);
}
return color(0,0,0);
}
unit_direction := unit_vector(r.direction());
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);
}
random_scene: () -> std::unique_ptr<hittable_list> = {
world := new<hittable_list>();
ground_material := cpp2::shared.new<lambertian>(color(0.5, 0.5, 0.5));
world*.add(cpp2::shared.new<sphere>(point3(0,-1000,0), 1000, ground_material));
a := -11; while a < 11 next a++ {
b := -11; while b < 11 next b++ {
choose_mat := random_double();
center := point3(a + 0.9 * random_double(), 0.2, b + 0.9 * random_double());
if (center - point3(4.0, 0.2, 0.0)).length() > 0.9 {
sphere_material := std::shared_ptr<material>();
if choose_mat < 0.8 {
// diffuse
albedo := color::random() * color::random();
sphere_material = cpp2::shared.new<lambertian>(albedo);
world*.add(cpp2::shared.new<sphere>(center, 0.2, sphere_material));
} else {
if choose_mat < 0.95 {
// metal
albedo := color::random(0.5, 1.0);
fuzz := random_double(0.0, 0.5);
sphere_material = cpp2::shared.new<metal>(albedo, fuzz);
world*.add(cpp2::shared.new<sphere>(center, 0.2, sphere_material));
} else {
// glass
sphere_material = cpp2::shared.new<dielectric>(1.5);
world*.add(cpp2::shared.new<sphere>(center, 0.2, sphere_material));
}
}
}
}
}
material1 := cpp2::shared.new<dielectric>(1.5);
world*.add(cpp2::shared.new<sphere>(point3(0.0, 1.0, 0.0), 1.0, material1));
material2 := cpp2::shared.new<lambertian>(color(0.4, 0.2, 0.1));
world*.add(cpp2::shared.new<sphere>(point3(-4.0, 1.0, 0.0), 1.0, material2));
material3 := cpp2::shared.new<metal>(color(0.7, 0.6, 0.5), 0.0);
world*.add(cpp2::shared.new<sphere>(point3(4.0, 1.0, 0.0), 1.0, material3));
return world;
}
}
main: () -> int = {
// Image
aspect_ratio := 3.0 / 2.0;
image_width := 800;
image_height := cpp2::unsafe_narrow<int>(image_width / aspect_ratio);
max_depth := 50;
// World
world := rt::random_scene();
// Camera
lookfrom := rt::point3(13.0, 2.0, 3.0);
lookat := rt::point3(0.0, 0.0, 0.0);
vup := rt::vec3(0.0, 1.0, 0.0);
dist_to_focus := 10.0;
aperture := 0.1;
cam := rt::camera(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
// Render
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
j := image_height - 1; while j >= 0 next j-- {
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
i := 0; while i < image_width next i++ {
pixel_color := rt::color(0, 0, 0);
s := 0; while s < rt::samples_per_pixel next s++ {
u := (i + rt::random_double()) / (image_width-1);
v := (j + rt::random_double()) / (image_height-1);
r := cam.get_ray(u, v);
pixel_color += ray_color(r, world*, max_depth);
}
write_color(std::cout, pixel_color);
}
}
std::cerr << "\nDone.\n";
}