-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.cpp
More file actions
129 lines (105 loc) · 5.05 KB
/
render.cpp
File metadata and controls
129 lines (105 loc) · 5.05 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
#include "render.h"
Integrator::Integrator(Scene &scene)
{
this->scene = scene;
this->outputImage.allocate(TextureType::UNSIGNED_INTEGER_ALPHA, this->scene.imageResolution);
}
long long Integrator::render(int spp, int sampleStrat)
{
auto startTime = std::chrono::high_resolution_clock::now();
for (int x = 0; x < this->scene.imageResolution.x; x++) {
for (int y = 0; y < this->scene.imageResolution.y; y++) {
Vector3f result(0, 0, 0);
for (int num_iter = 0; num_iter < spp; num_iter++)
{
float dx = half_uniform(next_float());
float dy = half_uniform(next_float());
Ray cameraRay = this->scene.camera.generateRay(x, y, dx, dy);
Interaction si = this->scene.rayIntersect(cameraRay);
Interaction li = this->scene.rayEmitterIntersect(cameraRay);
if (li.didIntersect && li.t < si.t)
{
result += li.emissiveColor;
continue;
}
if (si.didIntersect) {
Vector3f radiance;
LightSample ls;
Vector3f areaContribs;
float dx2,dy2,dz2;
if (sampleStrat == 0)
{
dz2 = next_float();
float e2 = next_float();
dx2 = hemisphere_cos(e2)*hemisphere_root(dz2);
dy2 = hemisphere_sin(e2)*hemisphere_root(dz2);
}
else if (sampleStrat == 1)
{
Vector3f tempSamp = cosine_hemisphere();
dx2 = tempSamp.x;
dy2 = tempSamp.y;
dz2 = tempSamp.z;
}
else if (sampleStrat == 2)
{
Light light_to_sample = this->scene.lights[myRandomInt((std::size_t)0, this->scene.lights.size()-1)];
std::tie(radiance, ls) = light_to_sample.sample(si);
Ray shadowRay(si.p + 1e-3f * si.n, ls.wo);
Interaction siShadow = this->scene.rayIntersect(shadowRay);
if (!siShadow.didIntersect || siShadow.t > ls.d) {
result += si.bsdf->eval(&si, si.toLocal(ls.wo)) * radiance * std::abs(Dot(si.n, ls.wo)) * this->scene.lights.size();
}
}
else if (sampleStrat == 3)
{
for (Light &light : this->scene.lights)
{
if (light.type == LightType::AREA_LIGHT)
continue;
std::tie(radiance, ls) = light.sample(si);
Ray shadowRay(si.p + 1e-3f * si.n, ls.wo);
Interaction siShadow = this->scene.rayIntersect(shadowRay);
if (!siShadow.didIntersect || siShadow.t > ls.d) {
result += si.bsdf->eval(&si, si.toLocal(ls.wo)) * radiance * std::abs(Dot(si.n, ls.wo));
}
}
}
if (sampleStrat == 0 or sampleStrat == 1)
{
Vector3f randDir = si.toWorld(Vector3f(dx2, dy2, dz2));
Ray shadowRay(si.p + 1e-3f * si.n, randDir);
Interaction siShadow = this->scene.rayIntersect(shadowRay);
Interaction liShadow = this->scene.rayEmitterIntersect(shadowRay);
if (liShadow.didIntersect && liShadow.t < siShadow.t)
areaContribs += liShadow.emissiveColor * std::abs(Dot(si.n, randDir)) * si.bsdf->eval(&si, si.toLocal(randDir));
if (sampleStrat == 0)
areaContribs *= 2*M_PI;
else if (sampleStrat == 1)
areaContribs *= M_PI/dz2;
result += areaContribs;
}
}
}
result /= spp;
this->outputImage.writePixelColor(result, x, y);
}
}
auto finishTime = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<std::chrono::microseconds>(finishTime - startTime).count();
}
int main(int argc, char **argv)
{
if (argc != 5) {
std::cerr << "Usage: ./render <scene_config> <out_path> <num_samples> <sampling_strategy>";
return 1;
}
Scene scene(argv[1]);
Integrator rayTracer(scene);
int spp = atoi(argv[3]);
int sampleStrat = atoi(argv[4]);
auto renderTime = rayTracer.render(spp, sampleStrat);
std::cout << "Render Time: " << std::to_string(renderTime / 1000.f) << " ms" << std::endl;
rayTracer.outputImage.save(argv[2]);
return 0;
}