-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathTracing.cpp
359 lines (284 loc) · 7.98 KB
/
PathTracing.cpp
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
#include <cstdlib>
#include <cstring>
#include <cstdint>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#define _CRT_SECURE_NO_WARNINGS
#define __STDC_LIB_EXT1__
#include "png.h"
#include <iostream>
#include <iomanip>
struct Vec3
{
float x, y, z;
};
Vec3 operator-(const Vec3& a, const Vec3& b)
{
return { a.x - b.x, a.y - b.y, a.z - b.z };
}
Vec3 operator+(const Vec3& a, const Vec3& b)
{
return { a.x + b.x, a.y + b.y, a.z + b.z };
}
Vec3 operator*(const Vec3& a, float s)
{
return { a.x * s, a.y * s, a.z * s };
}
Vec3 operator*(float s, const Vec3& a)
{
return { a.x * s, a.y * s, a.z * s };
}
// Component wise multiplying, color * vector
Vec3 operator*(const Vec3& a, const Vec3& b)
{
return { a.x * b.x, a.y * b.y, a.z * b.z };
}
float dot(const Vec3& a, const Vec3& b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
float mag(const Vec3& a)
{
return sqrtf(dot(a, a));
}
float saturate(float a)
{
if (a < 0.0f)
return 0.0f;
if (a > 1.0f)
return 1.0f;
return a;
}
Vec3 norm(const Vec3& a)
{
// multiplication by the inverse is the same as division
return a * (1.f / mag(a));
}
Vec3 cross(const Vec3& a, const Vec3& b)
{
return { a.y * b.z - a.z * b.y, a.x * b.z - a.z * b.x, a.x * b.y - a.y * b.x };
}
// vector and normal
Vec3 reflect(const Vec3& v, const Vec3& n)
{
return v - 2 * dot(v, n) * n;
}
struct Ray
{
Vec3 pos; // where ray starts
Vec3 dir;
};
struct Sphere
{
float radius;
Vec3 pos;
Vec3 color;
};
// infinite plane
struct Plane
{
Vec3 normal; // perpendicular to the surface
float distance; // distance from 0,0,0 (center of the screen) to plane along normal
Vec3 color;
};
struct Hit
{
Vec3 pos; // position of a hit, where the ray hit the object
Vec3 normal; // vector perpendicular to the surface at the point of intersection
float distance; // distance along ray to the hit position
Vec3 color;
};
// Ray-Sphere intersection
bool intersect(const Ray& ray, const Sphere& sphere, Hit& hit)
{
Vec3 between = sphere.pos - ray.pos;
//float d = sqrt(mag(between) * mag(between) - mag(projected) * mag(projected));
//std::cout << std::setprecision(20) << std::fixed << "cross: " << d << ", dot: " << x << std::endl;
// ray vector projected on to the vector between the sphere and the ray
Vec3 projected = dot(between, ray.dir) * ray.dir;
float d = mag(cross(ray.dir, between));
float t1 = dot(ray.dir, between);
// Hit
// first argument checks if the ray and the sphere is in the same directions
// second checks if we hit the sphere
if (t1 > 0.0f && d <= sphere.radius)
{
float t2 = sqrt(sphere.radius * sphere.radius - d * d);
hit.distance = t1 - t2;
// the end of the vector between start of the ray and the hit position
hit.pos = ray.pos + ray.dir * hit.distance;
// vector from the sphere center to the hit position
hit.normal = norm(sphere.pos - hit.pos);
hit.color = sphere.color;
// @TODO: ???
// if the ray is perfectly in opposite direction hit will still be detected so we have to check
// if the ray and the sphere are in the same directions
//if (dot(ray.dir, hit.normal) > 0.0f)
//{
// hit.normal = -1.0f * hit.normal;
//}
return true;
}
return false;
}
// Ray-Plane intersection
bool intersect(const Ray& ray, const Plane& plane, Hit& hit)
{
float denom = dot(ray.dir, plane.normal);
if (denom > 0.00001f)
{
hit.distance = -(dot(ray.pos, plane.normal) + plane.distance) / denom;
hit.pos = ray.pos + (ray.dir * hit.distance);
hit.normal = plane.normal;
hit.color = plane.color;
return true;
}
return false;
}
struct Scene
{
Sphere s1;
Sphere s2;
Sphere s3;
Plane p1; // ground
};
bool intersect(const Ray& ray, const Scene& scene, Hit& hit)
{
Hit temp_hit = {};
float max_distance = 100000.0f;
float is_hit = false;
if (intersect(ray, scene.s1, temp_hit))
{
if (temp_hit.distance < max_distance)
{
hit = temp_hit;
max_distance = temp_hit.distance;
is_hit = true;
}
}
if (intersect(ray, scene.s2, temp_hit))
{
if (temp_hit.distance < max_distance)
{
hit = temp_hit;
max_distance = temp_hit.distance;
is_hit = true;
}
}
if (intersect(ray, scene.s3, temp_hit))
{
if (temp_hit.distance < max_distance)
{
hit = temp_hit;
max_distance = temp_hit.distance;
is_hit = true;
}
}
if (intersect(ray, scene.p1, temp_hit))
{
if (temp_hit.distance < max_distance)
{
hit = temp_hit;
max_distance = temp_hit.distance;
is_hit = true;
}
}
return is_hit;
}
// find color of single pixel based on what the ray hit
Vec3 path_tracing(const Ray& ray, const Scene& scene, uint32_t bounces)
{
Hit hit;
if (intersect(ray, scene, hit))
{
if (bounces != 0)
{
bounces--;
Vec3 reflected = reflect(ray.dir, hit.normal);
Ray ray_bounce;
ray_bounce.pos = hit.pos;
ray_bounce.dir = reflected;
return hit.color * path_tracing(ray_bounce, scene, bounces);
}
// hit.normal is in range <-1, 1> but we need <0, 1>
//return (hit.normal + Vec3{1.0f, 1.0f, 1.0f}) * 0.5f;
return hit.color;
}
// Sky color
Vec3 white = { 1.0f, 1.0f, 1.0f };
Vec3 blue = { 0.4f, 0.7f, 1.0f };
// linear interpolation between white and blue
// we don't want t to be more than 1 or less than 0, so we have to saturate it!
float t = saturate(0.5f * (ray.dir.y + 1.0f));
return white * t + blue * (1.0f - t);
//path_tracing(reflect(ray, hit.normal), scene, bounces - 1);
}
// Create a ray from the pixel
Vec3 render(const Scene& scene, uint32_t x, uint32_t y, uint32_t width, uint32_t height)
{
Vec3 camera_pos = { 0.f, 0.f, -8.f };
float camera_near = 2.f;
// pixel position on the camera near plane
float aspect_ratio = width / (float)height;
Vec3 pixel_pos = {
aspect_ratio * (float)x / (float)width - (aspect_ratio - 1.f) * 0.5f - 0.5f,
(float)y / (float)height - 0.5f,
camera_pos.z + camera_near
};
Ray ray;
ray.pos = pixel_pos;
// point - point = vector between them
ray.dir = norm(pixel_pos - camera_pos);
return path_tracing(ray, scene, 16);
}
int main()
{
const uint32_t width = 1024;
const uint32_t height = 768;
const uint32_t stride = 3;
const uint32_t image_size = width * height * stride;
void* image = malloc(image_size);
memset(image, 0xFF, image_size);
// pixel is 1 byte so it's max is 255 and the rgb max is also 255
// we will move by 3 (rgb = 3 values in range 0-255) every loop iteration
uint8_t* pixel = (uint8_t*)image;
// Scene
Scene scene;
// minus is up/forward/direction to camera, plus is down/back/direction away from camera
// 0,0,0 is a center of the image
scene.s1.pos = { 0.f, 0.0f, -4.f };
scene.s1.radius = 0.5f;
scene.s1.color = { 1.0f, 0.3f, 0.3f };
scene.s2.pos = { 1.f, 0.0f, -4.f };
scene.s2.radius = 0.5f;
scene.s2.color = { 0.3f, 1.0f, 0.4f };
scene.s3.pos = { -1.f, 0.0f, -4.f };
scene.s3.radius = 0.5f;
scene.s3.color = { 0.4f, 0.3f, 1.0f };
scene.p1.normal = { 0.0f, 1.0f, 0.0f };
// we lower the plane by sphere radius so that the sphere sits on it
scene.p1.distance = -0.5f;
scene.p1.color = { 0.5f, 0.5f, 0.5f };
for (uint32_t y = 0; y < height; ++y)
{
for (uint32_t x = 0; x < width; ++x)
{
// render of a single pixel
Vec3 color = render(scene, x, y, width, height);
// Assign the pixel colors
pixel[0] = color.x * 255.f;
pixel[1] = color.y * 255.f;
pixel[2] = color.z * 255.f;
pixel += stride;
}
}
int32_t res = stbi_write_png("render.png", width, height, 3, image, width*stride);
if (res)
{
printf("Saved to render.png\n");
std::system("start render.png");
}
else
printf("Cannot save to render.png\n");
//std::cin.get();
return 0;
}