-
Notifications
You must be signed in to change notification settings - Fork 2
/
raytracing.hh
303 lines (269 loc) · 10.6 KB
/
raytracing.hh
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
#ifndef RAYTRACING_HH
#define RAYTRACING_HH
#include <cassert>
#include "Vec3.hh"
#include "util.hh"
#include "UniformGrid.hh"
#include "AABB.hh"
#include "Geometry.hh"
#define PRIMARY_RAYS 1
#define SKY_COLOR Float3(1)
namespace raytracing {
DEVICE static Float3 trace(const Float3 &ray_orig, const Float3 &ray_dir,
AABB world_bounds, const UniformGrid &grid,
bool accel, int depth);
DEVICE static bool ray_intersect(const Float3 &ray_orig, const Float3 &ray_dir,
AABB world_bounds, const UniformGrid &grid,
Float3 &intersection, Geometry *&hit_geom);
template <typename II>
DEVICE static bool ray_intersect_items(const Float3 &ray_orig,
const Float3 &ray_dir, II b, II e,
Float3 &intersection,
Geometry *&hit_geom);
DEVICE static float fresnel(Float3 dir, Float3 normal, float ior) {
float cosi = dir.dot(normal);
float n1 = 1;
float n2 = ior;
if (cosi > 0.f)
util::swap(n1, n2);
float sint = (n1 / n2) * sqrt(util::max(0.f, 1.f - cosi * cosi));
if (sint >= 1.f) // total internal relfection
return 1.f;
float cost = sqrt(util::max(0.f, 1.f - sint * sint));
cosi = abs(cosi);
float Rs = ((n2 * cosi) - (n1 * cost)) / ((n2 * cosi) + (n1 * cost));
float Rp = ((n1 * cosi) - (n2 * cost)) / ((n1 * cosi) + (n2 * cost));
return (Rs * Rs + Rp * Rp) / 2;
}
DEVICE static Float3 tonemap(Float3 color, float gamma = 0.85f,
float alpha = 1.1f);
} // namespace raytracing
/**
* @brief Traces a primary ray and produces a color
*
* @param ray_orig Ray origin point
* @param ray_dir Ray direction as a unit vector
* @param spheres Scene geometry
* @param depth Maximum trace depth
* @return Color computed for this primary ray
*/
DEVICE static Float3 raytracing::trace(const Float3 &ray_orig,
const Float3 &ray_dir, AABB world_bounds,
const UniformGrid &grid, bool accel,
int depth) {
Float3 color = 1.0;
Float3 light = 0.0;
Float3 origin = ray_orig;
Float3 direction = ray_dir;
for (int i = 0; i < depth; ++i) {
// cast ray
Float3 intersection;
Geometry *hit_geom;
bool intersected;
if (accel)
intersected = raytracing::ray_intersect(
origin, direction, world_bounds, grid, intersection, hit_geom);
else
intersected = raytracing::ray_intersect_items(
origin, direction, grid.first(), grid.last(), intersection,
hit_geom);
if (!intersected) {
light += SKY_COLOR * color;
break;
}
// emissive material
if (!(hit_geom->material()->emission_color == 0)) {
light += hit_geom->material()->emission_color * color;
break;
}
color *= hit_geom->material()->surface_color;
origin = intersection;
Float3 normal = hit_geom->normal(ray_dir, intersection);
if (hit_geom->material()->transparency > util::randf(0, 1)) {
float fresneleffect = raytracing::fresnel(direction, normal, 1.1f);
if (util::randf(0, 1) < fresneleffect) {
// reflective material
direction = direction.reflect(normal);
origin += normal * 1e-4; // fudge
} else {
float refr_i;
if (direction.dot(normal) > 0)
refr_i = 1.1;
else
refr_i = 0.91;
float angle = normal.dot(direction);
float k = 1 - refr_i * refr_i * (1 - angle * angle);
Float3 refraction_dir =
direction * refr_i + normal * (refr_i * angle - sqrt(k));
refraction_dir.normalize();
direction = refraction_dir;
origin += direction * 1e-4; // fudge
}
} else if (hit_geom->material()->reflection > util::randf(0, 1)) {
direction = direction.reflect(normal);
origin += normal * 1e-4; // fudge
} else {
// diffuse material
// generate random number on a sphere, but we want only
// vectors pointing in the same hemisphere as the normal
direction = Float3::random_spherical();
if (direction.dot(normal) < 0)
direction *= -1;
origin += normal * 1e-4; // fudge
}
direction.normalize();
}
return light;
}
/**
* @brief Finds the nearest intersection between a ray and scene geometry.
* @detail Uses traversal algorithm proposed by Amanatides and Woo (1987).
*
* @param[in] ray_orig Ray origin point
* @param[in] ray_dir Ray direction as unit vector
* @param[in] geom Scene geometry
* @param[in] world_bounds an AABB for entire world (use geometry_bounds())
* @param[in] grid the UniformGrid acceleration structure
* @param[out] intersection The point of intersection
* @param[out] hit_geom The geometry that was intersected
* @return Whether there was an intersection
*/
DEVICE static bool raytracing::ray_intersect(
const Float3 &ray_orig, const Float3 &ray_dir, AABB world_bounds,
const UniformGrid &grid, Float3 &intersection, Geometry *&hit_geom) {
// find ray entry point into world bounds
const Geometry bbox(BoxData{world_bounds});
Float3 ray_entry;
if (world_bounds.contains(ray_orig)) {
ray_entry = ray_orig;
} else {
float t;
if (!bbox.intersect(ray_orig, ray_dir, t))
return false;
ray_entry = ray_orig + ray_dir * t;
}
const Float3 world_size = world_bounds.xmax - world_bounds.xmin;
Float3 relative_entry = ray_entry - world_bounds.xmin;
relative_entry = vmax(Float3(0), relative_entry);
relative_entry = vmin(world_size - 1e-5, relative_entry); // good tolerance
// compute voxel parameters
Int3 voxel_pos(floor(relative_entry.x / (grid.cell_size.x)),
floor(relative_entry.y / (grid.cell_size.y)),
floor(relative_entry.z / (grid.cell_size.z)));
const Int3 voxel_step(ray_dir.x < 0 ? -1 : 1, ray_dir.y < 0 ? -1 : 1,
ray_dir.z < 0 ? -1 : 1);
const Float3 next_voxel_bound =
Float3(voxel_pos + vmax(Int3(0), voxel_step)) * grid.cell_size;
// compute t values at which ray crosses voxel boundaries
Float3 t_max = (next_voxel_bound - relative_entry) / ray_dir;
// compute t deltas
Float3 t_delta = grid.cell_size / ray_dir * Float3(voxel_step);
// assert(t_delta.x >= 0 && t_delta.y >= 0 && t_delta.z >= 0);
// handle div by zero
if (ray_dir.x == 0)
t_max.x = INFINITY, t_delta.x = INFINITY;
if (ray_dir.y == 0)
t_max.y = INFINITY, t_delta.y = INFINITY;
if (ray_dir.z == 0)
t_max.z = INFINITY, t_delta.z = INFINITY;
assert(voxel_pos.x >= 0 && voxel_pos.y >= 0 && voxel_pos.z >= 0);
assert(voxel_pos.x < grid.res.x && voxel_pos.y < grid.res.y &&
voxel_pos.z < grid.res.z);
// traverse the grid
unsigned i = 0;
do {
// test objects
auto b = grid.first(voxel_pos);
auto e = grid.last(voxel_pos);
if (b != e) {
if (raytracing::ray_intersect_items(ray_orig, ray_dir, b, e,
intersection, hit_geom)) {
Float3 cell_pos =
world_bounds.xmin + Float3(voxel_pos) * grid.cell_size;
if (AABB(cell_pos - 1e-5, cell_pos + grid.cell_size + 1e-5)
.contains(intersection))
return true;
}
}
if (t_max.x < t_max.y) {
if (t_max.x < t_max.z) {
voxel_pos.x += voxel_step.x;
if (voxel_pos.x >= grid.res.x || voxel_pos.x < 0)
return false;
t_max.x += t_delta.x;
} else {
voxel_pos.z += voxel_step.z;
if (voxel_pos.z >= grid.res.z || voxel_pos.z < 0)
return false;
t_max.z += t_delta.z;
}
} else {
if (t_max.y < t_max.z) {
voxel_pos.y += voxel_step.y;
if (voxel_pos.y >= grid.res.y || voxel_pos.y < 0)
return false;
t_max.y += t_delta.y;
} else {
voxel_pos.z += voxel_step.z;
if (voxel_pos.z >= grid.res.z || voxel_pos.z < 0)
return false;
t_max.z += t_delta.z;
}
}
assert(voxel_pos.x >= 0 && voxel_pos.y >= 0 && voxel_pos.z >= 0);
assert(voxel_pos.x < grid.res.x && voxel_pos.y < grid.res.y &&
voxel_pos.z < grid.res.z);
} while (++i < 10000); // arbitrary traversal length limit
return false;
}
/**
* @brief Classic, grid-free brute-force ray intersection
* @details This is used as a component of cpu_ray_intersect
* It accepts input iterators and checks intersection for each item.
*
* @param[in] ray_orig Ray origin point
* @param[in] ray_dir Ray direction as unit vector
* @param[in] geom Scene geometry
* @param[out] intersection The point of intersection
* @param[out] hit_geom The geometry that was intersected
* @return Whether there was an intersection
*/
template <typename II>
DEVICE static bool raytracing::ray_intersect_items(const Float3 &ray_orig,
const Float3 &ray_dir, II b,
II e, Float3 &intersection,
Geometry *&hit_geom) {
float near_t = INFINITY;
Geometry *near_geom = nullptr;
while (b != e) {
Geometry &g = *b;
float t;
if (!g.intersect(ray_orig, ray_dir, t)) {
++b;
continue;
}
if (t < near_t) {
near_t = t;
near_geom = &g;
}
++b;
}
if (near_geom) {
intersection = ray_orig + ray_dir * near_t;
hit_geom = near_geom;
return true;
}
return false;
}
/**
* @brief Use Reinhard HDR tonemapping algorithm to transform a color
* @param[in] color the HDR color
* @param[in] gamma
*/
DEVICE static Float3 raytracing::tonemap(Float3 color, float gamma,
float alpha) {
Float3 ldr = color / (color + Float3(1));
ldr = pow(ldr, 1.0f / gamma) * alpha;
return ldr;
}
#endif