From a8a00d386e686997eb2fe15c651eddb753d0904a Mon Sep 17 00:00:00 2001 From: vonhyou Date: Tue, 27 Feb 2024 23:36:12 -0500 Subject: [PATCH] bugfix: color shadow --- src/Geometry.cc | 4 ++++ src/Geometry.h | 3 +++ src/Output.cc | 2 +- src/Output.h | 7 ++++--- src/RayTracer.cc | 12 +++++++----- 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Geometry.cc b/src/Geometry.cc index 9a98549..0822d14 100644 --- a/src/Geometry.cc +++ b/src/Geometry.cc @@ -2,6 +2,10 @@ #include +Vector3f Geometry::diffuse() const { return cd; } +Vector3f Geometry::specular() const { return cs; } +Vector3f Geometry::ambient() const { return ca; } + void Geometry::setTransform(const Matrix4f &transform) { this->transform = transform; } diff --git a/src/Geometry.h b/src/Geometry.h index d92709f..f2df2ee 100644 --- a/src/Geometry.h +++ b/src/Geometry.h @@ -29,6 +29,9 @@ protected: Matrix4f transform = Matrix4f::Identity(); public: + Vector3f diffuse() const; + Vector3f specular() const; + Vector3f ambient() const; void setTransform(const Matrix4f &); }; diff --git a/src/Output.cc b/src/Output.cc index e89ab7c..840212b 100644 --- a/src/Output.cc +++ b/src/Output.cc @@ -10,7 +10,7 @@ void Output::write() { 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 * height + x]); + << (char)(255.0f * blue[y * width + x]); fout.close(); } diff --git a/src/Output.h b/src/Output.h index 68f9350..d7a065d 100644 --- a/src/Output.h +++ b/src/Output.h @@ -12,9 +12,10 @@ using std::vector; class Output { public: Output(const Vector3f &bgc, string path, int w, int h) - : red(vector(w * h, bgc.x())), - green(vector(w * h, bgc.y())), - blue(vector(w * h, bgc.z())), path(path), width(w), height(h) {} + : red(vector(w * h + 1, bgc.x())), + green(vector(w * h + 1, bgc.y())), + blue(vector(w * h + 1, bgc.z())), path(path), width(w), + height(h) {} void write(); diff --git a/src/RayTracer.cc b/src/RayTracer.cc index e4a393b..27d9d5d 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -47,14 +47,16 @@ void RayTracer::render(Scene *scene) { for (int x = 0; x < width; ++x) { Ray ray = getRay(x, y, cameraPos, pxUpperLeft, du, dv); - for (auto geometry : geometries) - if (geometry->intersect(ray)) { - buffer->r(y * width + x, 1); - buffer->g(y * width + x, 1); - buffer->b(y * width + x, 1); + for (auto g : geometries) + if (g->intersect(ray)) { + Vector3f diffuse = g->diffuse(); + buffer->r(y * width + x, diffuse.x()); + buffer->g(y * width + x, diffuse.y()); + buffer->b(y * width + x, diffuse.z()); break; } } + outputs.push_back(buffer); }