/
SceneObject.cpp
373 lines (332 loc) · 9.69 KB
/
SceneObject.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
#include "StdAfx.h"
#include "SceneObject.h"
#include "UniformSphericalSampler.h"
#include "CountHashGrid.h"
SceneObject* SceneObject::findInsideObject(const Ray& ray) const
{
return scene->findInsideObject(ray, this);
}
void SceneObject::preprocessEmissionSampler()
{
totalArea = weight = 0.f;
for(unsigned k=0; k<getTriangleNum(); k++)
{
totalArea += getTriangleArea(k);
areaValues.push_back(totalArea);
}
weight = totalArea;
}
void SceneObject::preprocessOtherSampler()
{
totalEnergy = 0.f;
energyDensity.clear();
energyDensity.resize(getTriangleNum());
for (int i = 0; i < getTriangleNum(); i++)
{
energyDensity[i] = 0.f;
areaValues.push_back(getTriangleArea(i));
}
}
void SceneObject::preprocessVolumeSampler(bool isUniformOrigin , float mergeRadius)
{
totalVolume = volumeWeight = 0.f;
if (!isVolumetric())
return;
countHashGrid = new CountHashGrid();
countHashGrid->init(scene , objectIndex , mergeRadius);
if (isUniformOrigin)
{
countHashGrid->preprocess(scene , objectIndex);
volumeWeight = countHashGrid->totVolume;
}
else
{
countHashGrid->preprocessNonUniform(scene , objectIndex);
volumeWeight = countHashGrid->sumWeights;
}
totalVolume = countHashGrid->totVolume;
}
void SceneObject::normalizeVolumeWeight(float totalWeight)
{
volumeWeight /= totalWeight;
if (countHashGrid)
countHashGrid->weight = volumeWeight;
}
void SceneObject::scaleEnergyDensity(const float scale)
{
if (this->isVolumetric() && countHashGrid)
{
countHashGrid->scaleEnergyDensity(scale);
return;
}
totalEnergy *= scale;
for (int i = 0; i < getTriangleNum(); i++)
energyDensity[i] *= scale;
}
void SceneObject::addEnergyDensity(const int triId , const vec3f& thr)
{
float energyDens = intensity(thr) / areaValues[triId];
float value = energyDens * energyDens;
totalEnergy += value;
energyDensity[triId] += value;
}
void SceneObject::addEnergyDensity(const vec3f& pos , const vec3f& thr)
{
if (this->isVolumetric() && countHashGrid)
countHashGrid->addEnergyDensity(pos , thr);
}
void SceneObject::singleEnergyToSumEnergy()
{
if (this->isVolumetric() && countHashGrid)
{
countHashGrid->singleEnergyToSumEnergy();
return;
}
for (int i = 1; i < getTriangleNum(); i++)
energyDensity[i] += energyDensity[i - 1];
totalEnergy = energyDensity[getTriangleNum() - 1];
}
void SceneObject::sumEnergyToSingleEnergy()
{
if (this->isVolumetric() && countHashGrid)
{
countHashGrid->sumEnergyToSingleEnergy();
return;
}
totalEnergy = energyDensity[getTriangleNum() - 1];
for (int i = getTriangleNum() - 1; i >= 1; i--)
energyDensity[i] -= energyDensity[i - 1];
}
float SceneObject::getOriginProb(const int triId)
{
float res;
if (triId == 0)
res = energyDensity[triId];
else
res = energyDensity[triId] - energyDensity[triId - 1];
res *= weight / (totalEnergy * areaValues[triId]);
return res;
}
float SceneObject::getOriginProb(const vec3f& pos)
{
if (countHashGrid)
return countHashGrid->getOriginProb(pos);
return 0.f;
}
float SceneObject::getTotalEnergy() const
{
if (countHashGrid)
return countHashGrid->sumWeights;
else
return totalEnergy;
}
Ray SceneObject::emit(bool isUniformOrigin , bool isUniformDir) const
{
Ray ray;
if(!areaValues.size())
{
ray.direction = vec3f(0, 0, 0);
ray.directionProb = 1;
ray.color = vec3f(0, 0, 0);
return ray;
}
if (isUniformOrigin)
{
float rnd = RandGenerator::genFloat()*totalArea;
unsigned index = (lower_bound(areaValues.begin(), areaValues.end(), rnd)-areaValues.begin());
if(index >= areaValues.size())
index = areaValues.size() - 1;
ray.contactObject = (SceneObject*)this;
ray.contactObjectTriangleID = index;
ray.origin = genRandTrianglePosition(ray.contactObjectTriangleID);
ray.originProb = weight / totalArea;
}
else
{
float rnd = RandGenerator::genFloat()*totalEnergy;
unsigned index = (lower_bound(energyDensity.begin(), energyDensity.end(), rnd)-energyDensity.begin());
if(index >= energyDensity.size())
index = energyDensity.size() - 1;
ray.contactObject = (SceneObject*)this;
ray.contactObjectTriangleID = index;
ray.origin = genRandTrianglePosition(ray.contactObjectTriangleID);
float prob;
if (index == 0)
prob = energyDensity[index] / totalEnergy;
else
prob = (energyDensity[index] - energyDensity[index - 1]) / totalEnergy;
ray.originProb = weight * prob / areaValues[index];
}
UniformSphericalSampler uniformSphericalSampler;
CosineSphericalSampler cosineSphericalSampler;
LocalFrame lf = ray.contactObject->getAutoGenWorldLocalFrame(ray.contactObjectTriangleID, ray.origin);
if (isUniformDir)
ray.direction = uniformSphericalSampler.genSample(lf);
else
ray.direction = cosineSphericalSampler.genSample(lf);
if(ray.getContactNormal().dot(ray.direction) < 0)
ray.direction = -ray.direction;
ray.insideObject = scene->findInsideObject(ray, ray.contactObject);
ray.current_tid = scene->getContactTreeTid(ray);
ray.color = ray.getBSDF(ray);
if(!emissive())
ray.color = vec3f(1, 1, 1);
if (isUniformDir)
ray.directionProb = uniformSphericalSampler.getProbDensity(lf , ray.direction) * 2.f;
else
ray.directionProb = cosineSphericalSampler.getProbDensity(lf, ray.direction);
ray.directionSampleType = ray.originSampleType = Ray::RANDOM;
if(!scene->usingGPU())
{
Scene::ObjSourceInformation osi;
NoSelfIntersectionCondition condition(scene, ray);
float dist = scene->intersect(ray, osi, &condition);
if(dist > 0)
{
ray.intersectDist = dist;
ray.intersectObject = scene->objects[osi.objID];
ray.intersectObjectTriangleID = osi.triangleID;
}
}
return ray;
}
Ray SceneObject::emitVolume(bool isUniformDir /* = false */) const
{
Ray ray = countHashGrid->emitVolume(scene);
//printf("%.8f , %.8f\n" , volumeWeight , ray.originProb);
return ray;
}
float SceneObject::getDirectionSampleProbDensity(const Ray& inRay, const Ray& outRay) const
{
if(&inRay == &outRay) // emitted
{
//UniformSphericalSampler uniformSphericalSampler;
CosineSphericalSampler cosineSphericalSampler;
LocalFrame lf = outRay.contactObject->getAutoGenWorldLocalFrame(outRay.contactObjectTriangleID, outRay.origin);
//return uniformSphericalSampler.getProbDensity(lf, outRay.direction) * 2;
return cosineSphericalSampler.getProbDensity(lf , outRay.direction);
}
if(materialList.size())
{
float prob = 0;
for(unsigned i=0; i<materialList.size(); i++)
prob += materialList[i]->getDirectionSampleProbDensity(inRay, outRay);
return prob / materialList.size();
}
return 0;
}
vec3f SceneObject::getWorldNormal(unsigned fi, const vec3f& position, bool flat) const
{
vec3f original_normal = SimpleShape::getWorldNormal(fi, position, flat);
if(bumpTex.size() <= 1 || fi >= faceVertexTexCoordIndexList.size())
return original_normal;
printf("use not original normal\n");
vec3f vps[3], vts[3], vns[3];
for(unsigned i=0; i<3; i++)
{
vps[i] = getWorldVertexPosition(faceVertexIndexList[fi][i]);
if(faceVertexTexCoordIndexList[fi][i] >= vertexTexCoordList.size())
return original_normal;
vts[i] = vertexTexCoordList[faceVertexTexCoordIndexList[fi][i]];
}
vec3f uv_grad = bumpTex.getGrad(getTexCoord(fi, position));
vec3f b1 = vps[1] - vps[0];
vec3f b2 = vps[2] - vps[0];
vec3f duv1 = vts[1] - vts[0];
vec3f duv2 = vts[2] - vts[0];
float k2 = (uv_grad.x*duv1.y - uv_grad.y*duv1.x) / (duv1.y*duv2.x - duv1.x*duv2.y);
float k1 = (uv_grad.y - k2*duv2.y) / duv1.y;
b1.normalize();
b2.normalize();
vec3f dl = k1*b1+k2*b2;
vec3f dh = original_normal*uv_grad.z;
if(dh.length()*1000 < dl.length())
return original_normal;
float angle = atan2(dh.length(), dl.length());
vec3f axis = dl.cross(dh);
axis.normalize();
return vec3f(rotMat(axis, angle) * vec4f(original_normal, 0));
}
LocalFrame SceneObject::getAutoGenWorldLocalFrame(unsigned fi, const vec3f& position, bool flat) const
{
LocalFrame lf;
lf.buildFromNormal(getWorldNormal(fi, position, flat));
/*
lf.n = getWorldNormal(fi, position, flat);
vec3f tmpT;
tmpT = (std::abs(lf.n.z) > 0.99f) ? vec3f(1,0,0) : vec3f(0,0,1);
lf.s = lf.n.cross(tmpT);
lf.s.normalize();
lf.t = lf.s.cross(lf.n);
lf.t.normalize();
*/
/*
vec3f axis = up.cross(lf.n);
float angle = acos(clampf(up.dot(lf.n), -1, 1));
if (!(axis.length() >= 1e-6f || (axis.length() < 1e-6f && n.dot(lf.n) == 1.f)))
{
printf("error , %.8f\n" , n.dot(lf.n));
}
axis.normalize();
//if (axis.length() < 1e-6f)
//{
// lf.s = vec3f(1,0,0);
// lf.t = vec3f(0,0,1);
//}
//else
{
lf.s = vec3f(rotMat(axis, angle)*vec4<float>(vec3f(1,0,0), 0));
lf.t = vec3f(rotMat(axis, angle)*vec4<float>(vec3f(0,0,1), 0));
}
*/
return lf;
}
float SceneObject::getOriginSampleProbDensity(const Ray& inRay, const Ray& outRay) const
{
if(&inRay == &outRay) // emitted
{
return weight / totalArea;
}
if(materialList.size())
{
float prob = 0;
for(unsigned i=0; i<materialList.size(); i++)
prob += materialList[i]->getOriginSampleProbDensity(inRay, outRay);
return prob / materialList.size();
}
return 1;
}
float SceneObject::getContinueProbability(const Ray &inRay, const Ray &outRay) const
{
return 1;
}
vec3f SceneObject::getBSDF(const Ray& inRay, const Ray& outRay) const
{
if(materialList.size())
{
vec3f bsdfColor(0, 0, 0);
for(unsigned i=0; i<materialList.size(); i++)
bsdfColor += materialList[i]->getBSDF(inRay, outRay);
return bsdfColor;
}
return vec3f(1, 1, 1);
}
vec3f SceneObject::getRadianceDecay(const Ray& inRay, const float& dist) const
{
if(materialList.size())
{
vec3f decayColor(1, 1, 1);
for(unsigned i=0; i<materialList.size(); i++)
decayColor *= materialList[i]->getRadianceDecay(inRay, dist);
return decayColor;
}
return vec3f(1, 1, 1);
}
Ray SceneObject::scatter(const Ray& inRay, const bool russian) const
{
if(materialList.size() && this)
{
return materialList[rand()%materialList.size()]->scatter(this, inRay, russian);
}
return Ray();
}