From efab58561ae693fd6c14691d36c0adb19b99b142 Mon Sep 17 00:00:00 2001 From: vonhyou Date: Sun, 3 Mar 2024 23:22:47 -0500 Subject: [PATCH] commit --- .gitignore | 1 + src/Light.cc | 5 ++++- src/Light.h | 3 +++ src/Output.cc | 7 ++++--- src/Parser.cc | 1 + src/RayTracer.cc | 13 +++++++------ 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index ec45768..bf7cd72 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build/ *.ppm *.zip .vscode/ +.cache/ diff --git a/src/Light.cc b/src/Light.cc index 83c5cc8..aae27b6 100644 --- a/src/Light.cc +++ b/src/Light.cc @@ -1,7 +1,6 @@ #include "Light.h" #include #include -#include void Light::setTransform(const Matrix4f &transform) { this->transform = transform; @@ -11,10 +10,14 @@ void Light::setGridSize(unsigned int gridSize) { this->gridSize = gridSize; } void Light::setUseCenter(bool useCenter) { this->useCenter = useCenter; } +void Light::setIsUse(bool isUse) { this->use = isUse; } + Vector3f Light::getDiffuse() const { return diffuse; } Vector3f Light::getSpecular() const { return specular; } +bool Light::isUse() const { return use; } + Vector3f PointLight::illumination(const HitRecord &hit, const vector &geometries) const { Vector3f shadingPoint = hit.getPoint(); diff --git a/src/Light.h b/src/Light.h index be87209..60afbce 100644 --- a/src/Light.h +++ b/src/Light.h @@ -29,13 +29,16 @@ protected: Matrix4f transform = Matrix4f::Identity(); // optional member `transform` unsigned int gridSize = 0; // optional member `n` bool useCenter = false; // optional member `usecenter` + bool use = true; // this appears in a json file public: void setTransform(const Matrix4f &); void setGridSize(unsigned int); void setUseCenter(bool); + void setIsUse(bool); Vector3f getDiffuse() const; Vector3f getSpecular() const; + bool isUse() const; }; class AreaLight : public Light { diff --git a/src/Output.cc b/src/Output.cc index 840212b..c72c918 100644 --- a/src/Output.cc +++ b/src/Output.cc @@ -1,5 +1,6 @@ #include "Output.h" +#include #include void Output::write() { @@ -8,9 +9,9 @@ void Output::write() { for (unsigned int y = 0; y < height; ++y) for (unsigned int x = 0; x < 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 << (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.close(); } diff --git a/src/Parser.cc b/src/Parser.cc index 4d15d76..126c6b3 100644 --- a/src/Parser.cc +++ b/src/Parser.cc @@ -109,6 +109,7 @@ Light *Parser::getLight(const nlohmann::json &j) { l->setGridSize(j.value("n", 0)); l->setUseCenter(j.value("usecenter", false)); + l->setIsUse(j.value("use", true)); return l; } diff --git a/src/RayTracer.cc b/src/RayTracer.cc index 6ae028b..09b7113 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -32,12 +32,13 @@ void RayTracer::calculateColor(const HitRecord &hit, Output *buffer, int i) { buffer->g(i, 0); buffer->b(i, 0); - for (auto light : lights) { - Vector3f contribution = light->illumination(hit, geometries); - buffer->r(i, buffer->r(i) + contribution.x()); - buffer->g(i, buffer->g(i) + contribution.y()); - buffer->b(i, buffer->b(i) + contribution.z()); - } + for (auto light : lights) + if (light->isUse()) { + Vector3f contribution = light->illumination(hit, geometries); + buffer->r(i, buffer->r(i) + contribution.x()); + buffer->g(i, buffer->g(i) + contribution.y()); + buffer->b(i, buffer->b(i) + contribution.z()); + } } void RayTracer::render(Scene *scene) {