rewrite output

This commit is contained in:
Shuo Feng 2024-02-19 01:10:30 -05:00
parent 9a8e09207d
commit 1d19b6bcc4
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
2 changed files with 42 additions and 14 deletions

View file

@ -2,10 +2,10 @@
#include "../external/simpleppm.h"
#include "Parser.h"
#include <Eigen/Core>
#include <iostream>
#include <vector>
using std::vector;
void RayTracer::parse() {
for (auto i = json["output"].begin(); i != json["output"].end(); ++i)
scenes.push_back(Parser::getScene(*i));
@ -17,19 +17,41 @@ void RayTracer::parse() {
lights.push_back(Parser::getLight(*i));
}
void RayTracer::render() {}
void RayTracer::render(Scene *scene) {
int width = scene->getWidth();
int height = scene->getHeight();
Buffer buffer(width * height);
Task *task = new Task(scene, buffer);
tasks.push_back(task);
}
void RayTracer::output() {
for (auto scene : scenes) {
int width = scene->getWidth();
int height = scene->getHeight();
vector<double> buffer(3 * width * height);
save_ppm(scene->getName(), buffer, width, height);
}
void RayTracer::output(Task *task) {
string path = task->first->getName();
int width = task->first->getWidth();
int height = task->first->getHeight();
std::ofstream fout(path, std::ios_base::out | std::ios_base::binary);
fout << "P6" << std::endl
<< width << ' ' << height << std::endl
<< "255" << std::endl;
Buffer buffer = task->second;
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
fout << 255.0 * buffer[i * width + j].transpose() << ' ';
fout.close();
}
void RayTracer::run() {
parse();
render();
output();
for (auto scene : scenes)
render(scene);
for (auto task : tasks)
output(task);
Vector3f test(1, 2, 3);
std::cout << test.transpose() << std::endl;
}

View file

@ -6,8 +6,12 @@
#include "Light.h"
#include "Scene.h"
#include <utility>
#include <vector>
using Buffer = std::vector<Eigen::Vector3f>;
using Task = std::pair<Scene *, Buffer>;
class RayTracer {
public:
RayTracer(const nlohmann::json &j) : json(j) {}
@ -19,9 +23,11 @@ private:
std::vector<Light *> lights;
std::vector<Geometry *> geometries;
std::vector<Task *> tasks;
void parse();
void render();
void output();
void render(Scene *);
void output(Task *);
};
#endif // !RAY_TRACER_H_