-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.h2
More file actions
136 lines (114 loc) · 4.52 KB
/
camera.h2
File metadata and controls
136 lines (114 loc) · 4.52 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
#pragma once
#include "defines.h"
#include "hittable.h"
#include "optional_extensions.h"
/*
Camera is responsible for sending out rays and for
using their records to construct the image.
*/
camera: type = {
private aspect_ratio: const double;
private image_width: const uint;
private image_height: const uint;
private focal_length: const double;
private viewport_height: const double;
private viewport_width: const double;
private camera_center: const point3;
private viewport_u: const vec3;
private viewport_v: const vec3;
private pixel_du: const vec3;
private pixel_dv: const vec3;
private viewport_upper_left: const point3;
private pixel00_loc: const point3;
private samples_per_pixel: const uint;
private pixel_samples_scale: const double;
operator=: (
out this,
aspect_ratio_in: double,
image_width_in: uint,
focal_length_in: double,
camera_center_in: point3,
viewport_height_in: double,
samples_per_pixel_in: uint;
) == {
// Calculate image dimentions
aspect_ratio = aspect_ratio_in;
image_width = image_width_in;
image_height = cpp2::unsafe_narrow<uint>(image_width/aspect_ratio);
focal_length = focal_length_in;
// viewport_height = 2.0;
viewport_height = viewport_height_in;
viewport_width = viewport_height *
(cpp2::unsafe_narrow<double>(image_width)/image_height);
camera_center = camera_center_in;
// Vectors across horizontal and vertical edges of the viewport window
viewport_u = (viewport_width, 0, 0);
viewport_v = (0, -viewport_height, 0);
// Horizontal and Vecrtival vectors from pixel to pixel
pixel_du = viewport_u / image_width;
pixel_dv = viewport_v / image_height;
// Calculate the upper left corner of the viewport window
viewport_upper_left =
camera_center - // starting from 0
vec3(0, 0, focal_length) - // to the center of the viewport
viewport_u/2 - // vvv
viewport_v/2; // to the upper left side
// calculate the center of the upper left (0, 0) pixel
pixel00_loc =
viewport_upper_left +
0.5 * (pixel_du + pixel_dv);
samples_per_pixel = samples_per_pixel_in;
pixel_samples_scale = 1.0 / samples_per_pixel;
}
private random_square: () -> vec3 == {
width :== 1.0;
return vec3(
width*(random_double() - 0.5),
width*(random_double() - 0.5),
0.0
);
}
private get_nudged_ray: (this, i: int, j: int) -> ray = {
offset: const _ = random_square();
pixel_sample: const _ = pixel00_loc +
((i + offset.x()) * pixel_du) +
((j + offset.y()) * pixel_dv);
return ray(
camera_center, // origin
pixel_sample - camera_center // direction
);
}
render: (this, world: hittable, inout stream: std::ostream) == {
stream << "P3\n(image_width)$ (image_height)$\n255\n";
for std::views::iota(0 as uint, image_height) do(j) {
std::clog << "\rScanlines remaining: " << (image_height - j) << ' ' << std::flush;
for std::views::iota(0 as uint, image_width) do (i) {
pixel_color: color = (0, 0, 0);
for std::views::iota(0 as uint, samples_per_pixel) do (n) {
r: ray = get_nudged_ray(i, j);
pixel_color += r.ray_color(world);
}
pixel_color *= pixel_samples_scale;
stream.write_color(pixel_color);
}
}
std::clog << std::endl;
}
private ray_color: (this, r: ray, world: hittable) -> color == {
// This sort of represents the lazy value of the background
// color, evaluated by value_else only if needed.
background_color:= :() -> color == {
a := 0.5*(r&$*.direction().unit_vector().y() + 1.0);
white: color == (1.0, 1.0, 1.0);
blue_ish: color == (0.5, 0.7, 1.0);
return (1.0-a)*white + a*blue_ish;
};
surface_color:== :(h: hittable::hit_record) -> color == {
return 0.5 * (h.reflective_normal + color(1, 1, 1));
};
return world.
hit(r, interval::front).
transform(surface_color).
value_else(background_color); // Used thanks to UFCS
}
}