Skip to content

Commit

Permalink
add emissive materials and cornell box scene
Browse files Browse the repository at this point in the history
  • Loading branch information
PoneyUHC committed Sep 1, 2024
1 parent e6489e0 commit 4f3c0e3
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 18 deletions.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
- centralize asset loading in a resource manager
- find a good name for the project
- have a format describing scenes instead of code description
- add log system
- add log system
- add render queue
6 changes: 5 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "scenes/checkered_spheres_scene.hpp"
#include "scenes/solar_system_scene.hpp"
#include "scenes/quads_scene.hpp"
#include "scenes/cornell_box_scene.hpp"
#include "renderer/camera.hpp"
#include "export/png_exporter.hpp"

Expand All @@ -29,7 +30,7 @@ int main(int argc, char *argv[]){

unique_ptr<IScene> scene;

switch(4){
switch(5){
case 1:
scene = make_unique<SparsedSpheresScene>();
break;
Expand All @@ -42,6 +43,9 @@ int main(int argc, char *argv[]){
case 4:
scene = make_unique<QuadsScene>();
break;
case 5:
scene = make_unique<CornellBoxScene>();
break;
default:
cout << "Problem in scene selection" << endl;
return 1;
Expand Down
6 changes: 6 additions & 0 deletions src/material/IMaterial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class IMaterial {
public:

virtual ~IMaterial() {}

virtual bool Scatter(
const Ray& incoming_ray,
const HitRecord& rec,
Expand All @@ -23,5 +24,10 @@ class IMaterial {
return false;
}

virtual RGBColor Emitted(double u, double v, const Point3& point) const
{
return BLACK;
}


};
27 changes: 27 additions & 0 deletions src/material/diffuse_light.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

#pragma once

#include <memory>

#include "material/IMaterial.hpp"
#include "texture/solid_color_texture.hpp"


class DiffuseLight : public IMaterial {

private:

std::shared_ptr<ITexture> m_albedo;


public:

DiffuseLight(const RGBColor& albedo): m_albedo{std::make_shared<SolidColorTexture>(albedo)} {};
DiffuseLight(std::shared_ptr<ITexture> albedo): m_albedo{albedo} {};

RGBColor Emitted(double u, double v, const Point3& point) const override
{
return m_albedo->GetColor(u, v, point);
}

};
4 changes: 3 additions & 1 deletion src/math/vec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,6 @@ inline Vec3 operator*(const Vec3& u, const Vec3& v) { return Vec3{u.e[0]*v.e[0],


typedef Vec3 Point3;
typedef Vec3 RGBColor;
typedef Vec3 RGBColor;
inline const RGBColor BLACK = RGBColor(0.0, 0.0, 0.0);
inline const RGBColor WHITE = RGBColor(1.0, 1.0, 1.0);
3 changes: 2 additions & 1 deletion src/renderer/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class Camera {
double m_defocus_angle;
double m_focus_dist;

Vec3 m_defocus_disk_u; // Defocus disk horizontal radius
// Defocus disk horizontal radius
Vec3 m_defocus_disk_u;
Vec3 m_defocus_disk_v;


Expand Down
31 changes: 17 additions & 14 deletions src/renderer/pathtracing_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,25 @@ void PathTracingRenderer::Render() {

RGBColor PathTracingRenderer::GetRayColor(const Ray& ray, size_t depth)
{
if(depth == 0){
return RGBColor(0, 0, 0);
if(depth <= 0){
return BLACK;
}

HitRecord hitRecord;
Interval interval = Interval(0.001, INFINITY);
if( m_scene->Hit(ray, interval, hitRecord) ){
Ray scattered_ray;
RGBColor attenuation(0, 0, 0);
if (hitRecord.material->Scatter(ray, hitRecord, attenuation, scattered_ray)){
return attenuation * GetRayColor(scattered_ray, depth-1);
}
return RGBColor(0, 0, 0);
HitRecord hit_record;
static Interval interval = Interval(0.001, INFINITY);
if(!m_scene->Hit(ray, interval, hit_record)){
return m_params.background_color;
}

Vec3 unitDirection = ray.Direction().Normalized();
double a = 0.5 * (unitDirection[1] + 1.0);
return lerp(RGBColor(1.0, 1.0, 1.0), RGBColor(0.5, 0.7, 1.0), a);
Ray scattered_ray;
RGBColor attenuation = BLACK;
RGBColor color_from_emission = hit_record.material->Emitted(hit_record.u, hit_record.v, hit_record.hit_point);

if (!hit_record.material->Scatter(ray, hit_record, attenuation, scattered_ray)){
return color_from_emission;
}

RGBColor color_from_scatter = attenuation * GetRayColor(scattered_ray, depth-1);

return color_from_emission + color_from_scatter;
}
1 change: 1 addition & 0 deletions src/renderer/pathtracing_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Ray;
struct PathTracingRendererParams {
int aa_sample_per_pixel;
int max_depth;
RGBColor background_color;
};


Expand Down
1 change: 1 addition & 0 deletions src/scenes/checkered_spheres_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ shared_ptr<PathTracingRenderer> CheckeredSpheresScene::InitRenderer()
PathTracingRendererParams params;
params.aa_sample_per_pixel = 100;
params.max_depth = 20;
params.background_color = RGBColor(0.70, 0.80, 1.00);

return make_shared<PathTracingRenderer>(m_camera, m_objets, move(params));
}
Expand Down
81 changes: 81 additions & 0 deletions src/scenes/cornell_box_scene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

#include "cornell_box_scene.hpp"

#include "geometry/hittable_list.hpp"
#include "geometry/hittable_list.hpp"
#include "geometry/quad.hpp"
#include "material/lambertian.hpp"
#include "material/diffuse_light.hpp"
#include "texture/checker_texture.hpp"
#include "renderer/camera.hpp"
#include "renderer/pathtracing_renderer.hpp"

#include <memory>


using namespace std;


shared_ptr<Camera> CornellBoxScene::InitCamera()
{
CameraParams camera_params;
camera_params.aspect_ratio = 1.0;
camera_params.image_width = m_params.render_width;
camera_params.vfov = 40.0;
camera_params.lookfrom = Point3(278, 278, -800);
camera_params.lookat = Point3(278, 278, 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> CornellBoxScene::InitObjects()
{
auto hittable_list = make_shared<HittableList>();

auto red = make_shared<Lambertian>(RGBColor(.65, .05, .05));
auto white = make_shared<Lambertian>(RGBColor(.73, .73, .73));
auto green = make_shared<Lambertian>(RGBColor(.12, .45, .15));
auto light = make_shared<DiffuseLight>(RGBColor(15, 15, 15));

hittable_list->AddObjects({
make_shared<Quad>(Point3(555,0,0), Vec3(0,555,0), Vec3(0,0,555), green),
make_shared<Quad>(Point3(0,0,0), Vec3(0,555,0), Vec3(0,0,555), red),
make_shared<Quad>(Point3(343, 554, 332), Vec3(-130,0,0), Vec3(0,0,-105), light),
make_shared<Quad>(Point3(0,0,0), Vec3(555,0,0), Vec3(0,0,555), white),
make_shared<Quad>(Point3(555,555,555), Vec3(-555,0,0), Vec3(0,0,-555), white),
make_shared<Quad>(Point3(0,0,555), Vec3(555,0,0), Vec3(0,555,0), white)
});

return hittable_list;
}


shared_ptr<PathTracingRenderer> CornellBoxScene::InitRenderer()
{
PathTracingRendererParams params;
params.aa_sample_per_pixel = 1600;
params.max_depth = 20;
params.background_color = BLACK;

return make_shared<PathTracingRenderer>(m_camera, m_objets, move(params));
}


void CornellBoxScene::Build(SceneParams &&params)
{
m_params = params;
m_camera = InitCamera();
m_objets = InitObjects();
m_renderer = InitRenderer();
}


shared_ptr<RGBColor[]> CornellBoxScene::Render()
{
m_renderer->Render();
return m_renderer->GetBuffer();
}
25 changes: 25 additions & 0 deletions src/scenes/cornell_box_scene.hpp
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 CornellBoxScene: 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;

};
1 change: 1 addition & 0 deletions src/scenes/quads_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ shared_ptr<PathTracingRenderer> QuadsScene::InitRenderer()
PathTracingRendererParams params;
params.aa_sample_per_pixel = 100;
params.max_depth = 20;
params.background_color = RGBColor(0.70, 0.80, 1.00);

return make_shared<PathTracingRenderer>(m_camera, m_objets, move(params));
}
Expand Down
1 change: 1 addition & 0 deletions src/scenes/solar_system_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ shared_ptr<PathTracingRenderer> SolarSystemScene::InitRenderer()
PathTracingRendererParams params;
params.aa_sample_per_pixel = 300;
params.max_depth = 50;
params.background_color = RGBColor(0.70, 0.80, 1.00);

return make_shared<PathTracingRenderer>(m_camera, m_objets, move(params));
}
Expand Down
1 change: 1 addition & 0 deletions src/scenes/sparsed_spheres_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ shared_ptr<PathTracingRenderer> SparsedSpheresScene::InitRenderer()
PathTracingRendererParams params;
params.aa_sample_per_pixel = 100;
params.max_depth = 20;
params.background_color = RGBColor(0.70, 0.80, 1.00);

return make_shared<PathTracingRenderer>(m_camera, m_objets, move(params));
}
Expand Down

0 comments on commit 4f3c0e3

Please sign in to comment.