From 271f213969afb1af5a6a798c09a8c13c76430f4b Mon Sep 17 00:00:00 2001 From: vonhyou Date: Thu, 21 Mar 2024 19:36:37 -0400 Subject: [PATCH] bugfix: --- src/Parser.cc | 4 ++-- src/RayTracer.cc | 56 +++++++++++++++++++++--------------------------- src/RayTracer.h | 1 - 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/Parser.cc b/src/Parser.cc index abca50e..3fee6ef 100644 --- a/src/Parser.cc +++ b/src/Parser.cc @@ -39,8 +39,8 @@ Scene *Parser::getScene(const nlohmann::json &j) { sc->setAntialiasing(j.value("antialiasing", false)); sc->setTwoSideRender(j.value("twosiderender", false)); sc->setGlobalIllum(j.value("globalillum", false)); - sc->setMaxBounce(j.value("maxbounce", 3)); - sc->setProbTerminate(j.value("probTerminate", 0.33f)); + sc->setMaxBounce(j.value("maxbounces", 3)); + sc->setProbTerminate(j.value("probterminate", 0.33f)); if (j.contains("raysperpixel")) sc->setRaysPerPixel(getRpp(j["raysperpixel"])); diff --git a/src/RayTracer.cc b/src/RayTracer.cc index 80dd723..04103a8 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -19,7 +19,6 @@ using Eigen::VectorXi; using std::priority_queue; // help function declarations -Ray getRay(int, int); Ray getRay(int, int, int, int); void writeColor(int, const Vector3f &); utils::Optional trace(Ray r); @@ -87,11 +86,6 @@ void RayTracer::render() { using namespace camera; Output::current = new Output(Scene::current->name(), width, height); - std::cout << "Global Illumination: " << (globalIllum ? "T" : "F") << std::endl - << "Anti-aliasing: " << (antiAliasing ? "T" : "F") << std::endl; - - std::cout << gridHeight << " " << gridWidth << " " << raysPerPixel - << std::endl; for (int y = 0; y < height; ++y) { utils::Progress::of((y + 1.0f) / height); @@ -102,10 +96,9 @@ void RayTracer::render() { Vector3f accumulate = Vector3f::Zero(); for (int j = 0; j < gridHeight; ++j) for (int i = 0; i < gridWidth; ++i) { - Ray ray = getRay(x, y, i, j); for (int rayNum = 0; rayNum < raysPerPixel; ++rayNum) { - Optional result = - globalIllum ? trace(ray) : trace(ray, x, y); + Ray ray = getRay(x, y, i, j); + Optional result = trace(ray); if (result.hasValue()) { accumulate += result.value(); success++; @@ -169,14 +162,22 @@ Light *RayTracer::singleLightSource() const { return nullptr; } -Ray getRay(int x, int y) { - using namespace camera; - return Ray(pos, pxUpperLeft + x * du + y * dv - pos); +// This should generate a higher quality random number +float getRandomNumber() { + static std::uniform_real_distribution distribution(0.0, 1.0); + static std::mt19937 generator; + return distribution(generator); } Ray getRay(int x, int y, int i, int j) { using namespace camera; - return Ray(pos, vpUpperLeft + x * du + i * gdu + y * dv + j * gdv - pos); + Vector3f offset = Vector3f::Zero(); + + if (globalIllum || antiAliasing) + offset = getRandomNumber() * gdu + getRandomNumber() * gdv; + + return Ray(pos, + vpUpperLeft + x * du + i * gdu + y * dv + j * gdv + offset - pos); } Vector3f clamp(const Vector3f &color) { @@ -189,13 +190,6 @@ void writeColor(int i, const Vector3f &color) { Output::current->b(i, color.z()); } -// This should generate a higher quality random number -float getRandomNumber() { - static std::uniform_real_distribution distribution(0.0, 1.0); - static std::mt19937 generator; - return distribution(generator); -} - // Generate a randon point on a unit hemisphere Vector3f getRandomDirection() { RETRY_RANDOM: @@ -264,22 +258,20 @@ RETRY_TRACING: std::max(0.0f, hit.normal().dot(direction)); } -Optional RayTracer::trace(Ray r, int x, int y) const { - Optional hitRecord = getHitRecord(r); - if (hitRecord.hasValue()) - return Optional(calculateColor(hitRecord.value())); - - return Optional::nullopt; -} - Optional RayTracer::trace(Ray r) const { Optional hitRecord = getHitRecord(r); - if (hitRecord.hasValue()) { - Vector3f color = trace(hitRecord.value(), Scene::current->maxBounce(), - Scene::current->probTerminate()); + if (!camera::globalIllum) { + if (hitRecord.hasValue()) + return Optional(calculateColor(hitRecord.value())); + else if (camera::antiAliasing) + return Optional(Scene::current->backgroundColor()); + } else { + if (hitRecord.hasValue()) { + Vector3f color = trace(hitRecord.value(), Scene::current->maxBounce(), + Scene::current->probTerminate()); - if (color != Vector3f::Zero()) return Optional(color); + } } return Optional::nullopt; diff --git a/src/RayTracer.h b/src/RayTracer.h index 7995478..06bf7b1 100644 --- a/src/RayTracer.h +++ b/src/RayTracer.h @@ -30,7 +30,6 @@ private: Optional getHitRecord(Ray) const; Vector3f calculateColor(const HitRecord &) const; Light *singleLightSource() const; - Optional trace(Ray, int, int) const; Optional trace(Ray) const; Vector3f trace(HitRecord, int, float) const; };