diff options
author | Keuin <[email protected]> | 2022-04-16 14:31:38 +0800 |
---|---|---|
committer | Keuin <[email protected]> | 2022-04-16 14:31:38 +0800 |
commit | b26a0168788a28c384d394b9e3bb4ebaebc815c2 (patch) | |
tree | 19d7d1532e66c0b6c1dc976b4f34022554c49b1c | |
parent | 1c27339c33a9170a519da847d9f00f8ffd5f47b9 (diff) |
Add dielectric material. (glass ball)
-rw-r--r-- | CMakeLists.txt | 4 | ||||
-rw-r--r-- | main_simple_scanner.cpp | 16 | ||||
-rw-r--r-- | material_dielectric.cpp | 22 | ||||
-rw-r--r-- | material_dielectric.h | 18 |
4 files changed, 57 insertions, 3 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ed3de8..a197031 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,9 +12,9 @@ set(CMAKE_VERBOSE_MAKEFILE on) # main executable -add_executable(rt main.cpp vec.h bitmap.h ray.h bitfont.h hitlist.h object.h sphere.h viewport.h aa.h material.h material_diffusive.h material_diffusive.cpp material_reflective.h material_reflective.cpp) +add_executable(rt main.cpp vec.h bitmap.h ray.h bitfont.h hitlist.h object.h sphere.h viewport.h aa.h material.h material_diffusive.h material_diffusive.cpp material_reflective.h material_reflective.cpp material_dielectric.cpp material_dielectric.h) add_executable(image_output main_image_output.cpp vec.h bitmap.h bitfont.h hitlist.h object.h sphere.h viewport.h) -add_executable(simple_scanner main_simple_scanner.cpp vec.h bitmap.h ray.h timer.h bitfont.h hitlist.h object.h sphere.h viewport.h aa.h material.h material_diffusive.h material_diffusive.cpp material_reflective.h material_reflective.cpp) +add_executable(simple_scanner main_simple_scanner.cpp vec.h bitmap.h ray.h timer.h bitfont.h hitlist.h object.h sphere.h viewport.h aa.h material.h material_diffusive.h material_diffusive.cpp material_reflective.h material_reflective.cpp material_dielectric.cpp material_dielectric.h) # googletest diff --git a/main_simple_scanner.cpp b/main_simple_scanner.cpp index b116933..6e79293 100644 --- a/main_simple_scanner.cpp +++ b/main_simple_scanner.cpp @@ -16,10 +16,12 @@ #include "aa.h" #include "material_diffusive.h" #include "material_reflective.h" +#include "material_dielectric.h" // Select the scene to render //#define SCENE_DIFFUSE -#define SCENE_REFLECT +//#define SCENE_REFLECT +#define SCENE_DIALECT // T: intermediate color depth template<typename T> @@ -59,6 +61,18 @@ void generate_image(uint16_t image_width, uint16_t image_height, double viewport world.add_object(std::make_shared<sphere>(vec3d{0.0, 0.0, -1.0}, 0.5, m_ball_center)); world.add_object(std::make_shared<sphere>(vec3d{1.0, 0.0, -1.0}, 0.5, m_ball_right)); #endif +#ifdef SCENE_DIALECT + material_diffuse_lambertian m_ground{{0.8, 0.8, 0.0}}; + material_diffuse_lambertian m_ball_center{{0.7, 0.3, 0.3}}; + material_dielectric m_ball_left{1.5}; + material_reflective m_ball_right{{0.8, 0.6, 0.2}}; + // the earth + world.add_object(std::make_shared<sphere>(vec3d{0.0, -100.5, -1.0}, 100.0, m_ground)); + // three balls + world.add_object(std::make_shared<sphere>(vec3d{-1.0, 0.0, -1.0}, 0.5, m_ball_left)); + world.add_object(std::make_shared<sphere>(vec3d{0.0, 0.0, -1.0}, 0.5, m_ball_center)); + world.add_object(std::make_shared<sphere>(vec3d{1.0, 0.0, -1.0}, 0.5, m_ball_right)); +#endif timer tm; std::cerr << "Rendering..." << std::endl; tm.start_measure(); diff --git a/material_dielectric.cpp b/material_dielectric.cpp new file mode 100644 index 0000000..3aaf259 --- /dev/null +++ b/material_dielectric.cpp @@ -0,0 +1,22 @@ +// +// Created by Keuin on 2022/4/15. +// + +#include "material_dielectric.h" + +bool material_dielectric::scatter(ray3d &r, const object &hit_obj, double hit_t, random_uv_gen_3d &ruvg) const { + const auto hit_p = r.at(hit_t); + assert(hit_obj.is_on(hit_p)); + auto n = hit_obj.normal_vector(hit_p); + auto ri_ = ri_inv; + if (dot(r.direction(), n) > 0) { + // the ray is started from the object's inner, + // use normal vector and ri on the surface's inner side + n = -n; + ri_ = 1.0 / ri_; + } + const auto r2 = n.refract<true>(r.direction(), ri_); // refracted vector + r.direction(r2.unit_vec()); + r.source(hit_p); + return true; +} diff --git a/material_dielectric.h b/material_dielectric.h new file mode 100644 index 0000000..48382b4 --- /dev/null +++ b/material_dielectric.h @@ -0,0 +1,18 @@ +// +// Created by Keuin on 2022/4/15. +// + +#ifndef RT_MATERIAL_DIELECTRIC_H +#define RT_MATERIAL_DIELECTRIC_H + +#include "vec.h" +#include "material.h" + +class material_dielectric : public material { + double ri_inv; +public: + explicit material_dielectric(double ri) : ri_inv{1.0 / ri} {} + bool scatter(ray3d &r, const object &hit_obj, double hit_t, random_uv_gen_3d &ruvg) const override; +}; + +#endif //RT_MATERIAL_DIELECTRIC_H |