mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-08 07:22:01 +00:00
reformat rt
This commit is contained in:
parent
60a3180f1e
commit
bf19d3f7b0
4 changed files with 13 additions and 12 deletions
|
@ -2,6 +2,8 @@
|
|||
|
||||
#include <fstream>
|
||||
|
||||
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;
|
||||
|
|
|
@ -33,6 +33,8 @@ public:
|
|||
float g(int) const;
|
||||
void b(int, float);
|
||||
float b(int) const;
|
||||
|
||||
static Output *current;
|
||||
};
|
||||
|
||||
#endif // !OUTPUT_H_
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ private:
|
|||
std::vector<Output *> outputs;
|
||||
|
||||
void parse();
|
||||
void calculateColor(const HitRecord &, Output *, int);
|
||||
void calculateColor(const HitRecord &, int);
|
||||
void render();
|
||||
void output();
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue