From 29c5fe91d99eacc05716067042fa255fa3248c3f Mon Sep 17 00:00:00 2001 From: vonhyou Date: Mon, 19 Feb 2024 01:47:35 -0500 Subject: [PATCH] impl ray casted render --- src/Geometry.cc | 4 ++-- src/Geometry.h | 8 +++++--- src/RayTracer.cc | 25 +++++++++++++++++++++++++ src/Scene.cc | 8 ++++++++ src/Scene.h | 4 ++++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/Geometry.cc b/src/Geometry.cc index 9eb69f5..379ecaa 100644 --- a/src/Geometry.cc +++ b/src/Geometry.cc @@ -4,6 +4,6 @@ void Geometry::setTransform(const Matrix4f &transform) { this->transform = transform; } -bool Sphere::intersect() const { return false; } +bool Sphere::intersect(const Ray &r) const { return false; } -bool Rectangle::intersect() const { return false; } +bool Rectangle::intersect(const Ray &r) const { return false; } diff --git a/src/Geometry.h b/src/Geometry.h index b262b11..7889796 100644 --- a/src/Geometry.h +++ b/src/Geometry.h @@ -1,6 +1,8 @@ #ifndef GEOMETRY_H_ #define GEOMETRY_H_ +#include "Ray.h" + #include using Eigen::Matrix; @@ -13,7 +15,7 @@ public: enum class Type { SPHERE, RECTANGLE }; virtual ~Geometry() = default; - virtual bool intersect() const = 0; + virtual bool intersect(const Ray &) const = 0; protected: Geometry(Type type, float ka, float kd, float ks, const Vector3f &ca, @@ -37,7 +39,7 @@ public: : Geometry(Type::SPHERE, ka, kd, ks, ca, cd, cs, pc), radius(radius), center(center) {} - bool intersect() const override; + bool intersect(const Ray &) const override; private: float radius; @@ -51,7 +53,7 @@ public: : Geometry(Type::RECTANGLE, ka, kd, ks, ca, cd, cs, pc), corners(corners) {} - bool intersect() const override; + bool intersect(const Ray &) const override; private: Matrix corners; diff --git a/src/RayTracer.cc b/src/RayTracer.cc index a2a0eb3..0515d1a 100644 --- a/src/RayTracer.cc +++ b/src/RayTracer.cc @@ -1,6 +1,7 @@ #include "RayTracer.h" #include "../external/simpleppm.h" #include "Parser.h" +#include "Ray.h" void RayTracer::parse() { for (auto i = json["output"].begin(); i != json["output"].end(); ++i) @@ -13,10 +14,34 @@ void RayTracer::parse() { lights.push_back(Parser::getLight(*i)); } +Ray getRay(int x, int y, const Vector3f &camPos, const Vector3f &lookat, + float fov, int width, int height) { + // TODO: compute ray + return Ray(Vector3f(), Vector3f()); +} + void RayTracer::render(Scene *scene) { int width = scene->getWidth(); int height = scene->getHeight(); + float fov = scene->getFov(); + Vector3f cameraPos = scene->getCenter(); + Vector3f lookAt = scene->getLookAt(); + Vector3f up = scene->getUpVector(); + Buffer buffer(width * height * 3); + for (int y = 0; y < height; ++y) + for (int x = 0; x < width; ++x) { + Ray ray = getRay(x, y, cameraPos, lookAt, fov, width, height); + + for (auto geometry : geometries) + if (geometry->intersect(ray)) { + buffer[3 * y * width + 3 * x + 0] = 1; + buffer[3 * y * width + 3 * x + 1] = 1; + buffer[3 * y * width + 3 * x + 2] = 1; + break; + } + } + Task *task = new Task(scene, buffer); tasks.push_back(task); } diff --git a/src/Scene.cc b/src/Scene.cc index 5cf2980..8533ff3 100644 --- a/src/Scene.cc +++ b/src/Scene.cc @@ -6,6 +6,14 @@ int Scene::getWidth() { return width; } int Scene::getHeight() { return height; } +float Scene::getFov() { return fov; } + +Vector3f Scene::getCenter() const { return center; } + +Vector3f Scene::getUpVector() const { return up; } + +Vector3f Scene::getLookAt() const { return lookAt; } + void Scene::setRaysPerPixel(const Eigen::VectorXi &raysPerPixel) { this->raysPerPixel = raysPerPixel; } diff --git a/src/Scene.h b/src/Scene.h index b1a15b5..24a697e 100644 --- a/src/Scene.h +++ b/src/Scene.h @@ -35,6 +35,10 @@ public: string getName() const; int getWidth(); int getHeight(); + float getFov(); + Vector3f getCenter() const; + Vector3f getUpVector() const; + Vector3f getLookAt() const; void setRaysPerPixel(const Eigen::VectorXi &); void setAntialiasing(bool); void setTwoSideRender(bool);