diff --git a/src/Output.cc b/src/Output.cc index 840212b..222e585 100644 --- a/src/Output.cc +++ b/src/Output.cc @@ -2,6 +2,8 @@ #include +Output *Output::current = nullptr; + void Output::write() { std::ofstream fout(path, std::ios_base::out | std::ios_base::binary); fout << "P6\n" << width << ' ' << height << '\n' << "255" << std::endl; diff --git a/src/Output.h b/src/Output.h index d7a065d..8cf7b9c 100644 --- a/src/Output.h +++ b/src/Output.h @@ -33,6 +33,8 @@ public: float g(int) const; void b(int, float); float b(int) const; + + static Output *current; }; #endif // !OUTPUT_H_ diff --git a/src/RayTracer.cc b/src/RayTracer.cc index 8177d8b..1209fae 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -27,16 +27,16 @@ Ray getRay(int x, int y, const Vector3f &camPos, const Vector3f &pxUpperLeft, return Ray(camPos, pxUpperLeft + x * du + y * dv - camPos); } -void RayTracer::calculateColor(const HitRecord &hit, Output *buffer, int i) { +void RayTracer::calculateColor(const HitRecord &hit, int i) { Vector3f result(0, 0, 0); for (auto light : lights) result += light->isUse() ? light->illumination(hit, geometries) : Vector3f::Zero(); result = result.cwiseMax(0.0f).cwiseMin(1.0f); - buffer->r(i, result.x()); - buffer->g(i, result.y()); - buffer->b(i, result.z()); + Output::current->r(i, result.x()); + Output::current->g(i, result.y()); + Output::current->b(i, result.z()); } void RayTracer::render() { @@ -55,8 +55,8 @@ void RayTracer::render() { Vector3f vpUpperLeft = cameraPos + lookAt - vpU / 2.0 - vpV / 2.0; Vector3f pxUpperLeft = vpUpperLeft + (du + dv) / 2.0; - Output *buffer = new Output(Scene::current->backgroundColor(), - Scene::current->name(), width, height); + Output::current = new Output(Scene::current->backgroundColor(), + Scene::current->name(), width, height); for (int y = 0; y < height; ++y) for (int x = 0; x < width; ++x) { @@ -71,11 +71,9 @@ void RayTracer::render() { if (!records.empty()) { HitRecord hit = records.top(); hit.calcNormal(); - calculateColor(hit, buffer, y * width + x); + calculateColor(hit, y * width + x); } } - - outputs.push_back(buffer); } void RayTracer::output() { @@ -89,7 +87,6 @@ void RayTracer::run() { for (auto scene : scenes) { Scene::current = scene; render(); + Output::current->write(); } - - output(); } diff --git a/src/RayTracer.h b/src/RayTracer.h index 591cc72..764bd05 100644 --- a/src/RayTracer.h +++ b/src/RayTracer.h @@ -22,7 +22,7 @@ private: std::vector outputs; void parse(); - void calculateColor(const HitRecord &, Output *, int); + void calculateColor(const HitRecord &, int); void render(); void output(); };