reformat rt

This commit is contained in:
Shuo Feng 2024-03-14 19:31:50 -04:00
parent 60a3180f1e
commit bf19d3f7b0
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
4 changed files with 13 additions and 12 deletions

View file

@ -2,6 +2,8 @@
#include <fstream> #include <fstream>
Output *Output::current = nullptr;
void Output::write() { void Output::write() {
std::ofstream fout(path, std::ios_base::out | std::ios_base::binary); std::ofstream fout(path, std::ios_base::out | std::ios_base::binary);
fout << "P6\n" << width << ' ' << height << '\n' << "255" << std::endl; fout << "P6\n" << width << ' ' << height << '\n' << "255" << std::endl;

View file

@ -33,6 +33,8 @@ public:
float g(int) const; float g(int) const;
void b(int, float); void b(int, float);
float b(int) const; float b(int) const;
static Output *current;
}; };
#endif // !OUTPUT_H_ #endif // !OUTPUT_H_

View file

@ -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); 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); Vector3f result(0, 0, 0);
for (auto light : lights) for (auto light : lights)
result += light->isUse() ? light->illumination(hit, geometries) result += light->isUse() ? light->illumination(hit, geometries)
: Vector3f::Zero(); : Vector3f::Zero();
result = result.cwiseMax(0.0f).cwiseMin(1.0f); result = result.cwiseMax(0.0f).cwiseMin(1.0f);
buffer->r(i, result.x()); Output::current->r(i, result.x());
buffer->g(i, result.y()); Output::current->g(i, result.y());
buffer->b(i, result.z()); Output::current->b(i, result.z());
} }
void RayTracer::render() { void RayTracer::render() {
@ -55,7 +55,7 @@ void RayTracer::render() {
Vector3f vpUpperLeft = cameraPos + lookAt - vpU / 2.0 - vpV / 2.0; Vector3f vpUpperLeft = cameraPos + lookAt - vpU / 2.0 - vpV / 2.0;
Vector3f pxUpperLeft = vpUpperLeft + (du + dv) / 2.0; Vector3f pxUpperLeft = vpUpperLeft + (du + dv) / 2.0;
Output *buffer = new Output(Scene::current->backgroundColor(), Output::current = new Output(Scene::current->backgroundColor(),
Scene::current->name(), width, height); Scene::current->name(), width, height);
for (int y = 0; y < height; ++y) for (int y = 0; y < height; ++y)
@ -71,11 +71,9 @@ void RayTracer::render() {
if (!records.empty()) { if (!records.empty()) {
HitRecord hit = records.top(); HitRecord hit = records.top();
hit.calcNormal(); hit.calcNormal();
calculateColor(hit, buffer, y * width + x); calculateColor(hit, y * width + x);
} }
} }
outputs.push_back(buffer);
} }
void RayTracer::output() { void RayTracer::output() {
@ -89,7 +87,6 @@ void RayTracer::run() {
for (auto scene : scenes) { for (auto scene : scenes) {
Scene::current = scene; Scene::current = scene;
render(); render();
Output::current->write();
} }
output();
} }

View file

@ -22,7 +22,7 @@ private:
std::vector<Output *> outputs; std::vector<Output *> outputs;
void parse(); void parse();
void calculateColor(const HitRecord &, Output *, int); void calculateColor(const HitRecord &, int);
void render(); void render();
void output(); void output();
}; };