mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-08 07:22:01 +00:00
rewrite output
This commit is contained in:
parent
9a8e09207d
commit
1d19b6bcc4
2 changed files with 42 additions and 14 deletions
|
@ -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::output() {
|
||||
for (auto scene : scenes) {
|
||||
void RayTracer::render(Scene *scene) {
|
||||
int width = scene->getWidth();
|
||||
int height = scene->getHeight();
|
||||
vector<double> buffer(3 * width * height);
|
||||
save_ppm(scene->getName(), buffer, width, height);
|
||||
}
|
||||
Buffer buffer(width * height);
|
||||
Task *task = new Task(scene, buffer);
|
||||
tasks.push_back(task);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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_
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue