-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-shadows.cc
More file actions
241 lines (199 loc) · 7.62 KB
/
04-shadows.cc
File metadata and controls
241 lines (199 loc) · 7.62 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
// Shadows
//
// Book chapter: https://gabrielgambetta.com/computer-graphics-from-scratch/04-shadows-and-reflections.html#shadows
// Book example: https://github.com/ggambetta/computer-graphics-from-scratch/blob/master/demos/raytracer-04.html
//
// ```
// clang++ -std=c++17 examples/04-shadows.cc -o bin/04-shadows -O3 -fno-fast-math
// bin/04-shadows
// ```
#include "bmp.h"
struct fVec3 {
float x, y, z;
fVec3 operator+(const fVec3& other) const {
return { x + other.x, y + other.y, z + other.z };
}
fVec3 operator-(const fVec3& other) const {
return { x - other.x, y - other.y, z - other.z };
}
fVec3 operator*(float n) const {
return { x * n, y * n, z * n };
}
float dot(const fVec3& other) const {
return x * other.x + y * other.y + z * other.z;
}
float length() const {
return sqrt(this->dot(*this));
}
fVec3 normalized() const {
return *this * (1.0 / this->length());
}
};
enum LightType {
AMBIENT, POINT, DIRECTIONAL
};
struct Light {
fVec3 position;
float intensity;
LightType type;
};
struct Sphere {
fVec3 center;
float radius;
float specular;
BMPColor color;
};
struct Scene {
const float viewport_size = 1;
const float projection_plane_z = 1;
fVec3 camera_position;
BMPColor background_color;
vector<Sphere> spheres;
vector<Light> lights;
};
struct ImageSize {
int32_t width;
int32_t height;
};
struct Image {
const ImageSize size;
// flattened array of pixels
vector<BMPColor> data;
Image(int32_t width, int32_t height):
size{width, height}, data(width * height) {}
// computes the offset for the data based on x and y coordinates
int32_t offset(int32_t x, int32_t y) const {
x = size.width / 2 + x;
y = size.height / 2 - y - 1;
if (x < 0 || x >= size.width || y < 0 || y >= size.height) {
throw out_of_range("Pixel coordinates must be within [0 < width, 0 < height]");
}
return x + size.width * y;
}
};
BMPColor operator*(const BMPColor& color, float n) {
return {
.red = static_cast<uint8_t>(round(clamp<float>(color.red * n, 0, 255))),
.green = static_cast<uint8_t>(round(clamp<float>(color.green * n, 0, 255))),
.blue = static_cast<uint8_t>(round(clamp<float>(color.blue * n, 0, 255)))
};
}
// converts 2D canvas coordinates to 3D viewport coordinates.
fVec3 canvasToViewport(int32_t x, int32_t y, const ImageSize& size, const Scene& scene) {
return fVec3 {
(float)x * scene.viewport_size / size.width,
(float)y * scene.viewport_size / size.height,
scene.projection_plane_z
};
}
// computes the intersection of a ray and a sphere. Returns the values of t for the intersections.
pair<float, float> intersectRaySphere(const fVec3& origin, const fVec3& direction, const Sphere& sphere) {
fVec3 oc = origin - sphere.center;
float a = direction.dot(direction);
float b = 2 * oc.dot(direction);
float c = oc.dot(oc) - (sphere.radius * sphere.radius);
float discriminant = (b * b) - (4 * a * c);
if (discriminant < 0) {
return {INFINITY, INFINITY};
}
float t1 = (-b + sqrt(discriminant)) / (2 * a);
float t2 = (-b - sqrt(discriminant)) / (2 * a);
return {t1, t2};
}
// find the closest intersection between a ray and the spheres in the scene.
pair<Sphere, float> closestIntersection(const fVec3& origin, const fVec3& direction, float t_min, float t_max, const Scene& scene) {
float closest_t = INFINITY;
Sphere closest_sphere;
for (int32_t i = 0; i < scene.spheres.size(); i++) {
auto [xt_min, xt_max] = intersectRaySphere(origin, direction, scene.spheres[i]);
if (xt_min < closest_t && t_min < xt_min && xt_min < t_max) {
closest_t = xt_min;
closest_sphere = scene.spheres[i];
}
if (xt_max < closest_t && t_min < xt_max && xt_max < t_max) {
closest_t = xt_max;
closest_sphere = scene.spheres[i];
}
}
return {closest_sphere, closest_t};
}
float computeLighting(const fVec3& point, const fVec3& normal, const fVec3& view, float specular, const Scene& scene) {
float intensity = 0;
for (int i = 0; i < scene.lights.size(); i++) {
Light light = scene.lights[i];
if (light.type == AMBIENT) {
intensity += light.intensity;
} else {
fVec3 vec_l = {0, 0, 0};
float shadow_t_max;
if (light.type == POINT) {
vec_l = light.position - point;
shadow_t_max = 1;
} else if (light.type == DIRECTIONAL) {
vec_l = light.position;
shadow_t_max = INFINITY;
}
// shadow check
auto [shadow_sphere, shadow_t] = closestIntersection(point, vec_l, 0.001, shadow_t_max, scene);
if (shadow_t != INFINITY) {
continue;
}
// diffuse
float n_dot_l = normal.dot(vec_l);
// since we're not making sure N and L are normalized,
// we're using the following formula instead of simply `light.intensity * max(0.0f, n_dot_l)`
if (n_dot_l > 0) {
intensity += light.intensity * n_dot_l / (normal.length() * vec_l.length());
}
// specular
if (specular >= 0) {
fVec3 reflection = normal * 2 * n_dot_l - vec_l;
float r_dot_v = reflection.dot(view);
if (r_dot_v > 0) {
intensity += light.intensity * pow(r_dot_v / (reflection.length() * view.length()), specular);
}
}
}
}
return intensity;
}
// traces a ray against the set of spheres in the scene.
BMPColor traceRay(const fVec3& origin, const fVec3& direction, float t_min, float t_max, const Scene& scene) {
auto [closest_sphere, closest_t] = closestIntersection(origin, direction, t_min, t_max, scene);
if (closest_t == INFINITY) {
return scene.background_color;
}
fVec3 point = origin + (direction * closest_t);
fVec3 normal = (point - closest_sphere.center).normalized();
fVec3 view = direction * -1;
return closest_sphere.color * computeLighting(point, normal, view, closest_sphere.specular, scene);
}
int main() {
Image image = {600, 600};
Scene scene = {
.viewport_size = 1,
.projection_plane_z = 1,
.camera_position = {0, 0, 0},
.background_color = {255, 255, 255},
.spheres = {
Sphere{.center = {0, -1, 3}, .radius = 1, .specular = 500, .color = {255, 0, 0}},
Sphere{.center = {-2, 0, 4}, .radius = 1, .specular = 10, .color = {0, 255, 0}},
Sphere{.center = {2, 0, 4}, .radius = 1, .specular = 500, .color = {0, 0, 255}},
Sphere{.center = {0, -5001, 0}, .radius = 5000, .specular = 1000, .color = {255, 255, 0}}
},
.lights = {
Light{.type = AMBIENT, .intensity = 0.2},
Light{.type = POINT, .intensity = 0.6, .position = {2, 1, 0}},
Light{.type = DIRECTIONAL, .intensity = 0.2, .position = {1, 4, 4}}
}
};
for (int32_t x = -image.size.width / 2; x < image.size.width / 2; x++) {
for(int32_t y = -image.size.height / 2; y < image.size.height / 2; y++) {
fVec3 direction = canvasToViewport(x, y, image.size, scene);
BMPColor color = traceRay(scene.camera_position, direction, 1, INFINITY, scene);
image.data[image.offset(x, y)] = color;
}
}
save_image(image.data, image.size.width, image.size.height, "results/04-shadows.bmp");
return 0;
}