-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneVPMObject.cpp
272 lines (239 loc) · 9.13 KB
/
SceneVPMObject.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
#include "StdAfx.h"
#include "SceneVPMObject.h"
Ray SceneVPMObject::scatter(const Ray& inRay, const bool russian) const
{
Ray outRay;
outRay.directionSampleType = Ray::RANDOM;
HomoMediaDistSampler logDistSampler(dt);
float sampDist = logDistSampler.sampleDist();
bool go_in_vol = (inRay.intersectObject == this) && (inRay.insideObject != this);
bool be_in_vol = (inRay.insideObject == this);
bool out_of_vol = (sampDist >= inRay.intersectDist); // hit a surface
// go in volume
if(go_in_vol){
vec3f position = inRay.origin + inRay.direction*inRay.intersectDist;
LocalFrame lf = inRay.intersectObject->getAutoGenWorldLocalFrame(inRay.intersectObjectTriangleID, position);
vec3f normal = lf.n;
outRay.origin = position;
outRay.direction = inRay.direction;
vec3f reflDir = -normal.dot(inRay.direction)*normal*2 + inRay.direction;
reflDir.normalize();
float theta = acos(inRay.direction.dot(normal));
SceneObject* currentInsideObject = inRay.insideObject;
SceneObject* outSideObject = (SceneObject*)this;
float current_n = currentInsideObject ? currentInsideObject->getRefrCoeff() : 1;
float next_n = outSideObject ? outSideObject->getRefrCoeff() : 1;
float sin_phi = current_n / next_n * sin(theta);
outRay.intersectObject = NULL;
outRay.color = vec3f(1, 1, 1);
outRay.directionProb = 1;
outRay.contactObject = (SceneObject*)this;
outRay.contactObjectTriangleID = inRay.intersectObjectTriangleID;
if(sin_phi >= 1){
outRay.direction = reflDir;
outRay.insideObject = inRay.insideObject;
outRay.directionProb = 1;
outRay.directionSampleType = Ray::DEFINITE;
outRay.photonType = Ray::NOUSE;
outRay.color /= outRay.getCosineTerm();
//if (abs(outRay.getCosineTerm() - 1.f) > 1e-6f)
// printf("exception1: %.6f\n" , outRay.getCosineTerm());
}
else{
float phi = asin(sin_phi);
if(theta > M_PI/2)
phi = M_PI - phi;
vec3f axis = normal.cross(inRay.direction);
axis.normalize();
outRay.direction = vec3f(rotMat(axis, phi) * vec4<float>(normal, 0));
outRay.direction.normalize();
float cos_theta = abs(cos(theta));
float cos_phi = abs(cos(phi));
float esr = powf(abs(current_n*cos_theta-next_n*cos_phi)/(current_n*cos_theta+next_n*cos_phi),2);
float epr = powf(abs(next_n*cos_theta-current_n*cos_phi)/(next_n*cos_theta+current_n*cos_phi),2);
float er = (esr+epr)*0.5f;
float p = clampf(er , 0.f , 1.f);
if(RandGenerator::genFloat() < p)
{
outRay.direction = reflDir;
outRay.color *= er / outRay.getCosineTerm();
outRay.directionProb = p;
outRay.insideObject = inRay.insideObject;
outRay.directionSampleType = Ray::DEFINITE;
outRay.photonType = Ray::NOUSE;
}
else
{
outRay.color *= (1-er) / outRay.getCosineTerm();
outRay.directionProb = 1-p;
outRay.contactObject = outRay.insideObject = (SceneObject*)this;
outRay.directionSampleType = Ray::DEFINITE;
outRay.photonType = Ray::HITVOL;
}
outRay.direction.normalize();
}
return outRay;
}
// in volume
if(be_in_vol && !out_of_vol){
HGPhaseSampler hgPhaseSampler(g);
//IsotropicPhaseSampler sp;
LocalFrame lf; lf.buildFromNormal(inRay.direction);
outRay.origin = inRay.origin + inRay.direction * sampDist;
outRay.direction = hgPhaseSampler.genSample(lf);
outRay.color = bsdf->evaluate(lf, inRay.direction, outRay.direction);
outRay.insideObject = (SceneObject*)this;
//outRay.contactObject = NULL;
outRay.contactObjectTriangleID = inRay.intersectObjectTriangleID;
//outRay.contactObjectTriangleID = -1;
//outRay.directionProb = 1;
//outRay.color = vec3f(1, 1, 1);
float albedo = y(ds) / y(dt);
float rander = RandGenerator::genFloat();
//outRay.originSampleType = Ray::RANDOM;
if(rander < albedo || (!russian)){
outRay.contactObject = NULL;
float oPdfW = hgPhaseSampler.getProbDensity(lf, outRay.direction);//sp.getProbDensity(lf, outRay.direction);//
if (russian)
outRay.directionProb = albedo * oPdfW;
else
outRay.directionProb = oPdfW;
outRay.originProb = p_medium(sampDist);
outRay.directionSampleType = Ray::RANDOM;
outRay.color *= ds;
outRay.photonType = Ray::INVOL;
}
else{
// terminate
outRay.direction = vec3f(0, 0, 0);
outRay.color = vec3f(0, 0, 0);
outRay.directionProb = 1;
outRay.originProb = p_medium(sampDist);
outRay.insideObject = (SceneObject*)this; // FIXED
//outRay.insideObject = NULL;
outRay.contactObject = NULL;
outRay.directionSampleType = Ray::RANDOM;
outRay.photonType = Ray::INVOL;
//outRay.photonType = Ray::NOUSE;
}
return outRay;
}
// go out of volume
if(be_in_vol && out_of_vol){
outRay = inRay;
outRay.direction = inRay.direction;
outRay.origin = inRay.origin + inRay.intersectDist * inRay.direction;
outRay.contactObject = inRay.intersectObject;
outRay.contactObjectTriangleID = inRay.intersectObjectTriangleID;
outRay.insideObject = (SceneObject*)this;
outRay.directionProb = 1;
outRay.color = vec3f(1,1,1);
bool going_out = (inRay.intersectObject == this);
if(going_out){
LocalFrame lf = inRay.intersectObject->getAutoGenWorldLocalFrame(inRay.intersectObjectTriangleID, outRay.origin);
vec3f normal = lf.n;
vec3f reflDir = -normal.dot(inRay.direction)*normal*2 + inRay.direction;
reflDir.normalize();
float theta = acos(inRay.direction.dot(normal));
SceneObject* currentInsideObject = (SceneObject*)this;
SceneObject* outSideObject = scene->findInsideObject(outRay, (SceneObject*)this);
float current_n = currentInsideObject ? currentInsideObject->getRefrCoeff() : 1;
float next_n = outSideObject ? outSideObject->getRefrCoeff() : 1;
float sin_phi = current_n / next_n * sin(theta);
outRay.intersectObject = NULL;
if(sin_phi >= 1){
outRay.direction = reflDir;
outRay.insideObject = inRay.insideObject;
outRay.contactObject = (SceneObject*)this;
outRay.originProb = P_surface(inRay.intersectDist);
outRay.photonType = Ray::NOUSE;
outRay.directionSampleType = Ray::DEFINITE;
outRay.color /= outRay.getCosineTerm();
//if (abs(outRay.getCosineTerm() - 1.f) > 1e-6f)
// printf("exception2: %.6f\n" , outRay.getCosineTerm());
}
else{
float phi = asin(sin_phi);
if(theta > M_PI/2)
phi = M_PI - phi;
vec3f axis = normal.cross(inRay.direction);
axis.normalize();
outRay.direction = vec3f(rotMat(axis, phi) * vec4<float>(normal, 0));
outRay.direction.normalize();
float cos_theta = abs(cos(theta));
float cos_phi = abs(cos(phi));
float esr = powf(abs(current_n*cos_theta-next_n*cos_phi)/(current_n*cos_theta+next_n*cos_phi),2);
float epr = powf(abs(next_n*cos_theta-current_n*cos_phi)/(next_n*cos_theta+current_n*cos_phi),2);
float er = (esr+epr)/2.f;
float p = clampf(er , 0.f , 1.f);
if(RandGenerator::genFloat() < p)
{
outRay.direction = reflDir;
outRay.color *= er / outRay.getCosineTerm();
outRay.directionProb = p;
outRay.originProb = P_surface(inRay.intersectDist);
outRay.insideObject = inRay.insideObject;
outRay.directionSampleType = Ray::DEFINITE;
outRay.photonType = Ray::NOUSE;
}
else
{
outRay.color *= (1-er) / outRay.getCosineTerm();
outRay.directionProb = (1-p);
outRay.originProb = P_surface(inRay.intersectDist);
outRay.insideObject = outSideObject;
outRay.directionSampleType = Ray::DEFINITE;
outRay.photonType = Ray::NOUSE;
}
outRay.direction.normalize();
}
}
else{
//std::cout << "in vol hit surface " << std::endl;
outRay.contactObject = NULL;
outRay.intersectDist = 0;
//------ FIX ME -------
if (inRay.intersectObject != NULL)
outRay = inRay.intersectObject->scatter(outRay , russian);
else
printf("error VPM object scattering\n");
//---------------------
outRay.originProb *= P_surface(inRay.intersectDist);
outRay.photonType = inRay.intersectObject->isVolumetric() ? Ray::NOUSE : Ray::OUTVOL;
}
return outRay;
}
}
vec3f SceneVPMObject::getRadianceDecay(const Ray &inRay, const float &dist) const {
return transmittance(dist);
}
vec3f SceneVPMObject::getBSDF(const Ray& inRay, const Ray& outRay) const
{
if(outRay.contactObject == NULL)
return ds * bsdf->evaluate(LocalFrame(), inRay.direction, outRay.direction);
if(outRay.contactObject && outRay.contactObject != this)
return outRay.contactObject->getBSDF(inRay, outRay);
return vec3f(0, 0, 0);
}
float SceneVPMObject::getOriginSampleProbDensity(const Ray &inRay, const Ray &outRay) const{
float dist = max((inRay.origin - outRay.origin).length(), EPSILON);
if(outRay.contactObject)
return P_surface(dist);
else
return p_medium(dist);
}
float SceneVPMObject::getDirectionSampleProbDensity(const Ray &inRay, const Ray &outRay) const{
if(outRay.directionSampleType == Ray::DEFINITE)
return 0;
if(outRay.contactObject)
return outRay.contactObject->getDirectionSampleProbDensity(inRay, outRay);
HGPhaseSampler hgPhaseSampler(g);
LocalFrame lf; lf.buildFromNormal(inRay.direction);
float albedo = y(ds) / y(dt);
float oPdfW = hgPhaseSampler.getProbDensity(lf, outRay.direction);
return albedo*oPdfW;
}
float SceneVPMObject::getContinueProbability(const Ray &inRay, const Ray &outRay) const {
float albedo = y(ds) / y(dt);
return albedo;
}