-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.cpp
280 lines (268 loc) · 15.1 KB
/
scene.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
#include "scene.hpp"
void Scene::buildBVH() {
if (!IS_SAH)
bvh = new BVH(objects, BVH::SplitMethod::NAIVE);
else
bvh = new BVH(objects, BVH::SplitMethod::SAH);
}
Intersection Scene::intersect(const Ray &ray) const {
if (!IS_BVH) {
Intersection intersection;
float tNear = kInfinity;
for (const auto &object: objects) {
Intersection intersectionTemp = object->getIntersection(ray);
if (intersectionTemp.happened == true && intersectionTemp.distance < tNear) {
intersection = intersectionTemp;
tNear = intersectionTemp.distance;
}
}
return intersection;
} else {
return bvh->intersect(ray);
}
}
Vector3f Scene::castRay(const Ray &ray, int depth) const {
if (IS_PATH) {
// path tracing
if (depth > MAX_DEPTH)
return Vector3f(0.0, 0.0, 0.0);
Vector3f hitColor = backgroundColor;
Intersection intersection = intersect(ray);
Material *material = intersection.material;
Object *hitObject = intersection.object;
if (intersection.happened) {
Vector3f hitCoordinate = intersection.coordinate;
Vector3f hitNormal = intersection.normal;
switch (material->getType()) {
case DIFFUSE:
{
Vector3f LDir = {0.0, 0.0, 0.0};
Vector3f LIndir = {0.0, 0.0, 0.0};
Vector3f hitPointOrig = (dotProduct(ray.direction, hitNormal) < 0) ?
hitCoordinate + hitNormal * epsilon2 :
hitCoordinate - hitNormal * epsilon2;
// sample on light
bool isPointLight = getRandomFloat() <= POINT_LIGHT_RATIO;
if (isPointLight && lights.size() > 0) {
// point light
int lightIndex = getRandomInt(0, lights.size() - 1);
auto &light = lights[lightIndex];
Vector3f lightPosition = light->position;
Vector3f lightIntensity = light->intensity;
Vector3f lightDir = lightPosition - hitPointOrig;
float lightDistance2 = dotProduct(lightDir, lightDir);
lightDir = normalize(lightDir);
Intersection intersection2 = intersect(Ray(hitPointOrig, lightDir));
bool isDir = (!intersection2.happened) || (intersection2.happened && intersection2.distance >= std::sqrt(lightDistance2) - epsilon2);
if (isDir) {
LDir = lightIntensity * material->brdf(ray.direction, lightDir, hitNormal)
* dotProduct(lightDir, hitNormal) / lightDistance2;
}
} else {
// area light
Intersection lightSample;
float lightPdf = 0.0;
sampleLight(lightSample, lightPdf);
Vector3f lightPosition = lightSample.coordinate;
Vector3f lightNormal = lightSample.normal;
Vector3f lightIntensity = lightSample.material->intensity;
// shoot a ray from hit point to light
Vector3f lightDir = lightPosition - hitPointOrig;
float lightDistance2 = dotProduct(lightDir, lightDir);
lightDir = normalize(lightDir);
// direct illumination
Intersection intersection2 = intersect(Ray(hitPointOrig, lightDir));
bool isDir = (!intersection2.happened) || (intersection2.happened && intersection2.distance >= std::sqrt(lightDistance2) - epsilon2);
if (isDir) {
LDir = lightIntensity * material->brdf(ray.direction, lightDir, hitNormal)
* dotProduct(lightDir, hitNormal) * dotProduct(-lightDir, lightNormal)
/ lightDistance2 / lightPdf;
}
}
// russian roulette
bool isIndir = getRandomFloat() <= PATH_RR;
if (isIndir) {
// sample on hemisphere
Vector3f wi = material->sample(ray.direction, hitNormal).normalized();
// shoot a ray from hit point to wi direction
Ray rayIndir(hitPointOrig, wi);
Intersection interIndir = intersect(rayIndir);
if (interIndir.happened && interIndir.material->getType() != EMISSION) {
LIndir = castRay(rayIndir, depth + 1) * material->brdf(ray.direction, wi, hitNormal)
* dotProduct(wi, hitNormal) / material->pdf(ray.direction, wi, hitNormal) / PATH_RR;
}
}
hitColor = LDir + LIndir;
} break;
case REFLECTION:
{
Vector3f reflectionDirection = normalize(reflect(ray.direction, hitNormal));
Vector3f reflectionRayOrig = (dotProduct(reflectionDirection, hitNormal) < 0) ?
hitCoordinate - hitNormal * epsilon2 :
hitCoordinate + hitNormal * epsilon2;
hitColor = castRay(Ray(reflectionRayOrig, reflectionDirection), depth + 1);
} break;
case REFRACTION:
{
Vector3f refractionDirection = normalize(refract(ray.direction, hitNormal, material->ior));
Vector3f refractionRayOrig = (dotProduct(refractionDirection, hitNormal) < 0) ?
hitCoordinate - hitNormal * epsilon2 :
hitCoordinate + hitNormal * epsilon2;
hitColor = castRay(Ray(refractionRayOrig, refractionDirection), depth + 1);
} break;
case REFLECTION_AND_REFRACTION:
{
Vector3f reflectionDirection = normalize(reflect(ray.direction, hitNormal));
Vector3f refractionDirection = normalize(refract(ray.direction, hitNormal, material->ior));
Vector3f reflectionRayOrig = (dotProduct(reflectionDirection, hitNormal) < 0) ?
hitCoordinate - hitNormal * epsilon2 :
hitCoordinate + hitNormal * epsilon2;
Vector3f refractionRayOrig = (dotProduct(refractionDirection, hitNormal) < 0) ?
hitCoordinate - hitNormal * epsilon2 :
hitCoordinate + hitNormal * epsilon2;
Vector3f reflectionColor = castRay(Ray(reflectionRayOrig, reflectionDirection), depth + 1);
Vector3f refractionColor = castRay(Ray(refractionRayOrig, refractionDirection), depth + 1);
float kr = fresnel(ray.direction, hitNormal, material->ior);
hitColor = reflectionColor * kr + refractionColor * (1 - kr);
} break;
case EMISSION:
{
hitColor = material->intensity;
} break;
default:
{
throw std::runtime_error("Unsupported material type.");
}
}
}
return hitColor;
} else {
// Whitted-style ray tracing
if (depth > MAX_DEPTH)
return Vector3f(0.0, 0.0, 0.0);
Vector3f hitColor = backgroundColor;
Intersection intersection = intersect(ray);
Material *material = intersection.material;
Object *hitObject = intersection.object;
if (intersection.happened) {
Vector3f hitCoordinate = intersection.coordinate;
Vector3f hitNormal = intersection.normal;
switch (material->getType()) {
case DIFFUSE:
{
// Blinn-Phong model
Vector3f ambientColor = 0, diffuseColor = 0, specularColor = 0;
bool outside = dotProduct(ray.direction, hitNormal) < 0;
Vector3f shadowPointOrig = outside ?
hitCoordinate + hitNormal * epsilon2 :
hitCoordinate - hitNormal * epsilon2;
// ambient
ambientColor += outside ? 0 : ambientIntensity;
// point light
for (auto &light: lights) {
Vector3f lightDir = light->position - shadowPointOrig;
float lightDistance2 = dotProduct(lightDir, lightDir);
lightDir = normalize(lightDir);
float LdotN = std::max(0.f, dotProduct(lightDir, hitNormal));
// hard shadow
Intersection intersection2 = intersect(Ray(shadowPointOrig, lightDir));
bool inShadow = intersection2.happened && (intersection2.distance < std::sqrt(lightDistance2) - epsilon2);
// diffuse
diffuseColor += inShadow ? 0 : light->intensity * LdotN / lightDistance2;
// specular
Vector3f halfVector = normalize(lightDir - ray.direction);
specularColor += inShadow ? 0 : light->intensity / lightDistance2 *
powf(std::max(0.f, dotProduct(hitNormal, halfVector)), material->specularExponent);
}
// area light
for (auto &object: objects) {
if (object->material->getType() == EMISSION) {
for (int i = 0; i < AREA2POINT_NUM; ++i) {
// sample on area light
Intersection lightSample;
float lightPdf = 0.0;
object->sample(lightSample, lightPdf);
// similar to point light
Vector3f lightDir = lightSample.coordinate - shadowPointOrig;
float lightDistance2 = dotProduct(lightDir, lightDir);
lightDir = normalize(lightDir);
float LdotN = std::max(0.f, dotProduct(lightDir, hitNormal));
// hard shadow
Intersection intersection2 = intersect(Ray(shadowPointOrig, lightDir));
bool inShadow = intersection2.happened && (intersection2.distance < std::sqrt(lightDistance2) - epsilon2);
// diffuse
diffuseColor += inShadow ? 0 : object->material->intensity * object->getArea() / AREA2POINT_NUM * LdotN / lightDistance2;
// specular
Vector3f halfVector = normalize(lightDir - ray.direction);
specularColor += inShadow ? 0 : object->material->intensity * object->getArea() / AREA2POINT_NUM / lightDistance2 *
powf(std::max(0.f, dotProduct(hitNormal, halfVector)), material->specularExponent);
}
}
}
hitColor = material->Ka * ambientColor + material->Kd * diffuseColor + material->Ks * specularColor;
} break;
case REFLECTION:
{
Vector3f reflectionDirection = normalize(reflect(ray.direction, hitNormal));
Vector3f reflectionRayOrig = (dotProduct(reflectionDirection, hitNormal) < 0) ?
hitCoordinate - hitNormal * epsilon2 :
hitCoordinate + hitNormal * epsilon2;
hitColor = castRay(Ray(reflectionRayOrig, reflectionDirection), depth + 1);
} break;
case REFRACTION:
{
Vector3f refractionDirection = normalize(refract(ray.direction, hitNormal, material->ior));
Vector3f refractionRayOrig = (dotProduct(refractionDirection, hitNormal) < 0) ?
hitCoordinate - hitNormal * epsilon2 :
hitCoordinate + hitNormal * epsilon2;
hitColor = castRay(Ray(refractionRayOrig, refractionDirection), depth + 1);
} break;
case REFLECTION_AND_REFRACTION:
{
Vector3f reflectionDirection = normalize(reflect(ray.direction, hitNormal));
Vector3f refractionDirection = normalize(refract(ray.direction, hitNormal, material->ior));
Vector3f reflectionRayOrig = (dotProduct(reflectionDirection, hitNormal) < 0) ?
hitCoordinate - hitNormal * epsilon2 :
hitCoordinate + hitNormal * epsilon2;
Vector3f refractionRayOrig = (dotProduct(refractionDirection, hitNormal) < 0) ?
hitCoordinate - hitNormal * epsilon2 :
hitCoordinate + hitNormal * epsilon2;
Vector3f reflectionColor = castRay(Ray(reflectionRayOrig, reflectionDirection), depth + 1);
Vector3f refractionColor = castRay(Ray(refractionRayOrig, refractionDirection), depth + 1);
float kr = fresnel(ray.direction, hitNormal, material->ior);
hitColor = reflectionColor * kr + refractionColor * (1 - kr);
} break;
case EMISSION:
{
hitColor = material->intensity;
} break;
default:
{
throw std::runtime_error("Unsupported material type.");
}
}
}
return hitColor;
}
// never reach here actually
return Vector3f(0, 0, 0);
}
void Scene::sampleLight(Intersection &position, float &pdf) const {
float emissionAreaSum = 0;
for (uint32_t k = 0; k < objects.size(); ++k) {
if (objects[k]->material->getType() == EMISSION){
emissionAreaSum += objects[k]->getArea();
}
}
float p = getRandomFloat() * emissionAreaSum;
emissionAreaSum = 0;
for (uint32_t k = 0; k < objects.size(); ++k) {
if (objects[k]->material->getType() == EMISSION) {
emissionAreaSum += objects[k]->getArea();
if (p <= emissionAreaSum) {
objects[k]->sample(position, pdf);
break;
}
}
}
}