This commit is contained in:
Shuo Feng 2024-03-19 18:56:58 -04:00
parent f5936b2ada
commit 05914ac768
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
3 changed files with 38 additions and 32 deletions

View file

@ -11,11 +11,9 @@ using std::vector;
class Output { class Output {
public: public:
Output(const Vector3f &bgc, string path, int w, int h) Output(string path, int w, int h)
: red(vector<float>(w * h + 1, bgc.x())), : red(vector<float>(w * h + 1)), green(vector<float>(w * h + 1)),
green(vector<float>(w * h + 1, bgc.y())), blue(vector<float>(w * h + 1)), path(path), width(w), height(h) {}
blue(vector<float>(w * h + 1, bgc.z())), path(path), width(w),
height(h) {}
void write(); void write();

View file

@ -28,16 +28,19 @@ Ray getRay(int x, int y, const Vector3f &camPos, const Vector3f &pxUpperLeft,
return Ray(camPos, pxUpperLeft + x * du + y * dv - camPos); return Ray(camPos, pxUpperLeft + x * du + y * dv - camPos);
} }
void RayTracer::calculateColor(const HitRecord &hit, int i) { 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); Vector3f result(0, 0, 0);
for (auto light : lights) for (auto light : lights)
result += light->isUse() ? light->illumination(hit, geometries) result += light->isUse() ? light->illumination(hit, geometries)
: Vector3f::Zero(); : Vector3f::Zero();
result = result.cwiseMax(0.0f).cwiseMin(1.0f); return result.cwiseMax(0.0f).cwiseMin(1.0f);
Output::current->r(i, result.x());
Output::current->g(i, result.y());
Output::current->b(i, result.z());
} }
int getGridWidth(Eigen::VectorXi data) { int getGridWidth(Eigen::VectorXi data) {
@ -52,6 +55,8 @@ int getRayNumber(Eigen::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);
} }
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();
@ -68,8 +73,7 @@ void RayTracer::render() {
Vector3f vpUpperLeft = cameraPos + lookAt - u / 2.0 - v / 2.0; Vector3f vpUpperLeft = cameraPos + lookAt - u / 2.0 - v / 2.0;
Vector3f pxUpperLeft = vpUpperLeft + (du + dv) / 2.0; Vector3f pxUpperLeft = vpUpperLeft + (du + dv) / 2.0;
Output::current = new Output(Scene::current->backgroundColor(), Output::current = new Output(Scene::current->name(), width, height);
Scene::current->name(), width, height);
Eigen::VectorXi data = Scene::current->raysPerPixel(); Eigen::VectorXi data = Scene::current->raysPerPixel();
int gridWidth = getGridWidth(data); int gridWidth = getGridWidth(data);
@ -84,13 +88,17 @@ void RayTracer::render() {
} }
for (int y = 0; y < height; ++y) { for (int y = 0; y < height; ++y) {
// print progress bar
utils::Progress::of((y + 1.0f) / height); utils::Progress::of((y + 1.0f) / height);
for (int x = 0; x < width; ++x) for (int x = 0; x < width; ++x) {
Vector3f color = Scene::current->backgroundColor();
if (Scene::current->globalIllum()) {
for (int j = 0; j < gridHeight; ++j) for (int j = 0; j < gridHeight; ++j)
for (int i = 0; i < gridWidth; ++i) { for (int i = 0; i < gridWidth; ++i) {
if (Scene::current->globalIllum()) { color = trace();
// TODO: Path tracing for global illumination }
} else { } else {
Ray ray = getRay(x, y, cameraPos, pxUpperLeft, du, dv); Ray ray = getRay(x, y, cameraPos, pxUpperLeft, du, dv);
priority_queue<HitRecord> records; priority_queue<HitRecord> records;
@ -103,12 +111,12 @@ void RayTracer::render() {
if (!records.empty()) { if (!records.empty()) {
HitRecord hit = records.top(); HitRecord hit = records.top();
hit.calcNormal(); hit.calcNormal();
calculateColor(hit, y * width + x); color = calculateColor(hit, y * width + x);
} }
} }
writeColor(y * width + x, color);
} }
} }
std::cout << std::endl; std::cout << std::endl;
} }

View file

@ -22,7 +22,7 @@ private:
std::vector<Output *> outputs; std::vector<Output *> outputs;
void parse(); void parse();
void calculateColor(const HitRecord &, int); Vector3f calculateColor(const HitRecord &, int) const;
void render(); void render();
}; };