-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathinteractions.h
More file actions
122 lines (113 loc) · 4.59 KB
/
interactions.h
File metadata and controls
122 lines (113 loc) · 4.59 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
#pragma once
#include "intersections.h"
// CHECKITOUT
/**
* Computes a cosine-weighted random direction in a hemisphere.
* Used for diffuse lighting.
*/
__host__ __device__
glm::vec3 calculateRandomDirectionInHemisphere(
glm::vec3 normal, thrust::default_random_engine &rng) {
thrust::uniform_real_distribution<float> u01(0, 1);
float up = sqrt(u01(rng)); // cos(theta)
float over = sqrt(1 - up * up); // sin(theta)
float around = u01(rng) * TWO_PI;
// Find a direction that is not the normal based off of whether or not the
// normal's components are all equal to sqrt(1/3) or whether or not at
// least one component is less than sqrt(1/3). Learned this trick from
// Peter Kutz.
glm::vec3 directionNotNormal;
if (abs(normal.x) < SQRT_OF_ONE_THIRD) {
directionNotNormal = glm::vec3(1, 0, 0);
} else if (abs(normal.y) < SQRT_OF_ONE_THIRD) {
directionNotNormal = glm::vec3(0, 1, 0);
} else {
directionNotNormal = glm::vec3(0, 0, 1);
}
// Use not-normal direction to generate two perpendicular directions
glm::vec3 perpendicularDirection1 =
glm::normalize(glm::cross(normal, directionNotNormal));
glm::vec3 perpendicularDirection2 =
glm::normalize(glm::cross(normal, perpendicularDirection1));
return up * normal
+ cos(around) * over * perpendicularDirection1
+ sin(around) * over * perpendicularDirection2;
}
/**
* Scatter a ray with some probabilities according to the material properties.
* For example, a diffuse surface scatters in a cosine-weighted hemisphere.
* A perfect specular surface scatters in the reflected ray direction.
* In order to apply multiple effects to one surface, probabilistically choose
* between them.
*
* The visual effect you want is to straight-up add the diffuse and specular
* components. You can do this in a few ways. This logic also applies to
* combining other types of materias (such as refractive).
*
* - Always take an even (50/50) split between a each effect (a diffuse bounce
* and a specular bounce), but divide the resulting color of either branch
* by its probability (0.5), to counteract the chance (0.5) of the branch
* being taken.
* - This way is inefficient, but serves as a good starting point - it
* converges slowly, especially for pure-diffuse or pure-specular.
* - Pick the split based on the intensity of each material color, and divide
* branch result by that branch's probability (whatever probability you use).
*
* This method applies its changes to the Ray parameter `ray` in place.
* It also modifies the color `color` of the ray in place.
*
* You may need to change the parameter list for your purposes!
*/
__host__ __device__
void scatterRay(
PathSegment & pathSegment,
glm::vec3 intersect,
glm::vec3 normal,
const Material &m,
thrust::default_random_engine &rng) {
// TODO: implement this.
// A basic implementation of pure-diffuse shading will just call the
// calculateRandomDirectionInHemisphere defined above.
thrust::uniform_real_distribution<float> u01(0, 1);
float probability = u01(rng);
if (m.hasRefractive) {
float costheta = glm::dot(pathSegment.ray.direction, normal);
float n1 = costheta < 0 ? 1 : m.indexOfRefraction;
float n2 = costheta < 0 ? m.indexOfRefraction : 1;
float eta = n1 / n2;
float R0 = powf((n1 - n2) / (n1 + n2), 2);
float R_theta = R0 + (1 - R0)*powf(1 - glm::abs(costheta), 5);
if (probability > R_theta) {
pathSegment.ray.direction = glm::refract(pathSegment.ray.direction, normal, eta);
if(costheta >= 0) pathSegment.color *= m.color;
}
else {
pathSegment.ray.direction = glm::reflect(pathSegment.ray.direction, normal);
pathSegment.color *= m.specular.color;
}
}
else if (probability > m.hasReflective) {
pathSegment.color *= m.color;
pathSegment.ray.direction = calculateRandomDirectionInHemisphere(normal, rng);
}
else {
// specular reflection
pathSegment.color *= m.specular.color;
pathSegment.ray.direction = glm::reflect(pathSegment.ray.direction, normal);
}
pathSegment.ray.origin = intersect + 0.01f * pathSegment.ray.direction;
}
__host__ __device__
void scatterRay1(
PathSegment & pathSegment,
glm::vec3 intersect,
glm::vec3 normal,
const Material &m,
thrust::default_random_engine &rng) {
// TODO: implement this.
// A basic implementation of pure-diffuse shading will just call the
// calculateRandomDirectionInHemisphere defined above.
pathSegment.ray.direction = calculateRandomDirectionInHemisphere(normal, rng);
pathSegment.color *= m.color;
pathSegment.ray.origin = intersect + normal * 0.01f;
}