This commit is contained in:
Shuo Feng 2024-03-03 23:22:47 -05:00
parent f98939c206
commit efab58561a
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
6 changed files with 20 additions and 10 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ build/
*.ppm *.ppm
*.zip *.zip
.vscode/ .vscode/
.cache/

View file

@ -1,7 +1,6 @@
#include "Light.h" #include "Light.h"
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <iostream>
void Light::setTransform(const Matrix4f &transform) { void Light::setTransform(const Matrix4f &transform) {
this->transform = 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::setUseCenter(bool useCenter) { this->useCenter = useCenter; }
void Light::setIsUse(bool isUse) { this->use = isUse; }
Vector3f Light::getDiffuse() const { return diffuse; } Vector3f Light::getDiffuse() const { return diffuse; }
Vector3f Light::getSpecular() const { return specular; } Vector3f Light::getSpecular() const { return specular; }
bool Light::isUse() const { return use; }
Vector3f PointLight::illumination(const HitRecord &hit, Vector3f PointLight::illumination(const HitRecord &hit,
const vector<Geometry *> &geometries) const { const vector<Geometry *> &geometries) const {
Vector3f shadingPoint = hit.getPoint(); Vector3f shadingPoint = hit.getPoint();

View file

@ -29,13 +29,16 @@ protected:
Matrix4f transform = Matrix4f::Identity(); // optional member `transform` Matrix4f transform = Matrix4f::Identity(); // optional member `transform`
unsigned int gridSize = 0; // optional member `n` unsigned int gridSize = 0; // optional member `n`
bool useCenter = false; // optional member `usecenter` bool useCenter = false; // optional member `usecenter`
bool use = true; // this appears in a json file
public: public:
void setTransform(const Matrix4f &); void setTransform(const Matrix4f &);
void setGridSize(unsigned int); void setGridSize(unsigned int);
void setUseCenter(bool); void setUseCenter(bool);
void setIsUse(bool);
Vector3f getDiffuse() const; Vector3f getDiffuse() const;
Vector3f getSpecular() const; Vector3f getSpecular() const;
bool isUse() const;
}; };
class AreaLight : public Light { class AreaLight : public Light {

View file

@ -1,5 +1,6 @@
#include "Output.h" #include "Output.h"
#include <algorithm>
#include <fstream> #include <fstream>
void Output::write() { void Output::write() {
@ -8,9 +9,9 @@ void Output::write() {
for (unsigned int y = 0; y < height; ++y) for (unsigned int y = 0; y < height; ++y)
for (unsigned int x = 0; x < width; ++x) for (unsigned int x = 0; x < width; ++x)
fout << (char)(255.0f * red[y * width + x]) fout << (char)std::min(255.0f, 255.0f * red[y * width + x])
<< (char)(255.0f * green[y * width + x]) << (char)std::min(255.0f, 255.0f * green[y * width + x])
<< (char)(255.0f * blue[y * width + x]); << (char)std::min(255.0f, 255.0f * blue[y * width + x]);
fout.close(); fout.close();
} }

View file

@ -109,6 +109,7 @@ Light *Parser::getLight(const nlohmann::json &j) {
l->setGridSize(j.value("n", 0)); l->setGridSize(j.value("n", 0));
l->setUseCenter(j.value("usecenter", false)); l->setUseCenter(j.value("usecenter", false));
l->setIsUse(j.value("use", true));
return l; return l;
} }

View file

@ -32,12 +32,13 @@ void RayTracer::calculateColor(const HitRecord &hit, Output *buffer, int i) {
buffer->g(i, 0); buffer->g(i, 0);
buffer->b(i, 0); buffer->b(i, 0);
for (auto light : lights) { for (auto light : lights)
Vector3f contribution = light->illumination(hit, geometries); if (light->isUse()) {
buffer->r(i, buffer->r(i) + contribution.x()); Vector3f contribution = light->illumination(hit, geometries);
buffer->g(i, buffer->g(i) + contribution.y()); buffer->r(i, buffer->r(i) + contribution.x());
buffer->b(i, buffer->b(i) + contribution.z()); buffer->g(i, buffer->g(i) + contribution.y());
} buffer->b(i, buffer->b(i) + contribution.z());
}
} }
void RayTracer::render(Scene *scene) { void RayTracer::render(Scene *scene) {