/
PathTracer.cpp
198 lines (167 loc) · 5.44 KB
/
PathTracer.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
#include "StdAfx.h"
#include "PathTracer.h"
vector<vec3f> PathTracer::renderPixels(const Camera& camera)
{
int t_start = clock();
vector<vec3f> pixelColors(camera.width*camera.height, vec3f(0, 0, 0));
if(useConnection)
renderer->scene.preprocessEmissionSampler();
if(!renderer->scene.usingGPU())
{
for(unsigned s=0; s<spp; s++)
{
int t = clock();
#pragma omp parallel for
for(int p=0; p<pixelColors.size(); p++)
{
Path eyePath;
samplePath(eyePath, camera.generateRay(p));
pixelColors[p] *= s/float(s+1);
if (!(eyePath.back().contactObject && eyePath.back().contactObject->emissive()))
continue;
vec3f color = vec3f(1, 1, 1);
for(unsigned i=0; i<eyePath.size(); i++)
{
color *= eyePath[i].color / eyePath[i].directionProb / eyePath[i].originProb;
if(i!=eyePath.size()-1)
{
color *= eyePath[i].getCosineTerm();
float dist = (eyePath[i+1].origin - eyePath[i].origin).length();
// NOTE: Must multiply the decay !!!!!!!!!
color *= eyePath[i].getRadianceDecay(dist);
}
}
pixelColors[p] += renderer->camera.eliminateVignetting(color, p)/(s+1);//*camera.width*camera.height;
//pixelColors[p] += color * eyePath[0].directionProb / (s+1);
}
//if (clock() / 1000 >= lastTime)
if (s % outputIter == 0)
{
unsigned nowTime = (clock()) / 1000;
showCurrentResult(pixelColors , &nowTime , &s);
//showCurrentResult(pixelColors , &lastTime , &s);
//lastTime += timeInterval;
}
else
showCurrentResult(pixelColors);
printf("Iter: %d IterTime: %ds TotalTime: %ds\n", s+1, (clock()-t)/1000, (clock()-t_start)/1000);
}
}
else
{
for(unsigned s=0; s<spp; s++)
{
int t = clock();
vector<Ray> eyeRays(pixelColors.size());
#pragma omp parallel for
for(int p=0; p<pixelColors.size(); p++)
{
eyeRays[p] = camera.generateRay(p);
}
int clk = clock();
vector<Path> pathList = samplePathList(eyeRays);
vector<Path> lightPathList;
vector<vector<unsigned>> visList;
if(useConnection)
{
lightPathList.resize(pathList.size());
for(unsigned i=0; i<pathList.size(); i++)
lightPathList[i].push_back(genEmissiveSurfaceSample(true , false));
visList = testPathListVisibility(pathList, lightPathList);
}
#pragma omp parallel for
for(int p=0; p<pathList.size(); p++)
{
pixelColors[p] *= s/float(s+1);
vec3f color = vec3f(0, 0, 0);
//pathList[p][0].directionProb = 1.f;
//if(!useConnection || mustUsePT(pathList[p]) ||
// pathList[p].size()==2 && pathList[p].back().contactObject && pathList[p].back().contactObject->emissive())
if (pathList[p].back().contactObject && pathList[p].back().contactObject->emissive())
{
vec3f c(1, 1, 1);
for(unsigned i=0; i<pathList[p].size(); i++)
{
c *= pathList[p][i].color / pathList[p][i].directionProb / pathList[p][i].originProb;
if(i!=pathList[p].size()-1)
{
c *= pathList[p][i].getCosineTerm();
float dist = (pathList[p][i+1].origin - pathList[p][i].origin).length();
// NOTE: Must multiply the decay !!!!!!!!!
c *= pathList[p][i].getRadianceDecay(dist);
}
}
color += c;
}
/*
else
{
Ray &lightRay = lightPathList[p][0];
for(unsigned i=1; i<pathList[p].size(); i++)
{
if(!((visList[p][i/32]>>(i%32)) & 1))
continue;
Path connectedPath;
connectedPath.push_back(lightRay);
Path &eyePath = pathList[p];
if(eyePath[i].contactObject && eyePath[i].contactObject->emissive())
break;
if(eyePath[i].directionSampleType != Ray::RANDOM)
continue;
for(unsigned k=0; k<=i; k++)
connectedPath.push_back(eyePath[i-k]);
connectRays(connectedPath, 0);
vec4f color_prob = connectColorProb(connectedPath, 0);
if(vec3f(color_prob).length()>0 && color_prob.w > 0)
color += vec3f(color_prob) / color_prob.w;// / camera.width / camera.height;
}
}
*/
pixelColors[p] += renderer->camera.eliminateVignetting(color, p)/(s+1);//*camera.width*camera.height;
//pixelColors[p] += color / (s+1);
}
//if (clock() / 1000 >= lastTime)
if (s % outputIter == 0)
{
unsigned nowTime = (clock()) / 1000;
showCurrentResult(pixelColors , &nowTime , &s);
//showCurrentResult(pixelColors , &lastTime , &s);
//lastTime += timeInterval;
}
else
showCurrentResult(pixelColors);
printf("Iter: %d IterTime: %ds TotalTime: %ds\n", s+1, (clock()-t)/1000, (clock()-t_start)/1000);
}
}
return pixelColors;
}
vec4f PathTracer::connectColorProb(const Path& connectedPath, int connectIndex)
{
vec3f color(1, 1, 1);
float prob = 1;
float connectDist;
for(int i=0; i<connectedPath.size(); i++)
{
color *= connectedPath[i].color;
double dist;
if(i <= connectIndex)
{
dist = max2((connectedPath[i+1].origin - connectedPath[i].origin).length(), EPSILON);
color *= connectedPath[i].getRadianceDecay(dist);
}
else if(i>connectIndex+1)
{
dist = max2((connectedPath[i-1].origin - connectedPath[i].origin).length(), EPSILON);
color *= connectedPath[i].getRadianceDecay(dist);
}
if(i==connectIndex && i<connectedPath.size()-1)
{
color *= connectedPath[i].getCosineTerm() * connectedPath[i+1].getCosineTerm() / (dist*dist);
connectDist = dist;
}
if(i!=connectIndex && i!=connectIndex+1)
color *= connectedPath[i].getCosineTerm();
prob *= connectedPath[i].directionProb * connectedPath[i].originProb;
}
return vec4<float>(color, prob);
}