-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhotonmap.cpp
388 lines (347 loc) · 12.7 KB
/
Photonmap.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include "StdAfx.h"
#include "Photonmap.h"
#include "NoSelfIntersectionCondition.h"
#include <omp.h>
#include "macros.h"
#include "RandGenerator.h"
#define MERGE_LEN 10
#define PPM
static FILE* fp = fopen("debug_pm.txt" , "w");
vector<vec3f> PhotonMap::renderPixels(const Camera& camera){
uint width = camera.width, height = camera.height;
std::vector<vec3f> pixelColors(width * height, vec3f(0,0,0));
omp_init_lock(&surfaceHashGridLock);
omp_init_lock(&volumeHashGridLock);
omp_init_lock(&debugPrintLock);
//std::vector<int> pixelMaps(pixelColors.size(), 0);
preprocessEmissionSampler();
mRadius = mBaseRadius;
clock_t startTime = clock();
for(uint s = 0; s < spp; s++){
std::cout << "iteration : " << s << std::endl;
std::vector<vec3f> oneIterColors(pixelColors.size(), vec3f(0,0,0));
#ifdef PPM
//if (renderer->scene.getTotalVolume() > 1e-6f)
if (true)
{
rayMarching = true;
mRadius = MAX(mBaseRadius * powf(powf(s+1 , mAlpha-1) , 1.f / 3.f) , EPSILON);
}
else
{
rayMarching = false;
mRadius = MAX(mBaseRadius * sqrt(powf(s+1, mAlpha-1)), EPSILON);
}
#endif
std::vector<Path*> pixelLightPaths(mPhotonsNum, NULL);
std::vector<LightPoint> surfaceLightVertices(0);
std::vector<LightPoint> volumeLightVertices(0);
surfaceHashGrid.Reserve(pixelColors.size());
volumeHashGrid.Reserve(pixelColors.size());
#pragma omp parallel for
// step1: sample light paths and build range search struct independently for surface and volume
for(int p = 0; p < mPhotonsNum; p++){
Ray lightRay = genEmissiveSurfaceSample(true , false);
pixelLightPaths[p] = new Path;
Path &lightPath = *pixelLightPaths[p];
samplePath(lightPath, lightRay);
for(int i = 1; i < lightPath.size(); i++){
// light is not reflective
if(lightPath[i].contactObject && lightPath[i].contactObject->emissive())
break;
// only store particles non-specular
if(lightPath[i].directionSampleType == Ray::DEFINITE)
continue;
LightPoint lightPoint;
lightPoint.position = lightPath[i].origin;
lightPoint.indexInThePath = i;
lightPoint.pathThePointIn = &lightPath;
lightPoint.photonType = lightPath[i].photonType;
if(lightPoint.photonType == Ray::OUTVOL){
omp_set_lock(&surfaceHashGridLock);
surfaceLightVertices.push_back(lightPoint);
omp_unset_lock(&surfaceHashGridLock);
}
if(lightPoint.photonType == Ray::INVOL){
omp_set_lock(&volumeHashGridLock);
volumeLightVertices.push_back(lightPoint);
omp_unset_lock(&volumeHashGridLock);
}
}
}
std::cout<< "vol vertices= " << volumeLightVertices.size() << " sur vertices= " << surfaceLightVertices.size() << std::endl;
surfaceHashGrid.Build(surfaceLightVertices, mRadius);
volumeHashGrid.Build(volumeLightVertices, mRadius);
std::cout<< "finish building hashgrid" << std::endl;
// step2: calculate pixel colors by progressive photon mapping
#pragma omp parallel for
for(int p = 0; p < pixelColors.size(); p++){
Path eyePath;
if (rayMarching)
sampleMergePath(eyePath, camera.generateRay(p), 0);
else
samplePath(eyePath, camera.generateRay(p));
//fprintf(fp , "===================\n");
//for (int i = 0; i < eyePath.size(); i++)
//{
// fprintf(fp , "l=%d, bsdf=(%.8f,%.8f,%.8f), originPdf=%.8f, dirPdf=%.8f\n" , i , eyePath[i].color.x ,
// eyePath[i].color.y , eyePath[i].color.z , eyePath[i].originProb , eyePath[i].directionProb);
//}
/*if(eyePath[1].contactObj && eyePath[1].contactObj->anisotropic()){
pixelMaps[p] = 1;
}*/
throughputByDensityEstimation(oneIterColors[p], eyePath, surfaceLightVertices, volumeLightVertices);
}
/*std::ofstream fout(engine->renderer->name + engine->scene.name+"pixelMap.txt");
for(int p = 0; p < pixelMaps.size(); p++)
fout << pixelMaps[p] << ' ' ;
fout << std::endl;
fout.close();*/
std::cout << "calculation done" << std::endl;
for(uint i = 0; i < pixelColors.size(); i++){
pixelColors[i] *= s / float(s+1);
pixelColors[i] += camera.eliminateVignetting(oneIterColors[i], i) / (s + 1);
delete pixelLightPaths[i];
}
unsigned nowTime = (float)(clock() - startTime) / 1000;
//if (nowTime > recordTime)
if (s % outputIter == 0)
{
showCurrentResult(pixelColors , &nowTime , &s);
//showCurrentResult(pixelColors , &lastTime , &s);
//recordTime += timeInterval;
}
else
showCurrentResult(pixelColors);
}
return pixelColors;
}
void PhotonMap::sampleMergePath(Path &path, Ray &prevRay, uint depth) const{
path.push_back(prevRay);
Ray terminateRay;
terminateRay.origin = prevRay.origin;
terminateRay.color = vec3f(0,0,0);
terminateRay.direction = vec3f(0,0,0);
terminateRay.directionSampleType = Ray::DEFINITE;
terminateRay.insideObject = NULL;
terminateRay.contactObject = NULL;
terminateRay.intersectObject = NULL;
Ray nextRay;
if(prevRay.insideObject && !prevRay.insideObject->isVolumetric())
nextRay = prevRay.insideObject->scatter(prevRay);
else if(prevRay.intersectObject){
if(prevRay.intersectObject->isVolumetric() && prevRay.contactObject && prevRay.contactObject->isVolumetric()){
prevRay.origin += prevRay.direction * prevRay.intersectDist;
prevRay.intersectDist = 0;
}
nextRay = prevRay.intersectObject->scatter(prevRay);
}
else{
path.push_back(terminateRay); return ;
}
if(nextRay.direction.length() < 0.5){
path.push_back(nextRay); return ;
}
if(depth + 1 > MERGE_LEN){
path.push_back(terminateRay); return ;
}
NoSelfIntersectionCondition condition(&renderer->scene, nextRay);
Scene::ObjSourceInformation info;
float dist = renderer->scene.intersect(nextRay, info, &condition);
if(dist < 0){
path.push_back(nextRay);
path.push_back(terminateRay);
return ;
}
else{
nextRay.intersectObject = renderer->scene.objects[info.objID];
nextRay.intersectObjectTriangleID = info.triangleID;
nextRay.intersectDist = dist;
}
sampleMergePath(path, nextRay, depth + 1);
}
void PhotonMap::throughputByDensityEstimation(vec3f &color, Path &eyeMergePath,
std::vector<LightPoint> &surfaceVertices, std::vector<LightPoint> &volumeVertices)
{
class Query{
PhotonMap *photonMap;
vec3f contrib;
vec3f position;
vec3f hitNormal;
float radius;
int photonsNum;
Ray outRay;
float GaussianKernel(float mahalanobisDist) const{
double exponent = exp((double)-mahalanobisDist/2);
//photonMap->fout << " Gaussian exp = " << exponent << std::endl;
return exponent / (2*M_PI);
}
float Kernel(float distSqr, float radiusSqr) const{
float s = MAX(0, 1 - distSqr / radiusSqr);
return 3 * s * s / M_PI;
}
public:
Query(PhotonMap *map, float r, int n) : photonMap(map), radius(r), photonsNum(n)
{}
bool volumeMedia;
void SetContrib(const vec3f &color) { contrib = color; }
void SetPosition(const vec3f &pos) { position = pos; }
void SetOutRay(const Ray &ray) { outRay = ray; }
void SetNormal(const vec3f &n) { hitNormal = n; }
vec3f GetContrib() const { return contrib; }
vec3f GetPosition() const { return position; }
void Process(const LightPoint &lightPoint){
if(volumeMedia && lightPoint.photonType != Ray::INVOL) return ;
if(!volumeMedia && lightPoint.photonType != Ray::OUTVOL) return ;
if(!lightPoint.pathThePointIn || lightPoint.indexInThePath < 0)
return;
Path &lightPath = *lightPoint.pathThePointIn;
int index = lightPoint.indexInThePath;
/*
if (volumeMedia && lightPath[index].insideObject != outRay.insideObject)
{
printf("aye\n");
return;
}
if (!volumeMedia && lightPath[index].contactObject != outRay.contactObject)
{
printf("aye\n");
return;
}
*/
vec3f photonThroughput(1,1,1);
for(int i = 0; i < index; i++){
photonThroughput *= lightPath[i].color / lightPath[i].directionProb / lightPath[i].originProb;
photonThroughput *= lightPath[i].getCosineTerm();
float dist = (lightPath[i].origin-lightPath[i+1].origin).length();
photonThroughput *= lightPath[i].getRadianceDecay(dist);
}
photonThroughput /= lightPath[index].originProb;
// runs here, photon's f/p is done.
Ray photonRay = lightPath[index];
photonRay.direction = lightPath[index-1].direction;
vec3f color = photonThroughput * photonRay.getBSDF(outRay);
float distSqr = powf((outRay.origin-lightPath[index].origin).length(), 2);
if(intensity(color) < 1e-6f) return ;
float kernel = Kernel(distSqr, radius*radius);
float normalization = volumeMedia ? kernel/(photonsNum*radius*radius*radius) : kernel/(photonsNum*radius*radius);
//float normalization = volumeMedia==false ? 1.0 / (photonsNum*PI*radius*radius) : 1.0 / (photonsNum*PI*4.0/3*radius*radius*radius);
contrib += color * normalization;
}
double sumWeight;
int photonsCount;
void weightScale(){
contrib /= ( sumWeight / photonsCount );
}
};
Query query(this, mRadius, mPhotonsNum);
vec3f Tr(1,1,1), SurfaceColor(0,0,0), VolumeColor(0,0,0);
int mergeIndex = 1;
for(int i = 1; i < eyeMergePath.size(); i++){
float dist = MAX((eyeMergePath[i-1].origin-eyeMergePath[i].origin).length(), EPSILON);
if(eyeMergePath[i-1].insideObject && eyeMergePath[i-1].insideObject->isVolumetric()){
if(eyeMergePath[i-1].insideObject->isHomogeneous())
{
// ray marching volume radiance
Ray volThroughRay = eyeMergePath[i-1];
SceneVPMObject *volume = static_cast<SceneVPMObject*>(volThroughRay.insideObject);
float stepSize = volume->stepSize;
int N = dist / stepSize;
if(N == 0) N++;
float step = dist / N;
float offset = step * RandGenerator::genFloat();
float t = offset;
Tr *= volume->getRadianceDecay(volThroughRay, offset);
for(int j = 0; j < N; j++, t+=step){
query.SetContrib(vec3f(0,0,0));
query.SetPosition(volThroughRay.origin + volThroughRay.direction*t);
Ray outRay = volThroughRay;
outRay.direction = -volThroughRay.direction;
outRay.origin = volThroughRay.origin + volThroughRay.direction*t;
outRay.contactObject = NULL;
query.SetOutRay(outRay);
query.volumeMedia = true;
volumeHashGrid.Process(volumeVertices, query);
Tr *= volume->getRadianceDecay(outRay, step);
vec3f volColor = query.GetContrib();
VolumeColor += volColor * Tr * step;
}
}
else{
// ray marching volume radiance
Ray volThroughRay = eyeMergePath[i-1];
HeterogeneousVolume *volume = static_cast<HeterogeneousVolume*>(volThroughRay.insideObject);
float stepSize = volume->getStepSize();
int N = dist / stepSize;
if(N == 0) N++;
float step = dist / N;
float offset = step * RandGenerator::genFloat();
float t = offset;
Tr *= volume->getRadianceDecay(volThroughRay, offset);
for(int j = 0; j < N; j++, t+=step){
query.SetContrib(vec3f(0,0,0));
query.SetPosition(volThroughRay.origin + volThroughRay.direction*t);
Ray outRay = volThroughRay;
outRay.direction = -volThroughRay.direction;
outRay.origin = volThroughRay.origin + volThroughRay.direction*t;
outRay.contactObject = NULL;
query.SetOutRay(outRay);
query.volumeMedia = true;
volumeHashGrid.Process(volumeVertices, query);
Tr *= volume->getRadianceDecay(outRay, step);
vec3f volColor = query.GetContrib();
VolumeColor += volColor * Tr * step;
}
}
}
else
{
if (eyeMergePath[i - 1].insideObject)
Tr *= eyeMergePath[i - 1].getRadianceDecay(dist);
}
if(eyeMergePath[i].contactObject && eyeMergePath[i].contactObject->emissive()){
// eye path hit light, surface color equals to light radiance
SurfaceColor = eyeMergePath[i].color;
mergeIndex = i;
break;
}
if(eyeMergePath[i].contactObject && eyeMergePath[i].directionSampleType == Ray::RANDOM){
// non-specular photon density estimation
if(eyeMergePath[i].contactObject->isVolumetric())
continue;
query.SetContrib(vec3f(0,0,0));
query.SetPosition(eyeMergePath[i].origin);
Ray outRay = eyeMergePath[i];
outRay.direction = -eyeMergePath[i-1].direction;
query.SetOutRay(outRay);
query.volumeMedia = false;
Ray fromRay = eyeMergePath[i-1];
omp_set_lock(&surfaceHashGridLock);
surfaceHashGrid.Process(surfaceVertices, query);
omp_unset_lock(&surfaceHashGridLock);
SurfaceColor = query.GetContrib();
mergeIndex = i;
break;
}
}
color = Tr * SurfaceColor + VolumeColor;
if (rayMarching)
{
for(int i = 0; i < 1/*eyeMergePath.size()-1*/; i++){
color *= eyeMergePath[i].getCosineTerm() * eyeMergePath[i].color
/ eyeMergePath[i].directionProb / eyeMergePath[i].originProb;
}
}
else
{
for(int i = 0; i < mergeIndex; i++){
color *= eyeMergePath[i].getCosineTerm() * eyeMergePath[i].color
/ eyeMergePath[i].directionProb / eyeMergePath[i].originProb;
if (i + 1 < mergeIndex)
{
float dist = (eyeMergePath[i].origin - eyeMergePath[i+1].origin).length();
color *= eyeMergePath[i].getRadianceDecay(dist);
}
}
}
}