From ebae7ccfafe7b12e763d0368b66ca162041da949 Mon Sep 17 00:00:00 2001 From: vonhyou Date: Wed, 20 Mar 2024 06:33:05 -0400 Subject: [PATCH] file update --- src/Random.h | 23 ----------------------- src/RayTracer.cc | 29 +++++++++++++++++------------ 2 files changed, 17 insertions(+), 35 deletions(-) delete mode 100644 src/Random.h diff --git a/src/Random.h b/src/Random.h deleted file mode 100644 index c0d3287..0000000 --- a/src/Random.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef RANDOM_H_ -#define RANDOM_H_ - -#include -#include -namespace utils { -class Random { -public: - /** - * A way to generate higher quality random number - * since C++ 11 - */ - static float get() { - std::default_random_engine generator( - std::chrono::system_clock::now().time_since_epoch().count()); - std::uniform_real_distribution distribution(0.0f, 1.0f); - - return distribution(generator); - } -}; -} // namespace utils - -#endif // !RANDOM_H_ diff --git a/src/RayTracer.cc b/src/RayTracer.cc index 2631274..f89f31c 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -5,12 +5,12 @@ #include "Output.h" #include "Parser.h" #include "Progress.h" -#include "Random.h" #include "Ray.h" #include #include #include +#include #include #include @@ -91,6 +91,9 @@ void RayTracer::render() { Vector3f accumulate = Vector3f::Zero(); for (int j = 0; j < gridHeight; ++j) for (int i = 0; i < gridWidth; ++i) { + + if (x != width / 2 || y != height / 2 || i || j) + ; // goto DEBUG_COLOR; Ray ray = getRay(x, y, i, j); for (int rayNum = 0; rayNum < raysPerPixel; ++rayNum) { utils::Optional result = trace(ray); @@ -112,6 +115,7 @@ void RayTracer::render() { color = calculateColor(hit, y * width + x); } } + DEBUG_COLOR: writeColor(y * width + x, clamp(color)); } } @@ -134,12 +138,13 @@ Vector3f RayTracer::calculateColor(const HitRecord &hit, int i) const { * Find the nearest geometry to intersect */ Optional RayTracer::getHitRecord(Ray r, const Geometry *self, - bool notSphere) const { + bool notRectangle) const { priority_queue records; for (auto g : geometries) { Optional t = g->intersect(r); if (t.hasValue() && g != self) - if (!notSphere || notSphere && g->type() != Geometry::Type::SPHERE) + if (!notRectangle || + notRectangle && g->type() != Geometry::Type::RECTANGLE) records.push(HitRecord(t.value(), r, g)); } @@ -190,8 +195,8 @@ void writeColor(int i, const Vector3f &color) { Vector3f getRandomDirection() { RETRY_RANDOM: - float x = utils::Random::get(); - float y = utils::Random::get(); + float x = (float)rand() / RAND_MAX; + float y = (float)rand() / RAND_MAX; if (x * x + y * y > 1) goto RETRY_RANDOM; @@ -199,25 +204,25 @@ RETRY_RANDOM: } Vector3f RayTracer::trace(HitRecord hit, int bounce, float prob) const { - bool notFinish = bounce && (utils::Random::get() > prob); + bool finish = !bounce || ((float)rand() / RAND_MAX < prob); Vector3f point = hit.point(); Light *light = singleLightSource(); Vector3f direction; Geometry *geometry = hit.geometry(); - if (notFinish) - direction = point + getRandomDirection(); - else + if (finish) direction = light->getCenter() - point; + else + direction = point + getRandomDirection(); direction.normalize(); Ray ray(point, direction); - Optional hitRecord = getHitRecord(ray, geometry, !notFinish); + Optional hitRecord = getHitRecord(ray, geometry, finish); Vector3f traceColor = Vector3f::Zero(); - if (notFinish && hitRecord.hasValue()) + if (!finish && hitRecord.hasValue()) traceColor = trace(hitRecord.value(), bounce - 1, prob); - else if (!notFinish) + else if (finish && !hitRecord.hasValue()) traceColor = light->id(); return traceColor.array() * geometry->cd().array() *