From de977cf9d65eba7d1a1c53d30221a01a50a4d192 Mon Sep 17 00:00:00 2001 From: PoneyUHC Date: Sun, 11 Aug 2024 23:46:27 +0200 Subject: [PATCH] add SolidColor texture, checkered texture and a scene --- src/geometry/IHittable.hpp | 3 + src/main.cpp | 8 ++- src/material/lambertian.cpp | 2 +- src/material/lambertian.hpp | 8 ++- src/scenes/IScene.hpp | 2 +- src/scenes/checkered_spheres_scene.cpp | 79 ++++++++++++++++++++++++++ src/scenes/checkered_spheres_scene.hpp | 25 ++++++++ src/scenes/sparsed_spheres_scene.cpp | 14 ++--- src/scenes/sparsed_spheres_scene.hpp | 4 +- src/texture/ITexture.hpp | 14 +++++ src/texture/checker_texture.hpp | 41 +++++++++++++ src/texture/solid_color_texture.hpp | 23 ++++++++ 12 files changed, 208 insertions(+), 15 deletions(-) create mode 100644 src/scenes/checkered_spheres_scene.cpp create mode 100644 src/scenes/checkered_spheres_scene.hpp create mode 100644 src/texture/ITexture.hpp create mode 100644 src/texture/checker_texture.hpp create mode 100644 src/texture/solid_color_texture.hpp diff --git a/src/geometry/IHittable.hpp b/src/geometry/IHittable.hpp index 1d68d3e..5c77a52 100644 --- a/src/geometry/IHittable.hpp +++ b/src/geometry/IHittable.hpp @@ -22,6 +22,9 @@ struct HitRecord { std::shared_ptr object; std::shared_ptr material; + double u = 0.0; + double v = 0.0; + void SetFaceNormal(const Ray& ray, const Vec3& outwardNormal); }; diff --git a/src/main.cpp b/src/main.cpp index 864c06c..7180297 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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" @@ -26,9 +27,12 @@ int main(int argc, char *argv[]){ unique_ptr scene; - switch(1){ + switch(2){ case 1: - scene = make_unique(); + scene = make_unique(); + break; + case 2: + scene = make_unique(); break; default: cout << "Problem in scene selection" << endl; diff --git a/src/material/lambertian.cpp b/src/material/lambertian.cpp index 5838576..e4c3ba5 100644 --- a/src/material/lambertian.cpp +++ b/src/material/lambertian.cpp @@ -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; } \ No newline at end of file diff --git a/src/material/lambertian.hpp b/src/material/lambertian.hpp index 5e0cb62..5a26495 100644 --- a/src/material/lambertian.hpp +++ b/src/material/lambertian.hpp @@ -1,19 +1,23 @@ #pragma once +#include + #include "material/IMaterial.hpp" +#include "texture/solid_color_texture.hpp" class Lambertian : public IMaterial { private: - RGBColor m_albedo; + std::shared_ptr m_albedo; public: - Lambertian(const RGBColor& albedo): m_albedo{albedo} {}; + Lambertian(const RGBColor& albedo): m_albedo{std::make_shared(albedo)} {}; + Lambertian(std::shared_ptr albedo): m_albedo{albedo} {}; virtual bool Scatter( const Ray& incoming_ray, diff --git a/src/scenes/IScene.hpp b/src/scenes/IScene.hpp index 9acd1a8..60ee0a0 100644 --- a/src/scenes/IScene.hpp +++ b/src/scenes/IScene.hpp @@ -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 Render() = 0; const Camera* GetCamera() { return m_camera.get(); } diff --git a/src/scenes/checkered_spheres_scene.cpp b/src/scenes/checkered_spheres_scene.cpp new file mode 100644 index 0000000..f300bfa --- /dev/null +++ b/src/scenes/checkered_spheres_scene.cpp @@ -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 + + +using namespace std; + + +shared_ptr 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(std::move(camera_params)); +} + + +shared_ptr CheckeredSpheresScene::InitObjects() +{ + auto hittable_list = make_shared(); + + auto checker = make_shared( + 0.32, + RGBColor(0.2, 0.3, 0.1), + RGBColor(0.9, 0.9, 0.9) + ); + + hittable_list->AddObject( + make_shared(Point3(0,-10, 0), 10, make_shared(checker)) + ); + + hittable_list->AddObject( + make_shared(Point3(0, 10, 0), 10, make_shared(checker)) + ); + + return hittable_list; +} + + +shared_ptr CheckeredSpheresScene::InitRenderer() +{ + PathTracingRendererParams params; + params.aa_sample_per_pixel = 100; + params.max_depth = 20; + + return make_shared(m_camera, m_objets, move(params)); +} + + +void CheckeredSpheresScene::Build(SceneParams &¶ms) +{ + m_params = params; + m_camera = InitCamera(); + m_objets = InitObjects(); + m_renderer = InitRenderer(); +} + + +shared_ptr CheckeredSpheresScene::Render() +{ + m_renderer->Render(); + return m_renderer->GetBuffer(); +} diff --git a/src/scenes/checkered_spheres_scene.hpp b/src/scenes/checkered_spheres_scene.hpp new file mode 100644 index 0000000..9a42ea5 --- /dev/null +++ b/src/scenes/checkered_spheres_scene.hpp @@ -0,0 +1,25 @@ + +#pragma once + +#include "scenes/IScene.hpp" + + +class Camera; +class HittableList; +class PathTracingRenderer; + +class CheckeredSpheresScene: public IScene { + +private: + + std::shared_ptr InitCamera(); + std::shared_ptr InitObjects(); + std::shared_ptr InitRenderer(); + + +public: + + void Build(SceneParams&& params) override; + std::shared_ptr Render() override; + +}; \ No newline at end of file diff --git a/src/scenes/sparsed_spheres_scene.cpp b/src/scenes/sparsed_spheres_scene.cpp index 68dfbbb..b8e300c 100644 --- a/src/scenes/sparsed_spheres_scene.cpp +++ b/src/scenes/sparsed_spheres_scene.cpp @@ -14,7 +14,7 @@ using namespace std; -shared_ptr SparsedSphereScene::InitCamera() +shared_ptr SparsedSpheresScene::InitCamera() { CameraParams camera_params; camera_params.aspect_ratio = 16.0 / 9.0; @@ -30,7 +30,7 @@ shared_ptr SparsedSphereScene::InitCamera() } -shared_ptr SparsedSphereScene::InitObjects() +shared_ptr SparsedSpheresScene::InitObjects() { auto hittable_list = make_shared(); @@ -100,26 +100,26 @@ shared_ptr SparsedSphereScene::InitObjects() } -shared_ptr SparsedSphereScene::InitRenderer() +shared_ptr SparsedSpheresScene::InitRenderer() { PathTracingRendererParams params; params.aa_sample_per_pixel = 100; params.max_depth = 20; - return make_shared(m_camera, m_objets, std::move(params)); + return make_shared(m_camera, m_objets, move(params)); } -int SparsedSphereScene::Build(SceneParams &¶ms) +void SparsedSpheresScene::Build(SceneParams &¶ms) { m_params = params; m_camera = InitCamera(); m_objets = InitObjects(); m_renderer = InitRenderer(); - return 0; } -std::shared_ptr SparsedSphereScene::Render() + +shared_ptr SparsedSpheresScene::Render() { m_renderer->Render(); return m_renderer->GetBuffer(); diff --git a/src/scenes/sparsed_spheres_scene.hpp b/src/scenes/sparsed_spheres_scene.hpp index ee56d01..c2e0265 100644 --- a/src/scenes/sparsed_spheres_scene.hpp +++ b/src/scenes/sparsed_spheres_scene.hpp @@ -8,7 +8,7 @@ class Camera; class HittableList; class PathTracingRenderer; -class SparsedSphereScene: public IScene { +class SparsedSpheresScene: public IScene { private: @@ -19,7 +19,7 @@ class SparsedSphereScene: public IScene { public: - int Build(SceneParams&& params) override; + void Build(SceneParams&& params) override; std::shared_ptr Render() override; }; \ No newline at end of file diff --git a/src/texture/ITexture.hpp b/src/texture/ITexture.hpp new file mode 100644 index 0000000..0783001 --- /dev/null +++ b/src/texture/ITexture.hpp @@ -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; + +}; \ No newline at end of file diff --git a/src/texture/checker_texture.hpp b/src/texture/checker_texture.hpp new file mode 100644 index 0000000..a1eac0f --- /dev/null +++ b/src/texture/checker_texture.hpp @@ -0,0 +1,41 @@ + +#pragma once + +#include "texture/ITexture.hpp" +#include "texture/solid_color_texture.hpp" + +#include + + +class CheckerTexture : public ITexture { + +private: + + double m_inv_scale; + std::shared_ptr m_even_texture; + std::shared_ptr m_odd_texture; + + +public: + + CheckerTexture(double scale, std::shared_ptr even, std::shared_ptr 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(even)}, + m_odd_texture{std::make_shared(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); + } + +}; \ No newline at end of file diff --git a/src/texture/solid_color_texture.hpp b/src/texture/solid_color_texture.hpp new file mode 100644 index 0000000..3219638 --- /dev/null +++ b/src/texture/solid_color_texture.hpp @@ -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; + } + +}; \ No newline at end of file