This commit is contained in:
Shuo Feng 2024-03-21 19:36:37 -04:00
parent f7e2ad664b
commit 271f213969
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
3 changed files with 26 additions and 35 deletions

View file

@ -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"]));

View file

@ -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<Vector3f> 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<Vector3f> result =
globalIllum ? trace(ray) : trace(ray, x, y);
Ray ray = getRay(x, y, i, j);
Optional<Vector3f> 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<float> 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<float> 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<Vector3f> RayTracer::trace(Ray r, int x, int y) const {
Optional<HitRecord> hitRecord = getHitRecord(r);
if (hitRecord.hasValue())
return Optional<Vector3f>(calculateColor(hitRecord.value()));
return Optional<Vector3f>::nullopt;
}
Optional<Vector3f> RayTracer::trace(Ray r) const {
Optional<HitRecord> 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<Vector3f>(calculateColor(hitRecord.value()));
else if (camera::antiAliasing)
return Optional<Vector3f>(Scene::current->backgroundColor());
} else {
if (hitRecord.hasValue()) {
Vector3f color = trace(hitRecord.value(), Scene::current->maxBounce(),
Scene::current->probTerminate());
if (color != Vector3f::Zero())
return Optional<Vector3f>(color);
}
}
return Optional<Vector3f>::nullopt;

View file

@ -30,7 +30,6 @@ private:
Optional<HitRecord> getHitRecord(Ray) const;
Vector3f calculateColor(const HitRecord &) const;
Light *singleLightSource() const;
Optional<Vector3f> trace(Ray, int, int) const;
Optional<Vector3f> trace(Ray) const;
Vector3f trace(HitRecord, int, float) const;
};