Skip to content

Commit

Permalink
add SolidColor texture, checkered texture and a scene
Browse files Browse the repository at this point in the history
  • Loading branch information
PoneyUHC committed Aug 11, 2024
1 parent 5606611 commit de977cf
Show file tree
Hide file tree
Showing 12 changed files with 208 additions and 15 deletions.
3 changes: 3 additions & 0 deletions src/geometry/IHittable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ struct HitRecord {
std::shared_ptr<IHittable> object;
std::shared_ptr<IMaterial> material;

double u = 0.0;
double v = 0.0;


void SetFaceNormal(const Ray& ray, const Vec3& outwardNormal);
};
Expand Down
8 changes: 6 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#include "scenes/IScene.hpp"
#include "scenes/sparsed_spheres_scene.hpp"
#include "scenes/checkered_spheres_scene.hpp"
#include "renderer/camera.hpp"
#include "export/png_exporter.hpp"

Expand All @@ -26,9 +27,12 @@ int main(int argc, char *argv[]){

unique_ptr<IScene> scene;

switch(1){
switch(2){
case 1:
scene = make_unique<SparsedSphereScene>();
scene = make_unique<SparsedSpheresScene>();
break;
case 2:
scene = make_unique<CheckeredSpheresScene>();
break;
default:
cout << "Problem in scene selection" << endl;
Expand Down
2 changes: 1 addition & 1 deletion src/material/lambertian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ bool Lambertian::Scatter(
}

scattered_ray = Ray(rec.hit_point, scatter_direction);
attenuation = m_albedo;
attenuation = m_albedo->GetColor(rec.u, rec.v, rec.hit_point);
return true;
}
8 changes: 6 additions & 2 deletions src/material/lambertian.hpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@

#pragma once

#include <memory>

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


class Lambertian : public IMaterial {

private:

RGBColor m_albedo;
std::shared_ptr<ITexture> m_albedo;


public:

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

virtual bool Scatter(
const Ray& incoming_ray,
Expand Down
2 changes: 1 addition & 1 deletion src/scenes/IScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class IScene {
public:

~IScene() = default;
virtual int Build(SceneParams&& params) = 0;
virtual void Build(SceneParams&& params) = 0;
virtual std::shared_ptr<RGBColor[]> Render() = 0;

const Camera* GetCamera() { return m_camera.get(); }
Expand Down
79 changes: 79 additions & 0 deletions src/scenes/checkered_spheres_scene.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

#include "checkered_spheres_scene.hpp"

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

#include <memory>


using namespace std;


shared_ptr<Camera> CheckeredSpheresScene::InitCamera()
{
CameraParams camera_params;
camera_params.aspect_ratio = 16.0 / 9.0;
camera_params.image_width = m_params.render_width;
camera_params.vfov = 20.0;
camera_params.lookfrom = Point3(13,2,3);
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> CheckeredSpheresScene::InitObjects()
{
auto hittable_list = make_shared<HittableList>();

auto checker = make_shared<CheckerTexture>(
0.32,
RGBColor(0.2, 0.3, 0.1),
RGBColor(0.9, 0.9, 0.9)
);

hittable_list->AddObject(
make_shared<Sphere>(Point3(0,-10, 0), 10, make_shared<Lambertian>(checker))
);

hittable_list->AddObject(
make_shared<Sphere>(Point3(0, 10, 0), 10, make_shared<Lambertian>(checker))
);

return hittable_list;
}


shared_ptr<PathTracingRenderer> CheckeredSpheresScene::InitRenderer()
{
PathTracingRendererParams params;
params.aa_sample_per_pixel = 100;
params.max_depth = 20;

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


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


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

};
14 changes: 7 additions & 7 deletions src/scenes/sparsed_spheres_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using namespace std;


shared_ptr<Camera> SparsedSphereScene::InitCamera()
shared_ptr<Camera> SparsedSpheresScene::InitCamera()
{
CameraParams camera_params;
camera_params.aspect_ratio = 16.0 / 9.0;
Expand All @@ -30,7 +30,7 @@ shared_ptr<Camera> SparsedSphereScene::InitCamera()
}


shared_ptr<HittableList> SparsedSphereScene::InitObjects()
shared_ptr<HittableList> SparsedSpheresScene::InitObjects()
{
auto hittable_list = make_shared<HittableList>();

Expand Down Expand Up @@ -100,26 +100,26 @@ shared_ptr<HittableList> SparsedSphereScene::InitObjects()
}


shared_ptr<PathTracingRenderer> SparsedSphereScene::InitRenderer()
shared_ptr<PathTracingRenderer> SparsedSpheresScene::InitRenderer()
{
PathTracingRendererParams params;
params.aa_sample_per_pixel = 100;
params.max_depth = 20;

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


int SparsedSphereScene::Build(SceneParams &&params)
void SparsedSpheresScene::Build(SceneParams &&params)
{
m_params = params;
m_camera = InitCamera();
m_objets = InitObjects();
m_renderer = InitRenderer();
return 0;
}

std::shared_ptr<RGBColor[]> SparsedSphereScene::Render()

shared_ptr<RGBColor[]> SparsedSpheresScene::Render()
{
m_renderer->Render();
return m_renderer->GetBuffer();
Expand Down
4 changes: 2 additions & 2 deletions src/scenes/sparsed_spheres_scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Camera;
class HittableList;
class PathTracingRenderer;

class SparsedSphereScene: public IScene {
class SparsedSpheresScene: public IScene {

private:

Expand All @@ -19,7 +19,7 @@ class SparsedSphereScene: public IScene {

public:

int Build(SceneParams&& params) override;
void Build(SceneParams&& params) override;
std::shared_ptr<RGBColor[]> Render() override;

};
14 changes: 14 additions & 0 deletions src/texture/ITexture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#pragma once

#include "math/vec.hpp"


class ITexture {

public:

virtual ~ITexture() = default;
virtual RGBColor GetColor(double u, double v, const Point3& point) const = 0;

};
41 changes: 41 additions & 0 deletions src/texture/checker_texture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#pragma once

#include "texture/ITexture.hpp"
#include "texture/solid_color_texture.hpp"

#include <memory>


class CheckerTexture : public ITexture {

private:

double m_inv_scale;
std::shared_ptr<ITexture> m_even_texture;
std::shared_ptr<ITexture> m_odd_texture;


public:

CheckerTexture(double scale, std::shared_ptr<ITexture> even, std::shared_ptr<ITexture> odd)
: m_inv_scale{1.0 / scale}, m_even_texture{even}, m_odd_texture{odd} {}

CheckerTexture(double scale, const RGBColor& even, const RGBColor& odd)
: m_inv_scale{1.0 / scale},
m_even_texture{std::make_shared<SolidColorTexture>(even)},
m_odd_texture{std::make_shared<SolidColorTexture>(odd)}
{}


RGBColor GetColor(double u, double v, const Point3& point) const override {
int x_value = int(std::floor(m_inv_scale * point[0]));
int y_value = int(std::floor(m_inv_scale * point[1]));
int z_value = int(std::floor(m_inv_scale * point[2]));

bool is_even = (x_value + y_value + z_value) % 2 == 0;

return is_even ? m_even_texture->GetColor(u, v, point) : m_odd_texture->GetColor(u, v, point);
}

};
23 changes: 23 additions & 0 deletions src/texture/solid_color_texture.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#pragma once

#include "texture/ITexture.hpp"


class SolidColorTexture : public ITexture {

private:

RGBColor m_albedo;


public:

SolidColorTexture(const RGBColor& color): m_albedo{color} {}
SolidColorTexture(double r, double g, double b): m_albedo{RGBColor(r, g, b)} {}

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

};

0 comments on commit de977cf

Please sign in to comment.