From b5878e06c90e8089482ae4e9c3eb4a20aa069a0c Mon Sep 17 00:00:00 2001 From: vonhyou Date: Mon, 4 Mar 2024 02:25:59 -0500 Subject: [PATCH] bugfix: clamp --- src/Light.cc | 12 +++++++----- src/Output.cc | 7 +++---- src/RayTracer.cc | 5 +++-- src/RayTracer.h | 1 - 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Light.cc b/src/Light.cc index aae27b6..e9f1075 100644 --- a/src/Light.cc +++ b/src/Light.cc @@ -30,16 +30,18 @@ Vector3f PointLight::illumination(const HitRecord &hit, return Vector3f::Zero(); Vector3f ambient_ = geometry->coefAmbient() * geometry->ambient(); - Vector3f diffuse_ = geometry->coefDiffuse() * diffuse.array() * - geometry->diffuse().array() * + + Vector3f diffuse_ = geometry->coefDiffuse() * geometry->diffuse().array() * + diffuse.array() * std::max(0.0f, hit.normal().dot(rayDirection)); Vector3f halfWay = (hit.viewDirection() + rayDirection).normalized(); Vector3f specular_ = - geometry->coefSpecular() * specular.array() * - geometry->specular().array() * + geometry->coefSpecular() * geometry->specular().array() * + specular.array() * pow(std::max(0.0f, hit.normal().dot(halfWay)), geometry->getPhong()); - return diffuse_ + specular_ + ambient_; + + return specular_ + ambient_ + diffuse_; } Vector3f AreaLight::illumination(const HitRecord &hit, diff --git a/src/Output.cc b/src/Output.cc index c72c918..840212b 100644 --- a/src/Output.cc +++ b/src/Output.cc @@ -1,6 +1,5 @@ #include "Output.h" -#include #include void Output::write() { @@ -9,9 +8,9 @@ void Output::write() { for (unsigned int y = 0; y < height; ++y) for (unsigned int x = 0; x < width; ++x) - fout << (char)std::min(255.0f, 255.0f * red[y * width + x]) - << (char)std::min(255.0f, 255.0f * green[y * width + x]) - << (char)std::min(255.0f, 255.0f * blue[y * width + x]); + fout << (char)(255.0f * red[y * width + x]) + << (char)(255.0f * green[y * width + x]) + << (char)(255.0f * blue[y * width + x]); fout.close(); } diff --git a/src/RayTracer.cc b/src/RayTracer.cc index 09b7113..c79add8 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -31,10 +31,11 @@ void RayTracer::calculateColor(const HitRecord &hit, Output *buffer, int i) { buffer->r(i, 0); buffer->g(i, 0); buffer->b(i, 0); - for (auto light : lights) if (light->isUse()) { - Vector3f contribution = light->illumination(hit, geometries); + Vector3f contribution = + light->illumination(hit, geometries).cwiseMax(0.0f).cwiseMin(1.0f) / + lights.size(); buffer->r(i, buffer->r(i) + contribution.x()); buffer->g(i, buffer->g(i) + contribution.y()); buffer->b(i, buffer->b(i) + contribution.z()); diff --git a/src/RayTracer.h b/src/RayTracer.h index 6ca8254..43a9075 100644 --- a/src/RayTracer.h +++ b/src/RayTracer.h @@ -7,7 +7,6 @@ #include "Output.h" #include "Scene.h" -#include #include class RayTracer {