-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
213 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
|
||
#include "quad.hpp" | ||
|
||
#include "math/ray.hpp" | ||
|
||
|
||
using namespace std; | ||
|
||
|
||
Quad::Quad(const Point3 &q, const Vec3 &u, const Vec3 &v, shared_ptr<IMaterial> material) | ||
: m_q{q}, m_u{u}, m_v{v}, m_material{material} | ||
{ | ||
Vec3 n = m_u.Cross(m_v); | ||
m_normal = n.Normalized(); | ||
m_d = m_normal.Dot(m_q); | ||
m_w = n / n.Dot(n); | ||
|
||
AABB diag1_aabb = AABB(m_q, m_q + m_u + m_v); | ||
AABB diag2_aabb = AABB(m_q + m_u, m_q + m_v); | ||
m_aabb = AABB(diag1_aabb, diag2_aabb); | ||
} | ||
|
||
|
||
bool Quad::Hit(const Ray &ray, const Interval &interval, HitRecord &outRecord) const | ||
{ | ||
double denominator = m_normal.Dot(ray.Direction()); | ||
if(std::fabs(denominator) < 1e-8){ | ||
return false; | ||
} | ||
|
||
double t = (m_d - m_normal.Dot(ray.Origin())) / denominator; | ||
|
||
if(! interval.Contains(t)){ | ||
return false; | ||
} | ||
|
||
Vec3 intersection = ray.At(t); | ||
|
||
Vec3 plan_centered_hitpoint = intersection - m_q; | ||
double alpha = m_w.Dot(plan_centered_hitpoint.Cross(m_v)); | ||
double beta = m_w.Dot(m_u.Cross(plan_centered_hitpoint)); | ||
|
||
if(! IsInQuad(alpha, beta, outRecord)){ | ||
return false; | ||
} | ||
|
||
outRecord.t = t; | ||
outRecord.hit_point = intersection; | ||
outRecord.material = m_material; | ||
outRecord.SetFaceNormal(ray, m_normal); | ||
|
||
return true; | ||
} | ||
|
||
|
||
bool Quad::IsInQuad(double alpha, double beta, HitRecord& hit_record) const { | ||
static const Interval unit_interval = Interval(0.0, 1.0); | ||
|
||
// set it anyway to make code branchless | ||
hit_record.u = alpha; | ||
hit_record.v = beta; | ||
|
||
return unit_interval.Contains(alpha) && unit_interval.Contains(beta); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
#pragma once | ||
|
||
|
||
#include "geometry/IHittable.hpp" | ||
|
||
#include "math/vec.hpp" | ||
|
||
|
||
class Quad : public IHittable { | ||
|
||
private: | ||
|
||
Point3 m_q; | ||
Vec3 m_u; | ||
Vec3 m_v; | ||
|
||
Vec3 m_normal; | ||
// d is the intersection equation result for the plan | ||
// for a point Q on the plane, n.Q = d | ||
double m_d; | ||
// w is a constant for a given quadrilateral | ||
// It simplifies the check to know if we are inside the quad | ||
Vec3 m_w; | ||
|
||
AABB m_aabb; | ||
std::shared_ptr<IMaterial> m_material; | ||
|
||
bool IsInQuad(double alpha, double beta, HitRecord& hit_record) const; | ||
|
||
|
||
public: | ||
|
||
Quad(const Point3& q, const Vec3& u, const Vec3& v, std::shared_ptr<IMaterial> material); | ||
|
||
bool Hit(const Ray& ray, const Interval& interval, HitRecord& outRecord) const override; | ||
AABB GetAABB() const override { return m_aabb; } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
|
||
#include "quads_scene.hpp" | ||
|
||
#include "geometry/hittable_list.hpp" | ||
#include "geometry/quad.hpp" | ||
#include "material/lambertian.hpp" | ||
#include "renderer/camera.hpp" | ||
#include "renderer/pathtracing_renderer.hpp" | ||
|
||
#include <memory> | ||
|
||
|
||
using namespace std; | ||
|
||
|
||
shared_ptr<Camera> QuadsScene::InitCamera() | ||
{ | ||
CameraParams camera_params; | ||
camera_params.aspect_ratio = 1.0; | ||
camera_params.image_width = m_params.render_width; | ||
camera_params.vfov = 80.0; | ||
camera_params.lookfrom = Point3(0,0,9); | ||
camera_params.lookat = Point3(0,0,0); | ||
camera_params.vup = Vec3(0,1,0); | ||
camera_params.defocus_angle = 0.0; | ||
camera_params.focus_dist = 10.0; | ||
|
||
return make_shared<Camera>(std::move(camera_params)); | ||
} | ||
|
||
|
||
shared_ptr<HittableList> QuadsScene::InitObjects() | ||
{ | ||
auto hittable_list = make_shared<HittableList>(); | ||
|
||
// Materials | ||
auto left_red = make_shared<Lambertian>(RGBColor(1.0, 0.2, 0.2)); | ||
auto back_green = make_shared<Lambertian>(RGBColor(0.2, 1.0, 0.2)); | ||
auto right_blue = make_shared<Lambertian>(RGBColor(0.2, 0.2, 1.0)); | ||
auto upper_orange = make_shared<Lambertian>(RGBColor(1.0, 0.5, 0.0)); | ||
auto lower_teal = make_shared<Lambertian>(RGBColor(0.2, 0.8, 0.8)); | ||
|
||
// Quads | ||
hittable_list->AddObjects({ | ||
make_shared<Quad>(Point3(-3,-2, 5), Vec3(0, 0,-4), Vec3(0, 4, 0), left_red), | ||
make_shared<Quad>(Point3(-2,-2, 0), Vec3(4, 0, 0), Vec3(0, 4, 0), back_green), | ||
make_shared<Quad>(Point3( 3,-2, 1), Vec3(0, 0, 4), Vec3(0, 4, 0), right_blue), | ||
make_shared<Quad>(Point3(-2, 3, 1), Vec3(4, 0, 0), Vec3(0, 0, 4), upper_orange), | ||
make_shared<Quad>(Point3(-2,-3, 5), Vec3(4, 0, 0), Vec3(0, 0,-4), lower_teal) | ||
}); | ||
|
||
return hittable_list; | ||
} | ||
|
||
|
||
shared_ptr<PathTracingRenderer> QuadsScene::InitRenderer() | ||
{ | ||
PathTracingRendererParams params; | ||
params.aa_sample_per_pixel = 100; | ||
params.max_depth = 20; | ||
|
||
return make_shared<PathTracingRenderer>(m_camera, m_objets, move(params)); | ||
} | ||
|
||
|
||
void QuadsScene::Build(SceneParams &¶ms) | ||
{ | ||
m_params = params; | ||
m_camera = InitCamera(); | ||
m_objets = InitObjects(); | ||
m_renderer = InitRenderer(); | ||
} | ||
|
||
|
||
shared_ptr<RGBColor[]> QuadsScene::Render() | ||
{ | ||
m_renderer->Render(); | ||
return m_renderer->GetBuffer(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
#pragma once | ||
|
||
#include "scenes/IScene.hpp" | ||
|
||
|
||
class Camera; | ||
class HittableList; | ||
class PathTracingRenderer; | ||
|
||
class QuadsScene: public IScene { | ||
|
||
private: | ||
|
||
std::shared_ptr<Camera> InitCamera(); | ||
std::shared_ptr<HittableList> InitObjects(); | ||
std::shared_ptr<PathTracingRenderer> InitRenderer(); | ||
|
||
|
||
public: | ||
|
||
void Build(SceneParams&& params) override; | ||
std::shared_ptr<RGBColor[]> Render() override; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters