From 48f00d194fea83de6626d9ad7b1002d6d3e45fdb Mon Sep 17 00:00:00 2001 From: vonhyou Date: Tue, 13 Feb 2024 21:40:56 -0500 Subject: [PATCH 01/11] design parser class --- src/Parser.h | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Parser.h b/src/Parser.h index 709f386..bbf1d64 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -1,6 +1,23 @@ #ifndef PARSER_H_ #define PARSER_H_ -class Parser {}; +#include "../external/json.hpp" + +#include "Geometry.h" +#include "Light.h" +#include "Scene.h" + +class Parser { +public: + static Geometry *parseGeometry(const nlohmann::json &); + static Light *parseLight(const nlohmann::json &); + static Scene *parseScene(const nlohmann::json &); + +private: + static PointLight *parsePointLight(const nlohmann::json &); + static AreaLight *parseAreaLight(const nlohmann::json &); + static Rectangle *parseRectangle(const nlohmann::json &); + static Sphere *parseSphere(const nlohmann::json &); +}; #endif // !PARSER_H_ From fd2b57b4aee5ace247e05239105decb1066b9752 Mon Sep 17 00:00:00 2001 From: vonhyou Date: Tue, 13 Feb 2024 22:17:27 -0500 Subject: [PATCH 02/11] rename method names --- src/Parser.cc | 6 ++++++ src/Parser.h | 14 +++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 src/Parser.cc diff --git a/src/Parser.cc b/src/Parser.cc new file mode 100644 index 0000000..783dea6 --- /dev/null +++ b/src/Parser.cc @@ -0,0 +1,6 @@ +#include "Parser.h" +#include + +static Scene *parseScene(const nlohmann::json &j) { + string name = j["filename"].get(); +} diff --git a/src/Parser.h b/src/Parser.h index bbf1d64..1df3307 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -9,15 +9,15 @@ class Parser { public: - static Geometry *parseGeometry(const nlohmann::json &); - static Light *parseLight(const nlohmann::json &); - static Scene *parseScene(const nlohmann::json &); + static Geometry *getGeometry(const nlohmann::json &); + static Light *getLight(const nlohmann::json &); + static Scene *getScene(const nlohmann::json &); private: - static PointLight *parsePointLight(const nlohmann::json &); - static AreaLight *parseAreaLight(const nlohmann::json &); - static Rectangle *parseRectangle(const nlohmann::json &); - static Sphere *parseSphere(const nlohmann::json &); + static PointLight *getPointLight(const nlohmann::json &); + static AreaLight *getAreaLight(const nlohmann::json &); + static Rectangle *getRectangle(const nlohmann::json &); + static Sphere *getSphere(const nlohmann::json &); }; #endif // !PARSER_H_ From e7b7d94cf6a1f2843c7f59f53414117fd791dbff Mon Sep 17 00:00:00 2001 From: vonhyou Date: Tue, 13 Feb 2024 22:17:49 -0500 Subject: [PATCH 03/11] finish parse scene --- src/Parser.cc | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/Parser.cc b/src/Parser.cc index 783dea6..5acd860 100644 --- a/src/Parser.cc +++ b/src/Parser.cc @@ -1,6 +1,45 @@ #include "Parser.h" + +#include #include -static Scene *parseScene(const nlohmann::json &j) { - string name = j["filename"].get(); +using Eigen::Vector3f; +using Eigen::VectorXi; +using std::string; + +// a helper function to get Vector3f +const Vector3f getVector3f(const nlohmann::json &j) { + return Vector3f(j[0].get(), j[1].get(), j[2].get()); +} + +// A helper function to get raysperpixel array +const VectorXi getRpp(const nlohmann::json &j) { + VectorXi rpp(j.size()); + + for (int i = 0; i < j.size(); ++i) { + rpp[i] = j[i].get(); + } + + return rpp; +} + +Scene *Parser::getScene(const nlohmann::json &j) { + string name = j["filename"].get(); + int width = j["size"][0].get(); + int height = j["size"][1].get(); + float fov = j["fov"].get(); + Vector3f lookAt = getVector3f(j["lookat"]); + Vector3f up = getVector3f(j["up"]); + Vector3f center = getVector3f(j["center"]); + Vector3f ai = getVector3f(j["ai"]); + Vector3f bgc = getVector3f(j["bkc"]); + + Scene *sc = new Scene(name, width, height, fov, center, up, lookAt, ai, bgc); + sc->setAntialiasing(j.value("antialiasing", false)); + sc->setTwoSideRender(j.value("twosiderender", false)); + sc->setGlobalIllum(j.value("globalillum", false)); + if (j.contains("raysperpixel")) + sc->setRaysPerPixel(getRpp(j["raysperpixel"])); + + return sc; } From e308c901f577860ed8f45548b3bdce36d311a065 Mon Sep 17 00:00:00 2001 From: vonhyou Date: Tue, 13 Feb 2024 22:35:34 -0500 Subject: [PATCH 04/11] bugfix: center and centre --- src/Parser.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parser.cc b/src/Parser.cc index 5acd860..60e9e2b 100644 --- a/src/Parser.cc +++ b/src/Parser.cc @@ -30,7 +30,7 @@ Scene *Parser::getScene(const nlohmann::json &j) { float fov = j["fov"].get(); Vector3f lookAt = getVector3f(j["lookat"]); Vector3f up = getVector3f(j["up"]); - Vector3f center = getVector3f(j["center"]); + Vector3f center = getVector3f(j["centre"]); Vector3f ai = getVector3f(j["ai"]); Vector3f bgc = getVector3f(j["bkc"]); From 2f4d439e97e1415df9836efe305164762d2f4c66 Mon Sep 17 00:00:00 2001 From: vonhyou Date: Tue, 13 Feb 2024 22:36:04 -0500 Subject: [PATCH 05/11] update RT class --- src/RayTracer.cc | 9 ++++++--- src/RayTracer.h | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/RayTracer.cc b/src/RayTracer.cc index cf70d8a..e3e225e 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -1,6 +1,9 @@ #include "RayTracer.h" #include "../external/simpleppm.h" +#include "Parser.h" +#include "Scene.h" -RayTracer::RayTracer(const nlohmann::json &json) : json(json) {} - -void RayTracer::run() {} +void RayTracer::run() { + nlohmann::json sceneJson = this->json["output"][0]; + Scene *sc = Parser::getScene(sceneJson); +} diff --git a/src/RayTracer.h b/src/RayTracer.h index 0975344..d356c0e 100644 --- a/src/RayTracer.h +++ b/src/RayTracer.h @@ -5,7 +5,7 @@ class RayTracer { public: - RayTracer(const nlohmann::json &); + RayTracer(const nlohmann::json &j) : json(j) {} void run(); private: From 03b6bbbb8ef655d598d081d2392e22a6773158c4 Mon Sep 17 00:00:00 2001 From: vonhyou Date: Tue, 13 Feb 2024 23:21:42 -0500 Subject: [PATCH 06/11] parse scenes --- src/RayTracer.cc | 16 ++++++++++++---- src/RayTracer.h | 8 ++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/RayTracer.cc b/src/RayTracer.cc index e3e225e..d505d56 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -1,9 +1,17 @@ #include "RayTracer.h" -#include "../external/simpleppm.h" #include "Parser.h" -#include "Scene.h" + +void RayTracer::parse() { + for (auto i = json["output"].begin(); i != json["output"].end(); ++i) + scenes.push_back(Parser::getScene(*i)); +} + +void RayTracer::render() {} + +void RayTracer::output() {} void RayTracer::run() { - nlohmann::json sceneJson = this->json["output"][0]; - Scene *sc = Parser::getScene(sceneJson); + parse(); + render(); + output(); } diff --git a/src/RayTracer.h b/src/RayTracer.h index d356c0e..427a469 100644 --- a/src/RayTracer.h +++ b/src/RayTracer.h @@ -2,6 +2,9 @@ #define RAY_TRACER_H_ #include "../external/json.hpp" +#include "Scene.h" + +#include class RayTracer { public: @@ -10,6 +13,11 @@ public: private: nlohmann::json json; + std::vector scenes; + + void parse(); + void render(); + void output(); }; #endif // !RAY_TRACER_H_ From 7d273f9420143a2b2519a58fbb17a31cbe6f0f0b Mon Sep 17 00:00:00 2001 From: vonhyou Date: Tue, 13 Feb 2024 23:45:29 -0500 Subject: [PATCH 07/11] parse and write scene --- src/RayTracer.cc | 14 +++++++++++++- src/Scene.cc | 6 ++++++ src/Scene.h | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/RayTracer.cc b/src/RayTracer.cc index d505d56..f2b26b4 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -1,6 +1,11 @@ #include "RayTracer.h" +#include "../external/simpleppm.h" #include "Parser.h" +#include + +using std::vector; + void RayTracer::parse() { for (auto i = json["output"].begin(); i != json["output"].end(); ++i) scenes.push_back(Parser::getScene(*i)); @@ -8,7 +13,14 @@ void RayTracer::parse() { void RayTracer::render() {} -void RayTracer::output() {} +void RayTracer::output() { + for (auto scene : scenes) { + int width = scene->getWidth(); + int height = scene->getHeight(); + vector buffer(3 * width * height); + save_ppm("build/" + scene->getName(), buffer, width, height); + } +} void RayTracer::run() { parse(); diff --git a/src/Scene.cc b/src/Scene.cc index 5bec7c5..ea22c48 100644 --- a/src/Scene.cc +++ b/src/Scene.cc @@ -1,5 +1,11 @@ #include "Scene.h" +string Scene::getName() { return name; } + +int Scene::getWidth() { return width; } + +int Scene::getHeight() { return height; } + void Scene::setRaysPerPixel(const Eigen::VectorXi &raysPerPixel) { this->raysPerPixel = raysPerPixel; } diff --git a/src/Scene.h b/src/Scene.h index 0411791..74ec55f 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -32,6 +32,9 @@ private: bool globalIllum = false; public: + string getName(); + int getWidth(); + int getHeight(); void setRaysPerPixel(const Eigen::VectorXi &); void setAntialiasing(bool); void setTwoSideRender(bool); From a42eae91ded38d33230370e9180b95d3f623f473 Mon Sep 17 00:00:00 2001 From: vonhyou Date: Thu, 15 Feb 2024 17:12:41 -0500 Subject: [PATCH 08/11] remove redundant path --- src/RayTracer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RayTracer.cc b/src/RayTracer.cc index f2b26b4..3f04ca6 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -18,7 +18,7 @@ void RayTracer::output() { int width = scene->getWidth(); int height = scene->getHeight(); vector buffer(3 * width * height); - save_ppm("build/" + scene->getName(), buffer, width, height); + save_ppm(scene->getName(), buffer, width, height); } } From 9f63434b12e97fa442d92cb003557b7d22c7e85d Mon Sep 17 00:00:00 2001 From: vonhyou Date: Thu, 15 Feb 2024 18:02:19 -0500 Subject: [PATCH 09/11] add geometry parser --- src/Parser.cc | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Parser.h | 8 ++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Parser.cc b/src/Parser.cc index 60e9e2b..259d1d4 100644 --- a/src/Parser.cc +++ b/src/Parser.cc @@ -1,4 +1,5 @@ #include "Parser.h" +#include "Geometry.h" #include #include @@ -43,3 +44,52 @@ Scene *Parser::getScene(const nlohmann::json &j) { return sc; } + +// TODO: Get Matrix4f transform for geometries and lights +const Matrix4f getTransform(const nlohmann::json &j) { return Matrix4f(); } + +Geometry *Parser::getGeometry(const nlohmann::json &j) { + float ka = j["ka"].get(); + float kd = j["kd"].get(); + float ks = j["ks"].get(); + Vector3f ca = getVector3f(j["ac"]); + Vector3f cd = getVector3f(j["dc"]); + Vector3f cs = getVector3f(j["sc"]); + float phong = j["pc"].get(); + + Geometry *g; + + if (j["type"].get().compare("rectangle")) + g = getSphere(j, ka, kd, ks, ca, cd, cs, phong); + else + g = getRectangle(j, ka, kd, ks, ca, cd, cs, phong); + + if (j.contains("transform")) { + g->setTransform(getTransform(j["transform"])); + } + + return g; +} + +// TODO: return four corners for rectangles and area lights +const Matrix getCorners(const nlohmann::json &j) { + return Matrix(); +} + +Sphere *Parser::getSphere(const nlohmann::json &j, float ka, float kd, float ks, + const Vector3f &ca, const Vector3f &cd, + const Vector3f &cs, float pc) { + float radius = j["radius"].get(); + Vector3f center = getVector3f(j["centre"]); + + return new Sphere(ka, kd, ks, ca, cd, cs, pc, radius, center); +} + +Rectangle *Parser::getRectangle(const nlohmann::json &j, float ka, float kd, + float ks, const Vector3f &ca, + const Vector3f &cd, const Vector3f &cs, + float pc) { + Matrix corners = getCorners(j); + + return new Rectangle(ka, kd, ks, ca, cd, cs, pc, corners); +} diff --git a/src/Parser.h b/src/Parser.h index 1df3307..6b3b3d4 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -16,8 +16,12 @@ public: private: static PointLight *getPointLight(const nlohmann::json &); static AreaLight *getAreaLight(const nlohmann::json &); - static Rectangle *getRectangle(const nlohmann::json &); - static Sphere *getSphere(const nlohmann::json &); + static Rectangle *getRectangle(const nlohmann::json &, float, float, float, + const Vector3f &, const Vector3f &, + const Vector3f &, float); + static Sphere *getSphere(const nlohmann::json &, float, float, float, + const Vector3f &, const Vector3f &, const Vector3f &, + float); }; #endif // !PARSER_H_ From 73b90f7535b689eb508a503d0cd7dc4fbee09efb Mon Sep 17 00:00:00 2001 From: vonhyou Date: Sun, 18 Feb 2024 20:21:43 -0500 Subject: [PATCH 10/11] add geometry parser --- src/Parser.cc | 8 ++++++-- src/RayTracer.cc | 3 +++ src/RayTracer.h | 4 ++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Parser.cc b/src/Parser.cc index 259d1d4..7dd1b76 100644 --- a/src/Parser.cc +++ b/src/Parser.cc @@ -71,9 +71,13 @@ Geometry *Parser::getGeometry(const nlohmann::json &j) { return g; } -// TODO: return four corners for rectangles and area lights +// helper function to get four corners of a rectangle const Matrix getCorners(const nlohmann::json &j) { - return Matrix(); + Matrix corners; + for (int i = 0; i < 4; ++i) { + corners.col(i) = getVector3f(j["p" + std::to_string(i + 1)]); + } + return corners; } Sphere *Parser::getSphere(const nlohmann::json &j, float ka, float kd, float ks, diff --git a/src/RayTracer.cc b/src/RayTracer.cc index 3f04ca6..b130c70 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -9,6 +9,9 @@ using std::vector; void RayTracer::parse() { for (auto i = json["output"].begin(); i != json["output"].end(); ++i) scenes.push_back(Parser::getScene(*i)); + + for (auto i = json["geometry"].begin(); i != json["geometry"].end(); ++i) + geometries.push_back(Parser::getGeometry(*i)); } void RayTracer::render() {} diff --git a/src/RayTracer.h b/src/RayTracer.h index 427a469..1c18f19 100644 --- a/src/RayTracer.h +++ b/src/RayTracer.h @@ -2,6 +2,8 @@ #define RAY_TRACER_H_ #include "../external/json.hpp" +#include "Geometry.h" +#include "Light.h" #include "Scene.h" #include @@ -14,6 +16,8 @@ public: private: nlohmann::json json; std::vector scenes; + std::vector lights; + std::vector geometries; void parse(); void render(); From e13fd8a438c7e406399d5215f035ff1c94d5fb76 Mon Sep 17 00:00:00 2001 From: vonhyou Date: Sun, 18 Feb 2024 20:52:59 -0500 Subject: [PATCH 11/11] add light parser --- src/Light.cc | 4 ++++ src/Parser.cc | 35 +++++++++++++++++++++++++++++++++++ src/Parser.h | 6 ++++-- src/RayTracer.cc | 3 +++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/Light.cc b/src/Light.cc index dddb575..07d7c27 100644 --- a/src/Light.cc +++ b/src/Light.cc @@ -7,3 +7,7 @@ void Light::setTransform(const Matrix4f &transform) { void Light::setGridSize(unsigned int gridSize) { this->gridSize = gridSize; } void Light::setUseCenter(bool useCenter) { this->useCenter = useCenter; } + +void PointLight::illumination() const {} + +void AreaLight::illumination() const {} diff --git a/src/Parser.cc b/src/Parser.cc index 7dd1b76..69fabb3 100644 --- a/src/Parser.cc +++ b/src/Parser.cc @@ -1,5 +1,6 @@ #include "Parser.h" #include "Geometry.h" +#include "Light.h" #include #include @@ -97,3 +98,37 @@ Rectangle *Parser::getRectangle(const nlohmann::json &j, float ka, float kd, return new Rectangle(ka, kd, ks, ca, cd, cs, pc, corners); } + +Light *Parser::getLight(const nlohmann::json &j) { + Vector3f id = getVector3f(j["id"]); + Vector3f is = getVector3f(j["is"]); + + Light *l; + if (j["type"].get().compare("point")) + l = getAreaLight(j, id, is); + else + l = getPointLight(j, id, is); + + if (j.contains("transform")) { + l->setTransform(getTransform(j["transform"])); + } + + l->setGridSize(j.value("n", 0)); + l->setUseCenter(j.value("usecenter", false)); + + return l; +} + +AreaLight *Parser::getAreaLight(const nlohmann::json &j, const Vector3f &id, + const Vector3f &is) { + Matrix corners = getCorners(j); + + return new AreaLight(id, is, corners); +} + +PointLight *Parser::getPointLight(const nlohmann::json &j, const Vector3f &id, + const Vector3f &is) { + Vector3f center = getVector3f(j["centre"]); + + return new PointLight(id, is, center); +} diff --git a/src/Parser.h b/src/Parser.h index 6b3b3d4..f82c63c 100644 --- a/src/Parser.h +++ b/src/Parser.h @@ -14,8 +14,10 @@ public: static Scene *getScene(const nlohmann::json &); private: - static PointLight *getPointLight(const nlohmann::json &); - static AreaLight *getAreaLight(const nlohmann::json &); + static PointLight *getPointLight(const nlohmann::json &, const Vector3f &, + const Vector3f &); + static AreaLight *getAreaLight(const nlohmann::json &, const Vector3f &, + const Vector3f &); static Rectangle *getRectangle(const nlohmann::json &, float, float, float, const Vector3f &, const Vector3f &, const Vector3f &, float); diff --git a/src/RayTracer.cc b/src/RayTracer.cc index b130c70..2cb39a1 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -12,6 +12,9 @@ void RayTracer::parse() { for (auto i = json["geometry"].begin(); i != json["geometry"].end(); ++i) geometries.push_back(Parser::getGeometry(*i)); + + for (auto i = json["light"].begin(); i != json["light"].end(); ++i) + lights.push_back(Parser::getLight(*i)); } void RayTracer::render() {}