impl ray casted render

This commit is contained in:
Shuo Feng 2024-02-19 01:47:35 -05:00
parent ed42a0f9ca
commit 29c5fe91d9
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
5 changed files with 44 additions and 5 deletions

View file

@ -4,6 +4,6 @@ void Geometry::setTransform(const Matrix4f &transform) {
this->transform = 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; }

View file

@ -1,6 +1,8 @@
#ifndef GEOMETRY_H_ #ifndef GEOMETRY_H_
#define GEOMETRY_H_ #define GEOMETRY_H_
#include "Ray.h"
#include <Eigen/Core> #include <Eigen/Core>
using Eigen::Matrix; using Eigen::Matrix;
@ -13,7 +15,7 @@ public:
enum class Type { SPHERE, RECTANGLE }; enum class Type { SPHERE, RECTANGLE };
virtual ~Geometry() = default; virtual ~Geometry() = default;
virtual bool intersect() const = 0; virtual bool intersect(const Ray &) const = 0;
protected: protected:
Geometry(Type type, float ka, float kd, float ks, const Vector3f &ca, 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), : Geometry(Type::SPHERE, ka, kd, ks, ca, cd, cs, pc), radius(radius),
center(center) {} center(center) {}
bool intersect() const override; bool intersect(const Ray &) const override;
private: private:
float radius; float radius;
@ -51,7 +53,7 @@ public:
: Geometry(Type::RECTANGLE, ka, kd, ks, ca, cd, cs, pc), : Geometry(Type::RECTANGLE, ka, kd, ks, ca, cd, cs, pc),
corners(corners) {} corners(corners) {}
bool intersect() const override; bool intersect(const Ray &) const override;
private: private:
Matrix<float, 3, 4> corners; Matrix<float, 3, 4> corners;

View file

@ -1,6 +1,7 @@
#include "RayTracer.h" #include "RayTracer.h"
#include "../external/simpleppm.h" #include "../external/simpleppm.h"
#include "Parser.h" #include "Parser.h"
#include "Ray.h"
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)
@ -13,10 +14,34 @@ void RayTracer::parse() {
lights.push_back(Parser::getLight(*i)); 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) { void RayTracer::render(Scene *scene) {
int width = scene->getWidth(); int width = scene->getWidth();
int height = scene->getHeight(); 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); 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); Task *task = new Task(scene, buffer);
tasks.push_back(task); tasks.push_back(task);
} }

View file

@ -6,6 +6,14 @@ int Scene::getWidth() { return width; }
int Scene::getHeight() { return height; } 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) { void Scene::setRaysPerPixel(const Eigen::VectorXi &raysPerPixel) {
this->raysPerPixel = raysPerPixel; this->raysPerPixel = raysPerPixel;
} }

View file

@ -35,6 +35,10 @@ public:
string getName() const; string getName() const;
int getWidth(); int getWidth();
int getHeight(); int getHeight();
float getFov();
Vector3f getCenter() const;
Vector3f getUpVector() const;
Vector3f getLookAt() const;
void setRaysPerPixel(const Eigen::VectorXi &); void setRaysPerPixel(const Eigen::VectorXi &);
void setAntialiasing(bool); void setAntialiasing(bool);
void setTwoSideRender(bool); void setTwoSideRender(bool);