rewrite output

This commit is contained in:
Shuo Feng 2024-02-27 20:35:41 -05:00
parent 812317f2ee
commit 94f1898b51
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
4 changed files with 73 additions and 26 deletions

22
src/Output.cc Normal file
View file

@ -0,0 +1,22 @@
#include "Output.h"
#include <fstream>
void Output::write() {
std::ofstream fout(path, std::ios_base::out | std::ios_base::binary);
fout << "P6\n" << width << ' ' << height << '\n' << "255" << std::endl;
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.close();
}
float Output::r(int index) { return red.at(index); }
float Output::g(int index) { return green.at(index); }
float Output::b(int index) { return blue.at(index); }
void Output::r(int index, float value) { red.at(index) = value; }
void Output::g(int index, float value) { green.at(index) = value; }
void Output::b(int index, float value) { blue.at(index) = value; }

37
src/Output.h Normal file
View file

@ -0,0 +1,37 @@
#ifndef OUTPUT_H_
#define OUTPUT_H_
#include <Eigen/Core>
#include <string>
#include <vector>
using Eigen::Vector3f;
using std::string;
using std::vector;
class Output {
public:
Output(const Vector3f &bgc, string path, int w, int h)
: red(vector<float>(w * h, bgc.x())),
green(vector<float>(w * h, bgc.y())),
blue(vector<float>(w * h, bgc.z())), path(path), width(w), height(h) {}
void write();
private:
int width, height;
string path;
vector<float> red;
vector<float> green;
vector<float> blue;
public:
void r(int, float);
float r(int);
void g(int, float);
float g(int);
void b(int, float);
float b(int);
};
#endif // !OUTPUT_H_

View file

@ -1,5 +1,6 @@
#include "RayTracer.h" #include "RayTracer.h"
#include "../external/simpleppm.h" #include "../external/simpleppm.h"
#include "Output.h"
#include "Parser.h" #include "Parser.h"
#include "Ray.h" #include "Ray.h"
@ -39,13 +40,8 @@ void RayTracer::render(Scene *scene) {
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;
Buffer buffer(width * height * 3); Output *buffer =
Vector3f bgc = scene->getBackgroundColor(); new Output(scene->getBackgroundColor(), scene->getName(), width, height);
for (int i = 0; i < width * height; ++i) {
buffer[i * 3] = bgc.x();
buffer[i * 3 + 1] = bgc.y();
buffer[i * 3 + 2] = bgc.z();
}
for (int y = 0; y < height; ++y) for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x) { for (int x = 0; x < width; ++x) {
@ -53,23 +49,18 @@ void RayTracer::render(Scene *scene) {
for (auto geometry : geometries) for (auto geometry : geometries)
if (geometry->intersect(ray)) { if (geometry->intersect(ray)) {
buffer[3 * y * width + 3 * x + 0] = 1; buffer->r(y * width + x, 1);
buffer[3 * y * width + 3 * x + 1] = 1; buffer->g(y * width + x, 1);
buffer[3 * y * width + 3 * x + 2] = 1; buffer->b(y * width + x, 1);
break; break;
} }
} }
outputs.push_back(buffer);
Task *task = new Task(scene, buffer);
tasks.push_back(task);
} }
void RayTracer::output(Task *task) { void RayTracer::output() {
string path = task->first->getName(); for (auto output : outputs)
int width = task->first->getWidth(); output->write();
int height = task->first->getHeight();
save_ppm(path, task->second, width, height);
} }
void RayTracer::run() { void RayTracer::run() {
@ -78,6 +69,5 @@ void RayTracer::run() {
for (auto scene : scenes) for (auto scene : scenes)
render(scene); render(scene);
for (auto task : tasks) output();
output(task);
} }

View file

@ -4,14 +4,12 @@
#include "../external/json.hpp" #include "../external/json.hpp"
#include "Geometry.h" #include "Geometry.h"
#include "Light.h" #include "Light.h"
#include "Output.h"
#include "Scene.h" #include "Scene.h"
#include <utility> #include <utility>
#include <vector> #include <vector>
using Buffer = std::vector<double>;
using Task = std::pair<Scene *, Buffer>;
class RayTracer { class RayTracer {
public: public:
RayTracer(const nlohmann::json &j) : json(j) {} RayTracer(const nlohmann::json &j) : json(j) {}
@ -23,11 +21,11 @@ private:
std::vector<Light *> lights; std::vector<Light *> lights;
std::vector<Geometry *> geometries; std::vector<Geometry *> geometries;
std::vector<Task *> tasks; std::vector<Output *> outputs;
void parse(); void parse();
void render(Scene *); void render(Scene *);
void output(Task *); void output();
}; };
#endif // !RAY_TRACER_H_ #endif // !RAY_TRACER_H_