-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIptTracer.h
298 lines (241 loc) · 7.83 KB
/
IptTracer.h
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
#pragma once
#include "mcrenderer.h"
#include <omp.h>
#include <stack>
#include <utility>
#include <queue>
#include "PointKDTree.h"
#include "CountHashGrid.h"
#include "macros.h"
#include "SceneVPMObject.h"
static FILE *fp2 = fopen("debug_ipt_gather_y.txt" , "w");
struct IptPathState
{
vec3f throughput , indirContrib;
//vec3f contribs[4];
Ray *ray , *lastRay , *originRay;
int pathLen;
vec3f pos;
int index;
double mergedPath;
//double accuProb;
};
class IptTracer : public MCRenderer
{
protected:
unsigned spp;
int mergeIterations;
int checkCycleIters;
vector<vector<int> > partPathMergeIndex;
vector<float> weights;
int *revIndex;
vector<IptPathState> partialSubPathList;
vector<bool> vis;
vector<bool> inStack;
vector<bool> cannotBeCycle;
queue<int> q;
stack<int> cycle;
vector<pair<int , int> > edgeToRemove;
vector<bool> volMask;
bool dfs(int depth , int cur);
void movePaths(omp_lock_t& cmdLock , vector<Path>& , vector<Path*>&);
void genLightPaths(omp_lock_t& cmdLock , vector<Path*>& , bool);
void genIntermediatePaths(omp_lock_t& cmdLock , vector<Path*>&);
void mergePartialPaths(omp_lock_t& cmdLock);
Ray genIntermediateSamples(Scene& scene);
void mergePartialPaths(vector<vec3f>& contribs , vector<double>& mergedPath , const IptPathState& interState);
vec3f colorByMergingPaths(IptPathState& cameraState, PointKDTree<IptPathState>& partialSubPaths);
vec3f colorByRayMarching(Path& eyeMergePath , PointKDTree<IptPathState>& partialSubPaths , int pixelID);
vec3f colorByConnectingLights(Ray lastRay , Ray ray , bool dirIlluWeight = true);
void sampleMergePath(Path &path, Ray &prevRay, uint depth);
public:
Real mergeRadius;
Real gatherRadius;
Real pathRatio;
Real lightMergeKernel , interMergeKernel;
Real lightGatherKernel , interGatherKernel;
Real alpha;
Real totArea , totVol;
Real mergeRatio;
unsigned timeInterval , lastTime;
bool useWeight , usePPM , useDirIllu , useRayMarching , checkCycle;
bool useUniformInterSampler , useUniformSur , useUniformVol , useUniformDir;
bool useConstantKernel;
bool isDebug;
int pixelNum , lightPathNum , cameraPathNum , interPathNum , partialPathNum;
int totPathNum;
int lightPhotonNum , partialPhotonNum;
IptTracer(Renderer* renderer) : MCRenderer(renderer)
{
alpha = 2.f / 3.f;
spp = -1;
mergeRatio = 1.f;
timeInterval = lastTime = 3600;
gatherRadius = 0.f;
pathRatio = 0.5f;
pixelNum = renderer->camera.height * renderer->camera.width;
totPathNum = pixelNum;
usePPM = false;
useDirIllu = false;
useRayMarching = true;
useUniformSur = true;
useUniformVol = true;
useUniformDir = false;
useConstantKernel = false;
checkCycle = true;
checkCycleIters = 100;
isDebug = true;
}
void setRadius(const Real& r) { mergeRadius = r; }
virtual vector<vec3f> renderPixels(const Camera& camera);
Real connectFactor(Real pdf)
{
return pdf;
}
Real mergeFactor(Real *volScale , Real *initProb , Real *dirProb , int *pathNum)
{
Real s = 1.0;
if (volScale)
s = *volScale;
if (initProb)
s *= *initProb;
if (dirProb)
s *= *dirProb;
if (pathNum)
s *= (float)*pathNum;
Real res = M_PI * mergeRadius * mergeRadius * s;
return res * mergeRatio;
}
Real kernel(Real distSqr , Real radiusSqr)
{
Real s = max(0.f , 1.f - distSqr / radiusSqr);
return 3.f * s * s / M_PI;
}
};
struct GatherQuery
{
vec3f color;
IptTracer *tracer;
IptPathState* cameraState;
bool constKernel;
double mergeNum;
GatherQuery(IptTracer* tracer) { this->tracer = tracer; mergeNum = 0; constKernel = true; }
void process(IptPathState& lightState)
{
Real volMergeScale = 1;
if (cameraState->ray->insideObject && !cameraState->ray->contactObject)
volMergeScale = 4.f / 3.f * tracer->gatherRadius;
Real originProb = 1.f / tracer->totArea;
Real dirProb;
if (cameraState->ray->insideObject && cameraState->ray->contactObject == NULL)
{
if (lightState.ray->insideObject == NULL ||
lightState.ray->contactObject != NULL ||
cameraState->ray->insideObject != lightState.ray->insideObject ||
!cameraState->ray->insideObject->canMerge ||
!lightState.ray->insideObject->canMerge)
{
return;
}
volMergeScale = 4.f / 3.f * tracer->gatherRadius;
originProb = 1.f / tracer->totVol;
}
else if (cameraState->ray->contactObject)
{
if (lightState.ray->contactObject != cameraState->ray->contactObject ||
!lightState.ray->contactObject->canMerge ||
!cameraState->ray->contactObject->canMerge)
{
return;
}
}
else
{
return;
}
Ray outRay;
vec3f bsdfFactor;
outRay = *lightState.ray;
outRay.direction = -(cameraState->lastRay->direction);
bsdfFactor = lightState.lastRay->getBSDF(outRay);
if (y(bsdfFactor) < 1e-7f)
return;
vec3f totContrib(0.f);
if (lightState.index < tracer->lightPhotonNum)
totContrib = lightState.throughput;
else
totContrib = lightState.indirContrib;
//totContrib = lightState.indirContrib;
vec3f tmp = totContrib * bsdfFactor * cameraState->throughput;
/*
if (cameraState->ray->contactObject && cameraState->ray->contactObject->hasCosineTerm())
dirProb = cameraState->ray->getContactNormal().dot(-cameraState->lastRay->direction) / M_PI;
else
dirProb = 0.25f / M_PI;
Real lastPdf;
lastPdf = lightState.lastRay->getDirectionSampleProbDensity(outRay);
Real weightFactor = (tracer->mergeFactor(&volMergeScale , &originProb , &dirProb)) /
(tracer->connectFactor(lastPdf) + tracer->mergeFactor(&volMergeScale , &originProb , &dirProb));
*/
//fprintf(fp , "weight = %.8f , cameraAccuProb = %.8f , hashOriginProb = %.8f\n" , weightFactor , cameraState->accuProb , originProb);
//mergeNum++;
vec3f res;
float mergeKernel , pathNum;
if (lightState.index < tracer->lightPhotonNum)
{
mergeKernel = tracer->lightGatherKernel / volMergeScale;
pathNum = tracer->lightPathNum;
}
else
{
mergeKernel = tracer->interGatherKernel / volMergeScale;
pathNum = tracer->partialPathNum;
}
if (constKernel)
{
res = tmp * mergeKernel;
}
else
{
float distSqr = (cameraState->pos - lightState.pos).length();
distSqr = distSqr * distSqr;
mergeKernel = tracer->kernel(distSqr , tracer->gatherRadius * tracer->gatherRadius);
if (abs(volMergeScale - 1.f) > 1e-6f)
mergeKernel /= pathNum * tracer->gatherRadius * tracer->gatherRadius * tracer->gatherRadius;
else
mergeKernel /= pathNum * tracer->gatherRadius * tracer->gatherRadius;
res = tmp * mergeKernel;
}
//res *= weightFactor;
if (!tracer->usePPM && tracer->useDirIllu && lightState.index < tracer->lightPhotonNum &&
lightState.pathLen == 1 && lightState.ray->contactObject && !lightState.ray->contactObject->isVolumetric())
{
float dist = (lightState.lastRay->origin - lightState.ray->origin).length();
float cosToLight = clampf(lightState.ray->getContactNormal().dot(-lightState.lastRay->direction) , 0.f , 1.f);
float p1 = lightState.lastRay->originProb;
float p2 = lightState.lastRay->originProb * lightState.lastRay->directionProb * cosToLight / (dist * dist) *
M_PI * tracer->gatherRadius * tracer->gatherRadius * tracer->partialPathNum;
float weightFactor = p2 / (p1 + p2);
//printf("merge weight = %.8f\n" , weightFactor);
res *= weightFactor;
}
// if (lightState.index < tracer->lightPhotonNum)
// fprintf(fp2 , "dir , contrib = (%.4f,%.4f,%.4f)\n" , res.x , res.y , res.z);
// else
// fprintf(fp2 , "indir , contrib = (%.4f,%.4f,%.4f)\n" , res.x , res.y , res.z);
color += res;
/*
if (cameraState->ray->insideObject)
{
fprintf(fp , "=====================\n");
if (volMergeScale == 1)
fprintf(fp , "surface\n");
else
fprintf(fp , "volume\n");
fprintf(fp , "res = (%.8f,%.8f,%.8f) \ntotContrib = (%.8f,%.8f,%.8f), bsdf = (%.8f,%.8f,%.8f),\n" ,
res[0] , res[1] , res[2] ,
totContrib[0] , totContrib[1] , totContrib[2] , bsdfFactor[0] , bsdfFactor[1] , bsdfFactor[2]);
}
*/
}
};