mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-08 07:22:01 +00:00
rearrange
This commit is contained in:
parent
121910877e
commit
eb9b6934c0
1 changed files with 52 additions and 48 deletions
100
src/RayTracer.cc
100
src/RayTracer.cc
|
@ -10,8 +10,29 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
|
using Eigen::VectorXi;
|
||||||
using std::priority_queue;
|
using std::priority_queue;
|
||||||
|
|
||||||
|
// help function declarations
|
||||||
|
int getGridWidth(VectorXi);
|
||||||
|
int getGridHeight(VectorXi);
|
||||||
|
int getRayNumber(VectorXi);
|
||||||
|
Ray getRay(int, int, const Vector3f &, const Vector3f &, const Vector3f &);
|
||||||
|
Ray getRay(int, int, int, int, const Vector3f &, const Vector3f &,
|
||||||
|
const Vector3f &, const Vector3f &, const Vector3f &);
|
||||||
|
void writeColor(int, const Vector3f &);
|
||||||
|
Vector3f trace();
|
||||||
|
|
||||||
|
void RayTracer::run() {
|
||||||
|
parse();
|
||||||
|
|
||||||
|
for (auto scene : scenes) {
|
||||||
|
Scene::current = scene;
|
||||||
|
render();
|
||||||
|
Output::current->write();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void RayTracer::parse() {
|
void RayTracer::parse() {
|
||||||
for (auto i = json["output"].begin(); i != json["output"].end(); ++i)
|
for (auto i = json["output"].begin(); i != json["output"].end(); ++i)
|
||||||
scenes.push_back(Parser::getScene(*i));
|
scenes.push_back(Parser::getScene(*i));
|
||||||
|
@ -23,42 +44,6 @@ void RayTracer::parse() {
|
||||||
lights.push_back(Parser::getLight(*i));
|
lights.push_back(Parser::getLight(*i));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ray getRay(int x, int y, const Vector3f &upperLeft, const Vector3f &du,
|
|
||||||
const Vector3f &dv) {
|
|
||||||
Vector3f camPos = Scene::current->center();
|
|
||||||
return Ray(camPos, upperLeft + x * du + y * dv - camPos);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ray getRay(int x, int y, int i, int j, const Vector3f &upperLeft,
|
|
||||||
const Vector3f &du, const Vector3f &gdu, const Vector3f &dv,
|
|
||||||
const Vector3f &gdv) {
|
|
||||||
Vector3f camPos = Scene::current->center();
|
|
||||||
return Ray(camPos, upperLeft + x * du + i * gdu + y * dv + j * gdv - camPos);
|
|
||||||
}
|
|
||||||
|
|
||||||
void writeColor(int i, const Vector3f &color) {
|
|
||||||
Output::current->r(i, color.x());
|
|
||||||
Output::current->g(i, color.y());
|
|
||||||
Output::current->b(i, color.z());
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector3f RayTracer::calculateColor(const HitRecord &hit, int i) const {
|
|
||||||
Vector3f result(0, 0, 0);
|
|
||||||
for (auto light : lights)
|
|
||||||
result += light->isUse() ? light->illumination(hit, geometries)
|
|
||||||
: Vector3f::Zero();
|
|
||||||
|
|
||||||
return result.cwiseMax(0.0f).cwiseMin(1.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
int getGridWidth(Eigen::VectorXi);
|
|
||||||
|
|
||||||
int getGridHeight(Eigen::VectorXi);
|
|
||||||
|
|
||||||
int getRayNumber(Eigen::VectorXi);
|
|
||||||
|
|
||||||
Vector3f trace() { return Vector3f::Zero(); }
|
|
||||||
|
|
||||||
void RayTracer::render() {
|
void RayTracer::render() {
|
||||||
int width = Scene::current->width();
|
int width = Scene::current->width();
|
||||||
int height = Scene::current->height();
|
int height = Scene::current->height();
|
||||||
|
@ -77,7 +62,7 @@ void RayTracer::render() {
|
||||||
|
|
||||||
Output::current = new Output(Scene::current->name(), width, height);
|
Output::current = new Output(Scene::current->name(), width, height);
|
||||||
|
|
||||||
Eigen::VectorXi data = Scene::current->raysPerPixel();
|
VectorXi data = Scene::current->raysPerPixel();
|
||||||
int gridWidth = getGridWidth(data);
|
int gridWidth = getGridWidth(data);
|
||||||
int gridHeight = getGridHeight(data);
|
int gridHeight = getGridHeight(data);
|
||||||
int raysPerPixel = getRayNumber(data);
|
int raysPerPixel = getRayNumber(data);
|
||||||
|
@ -123,26 +108,45 @@ void RayTracer::render() {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RayTracer::run() {
|
Vector3f RayTracer::calculateColor(const HitRecord &hit, int i) const {
|
||||||
parse();
|
Vector3f result(0, 0, 0);
|
||||||
|
for (auto light : lights)
|
||||||
|
result += light->isUse() ? light->illumination(hit, geometries)
|
||||||
|
: Vector3f::Zero();
|
||||||
|
|
||||||
for (auto scene : scenes) {
|
return result.cwiseMax(0.0f).cwiseMin(1.0f);
|
||||||
Scene::current = scene;
|
|
||||||
render();
|
|
||||||
Output::current->write();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// helper functions
|
// helper functions
|
||||||
|
int getGridWidth(VectorXi data) {
|
||||||
int getGridWidth(Eigen::VectorXi data) {
|
|
||||||
return data.size() != 2 && data.size() != 3 ? 1 : data.x();
|
return data.size() != 2 && data.size() != 3 ? 1 : data.x();
|
||||||
}
|
}
|
||||||
|
|
||||||
int getGridHeight(Eigen::VectorXi data) {
|
int getGridHeight(VectorXi data) {
|
||||||
return data.size() == 2 ? data.x() : (data.size() == 3 ? data.y() : 1);
|
return data.size() == 2 ? data.x() : (data.size() == 3 ? data.y() : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int getRayNumber(Eigen::VectorXi data) {
|
int getRayNumber(VectorXi data) {
|
||||||
return data.size() == 2 ? data.y() : (data.size() == 3 ? data.z() : 1);
|
return data.size() == 2 ? data.y() : (data.size() == 3 ? data.z() : 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ray getRay(int x, int y, const Vector3f &upperLeft, const Vector3f &du,
|
||||||
|
const Vector3f &dv) {
|
||||||
|
Vector3f camPos = Scene::current->center();
|
||||||
|
return Ray(camPos, upperLeft + x * du + y * dv - camPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ray getRay(int x, int y, int i, int j, const Vector3f &upperLeft,
|
||||||
|
const Vector3f &du, const Vector3f &gdu, const Vector3f &dv,
|
||||||
|
const Vector3f &gdv) {
|
||||||
|
Vector3f camPos = Scene::current->center();
|
||||||
|
return Ray(camPos, upperLeft + x * du + i * gdu + y * dv + j * gdv - camPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeColor(int i, const Vector3f &color) {
|
||||||
|
Output::current->r(i, color.x());
|
||||||
|
Output::current->g(i, color.y());
|
||||||
|
Output::current->b(i, color.z());
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3f trace() { return Vector3f::Zero(); }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue