This repository was archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglossyprt2002.h
More file actions
266 lines (229 loc) · 9.43 KB
/
glossyprt2002.h
File metadata and controls
266 lines (229 loc) · 9.43 KB
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
#pragma once
#include "common/wurst.h"
#include "common/renderer.h"
#include "common/scene.h"
#include "common/trimeshvertexiterator.h"
#include "common/util/vector.h"
#include "common/util/json.h"
#include "common/viewer.h"
#include "pathsampler/generalsubpathsampler.h"
#include "sphericalrepresentation/sphericalharmonics.h"
#include "sphericalrepresentation/equirectangular.h"
#include "sphericalrepresentation/convertutil.h"
struct GlossyPrt2002 : public SingleCameraRenderer
{
static shared_ptr<GlossyPrt2002> FromSimple2Json(shared_ptr<const Camera> & camera,
shared_ptr<const Scene> & scene,
shared_ptr<Sampler> & sampler,
const json & json,
const filesystem::path & rootJsonPath)
{
const int numAaSamples = Util::Json::GetValue<int>(json, "num_aa_samples", 16);
const int numSamplesPerPoint = Util::Json::GetValue<int>(json, "num_samples_per_point", 10000);
const int numEnvmapCoeffs = Util::Json::GetValue<int>(json, "num_envmap_bands", 3);
const int numLocalCoeffs = Util::Json::GetValue<int>(json, "num_local_bands", 3);
const int numPathVertices = Util::Json::GetValue<int>(json, "num_path_vertices", 3);
return make_shared<GlossyPrt2002>(camera, scene, sampler, numSamplesPerPoint, numEnvmapCoeffs, numLocalCoeffs, numAaSamples, numPathVertices);
}
GlossyPrt2002(shared_ptr<const Camera> & camera,
shared_ptr<const Scene> & scene,
shared_ptr<Sampler> & sampler,
const int numSamplesPerPoint,
const int numEnvmapShBand,
const int numLocalShBand,
const int numAaSamples,
const int numPathVertices) :
SingleCameraRenderer(camera, scene, sampler),
mVerticesVisitor(scene),
mNumEnvmapCoeffs(Math::Square(numEnvmapShBand)),
mNumLocalCoeffs(Math::Square(numLocalShBand)),
mNumMatrixElements(Math::Square(numEnvmapShBand * numLocalShBand)),
mNumSamplesPerPoint(numSamplesPerPoint),
mNumAaSamples(numAaSamples),
mNumPathVertices(numPathVertices)
{
}
int64_t globalIndex(const int iMesh, const int iPoint) const
{
const int globaIVertex = mVerticesVisitor.mCumulativeNumPoints[iMesh] + iPoint;
return int64_t(globaIVertex) * int64_t(mNumMatrixElements);
}
void precomputeTransferMatrices() const
{
GeneralSubpathSampler pathSampler(mScene, false, false);
double invNumSamplesPerPoint = 1.0 / double(mNumSamplesPerPoint);
int64_t numElements = int64_t(mVerticesVisitor.mNumTotalPoints) * int64_t(mNumMatrixElements);
mTransferMatrices = std::vector<Spectrum>(numElements, Spectrum(0.0));
shared_ptr<ProgressReport> progress = make_shared<ProgressReport>(mVerticesVisitor.mNumTotalPoints);
Viewer::SetProgressReport(mCamera->mFilm, progress);
mVerticesVisitor.forEachPoint<Parallel>([&](const int iMesh, shared_ptr<const TriangleMeshGeometry> & triMesh, const int iPoint)
{
// get sampler and surface vertex
shared_ptr<Sampler> sampler = mSampler->clone(int(globalIndex(iMesh, iPoint)));
shared_ptr<SurfaceVertex> sv = mVerticesVisitor.getSurfaceVertex(iMesh, iPoint);
int64_t iMatrixBegin = globalIndex(iMesh, iPoint);
std::vector<double> localShValues(mNumLocalCoeffs);
std::vector<double> envmapShValues(mNumEnvmapCoeffs);
for (int i = 0; i < mNumSamplesPerPoint; i++)
{
// sampling incoming radiance to the surface
Vec3 sampled = Mapping::HemisphereFromSquare(sampler->get2d());
Vec3 initialLocalDirection = sv->mCoordFrame.toWorld(sampled);
double initialPdf = 0.5 * Math::InvPi;
Ray out(sv->mPosition, initialLocalDirection);
pathSampler.randomWalk(sampler.get(), sv, nullptr, out, Spectrum(1.0 / initialPdf), 0.0, mNumPathVertices - 2, SubpathSampler::Request::None,
[&](shared_ptr<Vertex> & currentVertex, shared_ptr<Vertex> & prevVertex, const Vec3 &, const double pdf) -> bool
{
assert(currentVertex != nullptr);
if (currentVertex->hasType(VertexType::Light))
{
// out going direction to envmap
Vec3 envmapDirection = Math::Normalize(currentVertex->mPosition - prevVertex->mPosition);
if (mNumPathVertices == 3) // this means direct light
{
assert(Math::IsApprox(Math::Dot(envmapDirection, initialLocalDirection), 1.0));
}
// evaluate all spherical harmonics terms for local
for (int iCoeff = 0; iCoeff < mNumLocalCoeffs; iCoeff++)
{
localShValues[iCoeff] = Math::SphericalHarmonics::Eval(iCoeff, initialLocalDirection);
}
// evaluate all spherical harmonics terms for envmap
for (int iCoeff = 0; iCoeff < mNumEnvmapCoeffs; iCoeff++)
{
envmapShValues[iCoeff] = Math::SphericalHarmonics::Eval(iCoeff, envmapDirection);
}
for (int iMatrixElement = 0; iMatrixElement < mNumMatrixElements; iMatrixElement++)
{
int row = iMatrixElement / mNumLocalCoeffs;
int col = iMatrixElement % mNumLocalCoeffs;
mTransferMatrices[iMatrixBegin + iMatrixElement] += localShValues[col] * envmapShValues[row] * currentVertex->mPathContrib * invNumSamplesPerPoint;
}
}
return true;
});
}
// update percentage on the viewer
progress->increment(1);
Viewer::Redraw(mCamera->mFilm, Ibound2(Ivec2(0, 0), Ivec2(0, 0)));
});
Util::Vector::WriteVectorBin("test.gprt2002.bin", mTransferMatrices);
}
void interpolateMatrix(std::vector<Spectrum> * matrix, std::vector<Spectrum> & matrices, const TriangleInfoVertex & tiv) const
{
int64_t begin[3];
// find begin and end
for (int i = 0; i < 3; i++)
{
begin[i] = globalIndex(tiv.mGeometryId, tiv.mIndices[i]);
}
for (int i = 0; i < mNumMatrixElements; i++)
{
Spectrum element[3];
for (int j = 0; j < 3; j++)
{
element[j] = matrices[begin[j] + i];
}
(*matrix)[i] = Math::BarycentricInterpolate(element[0], element[1], element[2], tiv.mBarycentricCoords);
}
}
void matrixVectorMultiply(std::vector<Spectrum> * result, const std::vector<Spectrum> & mat, const std::vector<Spectrum> vec) const
{
for (int r = 0; r < mNumEnvmapCoeffs; r++)
{
Spectrum x(0.0);
for (int c = 0; c < mNumLocalCoeffs; c++)
{
x += mat[r * mNumLocalCoeffs + c] * vec[c];
}
(*result)[r] = x;
}
}
std::vector<Spectrum> precomputeEnvmap() const
{
shared_ptr<const EquiRectRep<Spectrum>> equirectEnvmap = static_pointer_cast<const EquiRectRep<Spectrum>>(mScene->mEnvmapLight->mSphRep);
FimageIo::Save(SphRepConvertUtil::EquiRectRepFrom(SphRepConvertUtil::SphHarRepFrom(*equirectEnvmap, mNumEnvmapCoeffs), Ivec2(1024, 512)).mImage, "checkcheck.pfm");
return SphRepConvertUtil::SphHarRepFrom(*equirectEnvmap, mNumEnvmapCoeffs).mCoeffs;
}
void execute(const std::vector<Spectrum> & envmapCoeffs) const
{
mCamera->mFilm->requestBaseBuffer();
// act as a rasterizer
Parallel::Split2d(mCamera->mFilm->mResolution, [&](const Ibound2 & bound)
{
int tileSeed = bound.pMin[0] * mCamera->mFilm->mResolution[1] + bound.pMin[1];
shared_ptr<Sampler> tileSampler = mSampler->clone(tileSeed);
std::vector<Spectrum> interpolatedMatrix(mNumMatrixElements);
std::vector<Spectrum> localCoeffs(mNumLocalCoeffs);
for (const Ivec2 & pixel : bound)
{
Spectrum result(0.0);
for (Uint iSample = 0; iSample < mNumAaSamples; iSample++)
{
// sample camera ray
Ray3 r;
CameraVertex cv;
mCamera->sampleWe0ByPdf(&cv, nullptr, tileSampler->get2d());
r.origin = cv.mPosition;
mCamera->samplePerPixelWe1CosByPdf(&r.direction, nullptr, nullptr, cv, pixel, tileSampler->get2d());
// intersect triangle
TriangleInfoVertex vertex;
Ray3 r1 = r;
Ray3 r2 = r;
if (mScene->mAccel->intersectTriangleInfo(&vertex, &r1))
{
// get matrix at the shading point
interpolateMatrix(&interpolatedMatrix, mTransferMatrices, vertex);
// compute transfer
matrixVectorMultiply(&localCoeffs, interpolatedMatrix, envmapCoeffs);
// debug
shared_ptr<SurfaceVertex> v = Vertex::SafeCast<SurfaceVertex>(mScene->intersect(&r2));
Vec3 in = v->mCoordFrame.toLocal(-r.direction);
Vec3 out;
Spectrum bsdfContrib = v->mBsdf->sampleBsdfCosByPdf(&out, nullptr, *v, in, tileSampler->get2d());
out = v->mCoordFrame.toWorld(out);
// reconstruct radiance
for (int iCoeff = 0; iCoeff < mNumLocalCoeffs; iCoeff++)
{
const double shVal = Math::SphericalHarmonics::Eval(iCoeff, out);
for (int iChannel = 0; iChannel < Spectrum::NumElements; iChannel++)
{
result[iChannel] += shVal * bsdfContrib[iChannel] * localCoeffs[iCoeff][iChannel];
}
}
}
else
{
// reconstruct envmap from sh values
for (int iCoeff = 0; iCoeff < mNumEnvmapCoeffs; iCoeff++)
{
const double shVal = Math::SphericalHarmonics::Eval(iCoeff, -r.direction);
for (int iChannel = 0; iChannel < Spectrum::NumElements; iChannel++)
{
result += shVal * envmapCoeffs[iCoeff][iChannel];
}
}
}
}
mCamera->mFilm->addSample(pixel, Spectrum(result) / static_cast<double>(mNumAaSamples));
}
Viewer::Redraw(mCamera->mFilm, bound);
});
}
void render() override
{
precomputeTransferMatrices();
filesystem::path k = "test.gprt2002.bin";
mTransferMatrices = Util::Vector::LoadVectorBin<Spectrum>(k);
std::vector<Spectrum> envmapCoeffs = precomputeEnvmap();
execute(envmapCoeffs);
}
int mNumEnvmapCoeffs;
int mNumLocalCoeffs;
int mNumMatrixElements;
int mNumSamplesPerPoint;
int mNumAaSamples;
int mNumPathVertices;
TriangleMeshVerticesVisitor mVerticesVisitor;
mutable std::vector<Spectrum> mTransferMatrices;
};