Skip to content

Commit

Permalink
fix directSampleLight bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenxiZhou0619 committed Feb 27, 2023
1 parent ee6c7f9 commit 224cd40
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
- examples/area-lights
- examples/two-spotlights
- examples/bunny

## TODO
- 对mesh和sphere实现表面采样(目前只有parallelogram可以配置为面光源)
4 changes: 2 additions & 2 deletions examples/bunny/scene.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"output" : {
"filename" : "cornell-18.hdr"
"filename" : "cornell-18.png"
},
"sampler" : {
"type" : "independent",
Expand All @@ -24,7 +24,7 @@
}
},
"integrator" : {
"type" : "directSampleBSDF"
"type" : "directSampleLight"
},
"scene" : {
"shapes" : [
Expand Down
5 changes: 5 additions & 0 deletions src/CoreLayer/Math/Distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ template <typename T> class Distribution {
}

T sample(float sample, float *pdf) const {
if (cdf.size() == 1) {
// no data in distribution
*pdf = .0f;
return T();
}
auto entry = std::lower_bound(cdf.cbegin(), cdf.cend(), sample);
size_t index = entry - cdf.cbegin() - 1;
*pdf = cdf[index + 1] - cdf[index];
Expand Down
29 changes: 16 additions & 13 deletions src/FunctionLayer/Integrator/DirectIntegrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,23 @@ DirectIntegratorSampleLight::li(const Ray &ray, const Scene &scene,
//* 从场景中采样一个光源
float pdfLight = .0f;
auto light = scene.sampleLight(sampler->next1D(), &pdfLight);
//* 在光源上采样出一个点
auto lightSampleResult = light->sample(intersection, sampler->next2D());

//* 测试shadowray是否被场景遮挡
Ray shadowRay{intersection.position, lightSampleResult.direction, 1e-4f,
lightSampleResult.distance};
auto occlude = scene.rayIntersect(shadowRay);
if (!occlude.has_value()) {
auto material = intersection.shape->material;
auto bsdf = material->computeBSDF(intersection);
Spectrum f = bsdf->f(-ray.direction, shadowRay.direction);
lightSampleResult.pdf *= pdfLight;
float pdf = convertPDF(lightSampleResult, intersection);
spectrum += lightSampleResult.energy * f / pdf;
if (light && pdfLight != .0f) {
//* 在光源上采样出一个点
auto lightSampleResult = light->sample(intersection, sampler->next2D());

//* 测试shadowray是否被场景遮挡
Ray shadowRay{intersection.position, lightSampleResult.direction, 1e-4f,
lightSampleResult.distance};
auto occlude = scene.rayIntersect(shadowRay);
if (!occlude.has_value()) {
auto material = intersection.shape->material;
auto bsdf = material->computeBSDF(intersection);
Spectrum f = bsdf->f(-ray.direction, shadowRay.direction);
lightSampleResult.pdf *= pdfLight;
float pdf = convertPDF(lightSampleResult, intersection);
spectrum += lightSampleResult.energy * f / pdf;
}
}
return spectrum;
}
Expand Down
14 changes: 12 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <ResourceLayer/Image.h>
#include <ResourceLayer/JsonUtil.h>
#include <fstream>
#include <regex>
#include <stdio.h>

int main(int argc, char **argv) {
Expand All @@ -35,6 +36,15 @@ int main(int argc, char **argv) {
camera->film->deposit({x, y}, li / spp);
}
}
camera->film->saveHDR(
fetchRequired<std::string>(json["output"], "filename").c_str());

//* 目前支持输出为png/hdr两种格式
std::string outputName =
fetchRequired<std::string>(json["output"], "filename");
if (std::regex_match(outputName, std::regex("(.*)(\\.png)"))) {
camera->film->savePNG(outputName.c_str());
} else if (std::regex_match(outputName, std::regex("(.*)(\\.hdr)"))) {
camera->film->saveHDR(outputName.c_str());
} else {
std::cout << "Only support output as PNG/HDR\n";
}
}

0 comments on commit 224cd40

Please sign in to comment.